diff --git a/src/api.js b/src/api.js index 8a5f047..031b1d5 100644 --- a/src/api.js +++ b/src/api.js @@ -209,7 +209,12 @@ export async function startServer(port = 9876) { // MCP JSON-RPC endpoint if (path === "/mcp" && req.method === "POST") { const body = await parseBody(req); - const mcpResp = await handleMCP(body); + const { response: mcpResp, isNotification } = await handleMCP(body); + if (isNotification || !mcpResp) { + res.writeHead(202, { "Content-Type": "application/json" }); + res.end(); + return; + } const status = mcpResp.error ? 400 : 200; res.writeHead(status, { "Content-Type": "application/json" }); res.end(JSON.stringify(mcpResp)); diff --git a/src/mcp.js b/src/mcp.js index 6debae9..8838d61 100644 --- a/src/mcp.js +++ b/src/mcp.js @@ -32,35 +32,47 @@ const TOOLS = [ export function createMCPHandler(runner) { return async function handleMCP(body) { if (!body || body.jsonrpc !== "2.0") { - return mcpError(null, -32600, "Invalid Request: must be JSON-RPC 2.0"); + return { response: mcpError(null, -32600, "Invalid Request: must be JSON-RPC 2.0"), isNotification: false }; } const { id, method, params } = body; + const isNotification = id === undefined || id === null; switch (method) { case "tools/list": return { - jsonrpc: "2.0", - id, - result: { tools: TOOLS } + response: { jsonrpc: "2.0", id, result: { tools: TOOLS } }, + isNotification: false }; case "tools/call": - return handleToolCall(id, params, runner); + return { response: await handleToolCall(id, params, runner), isNotification: false }; case "initialize": return { - jsonrpc: "2.0", - id, - result: { - protocolVersion: "2024-11-05", - capabilities: { tools: {} }, - serverInfo: { name: "polysearch", version: "2.0.0" } - } + response: { + jsonrpc: "2.0", + id, + result: { + protocolVersion: "2024-11-05", + capabilities: { tools: {} }, + serverInfo: { name: "polysearch", version: "2.0.0" } + } + }, + isNotification: false }; + case "notifications/initialized": + case "notifications/cancelled": + log.debug({ method }, "notification received, no response"); + return { response: null, isNotification: true }; + default: - return mcpError(id, -32601, `Method not found: ${method}`); + if (isNotification) { + log.debug({ method }, "unknown notification, silently ignored"); + return { response: null, isNotification: true }; + } + return { response: mcpError(id, -32601, `Method not found: ${method}`), isNotification: false }; } }; }