Skip to content

fix: reinitialize expired streamable sessions#2150

Open
he-yufeng wants to merge 2 commits into
modelcontextprotocol:mainfrom
he-yufeng:fix/streamable-http-session-expiry-reinit
Open

fix: reinitialize expired streamable sessions#2150
he-yufeng wants to merge 2 commits into
modelcontextprotocol:mainfrom
he-yufeng:fix/streamable-http-session-expiry-reinit

Conversation

@he-yufeng

Copy link
Copy Markdown

Summary

Fixes #1476.

When a Streamable HTTP server restarts, a client can keep sending POST requests with a stale MCP-Session-Id. The SDK currently reports the server's 404 response as ClientHttpNotImplemented and leaves the expired session in place.

This change handles a 404 from a request that used a session ID as an expired-session signal:

  • clear the cached session ID
  • let Client run a fresh initialize handshake on the same transport
  • retry the original message once with the new session ID

The retry is limited to one attempt so repeated 404s still surface as errors instead of looping.

To verify

  • pnpm --filter @modelcontextprotocol/client exec vitest run test/client/streamableHttp.test.ts -t "reinitializes and retries once"
  • pnpm --filter @modelcontextprotocol/client exec vitest run test/client/streamableHttp.test.ts
  • pnpm --filter @modelcontextprotocol/client typecheck
  • pnpm --filter @modelcontextprotocol/client lint
  • git diff --check
  • pre-push hook: full workspace typecheck, build, and lint

@he-yufeng he-yufeng requested a review from a team as a code owner May 24, 2026 09:48
@changeset-bot

changeset-bot Bot commented May 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 116b048

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

pkg-pr-new Bot commented May 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/codemod@2150

@modelcontextprotocol/server

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

@modelcontextprotocol/server-legacy

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

@modelcontextprotocol/express

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

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/fastify@2150

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 116b048

@he-yufeng

Copy link
Copy Markdown
Author

Added a patch changeset for @modelcontextprotocol/client. Local validation: pnpm exec changeset status --since upstream/main shows the client patch bump, git diff --check passed, and the pre-push hook completed typecheck, build, and lint successfully.

@he-yufeng he-yufeng force-pushed the fix/streamable-http-session-expiry-reinit branch from 80b3f60 to b2a0df1 Compare June 6, 2026 21:01
@he-yufeng

Copy link
Copy Markdown
Author

Rebased on current main and force-pushed the branch.

Validated locally:

  • pnpm changeset status --since origin/main (@modelcontextprotocol/client patch)
  • pnpm --filter @modelcontextprotocol/client typecheck
  • pnpm --filter @modelcontextprotocol/client lint
  • pnpm --filter @modelcontextprotocol/client exec vitest run test/client/streamableHttp.test.ts (54 passed)
  • git diff --check origin/main..HEAD

The pre-push hook also completed the repo-wide build, typecheck, and lint steps successfully.

@he-yufeng he-yufeng force-pushed the fix/streamable-http-session-expiry-reinit branch from b2a0df1 to 0c15f0c Compare June 6, 2026 23:15
@he-yufeng

Copy link
Copy Markdown
Author

Updated the branch to include the e2e expectation changes for session-expiry recovery.

The red e2e job was from two stale expectations:

  • client-transport:http:session-404-reinitialize was still marked as a known failure even though this PR implements that behavior.
  • client-transport:http:404-surfaces now explicitly covers the no-recovery-hook fallback path, so the transport still surfaces a 404 when no session recovery callback is installed.

Validation:

pnpm --filter @modelcontextprotocol/client exec vitest run test/client/streamableHttp.test.ts
# 54 passed
pnpm --filter @modelcontextprotocol/test-e2e test -- scenarios/transport-http.test.ts
# 24 passed, 1 expected fail
pnpm --filter @modelcontextprotocol/client typecheck
pnpm --filter @modelcontextprotocol/client lint
pnpm --filter @modelcontextprotocol/client build
pnpm --filter @modelcontextprotocol/test-e2e typecheck
pnpm --filter @modelcontextprotocol/test-e2e lint
git diff --check origin/main..HEAD

The pre-push hook also completed full repo typecheck, build, and lint before the force-push.

@he-yufeng

Copy link
Copy Markdown
Author

Rebased onto current main; no conflicts.

Focused validation after the rebase:

pnpm --filter @modelcontextprotocol/client exec vitest run test/client/streamableHttp.test.ts
pnpm changeset status --since upstream/main
git diff --check upstream/main..HEAD

Result: client streamableHttp.test.ts passed (54 passed).

I also tried the touched e2e file:

pnpm --filter @modelcontextprotocol/test-e2e exec vitest run scenarios/transport-http.test.ts

That is blocked locally before collecting tests because the current workspace install cannot resolve @modelcontextprotocol/server-legacy/sse. This is the same local server-legacy workspace resolution issue that also breaks the full pre-push hook here, so I did not widen this PR into environment/package-linkage changes. Pushed with --no-verify after the focused client validation passed.

@he-yufeng he-yufeng force-pushed the fix/streamable-http-session-expiry-reinit branch 3 times, most recently from d2cf398 to 7abec0b Compare June 12, 2026 15:33
@he-yufeng he-yufeng force-pushed the fix/streamable-http-session-expiry-reinit branch from 7abec0b to 116b048 Compare June 12, 2026 21:45
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.

Client received "no session ID or not initialization request" when server restart

1 participant