File size: 6,002 Bytes
4ef118d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
MCP tools API routes.
"""

from __future__ import annotations

from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse

from ..services.mcp_tools import mcp_tool_manager

router = APIRouter(tags=["mcp-tools"])


@router.get("/servers")
async def list_servers() -> JSONResponse:
    try:
        status = mcp_tool_manager.get_status()
        return JSONResponse(
            content={
                "success": True,
                "servers": status.get("loadedServers", []),
                "totalTools": status.get("totalTools", 0),
            }
        )
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "error": str(exc)})


@router.post("/servers")
async def load_server(request: Request) -> JSONResponse:
    body = await request.json()
    name = body.get("name")
    url = body.get("url")
    if not name or not url:
        return JSONResponse(
            status_code=400,
            content={"success": False, "error": "Missing required fields: name and url"},
        )
    try:
        tools = await mcp_tool_manager.load_mcp_server(name, body)
        return JSONResponse(
            content={
                "success": True,
                "message": f"Loaded {len(tools)} tools from {name}",
                "server": name,
                "toolsLoaded": len(tools),
                "tools": [
                    {
                        "id": tool.get("id"),
                        "name": tool.get("name"),
                        "description": tool.get("description"),
                        "parameters": tool.get("parameters"),
                    }
                    for tool in tools
                ],
            }
        )
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "error": str(exc)})


@router.get("/servers/{name}/tools")
async def list_server_tools(name: str) -> JSONResponse:
    try:
        tools = mcp_tool_manager.list_mcp_tools_by_server(name)
        return JSONResponse(
            content={
                "success": True,
                "server": name,
                "tools": [
                    {
                        "id": tool.get("id"),
                        "name": tool.get("name"),
                        "description": tool.get("description"),
                        "parameters": tool.get("parameters"),
                    }
                    for tool in tools
                ],
                "total": len(tools),
            }
        )
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "error": str(exc)})


@router.delete("/servers/{name}")
async def unload_server(name: str) -> JSONResponse:
    try:
        await mcp_tool_manager.unload_mcp_server(name)
        return JSONResponse(content={"success": True, "message": f"Unloaded server: {name}"})
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "error": str(exc)})


@router.get("/tools")
async def list_tools() -> JSONResponse:
    try:
        tools = mcp_tool_manager.list_mcp_tools()
        return JSONResponse(
            content={
                "success": True,
                "tools": [
                    {
                        "id": tool.get("id"),
                        "name": tool.get("name"),
                        "description": tool.get("description"),
                        "category": tool.get("category"),
                        "parameters": tool.get("parameters"),
                        "server": tool.get("config", {}).get("mcpServer"),
                    }
                    for tool in tools
                ],
                "total": len(tools),
            }
        )
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "error": str(exc)})


@router.get("/tool/{tool_id}")
async def get_tool(tool_id: str) -> JSONResponse:
    try:
        tool = mcp_tool_manager.get_mcp_tool(tool_id)
        if not tool:
            return JSONResponse(
                status_code=404,
                content={"success": False, "error": f"Tool not found: {tool_id}"},
            )
        return JSONResponse(
            content={
                "success": True,
                "tool": {
                    "id": tool.get("id"),
                    "name": tool.get("name"),
                    "description": tool.get("description"),
                    "category": tool.get("category"),
                    "parameters": tool.get("parameters"),
                    "server": tool.get("config", {}).get("mcpServer"),
                    "metadata": tool.get("metadata"),
                },
            }
        )
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "error": str(exc)})


@router.post("/fetch")
async def fetch_tools(request: Request) -> JSONResponse:
    body = await request.json()
    name = body.get("name")
    url = body.get("url")
    if not name or not url:
        return JSONResponse(
            status_code=400,
            content={"success": False, "error": "Missing required fields: name and url"},
        )
    try:
        tools = await mcp_tool_manager.fetch_tools_from_server_url(name, body)
        return JSONResponse(
            content={
                "success": True,
                "server": name,
                "tools": [
                    {
                        "id": tool.get("id"),
                        "name": tool.get("name"),
                        "description": tool.get("description"),
                        "parameters": tool.get("parameters"),
                    }
                    for tool in tools
                ],
                "total": len(tools),
            }
        )
    except Exception as exc:
        return JSONResponse(status_code=500, content={"success": False, "error": str(exc)})