Skip to content

fix(mcp): recover expired HTTP sessions#1923

Open
mattzcarey wants to merge 2 commits into
mainfrom
fix/mcp-stale-session-self-heal
Open

fix(mcp): recover expired HTTP sessions#1923
mattzcarey wants to merge 2 commits into
mainfrom
fix/mcp-stale-session-self-heal

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • detect when a streamable HTTP session restored from storage is rejected by the server with HTTP 404
  • clear the stale session id from memory and storage before performing a fresh initialize handshake
  • rediscover once on the new session, while leaving ordinary discovery failures unchanged

Why

The MCP SDK skips initialization when given a persisted session id. If the server expired that session while the Durable Object slept, restore appeared connected but every discovery request returned 404. Because the id remained persisted, the same unusable session came back on every wake.

Recovery is deliberately narrow: it only handles the SDK's StreamableHTTPError with status 404 while probing a restored session. A JSON-RPC application error with code 404, or an HTTP 404 on a session negotiated in the current lifetime, does not trigger a reconnect.

Review follow-up

  • narrowed the detector from any numeric code: 404 to the SDK's actual StreamableHTTPError, avoiding collisions with JSON-RPC application errors
  • added a regression test proving the persisted id is cleared before reconnect, including when that fresh reconnect fails
  • no verifier findings remain

Validation

  • pnpm --filter agents exec vitest run src/tests/mcp — 30 files, 536 tests passed
  • pnpm run check — passed
  • integration coverage verifies stale-session rejection, fresh initialization, rediscovery, storage replacement, failed-reconnect cleanup, and the non-recovery guards

Includes a patch changeset for agents.


Open in Devin Review

@changeset-bot

changeset-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2ff3881

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

This PR includes changesets to release 2 packages
Name Type
agents Patch
@cloudflare/agent-think 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

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Jul 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1923

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1923

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1923

create-think

npm i https://pkg.pr.new/create-think@1923

hono-agents

npm i https://pkg.pr.new/hono-agents@1923

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1923

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1923

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1923

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1923

commit: 2ff3881

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.

1 participant