Skip to content

fix(client): fail promptly when a response stream closes without a reply - #3210

Open
anupamgit wants to merge 1 commit into
modelcontextprotocol:mainfrom
anupamgit:fix/protocol-on-request-stream-end
Open

anupamgit wants to merge 1 commit into
modelcontextprotocol:mainfrom
anupamgit:fix/protocol-on-request-stream-end

Conversation

@anupamgit

Copy link
Copy Markdown

A modern Streamable HTTP request could wait until its read timeout after the server closed the response stream without replying. Reject with ConnectionClosed when the stream ends, including when a request resumed with a saved token gets HTTP 405.

Validation: all 126 tests in the transport and protocol test files passed. Lint and type checks passed for client and core-internal.

Motivation and Context

On modern Streamable HTTP, an in-flight client request could stay pending until read timeout when the per-request SSE stream ended without a JSON-RPC result. The same hang could occur when resuming with a saved resumptionToken and the GET resume ended terminally (e.g. HTTP 405), because onRequestStreamEnd was not wired through Protocol and was not forwarded on the explicit resumption GET path in StreamableHTTPClientTransport.

How Has This Been Tested?

  • pnpm --filter @modelcontextprotocol/client exec vitest run test/client/streamableHttp.test.ts — 76 passed (including new explicit resumptionToken + 405 case)
  • pnpm --filter @modelcontextprotocol/core-internal exec vitest run test/shared/protocol.test.ts — 50 passed (including remote stream end → ConnectionClosed)
  • pnpm --filter @modelcontextprotocol/client and pnpm --filter @modelcontextprotocol/core-internal — lint and typecheck passed
  • Full pnpm lint:all was not run to completion here (pre-push hook fails on unrelated test/helpers ESLint)

Breaking Changes

None. Callers see faster ConnectionClosed rejection instead of waiting for read timeout when the stream ends without a response on the paths above.

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 (for client + core-internal packages and the test files above)
  • I have added appropriate error handling (ConnectionClosed via existing cancel() path)
  • I have added or updated documentation as needed (no public API doc change)

Additional context

  • Changeset: .changeset/reject-when-per-request-stream-ends.md (patch).

A modern Streamable HTTP request could wait until its read timeout after
the server closed the response stream without replying. Reject with
ConnectionClosed when the stream ends, including when a request resumed
with a saved token gets HTTP 405.

Validation: all 126 tests in the transport and protocol test files passed.
Lint and type checks passed for client and core-internal.
@changeset-bot

changeset-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3e50bd0

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

This PR includes changesets to release 6 packages
Name Type
@modelcontextprotocol/core-internal Patch
@modelcontextprotocol/client Patch
@modelcontextprotocol/server Patch
@modelcontextprotocol/codemod Patch
@modelcontextprotocol/core Patch
@modelcontextprotocol/server-legacy 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

pkg-pr-new Bot commented Nov 19, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@3210

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@3210

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@3210

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@3210

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@3210

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@3210

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@3210

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@3210

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@3210

commit: 3e50bd0

@anupamgit
anupamgit marked this pull request as ready for review November 19, 2026 09:10
@anupamgit
anupamgit requested a review from a team as a code owner November 19, 2026 09:10
@A7mdMe

A7mdMe commented Nov 19, 2026

Copy link
Copy Markdown

Great fix, @anupamgit — handling premature stream termination and forwarding onRequestStreamEnd on the resumption path solves one of the most frustrating failure modes in modern Streamable HTTP (where client requests hang for 30s-60s until read timeout).

Two production observations from our test harness that might be helpful for this path:

1. Terminal vs Transient Resumption Statuses

Classifying HTTP 405 Method Not Allowed as a terminal, non-resumable status is spot on. For production resilience, it's worth ensuring HTTP 404 (session expired/evicted) and HTTP 410 (Gone) are treated as terminal as well, so the client doesn't enter futile retry loops:

  • Terminal (Fail promptly via onRequestStreamEnd / ConnectionClosed): 400, 401, 403, 404, 405, 410.
  • Transient (Retryable if transport backoff is enabled): 502, 503, 504 (gateway timeouts).

2. Timers & Listener Cleanup on Stream End

When onRequestStreamEnd triggers cancel(ConnectionClosed):
Ensuring that any associated read-timeout timers (setTimeout) are explicitly cleared (clearTimeout) and listeners detached prevents unref/timer leaks in long-running agent hosts handling high request concurrency.

On the server side, pairing this with guaranteed terminal JSON-RPC error frames (-32603 Internal error or -32000 Connection closed) before stream flush ensures both sides fail-closed deterministically.

(Disclosure: I maintain A7mdMe/mcp-production-kit, where we test and harden production MCP client/server boundary defenses).

This branch has not been deployed

No deployments
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.

2 participants