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. 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');