File size: 2,219 Bytes
7830dce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import gradio as gr
import os
import sys
import importlib
# Configuration
TOOLS_DIR = "tools"
# Ensure tools directory exists and is a package
if not os.path.exists(TOOLS_DIR):
os.makedirs(TOOLS_DIR, exist_ok=True)
with open(os.path.join(TOOLS_DIR, "__init__.py"), "w") as f:
pass
# Add current directory to path so 'import tools.xxx' works
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
interfaces = []
names = []
print(f"π Starting Meta-MCP Toolbox...")
print(f"π Scanning '{TOOLS_DIR}' directory...")
# Scan and import tools
try:
for filename in sorted(os.listdir(TOOLS_DIR)):
if filename.endswith(".py") and not filename.startswith("_"):
module_name = filename[:-3]
full_module_name = f"{TOOLS_DIR}.{module_name}"
try:
print(f" π Importing {full_module_name}...")
# Standard dynamic import
# Use reload to ensure latest version is taken if restarted
module = importlib.import_module(full_module_name)
importlib.reload(module)
if hasattr(module, "create_interface"):
# Create Gradio interface for this tool
tool_interface = module.create_interface()
interfaces.append(tool_interface)
names.append(module_name)
print(f" β
Loaded {module_name}")
else:
print(f" β οΈ Module {module_name} has no create_interface()")
except Exception as e:
print(f" β Error loading {module_name}: {e}")
import traceback
traceback.print_exc()
except Exception as e:
print(f"Error scanning tools directory: {e}")
# Final interface construction
if not interfaces:
demo = gr.Interface(
fn=lambda x: "No tools loaded yet. Add a tool via Meta-MCP!",
inputs="text",
outputs="text",
title="Empty Toolbox",
description="This Space is ready to receive tools."
)
else:
demo = gr.TabbedInterface(interfaces, names)
if __name__ == "__main__":
demo.launch(mcp_server=True, show_error=True) |