61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
import asyncio
|
|
import uuid
|
|
import json
|
|
from typing import Any, Dict, Union, List
|
|
from starlette.responses import Response
|
|
|
|
class MCPServer:
|
|
def __init__(self):
|
|
self.sessions: Dict[str, asyncio.Queue] = {}
|
|
self.running = True
|
|
|
|
def create_session(self) -> str:
|
|
sid = str(uuid.uuid4())
|
|
self.sessions[sid] = asyncio.Queue()
|
|
return sid
|
|
|
|
async def send_to_session(self, sid: str, message: Dict[str, Any]):
|
|
if sid in self.sessions:
|
|
await self.sessions[sid].put(json.dumps(message))
|
|
|
|
async def handle_request(self, method: str, params: Dict[str, Any], req_id: Any, tool_registry: list) -> Union[Dict[str, Any], Response]:
|
|
if method == "initialize":
|
|
return {
|
|
"protocolVersion": "2024-11-05",
|
|
"capabilities": {"tools": {}},
|
|
"serverInfo": {"name": "MattCP", "version": "0.4.1"}
|
|
}
|
|
|
|
if method == "tools/list":
|
|
return {"tools": [t.to_mcp_dict() for t in tool_registry]}
|
|
|
|
if method == "tools/call":
|
|
name = params.get("name")
|
|
args = params.get("arguments", {})
|
|
|
|
tool = next((t for t in tool_registry if t.name == name), None)
|
|
if not tool:
|
|
return Response(
|
|
content=json.dumps({"jsonrpc": "2.0", "id": req_id, "error": {"code": -32601, "message": f"Tool {name} not found"}}),
|
|
media_type="application/json"
|
|
)
|
|
|
|
result_data = await tool.handler(args)
|
|
|
|
# Handle results that are already lists of content (for multimodal/multi-part responses)
|
|
if isinstance(result_data, list):
|
|
content = result_data
|
|
elif isinstance(result_data, dict) and "text" in result_data:
|
|
content = [{"type": "text", "text": result_data["text"]}]
|
|
elif isinstance(result_data, dict) and result_data.get("type") == "image":
|
|
content = [result_data]
|
|
else:
|
|
content = [result_data]
|
|
|
|
return {"content": content}
|
|
|
|
return Response(
|
|
content=json.dumps({"jsonrpc": "2.0", "id": req_id, "error": {"code": -32601, "message": f"Method {method} not found"}}),
|
|
media_type="application/json"
|
|
)
|