You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SSEClientTransport keeps reconnecting after Client.connect() rejects #3193
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]importnetfrom'node:net';import{Client}from'@modelcontextprotocol/sdk/client/index.js';import{SSEClientTransport}from'@modelcontextprotocol/sdk/client/sse.js';// Find a port nothing listens on.constserver=net.createServer().listen(0,'127.0.0.1');awaitnewPromise((resolve)=>server.once('listening',resolve));const{ port }=server.address();awaitnewPromise((resolve)=>server.close(resolve));consttransport=newSSEClientTransport(newURL(`http://127.0.0.1:${port}/sse`));leterrors=0;transport.onerror=()=>errors++;try{awaitnewClient({name: 'repro',version: '1.0.0'}).connect(transport);}catch(err){console.log('connect rejected:',err.message);}if(process.argv[2]==='close')awaittransport.close();constbefore=errors;setTimeout(()=>console.log(`errors in the next 10 s: ${errors-before}`),10_000).unref();
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.
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?
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 hooks this._eventSource.onerror:
When the handshake fails (e.g. ECONNREFUSED or unreachable port), onerror fires and rejects the start() promise.
However, unlike the endpoint validation branch (which explicitly invokes cleanup), this._eventSource is never closed on handshake rejection.
Because standard EventSource implementations 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.ts
Within _startOrAuth, the initial onerror handler should close the active EventSource before rejecting:
this._eventSource.onerror=(event: MessageEvent)=>{consterror=newError(`SSE error: ${event.message||'Connection failed'}`);// Guarantee teardown of background reconnect timers on handshake rejectiontry{this._eventSource?.close();}catch{}reject(error);};
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:
import{Client}from'@modelcontextprotocol/sdk/client/index.js';import{SSEClientTransport}from'@modelcontextprotocol/sdk/client/sse.js';exportfunctionwrapTransportWithTeardownGuard<Textends{start(): Promise<void>;close(): Promise<void>}>(transport: T): T{constoriginalStart=transport.start.bind(transport);transport.start=async()=>{try{awaitoriginalStart();}catch(err){// Teardown EventSource immediately so background reconnect loop haltstry{awaittransport.close();}catch{}throwerr;}};returntransport;}// Usage in orchestrators/runners:consttransport=wrapTransportWithTeardownGuard(newSSEClientTransport(newURL('http://127.0.0.1:8000/sse')));try{awaitclient.connect(transport);}catch(err){// Guaranteed clean exit; 0 zombie reconnect loops left behindconsole.error('Handshake failed cleanly:',err.message);}
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.
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.