Skip to content

feat(mcp): durable reconnect and authorization-expiry jobs - #2662

Open
mattzcarey wants to merge 6 commits into
refactor/mcp-client-manager-machinefrom
feat/mcp-durable-connection-jobs
Open

mattzcarey wants to merge 6 commits into
refactor/mcp-client-manager-machinefrom
feat/mcp-durable-connection-jobs

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

A failed MCP connect or discovery now schedules a durable connect:<serverId> Lifecycle job. It retries after 30s, 1m, 2m, 5m and 10m, then every 30m, and after 24h records a reconnect-exhausted failure. An auth-expiry:<serverId> job moves a server whose authorization link expired unused to failed, so a dead link is no longer shown and no new one gets minted.

Stacked on #2661, and part of the same work on #2658. Every retry used to live in memory. _connectWithRetry made 3 quick attempts, and then the server sat in failed until the isolate died, which with live sockets could take hours. Nothing woke the object to try again, and the connect after an OAuth callback was a fire-and-forget promise.

Things callers will notice:

  • retries stop when authorization was refused, since every attempt would get the same answer: a 401 the agent can't answer with a link (a revoked static token, no OAuth callback), a 403 left after any step-up, or an OAuth error such as invalid_target or access_denied. The server records an authorization-failed failure. Network errors, 5xx, timeouts, server_error and temporarily_unavailable still back off.
  • these retries add alarm wakes. An object with an unreachable MCP server wakes about 50 times in its first day, until it connects or gives up.
  • recorded failures survive wakes, and only an explicit reconnect clears them: addMcpServer(), connectToServer(), establishConnection() or registerServer()
  • addMcpServer() on a failed server reconnects. It used to throw.
  • a server with a reconnect already scheduled stays failed when the object wakes for another reason, and waits for that job
  • standalone managers take new MCPClientManager(name, version, { reconnect: { delaysMs, giveUpAfterMs } })
  • the manager adds a failure column to cf_agents_mcp_servers the first time it records a failure

pnpm run check, the agents workers suite and the conformance lanes pass. New tests run both jobs through real Lifecycle alarms on a plain Durable Object, including staying failed after eviction, giving up and then reconnecting explicitly, and moving jobs when migrateServerId() renames a server.

The durable reconnect schedule and the link-expiry job here are removed in #2778, which records failures and leaves retrying to reconnectMcpServer(), as Claude Code does. Review refusal and failure recording here, and the rest there.

@agent-think

agent-think Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

🟡 agents import sizes: 4 entry points grew

Entry point Exports Largest gzip change Size now
🟡 agents/mcp 15 resized up to +1.8 KiB (+0.49%) 361.4 KiB
🟡 agents 5 resized up to +1.8 KiB (+0.66%) 271.7 KiB
🟡 agents/chat-sdk 3 resized up to +1.8 KiB (+0.65%) 273.5 KiB
🟡 agents/mcp/client 1 resized +1.7 KiB (+1.02%) 167.2 KiB
Changed exports (24)
Import Gzip change Size now
🟡 agents/mcp#RPCClientTransport +1.8 KiB (+0.49%) 361.4 KiB
🟡 agents/mcp#normalizeServerId +1.8 KiB (+0.49%) 361.4 KiB
🟡 agents#getCurrentAgent +1.8 KiB (+0.66%) 271.7 KiB
🟡 agents/mcp#DurableObjectEventStore +1.8 KiB (+0.49%) 361.4 KiB
🟡 agents/chat-sdk#ChatSdkStateAgent +1.8 KiB (+0.65%) 273.5 KiB
🟡 agents#DEFAULT_AGENT_STATIC_OPTIONS +1.8 KiB (+0.65%) 271.7 KiB
🟡 agents#Agent +1.8 KiB (+0.65%) 271.7 KiB
🟡 agents#StreamingResponse +1.8 KiB (+0.65%) 271.7 KiB
🟡 agents/mcp#RPC_DO_PREFIX +1.8 KiB (+0.49%) 361.4 KiB
🟡 agents/mcp#ElicitRequestSchema +1.8 KiB (+0.49%) 361.4 KiB
🟡 agents/mcp#RPCServerTransport +1.8 KiB (+0.49%) 361.4 KiB
🟡 agents#routeAgentEmail +1.8 KiB (+0.65%) 272 KiB
🟡 agents/mcp#getMcpAuthContext +1.8 KiB (+0.49%) 361.4 KiB
🟡 agents/mcp#StreamableHTTPEdgeClientTransport +1.7 KiB (+0.48%) 361.5 KiB
🟡 agents/mcp#McpAgent +1.7 KiB (+0.48%) 361.4 KiB
🟡 agents/chat-sdk#ChatSdkStateAdapter +1.7 KiB (+0.64%) 274.3 KiB
🟡 agents/chat-sdk#createChatSdkState +1.7 KiB (+0.63%) 274.3 KiB
🟡 agents/mcp#SSEEdgeClientTransport +1.7 KiB (+0.48%) 361.4 KiB
🟡 agents/mcp#createLegacyMcpHandler +1.7 KiB (+0.44%) 395.6 KiB
🟡 agents/mcp#experimental_createMcpHandler +1.7 KiB (+0.43%) 395.8 KiB
🟡 agents/mcp/client#MCPClientManager +1.7 KiB (+1.02%) 167.2 KiB
🟡 agents/mcp#MCP_SERVER_ID_MAX_LENGTH +1.7 KiB (+0.47%) 361.3 KiB
🟡 agents/mcp#WorkerTransport +1.7 KiB (+0.45%) 365.1 KiB
🟡 agents/mcp#createMcpHandler +1.6 KiB (+0.4%) 408.5 KiB
How this works

