Repository navigation
Description
When an SSE connection fails, Client.connect() rejects, but the transport's EventSource stays open and keeps reconnecting. The process never exits.
Here's the smallest repro I found, with @modelcontextprotocol/sdk 1.32.2 on Node 22.23.4:
// Run: node sse-leak.mjs [close]
import net from 'node:net';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
// Find a port nothing listens on.
const server = net.createServer().listen(0, '127.0.0.1');
await new Promise((resolve) => server.once('listening', resolve));
const { port } = server.address();
await new Promise((resolve) => server.close(resolve));
const transport = new SSEClientTransport(new URL(`http://127.0.0.1:${port}/sse`));
let errors = 0;
transport.onerror = () => errors++;
try {
await new Client({ name: 'repro', version: '1.0.0' }).connect(transport);
} catch (err) {
console.log('connect rejected:', err.message);
}
if (process.argv[2] === 'close') await transport.close();
const before = errors;
setTimeout(() => console.log(`errors in the next 10 s: ${errors - before}`), 10_000).unref();$ timeout 15 node sse-leak.mjs; echo $?
connect rejected: SSE error: TypeError: fetch failed: connect ECONNREFUSED 127.0.0.1:53069
errors in the next 10 s: 3
124
$ timeout 15 node sse-leak.mjs close; echo $?
connect rejected: SSE error: TypeError: fetch failed: connect ECONNREFUSED 127.0.0.1:53087
0
Without close(), it's still reconnecting when timeout kills it. With it, it exits right away.
Why? On v1.x, the onerror handler in _startOrAuth() (src/client/sse.ts) rejects start() but never closes the EventSource, and Client.connect() doesn't close the transport either. I'd expect a failed start() to close it, as the invalid-endpoint path in the same function already does. main has the same handler, though I've only read that code.
This hurts anyone who opens a session per call. Google's ADK for TypeScript (2.3) does, so every failed MCP call leaves one more EventSource behind. DABH found this while reviewing temporalio/ai-integrations#534: four failed calls added up to about 800 transport errors in ten minutes.
Activity
google-adk: say SSE needs ADK 2.3, and document sync factories
[typescript/google-adk] breaking: reject invalid MCP factory results (#…
nithishreddy5771 commentedon Nov 17, 2026
I'd like to investigate the initial-start failure cleanup on v1.x. I saw #3140 covers terminal stream failures after start; I would keep this scoped to a failed initial start and coordinate any shared onerror changes. Could you assign this issue to me if that scope is welcome?
A7mdMe commentedon Nov 17, 2026
Great catch @campoy. This is an acute issue for production agent runners and orchestrators (like Google ADK, Temporal, and serverless workers) that spin up client sessions per tool call.
Root Cause Analysis
In
@modelcontextprotocol/sdk/client/sse.js(inside_startOrAuth), the initial connection lifecycle hooksthis._eventSource.onerror:ECONNREFUSEDor unreachable port),onerrorfires and rejects thestart()promise.this._eventSourceis never closed on handshake rejection.EventSourceimplementations auto-reconnect by design, the underlying background connection timers remain scheduled in the Node.js event loop, preventing clean process exit and generating background error noise indefinitely (explaining the ~800 errors across 4 failed calls observed in Temporal).Upstream Fix in
src/client/sse.tsWithin
_startOrAuth, the initialonerrorhandler should close the activeEventSourcebefore rejecting:Drop-in Client Wrapper Workaround (For Consumers Today)
For anyone running into this in consumer code or agent orchestrators before the upstream patch lands, you can wrap the transport with an automatic teardown guard:
Disclosure: I maintain MCP Production Kit, an open-source kit and commercial suite containing reliability, teardown, and Slowloris lifecycle guards for production MCP servers and clients.
1320800521 commentedon Nov 23, 2026
Independent confirmation on macOS (Node v22.18.0,
@modelcontextprotocol/[email protected]) against a localhost-only unused TCP port:Client.connect(new SSEClientTransport(...))rejected withSSE error: ... ECONNREFUSED.connect()had rejected.await transport.close()allowed the test process to terminate normally. This is the legacy SSE EventSource lifecycle; we are not extrapolating it to Streamable HTTP.A minimal downstream cleanup policy for this failure path is to close the same failed transport in the connection error handler, before retrying or dropping the session reference:
This is consumer-side containment, not a claim that upstream already fixed the initial-start handler. The pinned runnable reproduction, which measures the error count before explicitly closing, is at XBSTACK TypeScript MCP lifecycle fixture. No real MCP servers or credentials were involved.