Repository navigation
Description
What happened?
I am using WebStandardStreamableHTTPServerTransport behind toNodeHandler. When the client's GET connection drops, the session's GET stream stays registered until the next keep-alive write, which can take up to 15s by default.
During this time every reconnect gets a 409. With the default options, the SDK client retries twice within about 2.5s and then gives up. It does not reconnect after that, so it misses server notifications for the rest of the session. One proxy idle timeout is enough to trigger this.
My output:
notifications before the drop: 1
client error: SSE stream disconnected: TypeError: terminated
client error: Failed to open SSE stream: Conflict
client error: Failed to reconnect SSE stream: Failed to open SSE stream: Conflict
client error: Failed to open SSE stream: Conflict
client error: Failed to reconnect SSE stream: Failed to open SSE stream: Conflict
client error: Maximum reconnection attempts (2) exceeded.
notifications after the drop: 0
I think the cause is the for await over response.body in toNodeHandler.ts. It only checks abort.signal after the next chunk arrives, so nothing cancels an idle stream until a keep-alive is written.
I tried replacing it with a reader that gets cancelled on abort. With that change the stream was free in about 2ms, and the client reconnected and received the notification.
Tested at commit 1e608ca on Node 25.9.0.
I would like to send a PR for this if you are open to it.
What did you expect?
The stream should be released as soon as the client disconnects, so the reconnect succeeds. NodeStreamableHTTPServerTransport released it in about 2ms in the same test.
Code to reproduce
import { randomUUID } from 'node:crypto';
import { createServer } from 'node:http';
import type { AddressInfo, Socket } from 'node:net';
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client';
import { toNodeHandler } from '@modelcontextprotocol/node';
import { McpServer, WebStandardStreamableHTTPServerTransport } from '@modelcontextprotocol/server';
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const transport = new WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: () => randomUUID() });
const server = new McpServer({ name: 's', version: '1' }, { capabilities: { tools: { listChanged: true } } });
await server.connect(transport);
const node = toNodeHandler({ fetch: request => transport.handleRequest(request) });
let getSocket: Socket | undefined;
const http = createServer((req, res) => {
if (req.method === 'GET') getSocket = req.socket;
void node(req, res);
});
await new Promise<void>(resolve => http.listen(0, '127.0.0.1', resolve));
const url = new URL(`http://127.0.0.1:${(http.address() as AddressInfo).port}/`);
const client = new Client({ name: 'c', version: '1' });
client.onerror = error => console.log('client error:', error.message);
let notifications = 0;
client.setNotificationHandler('notifications/tools/list_changed', () => void notifications++);
await client.connect(new StreamableHTTPClientTransport(url));
await sleep(200);
await server.server.sendToolListChanged();
await sleep(200);
console.log(`notifications before the drop: ${notifications}`);
// The GET connection drops, as with a proxy idle timeout.
notifications = 0;
getSocket!.destroy();
await sleep(20_000); // longer than the 15s keep-alive
await server.server.sendToolListChanged();
await sleep(200);
console.log(`notifications after the drop: ${notifications}`);
process.exit(0);SDK version
main at 1e608ca (@modelcontextprotocol/node 2.1.2, @modelcontextprotocol/server 2.4.0, @modelcontextprotocol/client 2.4.0)
Area
Transports
Activity
media-ctrl commentedon Nov 5, 2026
SwapnilSkr commentedon Nov 5, 2026
Thanks for checking this on main. I tried the race version, but it doesn't release the stream. The loop exits, but nothing cancels the body:
breakonly callsreturn()in afor await, and callingit.return()by hand still waits for the pendingnext(). In my testcancelwas never called with the race, and it was called right away withreader.cancel(). The 409 only clears when thatcancelruns, so I'll stay with the reader.media-ctrl commentedon Nov 5, 2026
Run an app without Holon with holon-sh/serve (#58)
Give apps their origin and their page as an MCP Apps view (#61)