Each runtime export is bundled on its own, minified, and gzipped. Changes smaller than 100 B, or smaller than 1% and 1 KiB, are ignored. Growth over 10% or 5 KiB is marked 🔴. This report is informational and does not fail CI. The workflow artifact contains every measurement.

Compared 0406eaf1 → 446f3588 · workflow run · reported by agent-think[bot]

devin-ai-integration[bot]

This comment was marked as resolved.

@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 446f358

@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from e253db0 to e9d494e Compare October 22, 2026 14:06
@changeset-bot

changeset-bot Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 446f358

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

@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from 5515fea to d3544e0 Compare October 22, 2026 14:34
@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from d3544e0 to 26a4adc Compare October 27, 2026 12:05
@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from 26a4adc to 075a26b Compare October 27, 2026 13:04
@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from 075a26b to b888291 Compare October 27, 2026 13:49
@mattzcarey
mattzcarey added this pull request to stack #2750 October 27, 2026 18:20
@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from b888291 to f4e4a5f Compare October 27, 2026 20:30
@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from f4e4a5f to 833e3b6 Compare October 27, 2026 22:41
@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from 833e3b6 to 16a368b Compare October 28, 2026 10:05
devin-ai-integration[bot]

This comment was marked as resolved.

@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 found 1 new potential issue.

⚠️ 1 issue in files not directly in the diff

⚠️ MCP client release lacks a changeset

When this feature reaches main without another changeset, agents receives no release entry. The release workflow versions packages through changeset version, so the new public reconnect option remains unpublished.

Devin Review

- connect:<id> Lifecycle job retries failed connects/discovery on a
  30s..30m backoff, one bounded attempt per run, giving up after 24h with a
  recorded reconnect-exhausted failure
- auth-expiry:<id> job fails a server whose authorization link expired
  unused (authorization-expired), instead of minting a new link
- recorded failures persist in a failure column (added on first use);
  restore shows them without connecting, explicit reconnects clear them
- addMcpServer reconnects a failed server instead of throwing
- RPC restore builds its transport from the persisted binding and shares
  the HTTP restore path
- MCPClientManagerOptions.reconnect tunes the schedule
…ration

- restore leaves a server with a pending connect job failed (showing the
  job's last error) instead of connecting early
- migrateServerId moves connect and auth-expiry jobs to the new id
- the OAuth callback clears auth_url before redeeming the state, so a dead
  link on wake always means the flow expired unused
- reaching ready clears the connection error left by a recorded failure
A durable reconnect only helps when the failure can pass. A 401 the agent
cannot answer with a link (a revoked static token, no OAuth callback), a 403
left after any step-up, or an OAuth error such as invalid_target,
invalid_scope or access_denied gets the same answer on every attempt, so the
server now records an authorization-failed failure instead of retrying for a
day. Network errors, 5xx, timeouts, server_error and temporarily_unavailable
still back off as before; invalid_grant still starts a fresh authorization.
@mattzcarey
mattzcarey force-pushed the feat/mcp-durable-connection-jobs branch from 8a3b141 to 446f358 Compare October 29, 2026 19:28
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