Skip to content

fix(client): handle 404 by clearing session ID per MCP spec#1805

Open
lanxevo3 wants to merge 3 commits intomodelcontextprotocol:mainfrom
lanxevo3:fix/404-session-recovery
Open

fix(client): handle 404 by clearing session ID per MCP spec#1805
lanxevo3 wants to merge 3 commits intomodelcontextprotocol:mainfrom
lanxevo3:fix/404-session-recovery

Conversation

@lanxevo3
Copy link
Copy Markdown

Per MCP spec Section "Session Management" (2025-03-26), when a client receives HTTP 404 in response to a request containing an Mcp-Session-Id, it MUST clear the session ID. Previously, both send() (POST) and _startOrAuthSse() (GET) threw StreamableHTTPError without clearing _sessionId, permanently breaking SDK-based clients with stale session IDs.

Changes

packages/core/src/errors/sdkErrors.ts

  • Added ClientHttpNotFound = 'CLIENT_HTTP_NOT_FOUND' error code

packages/client/src/client/streamableHttp.ts

  • Added 404 handling block that clears this._sessionId = undefined before throwing, so subsequent calls don't keep retrying with the same dead session ID
  • Full re-initialization is left to the caller (InitializeRequest involves capability negotiation owned by the Client layer, not the Transport)

Before

404 with Mcp-Session-Id → throw error with stale session ID still set → client permanently broken

After

404 with Mcp-Session-Id → clear this._sessionId → throw informative error → caller can re-initialize cleanly

Fixes #1708.

OAuth 2.1 §3.2 requires token endpoint requests to use
application/x-www-form-urlencoded regardless of grant type.

Add an explicit header.set() call immediately before the fetch in
executeTokenRequest() to prevent any addClientAuthentication
implementation from accidentally overriding the Content-Type.

Fixes modelcontextprotocol/inspector#1160
… spec

Per MCP spec §Session Management (2025-03-26), when a client receives HTTP 404
in response to a request containing an Mcp-Session-Id, it MUST clear the session
ID. Previously, both send() (POST) and _startOrAuthSse() (GET) would throw
StreamableHTTPError without clearing _sessionId, permanently breaking the client
with stale session IDs.

This change:
- Adds SdkErrorCode.ClientHttpNotFound
- On 404, clears this._sessionId before throwing, so subsequent calls do not
  keep failing with the same stale session ID
- Leaves full re-initialization to the caller (as InitializeRequest involves
  capability negotiation owned by the Client layer)

Fixes modelcontextprotocol#1708.
@lanxevo3 lanxevo3 requested a review from a team as a code owner March 28, 2026 19:28
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 28, 2026

⚠️ No Changeset found

Latest commit: 77273a0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 28, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 77273a0

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.

StreamableHTTPClientTransport doesn't handle 404 per spec (no session clear + re-init)

1 participant