Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sse-tests-chunk-tolerant.md
Original file line number Diff line number Diff line change
@@ -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.
42 changes: 33 additions & 9 deletions packages/middleware/node/test/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ async function readSSEEvent(response: Response): Promise<string> {
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<string> {
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
*/
Expand Down Expand Up @@ -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"');
Expand Down Expand Up @@ -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');
Expand Down
Loading