Skip to content

fix(mcp): harden connection recovery#1924

Open
mattzcarey wants to merge 3 commits into
mainfrom
fix/mcp-reconnect-hardening
Open

fix(mcp): harden connection recovery#1924
mattzcarey wants to merge 3 commits into
mainfrom
fix/mcp-reconnect-hardening

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • make per-server retry settings apply when connectToServer() reports a failed connection as a resolved result
  • drain connection work under the old id before migrating a server to a stable id
  • close connections replaced through the deprecated connect() path and clear their terminated session id from storage

Why

tryN() retries thrown errors, while MCP connection failures are returned as { state: "failed" }. This meant the documented retry budget stopped after one attempt. Separately, stable-id migration could rename a connection while its restore task still addressed the old id, leaving discovery orphaned. Replacing a connection through connect() also dropped the old transport without closing it.

Review follow-up

  • connection failures now drive tryN() while authenticating still returns immediately
  • migration ignores a rejected background task but waits for all work under the old id to become quiescent, including work registered while an earlier task settles
  • migration deliberately has no timeout: renaming while old-id work still runs recreates the orphaned-discovery bug; callers already wait for these same connection attempts in the normal path
  • closing a replaced streamable HTTP connection also removes its now-terminated persisted session id
  • no verifier findings remain

Validation

  • pnpm --filter agents exec vitest run src/tests/mcp — 30 files, 541 tests passed
  • pnpm run check — passed
  • regression tests cover retry exhaustion/success, OAuth parking, restore-vs-migration ordering, work added during migration, rejected tasks, transport closing, and persisted session cleanup

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: f1ac7be

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@1924

@cloudflare/ai-chat

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

@cloudflare/codemode

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

create-think

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: f1ac7be

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