Skip to content

[v2] toNodeHandler: a dropped GET stream blocks reconnects until the next keep-alive, so the client stops receiving notifications #3148

Open

Description

@SwapnilSkr

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

  1. added
    v2Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes
    on Nov 4, 2026
  2. media-ctrl commented on Nov 5, 2026

    @media-ctrl
  3. SwapnilSkr commented on Nov 5, 2026

    @SwapnilSkr
    Author

    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: break only calls return() in a for await, and calling it.return() by hand still waits for the pending next(). In my test cancel was never called with the race, and it was called right away with reader.cancel(). The 409 only clears when that cancel runs, so I'll stay with the reader.

  4. media-ctrl commented on Nov 5, 2026

    @media-ctrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    v2Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions