MCP: поток GET /mcp (SSE) с notifications/tools/list_changed при подключении, capabilities.tools.listChanged
Some checks failed
ci / test (push) Failing after 41s
ci / deploy (push) Has been skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 17:43:17 +03:00
parent 5b2470a369
commit 66eefae745
2 changed files with 48 additions and 2 deletions

View File

@@ -486,7 +486,7 @@ export async function handleMcpMessage(tools: ReviewTools, personId: string, msg
switch (method) {
case "initialize": {
const asked = (msg.params as Json | undefined)?.protocolVersion;
return { jsonrpc: "2.0", id, result: { protocolVersion: typeof asked === "string" ? asked : "2025-03-26", capabilities: { tools: {}, resources: {}, prompts: {} }, serverInfo: { name: "accounting_automation", version: "0.1.0" }, instructions } };
return { jsonrpc: "2.0", id, result: { protocolVersion: typeof asked === "string" ? asked : "2025-03-26", capabilities: { tools: { listChanged: true }, resources: {}, prompts: {} }, serverInfo: { name: "accounting_automation", version: "0.1.0" }, instructions } };
}
case "ping":
return { jsonrpc: "2.0", id, result: {} };
@@ -629,6 +629,33 @@ export function registerMcp(app: FastifyInstance, deps: McpDeps): ReviewTools {
const res = await handle(body as Json);
return res ? reply.send(res) : reply.code(202).send();
});
app.get("/mcp", async (_req, reply) => reply.code(405).header("Allow", "POST").send({ error: "используйте POST" }));
// Поток сервер->клиент (Streamable HTTP, GET + SSE): при каждом подключении шлём tools/list_changed.
// После деплоя сервис перезапускается, клиент переподключает поток, получает уведомление и сам перечитывает список инструментов.
const streams = new Set<() => void>();
app.addHook("onClose", async () => {
for (const close of [...streams]) close();
});
app.get("/mcp", async (req, reply) => {
if (!String(req.headers.accept ?? "").includes("text/event-stream")) return reply.code(405).header("Allow", "POST").send({ error: "используйте POST (или GET с Accept: text/event-stream)" });
try {
await authenticate(req.headers.authorization);
} catch (e) {
const status = e instanceof McpAuthError ? e.status : 401;
if (status === 401) void reply.header("WWW-Authenticate", challenge);
return reply.code(status).send({ error: e instanceof Error ? e.message : "unauthorized" });
}
reply.hijack();
const raw = reply.raw;
raw.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache, no-transform", Connection: "keep-alive", "X-Accel-Buffering": "no" });
raw.write(`event: message\ndata: ${JSON.stringify({ jsonrpc: "2.0", method: "notifications/tools/list_changed" })}\n\n`);
const ping = setInterval(() => raw.write(": ping\n\n"), 20000);
const close = (): void => {
clearInterval(ping);
streams.delete(close);
if (!raw.writableEnded) raw.end();
};
streams.add(close);
raw.on("close", close);
});
return tools;
}