Spaces:
Sleeping
Sleeping
| """ | |
| MCP stdio server exposing execute_python_code and search_web tools (FastMCP) | |
| """ | |
| from fastmcp import FastMCP | |
| mcp = FastMCP("excel-tools") | |
| # Lazy singletons to avoid heavy imports at startup | |
| _python_tool = None | |
| _web_tool = None | |
| def execute_python_code(code: str, file_path: str) -> str: | |
| """Execute Python code and return results as JSON string to avoid MCP serialization issues""" | |
| import json | |
| global _python_tool | |
| if _python_tool is None: | |
| from app_agents.tools.python_tool import PythonSandboxTool | |
| _python_tool = PythonSandboxTool(timeout=30) | |
| result = _python_tool.execute(code=code, file_path=file_path) | |
| return json.dumps(result, ensure_ascii=False) | |
| def search_web(query: str) -> str: | |
| global _web_tool | |
| if _web_tool is None: | |
| from app_agents.tools.web_search_tool import WebSearchTool | |
| _web_tool = WebSearchTool(max_results=5) | |
| res = _web_tool.search(query) | |
| if res.get("success"): | |
| return _web_tool.format_results(res["results"]) | |
| return f"Search failed: {res.get('error', 'Unknown error')}" | |
| if __name__ == "__main__": | |
| # stdio is the default; we specify it explicitly for clarity | |
| mcp.run(transport="stdio") | |