Skip to content

fix(mcp): recover expired HTTP sessions#1923

Merged
mattzcarey merged 1 commit into
mainfrom
fix/mcp-stale-session-self-heal
Jul 22, 2026
Merged

fix(mcp): recover expired HTTP sessions#1923
mattzcarey merged 1 commit into
mainfrom
fix/mcp-stale-session-self-heal

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Recover a streamable HTTP MCP connection when the server returns HTTP 404 for a session restored from storage.

The client now:

  • identifies HTTP 404 only while probing a restored streamable HTTP session;
  • clears that session id from memory and durable storage;
  • initializes a fresh session and rediscovers capabilities once.

Application-level JSON-RPC errors with code 404 are unchanged, as are failures on sessions negotiated in the current lifetime.

Why this is needed

The MCP SDK skips initialize when given a persisted session id. If the server expires that session while the Agent sleeps, restore appears connected but discovery remains wedged on 404 across later wakes.

The regression still reproduces on current main; the new session-lifecycle test fails there and passes with this fix.

Validation

  • pnpm --filter agents exec vitest run src/tests/mcp — 30 files, 548 tests passed
  • focused package lint and TypeScript checks passed
  • pnpm run check passes through exports, formatting, lint, and packages/agents typechecks; repo-wide typecheck is currently blocked by unchanged main voice errors in examples/voice-agent and voice-providers/assemblyai

Includes a patch changeset for agents.

@changeset-bot

changeset-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: be0bdce

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

This PR includes changesets to release 1 package
Name Type
agents 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: be0bdce

@mattzcarey
mattzcarey force-pushed the fix/mcp-stale-session-self-heal branch from 72bc65b to e3fae6f Compare July 22, 2026 10:24
When discovery receives HTTP 404 on a restored streamable HTTP session, clear the stale id from memory and storage, initialize a new session, and discover once. Keep application-level 404 errors and fresh-session failures unchanged.
@mattzcarey
mattzcarey force-pushed the fix/mcp-stale-session-self-heal branch from e3fae6f to be0bdce Compare July 22, 2026 10:25
@mattzcarey
mattzcarey merged commit 33e59c4 into main Jul 22, 2026
7 checks passed
@mattzcarey
mattzcarey deleted the fix/mcp-stale-session-self-heal branch July 22, 2026 14:54
@github-actions github-actions Bot mentioned this pull request Jul 22, 2026
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