From 207e49f308a58da5c13441357c846acb180529d7 Mon Sep 17 00:00:00 2001 From: MIA_Ether Date: Thu, 3 Dec 2026 10:34:29 +0000 Subject: [PATCH 1/2] test(node): make streamable HTTP SSE tests chunk-tolerant Fixes #2661 The batch-response and notification-replay tests read the SSE stream once and assumed every event arrived in a single fetch chunk. On newer Node versions fetch can split the events across chunks, so the tests failed before the later events arrived. Add a readSSEUntil helper that keeps reading until the expected text appears (or a short timeout cancels the stream) and use it in the two affected tests. --- .../node/test/streamableHttp.test.ts | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/middleware/node/test/streamableHttp.test.ts b/packages/middleware/node/test/streamableHttp.test.ts index 901b78446f..959714dc0c 100644 --- a/packages/middleware/node/test/streamableHttp.test.ts +++ b/packages/middleware/node/test/streamableHttp.test.ts @@ -104,6 +104,32 @@ async function readSSEEvent(response: Response): Promise { return new TextDecoder().decode(value); } +/** + * Read SSE chunks until the accumulated text satisfies `predicate`. + * Fetch may deliver SSE events in separate chunks (e.g. on newer Node + * versions), so a single `reader.read()` is not guaranteed to contain + * every event sent so far. Cancels the stream after `timeoutMs` so a + * missing event fails the test instead of hanging it. + */ +async function readSSEUntil(response: Response, predicate: (text: string) => boolean, timeoutMs = 2000): Promise { + const reader = response.body?.getReader(); + let text = ''; + const decoder = new TextDecoder(); + const timeout = setTimeout(() => void reader?.cancel(), timeoutMs); + + try { + while (!predicate(text)) { + const { value, done } = await reader!.read(); + if (done) break; + text += decoder.decode(value, { stream: true }); + } + text += decoder.decode(); + return text; + } finally { + clearTimeout(timeout); + } +} + /** * Helper to send JSON-RPC request */ @@ -736,11 +762,9 @@ describe('Zod v4', () => { expect(response.status).toBe(200); expect(response.headers.get('content-type')).toBe('text/event-stream'); - const reader = response.body?.getReader(); - - // The responses may come in any order or together in one chunk - const { value } = await reader!.read(); - const text = new TextDecoder().decode(value); + // The responses may come in any order and in separate chunks, + // so keep reading until both have arrived. + const text = await readSSEUntil(response, text => text.includes('"id":"req-1"') && text.includes('"id":"req-2"')); // Check that both responses were sent on the same stream expect(text).toContain('"id":"req-1"'); @@ -1524,10 +1548,10 @@ describe('Zod v4', () => { expect(reconnectResponse.status).toBe(200); - // Read the replayed notification - const reconnectReader = reconnectResponse.body?.getReader(); - const reconnectData = await reconnectReader!.read(); - const reconnectText = new TextDecoder().decode(reconnectData.value); + // Read the replayed notification. Replayed events may arrive + // in a later chunk than an earlier event, so keep reading + // until the expected notification has arrived. + const reconnectText = await readSSEUntil(reconnectResponse, text => text.includes('Second notification from MCP server')); // Verify we received the second notification that was sent after our stored eventId expect(reconnectText).toContain('Second notification from MCP server'); From 8674e68488b133a2ad1df293e643388369c8d6e1 Mon Sep 17 00:00:00 2001 From: MIA_Ether Date: Fri, 4 Dec 2026 12:50:49 +0000 Subject: [PATCH 2/2] chore(node): add changeset for SSE test chunk tolerance The streamable HTTP SSE tests now keep reading until the expected event arrives instead of assuming a single fetch chunk. --- .changeset/sse-tests-chunk-tolerant.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sse-tests-chunk-tolerant.md diff --git a/.changeset/sse-tests-chunk-tolerant.md b/.changeset/sse-tests-chunk-tolerant.md new file mode 100644 index 0000000000..ccbd3ec276 --- /dev/null +++ b/.changeset/sse-tests-chunk-tolerant.md @@ -0,0 +1,5 @@ +--- +'@modelcontextprotocol/node': patch +--- + +Make the streamable HTTP SSE tests chunk-tolerant: the batch-response and notification-replay tests now keep reading the SSE stream until the expected event arrives instead of assuming every event arrives in a single fetch chunk.