Skip to content

fix(client): suppress onerror when SSE disconnect will be handled by reconnect#1827

Open
felixweinberger wants to merge 1 commit intomainfrom
fweinberger/fix-sse-disconnect-noise
Open

fix(client): suppress onerror when SSE disconnect will be handled by reconnect#1827
felixweinberger wants to merge 1 commit intomainfrom
fweinberger/fix-sse-disconnect-noise

Conversation

@felixweinberger
Copy link
Copy Markdown
Contributor

Moves onerror into the else branch of the reconnect check in _handleSseStream's catch block.

Motivation and Context

Fixes #1211. onerror fired unconditionally on every stream disconnect, before the reconnect decision at streamableHttp.ts:447. On long-lived GET SSE streams this produced "SSE stream disconnected: TypeError: terminated" every few minutes even though the transport recovered transparently via _scheduleReconnection.

Three users confirmed on #1211 (original report on 1.24.3, playwright-mcp user with screenshot, status-ask). #1349 attempted a fix via string-matching 'terminated' / 'body stream', but that's fragile (undici could change messages, and 'body stream' also matches 'body stream already read' which is a different bug). This takes the structural approach instead: only fire onerror when reconnection won't happen.

How Has This Been Tested?

Updated the existing "should reconnect a GET-initiated notification stream that fails" test to assert onerror is NOT called when reconnect fires (previously asserted the buggy behavior). Test fails on main (expected not called, but called 1 times), passes with the fix. All 346 client tests pass.

Breaking Changes

Behavioral change for users who relied on onerror as a connection-activity monitor: transient disconnects that will be handled by reconnection no longer fire onerror. Permanent failures ("Failed to reconnect", "Maximum reconnection attempts exceeded") still fire.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Credit to @hassan123789 for #1349 which identified the issue and the code location.

…reconnect

Previously onerror fired unconditionally on every stream disconnect, before
the reconnect decision. This produced 'SSE stream disconnected: TypeError:
terminated' noise every few minutes on long-lived connections even though
the transport recovered transparently. Moved onerror into the else branch
of the reconnect check so it only fires when the stream will not recover.

Fixes #1211
@felixweinberger felixweinberger requested a review from a team as a code owner March 31, 2026 13:23
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 31, 2026

🦋 Changeset detected

Latest commit: 31beb5b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 31, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1827

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1827

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1827

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1827

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1827

commit: 31beb5b

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSE stream disconnected: TypeError: terminated

1 participant