fix: Unhandled server error in MCP route (#12586)

This commit is contained in:
Tom Moor
2026-06-04 23:31:16 -04:00
committed by GitHub
parent 1cc10f5fff
commit bfddf4bb4c
2 changed files with 41 additions and 1 deletions
+11
View File
@@ -92,6 +92,17 @@ describe("POST /mcp/", () => {
expect(result?.serverInfo?.name).toEqual("outline");
});
it("should return 202 for the notifications/initialized lifecycle message", async () => {
const { accessToken } = await buildOAuthUser();
const res = await server.post("/mcp/", {
headers: mcpHeaders(accessToken),
body: { jsonrpc: "2.0", method: "notifications/initialized" },
});
expect(res.status).toEqual(202);
});
it("should set the MCP flag on the user after a successful request", async () => {
const { user, accessToken } = await buildOAuthUser();
expect(user.getFlag(UserFlag.MCP)).toEqual(0);
+30 -1
View File
@@ -4,6 +4,7 @@ import Router from "koa-router";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import type { AuthInfo } from "@modelcontextprotocol/sdk/server/auth/types.js";
import { ErrorCode } from "@modelcontextprotocol/sdk/types.js";
import { TeamPreference } from "@shared/types";
import { NotFoundError } from "@server/errors";
import Logger from "@server/logging/Logger";
@@ -113,7 +114,35 @@ router.post(
};
ctx.respond = false;
await transport.handleRequest(ctx.req, ctx.res, ctx.request.body);
// The SDK's handleRequest answers known protocol failures itself (4xx with a
// JSON-RPC body) via the transport. Anything that escapes here is unexpected.
try {
await transport.handleRequest(ctx.req, ctx.res, ctx.request.body);
} catch (error) {
Logger.error(
"MCP request handling failed",
error instanceof Error ? error : new Error(String(error)),
undefined,
ctx.req
);
if (!ctx.res.headersSent) {
ctx.res.writeHead(500, { "Content-Type": "application/json" });
ctx.res.end(
JSON.stringify({
jsonrpc: "2.0",
error: {
code: ErrorCode.InternalError,
message: "Internal server error",
},
id: null,
})
);
} else {
ctx.res.end();
}
}
}
);