Skip to content

Keep MCP refresh tokens valid across Cloudflare locations - #229

Merged
tnkuehne merged 4 commits into
mainfrom
mcp-grant-durable-object
Nov 19, 2026
Merged

tnkuehne merged 4 commits into
mainfrom
mcp-grant-durable-object

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

MCP clients connected to a hosted installation are sometimes told their connection has expired and must reconnect, although the grant is still valid.

@cloudflare/workers-oauth-provider stores each grant as one grant:{userId}:{grantId} KV record. Every refresh rotates the refresh token, and the record accepts only the current and the previous token. Cloudflare KV serves reads from a per-location cache for up to 60 seconds. A client that refreshes before each request and reaches the token endpoint through changing Cloudflare locations can hit a location that still holds an older copy of the grant. That location answers the client's current token with 400 invalid_grant ("Invalid refresh token") without calling tokenExchangeCallback, and the client asks the person to reconnect.

The upstream rotation race is tracked in cloudflare/workers-oauth-provider#214. 1.2.4 is the current release and has no consistent storage option.

What

  • McpGrants Durable Object (packages/app-runtime/src/mcp-grants.ts): one SQLite-backed object per grant owner holds that owner's grant: records with their expiration and key metadata. Reads always return the latest write. Expired grants are hidden immediately and deleted by an alarm. An owner without grants leaves no storage.
  • Storage adapter for the OAuth library: mcpAuthorizationServer passes a KV-compatible object that routes grant: keys to the owner's object. Clients, access tokens, consent and upstream transactions stay in KV. Grants are listed per owner only, which covers revokeExistingGrants and listUserGrants. A listing across all owners is rejected rather than silently missing grants. Widefleet does not call such a listing.
  • Existing connections: the first access for an owner copies that owner's grants from KV into the object. Connected clients keep working without reconnecting.
    • KV keeps its copies for five minutes. Until the object writes an imported grant, the grant follows its KV record, so a copy read from a stale location cache is replaced by the newer record.
    • The alarm then deletes the KV copies. A failed delete fails the alarm, which Cloudflare retries.
    • The import marker is never removed, so a grant revoked after the import cannot come back from a KV copy.
  • Configuration: WIDEFLEET_MCP_GRANTS binding and Durable Object migration v4 in the runtime's wrangler.jsonc. Hosted configuration already takes the runtime's bindings and migrations from this file.
  • Self-hosted runtimes are unchanged. celld reads KV from the namespace's owning node and never returns stale values. The agent does not configure the binding, and the runtime keeps grants in KV when the binding is absent.

Concurrent refreshes of the same token remain last-write-wins, as in the library's design. This change only removes stale reads.

Verification

  • mcp-login-runtime.test.ts (19 tests) now runs with the Durable Object binding. New tests:
    • Grants never reach KV.
    • Sequential rotations succeed, and a token several rotations old is rejected.
    • A grant stored only in KV moves into the object on its next refresh. Its KV copy is deleted when the migration window ends.
    • A stale import is replaced by the newer KV record. This test fails without the recheck.
    • A grant revoked after the import is not imported again from its remaining KV copy.
    • Reconnecting the same client revokes its earlier grant through the per-owner listing.
    • Expired grants are not returned.
  • RUN_CLOUDFLARE_TESTS=1 pnpm exec vitest run apps/control-plane/tests/cloudflare: 246 passed, after the CI build steps for the Cloudflare bundles. The runtime's wrangler deploy --dry-run lists the new Durable Object binding.
  • pnpm check passes.
  • pnpm exec vitest run: 1,106 passed. Three tests timed out or needed built artifacts under full-suite load; each passes when run alone after pnpm build.
  • Not verified: a deployment to Cloudflare. KV's per-location cache does not occur in Miniflare, so the stale read itself is not reproduced in a test.

RetriggerView in GreptileConfidence Score: 5/5

The reviewed changes appear safe to merge.

Summary

Moves hosted MCP grants from KV into per-owner Durable Objects so refresh tokens use the latest grant record. The latest update rereads untouched imported grants before deleting their KV copies.

  • Cloudflare MCP refreshes read grants from one current store per owner.
  • Existing MCP grants move into the new store without interrupting connections.

The earlier migration and cleanup findings are addressed. No new actionable issues were found.

Reviews (8) · Last reviewed commit: "Take the KV record for unread imported g..." · Reviewed by Greptile

The OAuth library rotates a grant's refresh token on every refresh and
accepts only the current and the previous token. Cloudflare KV serves
reads from a per-location cache for up to 60 seconds, so a refresh that
reaches another location shortly after a rotation can read an older
grant and reject the client's current token with invalid_grant. MCP
clients then ask the person to reconnect.

On Cloudflare, grant records now live in one SQLite-backed Durable
Object per grant owner, which always reads its latest write. Clients,
access tokens and transactions stay in KV. Grants created before this
change move from KV into the object on first use, so existing
connections keep working. celld reads KV from the namespace's owning
node and never returns stale values, so self-hosted runtimes keep
grants in KV.
Comment thread packages/app-runtime/src/mcp-grants.ts Resolved
Comment thread packages/app-runtime/src/mcp-grants.ts Resolved
An import can read a grant from a stale KV location cache. KV now keeps
its copies for five minutes after the import, and an imported grant
follows its KV record until the object writes it, so a newer record
replaces the stale copy. The alarm deletes the KV copies after this
window and is retried when a delete fails.

The import marker is never removed, so a grant revoked after the import
cannot be imported again from a remaining KV copy.
Comment thread packages/app-runtime/src/mcp-grants.ts Resolved
The alarm now stops imported grants from following KV before it deletes
their KV copies, so a copy that is already deleted can no longer read as
a revocation. Only the keys whose delete failed stay in the migration
record for the retried alarm.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs b51b105 Nov 19 2026, 05:44 PM

An imported grant followed KV only when it was read. If no refresh
arrived during the migration window, a copy imported from a stale KV
location cache became final. The alarm now rereads each imported grant
from KV before it stops following KV. The window outlasts KV's location
cache, so this read is current.
@tnkuehne
tnkuehne merged commit 26a1acc into main Nov 19, 2026
16 checks passed
@tnkuehne
tnkuehne deleted the mcp-grant-durable-object branch November 19, 2026 17:53
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