Skip to content

fix(mcp): stop MCP recovery from retrying or minting links on its own - #2758

Open
mattzcarey wants to merge 3 commits into
test/mcp-reauth-integrationfrom
fix/mcp-reauth-holes
Open

mattzcarey wants to merge 3 commits into
test/mcp-reauth-integrationfrom
fix/mcp-reauth-holes

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Closes the remaining ways MCP recovery acted on its own when only a user should. addMcpServer() on a registered server reconnects it when it isn't connected, at most once per first backoff step, but leaves alone a server stopped for a reason only a person can fix (authorization refused or denied, the step-up limit, reconnecting gave up). The new reconnectMcpServer(id) retries those. Denied callbacks, rejected client registration, a 401 right after a completed authorization, and links nobody completes now stop with a recorded failure instead of retrying or showing a link that can't work.

Stacked on #2751, part of the work on #2658. I found these auditing everything the stack does without a user:

  • the docs call addMcpServer() from onConnect and onRequest, and two examples call it from onStart. Because it counted as an explicit reconnect, every wake or connection cleared recorded failures, skipped the reconnect backoff and reset the step-up budget. It still reconnects what can pass, so apps that call it to bring a server back keep working.
  • a denied or failed callback marked the server failed in memory only, so the next wake started a new flow the user had just turned down
  • the SDK reports a rejected registration as RegistrationRejectedError, not an OAuthError, so a 400 from /register was retried for a day
  • after a completed authorization, a server that still answered 401 got the spent link saved again, which every later click rejected with "State not found or already used"
  • the link-expiry job rescheduled every ten minutes for as long as a custom provider kept the state valid, potentially forever

Callers will notice:

  • addMcpServer() reconnects a failed server (on main it threw) and replaces a dead link (as on main), at most once per 30 seconds by default. For a server stopped for a reason only a person can fix, it throws that reason and names reconnectMcpServer(id).
  • a "Reconnect" button should call reconnectMcpServer(id). It returns what addMcpServer() returns, including a fresh authUrl, and retries any failed server.

pnpm run check and the full agents workers suite pass. New integration tests cover each case, and each one fails without its fix.

@changeset-bot

changeset-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 9f93362

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

devin-ai-integration[bot]

This comment was marked as resolved.

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 9f93362

devin-ai-integration[bot]

This comment was marked as resolved.

@agent-think

agent-think Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

✅ agents import sizes: no significant changes (b61ea074 → 9f933622, workflow run)

@mattzcarey
mattzcarey added this pull request to stack #2750 October 29, 2026 18:57
@mattzcarey
mattzcarey force-pushed the fix/mcp-reauth-holes branch from baa16aa to 197c6b0 Compare October 29, 2026 18:58

@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 reconnect API lacks a release changeset

Without a changeset for reconnectMcpServer(), the release workflow has no entry to version this public API change. It cannot trigger an agents release on its own.

Devin Review

…ks on its own

- addMcpServer() for a registered server reports its state and never
  reconnects, so calling it from onStart or onConnect cannot clear a recorded
  failure, skip the reconnect backoff or reset the step-up budget; a dead link
  is recorded as expired instead of replaced. New reconnectMcpServer(id) is the
  explicit retry.
- a denied or failed OAuth callback is recorded, so a wake does not start
  another flow the user just turned down
- dynamic client registration rejected with a 4xx (other than 429) counts as
  refused instead of being retried for a day
- the manager remembers the state each callback redeemed; a 401 right after a
  completed authorization no longer re-shows that spent link and is recorded
  as refused
- an authorization link stays pending for at most 24h even when a custom
  provider would still redeem its state
…ept one a person must fix

addMcpServer() for a registered server reconnects it when it failed for a
reason that can pass, or replaces a link that can no longer complete, at
most once per first backoff step so calling it on every request cannot
hammer a down server. A server stopped for a reason only a person can fix
(authorization refused or denied, the step-up limit, reconnecting gave up)
throws that reason, naming reconnectMcpServer(id). The reconnect job now
records when its latest attempt failed.
@mattzcarey
mattzcarey force-pushed the fix/mcp-reauth-holes branch from 197c6b0 to 9f93362 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