File size: 1,228 Bytes
d853cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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


@mcp.tool
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)


@mcp.tool
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")