Repository navigation
Keep MCP refresh tokens valid across Cloudflare locations - #229
Merged
Merged
Conversation
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.
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.
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.
Deploying with
|
| 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-providerstores each grant as onegrant:{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 with400 invalid_grant("Invalid refresh token") without callingtokenExchangeCallback, 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
McpGrantsDurable Object (packages/app-runtime/src/mcp-grants.ts): one SQLite-backed object per grant owner holds that owner'sgrant: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.mcpAuthorizationServerpasses a KV-compatible object that routesgrant:keys to the owner's object. Clients, access tokens, consent and upstream transactions stay in KV. Grants are listed per owner only, which coversrevokeExistingGrantsandlistUserGrants. A listing across all owners is rejected rather than silently missing grants. Widefleet does not call such a listing.WIDEFLEET_MCP_GRANTSbinding and Durable Object migrationv4in the runtime'swrangler.jsonc. Hosted configuration already takes the runtime's bindings and migrations from this file.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: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'swrangler deploy --dry-runlists the new Durable Object binding.pnpm checkpasses.pnpm exec vitest run: 1,106 passed. Three tests timed out or needed built artifacts under full-suite load; each passes when run alone afterpnpm build.

Confidence 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.
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