Skip to content

Commit eba9588

Browse files
cliffhallclaude
andcommitted
fix(everything): take a refused POST's id only from a JSON-RPC request
Copilot round 2 on #5006: readRequestId took the id of any JSON object, so a response or a bare {"id":7} echoed its id back. It now uses the SDK's isJSONRPCRequest, and the null-id table covers a response and a non-JSON-RPC object. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 6a36cac commit eba9588

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ describe("Streamable HTTP transport", () => {
277277
it.each([
278278
["a notification", JSON.stringify({ jsonrpc: "2.0", method: "ping" })],
279279
["a batch", JSON.stringify([{ jsonrpc: "2.0", id: 8, method: "ping" }])],
280+
["a response", JSON.stringify({ jsonrpc: "2.0", id: 8, result: {} })],
281+
["an object that is not JSON-RPC", JSON.stringify({ id: 8 })],
280282
["malformed JSON", "{not json"],
281283
[
282284
"a body too large to read for its id",

‎src/everything/transports/streamableHttp.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// session map. `startStreamableHttpServer()` is what the launcher runs.
99

1010
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
11+
import { isJSONRPCRequest } from "@modelcontextprotocol/sdk/types.js";
1112
import express, { Express, Request, Response } from "express";
1213
import type { Server } from "node:http";
1314
import { createServer } from "../server/index.js";
@@ -21,8 +22,8 @@ const MAX_ID_BODY_BYTES = 64 * 1024;
2122

2223
/**
2324
* The JSON-RPC `id` of a POST's body, or `null` when the body is not a single
24-
* request (a notification, a batch, malformed JSON, or larger than
25-
* `MAX_ID_BODY_BYTES`). Used only for a POST that is refused before the SDK
25+
* JSON-RPC request (a notification, a response, a batch, any other JSON,
26+
* malformed JSON, or larger than `MAX_ID_BODY_BYTES`). Used only for a POST that is refused before the SDK
2627
* reads its body, so the error can still carry the request's id (#4982).
2728
*/
2829
async function readRequestId(req: Request): Promise<string | number | null> {
@@ -34,8 +35,8 @@ async function readRequestId(req: Request): Promise<string | number | null> {
3435
if (size > MAX_ID_BODY_BYTES) return null;
3536
chunks.push(chunk as Buffer);
3637
}
37-
const id = JSON.parse(Buffer.concat(chunks).toString("utf8"))?.id;
38-
return typeof id === "string" || typeof id === "number" ? id : null;
38+
const message: unknown = JSON.parse(Buffer.concat(chunks).toString("utf8"));
39+
return isJSONRPCRequest(message) ? message.id : null;
3940
} catch {
4041
return null;
4142
}

0 commit comments

Comments
 (0)