Skip to content

Commit e238929

Browse files
fix(server): answer MCP bridge requests that fail with a JSON error body
A non-2xx response carrying a JSON body was forwarded to stdout as if it were an MCP message, so the request id never received a reply. Gate body forwarding on response.ok and route all HTTP failures through the synthesized JSON-RPC error path. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent f42cf0a commit e238929

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎apps/server/src/mcp/McpStdioBridge.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ describe("McpStdioBridge", () => {
9292
res.writeHead(500, { "content-type": "text/plain" });
9393
res.end("upstream exploded");
9494
return;
95+
case "boom/json":
96+
res.writeHead(401, { "content-type": "application/json" });
97+
res.end(JSON.stringify({ error: "invalid_mcp_credential" }));
98+
return;
9599
default:
96100
res.writeHead(202);
97101
res.end();
@@ -138,7 +142,8 @@ describe("McpStdioBridge", () => {
138142
send({ jsonrpc: "2.0", id: 2, method: "tools/list", params: {} });
139143
send({ jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "x" } });
140144
send({ jsonrpc: "2.0", id: 4, method: "boom/fail", params: {} });
141-
await waitForResponses(4);
145+
send({ jsonrpc: "2.0", id: 5, method: "boom/json", params: {} });
146+
await waitForResponses(5);
142147

143148
const byId = new Map(
144149
stdout.filter((message) => "id" in message).map((message) => [message.id, message]),
@@ -149,6 +154,9 @@ describe("McpStdioBridge", () => {
149154
expect(byId.get(3)).toMatchObject({ result: { ok: true } });
150155
// A non-JSON error body becomes a JSON-RPC error carrying the request id.
151156
expect(byId.get(4)).toMatchObject({ error: { code: -32603 } });
157+
// A JSON error body on a non-2xx is not forwarded as an MCP message; the
158+
// request id still gets a JSON-RPC error.
159+
expect(byId.get(5)).toMatchObject({ error: { code: -32603 } });
152160

153161
const initialize = seen.find((request) => request.method === "initialize");
154162
expect(initialize?.authorization).toBe("Bearer test-token");

‎apps/server/src/mcp/McpStdioBridge.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ async function postMessage(line, parsed) {
6565
sessionId = response.headers.get("mcp-session-id") ?? sessionId;
6666
protocolVersion = response.headers.get("mcp-protocol-version") ?? protocolVersion;
6767
68+
if (!response.ok) {
69+
await response.arrayBuffer();
70+
if (parsed && typeof parsed === "object" && "id" in parsed) {
71+
respondError(parsed.id, "MCP endpoint returned HTTP " + response.status + ".");
72+
}
73+
return;
74+
}
75+
6876
const contentType = response.headers.get("content-type") ?? "";
6977
if (contentType.includes("text/event-stream")) {
7078
for (const message of parseSseEvents(await response.text())) {
@@ -85,9 +93,6 @@ async function postMessage(line, parsed) {
8593
// Fall through and synthesize an error below.
8694
}
8795
}
88-
if (!response.ok && parsed && typeof parsed === "object" && "id" in parsed) {
89-
respondError(parsed.id, "MCP endpoint returned HTTP " + response.status + ".");
90-
}
9196
}
9297
9398
function* parseSseEvents(body) {

0 commit comments

Comments
 (0)