Skip to content

Commit 0d01ee5

Browse files
cliffhallclaude
andcommitted
fix(everything): answer a JSON primitive POST body as an invalid request, not a parse error
Copilot round 8 on #5131: express.json is strict by default, so a body of valid JSON that is not an object or array (null, "x", 42) failed to parse and was answered -32700, which MCP reserves for text that is not JSON. The parser now accepts any JSON value and the SDK answers a primitive with -32600. Part of #4852 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 88410f8 commit 0d01ee5

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎src/everything/__tests__/streamable-http.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,23 @@ describe("2026-07-28 clients", () => {
654654
});
655655
});
656656

657+
it.each(["null", '"x"', "42", "true"])(
658+
"answers a session-less POST of the JSON value %s as an invalid request, not a parse error",
659+
async (body) => {
660+
const response = await fetch(mcp, {
661+
method: "POST",
662+
headers: JSON_HEADERS,
663+
body,
664+
});
665+
expect(response.status).toBe(400);
666+
expect(await response.json()).toMatchObject({
667+
jsonrpc: "2.0",
668+
error: { code: -32600 },
669+
id: null,
670+
});
671+
},
672+
);
673+
657674
it("answers a session-less POST over 4 MiB with 413", async () => {
658675
const response = await fetch(mcp, {
659676
method: "POST",

‎src/everything/transports/streamableHttp.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ function sessionNotFound(res: Response, id: string | number | null = null) {
8686
const MAX_BODY_BYTES = 4 * 1024 * 1024;
8787

8888
/** Parses a session-less POST's JSON body, to classify it and to serve it. */
89-
const parseJson = express.json({ limit: MAX_BODY_BYTES, type: () => true });
89+
const parseJson = express.json({
90+
limit: MAX_BODY_BYTES,
91+
type: () => true,
92+
// Any JSON value parses, so a primitive body reaches the SDK, which answers
93+
// it as an invalid request (-32600); -32700 is only for text that is not JSON.
94+
strict: false,
95+
});
9096

9197
/**
9298
* Answer a session-less POST whose body could not be parsed the way the SDK

0 commit comments

Comments
 (0)