Spaces:
Sleeping
Sleeping
| 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) |