Repository navigation
fix(mcp): recover an MCP OAuth refresh whose response never reached the client - #259
Open
agent-relay-code[bot] wants to merge 2 commits into
Open
agent-relay-code[bot] wants to merge 2 commits into
agent-relay-code[bot] wants to merge 2 commits into
Conversation
…he client A refresh rotates the pair before the response is written. When the response was lost and the client retried with its retired refresh token more than 30 seconds later, the retry was treated as token reuse and the whole grant was revoked, so the connector had to be reconnected. Each rotation now records the exact successor on the retired token, and bearer admission records the first use of each pair. A retired token whose successor pair has never been used rotates that pair again and returns the new pair on the same grant, for up to 36 hours after the first rotation and never past the retired token's own expiry. The re-issued pair keeps its refresh expiry. A retry within 30 seconds of a recovery is refused without revoking. Reuse after the successor pair was used still revokes the grant. Bearer admission now refuses a token that was rotated or re-issued away between the read and the first-use mark, so an unused pair is never both used and re-issued. Revoking a retired refresh token at the revocation endpoint now ends its grant. Refresh decisions are logged as `mcp_oauth_refresh` without token material. Migration 0084 adds the columns. Pairs rotated before the upgrade have no recorded successor and keep the previous behaviour. Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
|
Important Review skippedBot user detected. To trigger a single review, invoke the
|
This branch has not been deployed
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.
What
A hosted MCP connector lost its connection roughly a day after each reconnect: a timeout, then HTTP 401 on every call until the user reconnected.
A refresh rotates the token pair before the response is written. When that response never reaches the client and the client retries with the refresh token it still holds more than 30 seconds later, the server treated the retry as refresh-token reuse and revoked the whole grant. This is the self-hosted twin of the same fix on the hosted edition, and it follows the same rules.
Change
mcp_oauth_sessions.current_pair_used_at). Rotation clears it.invalid_grantand revokes nothing (two retries racing each other).mcp_oauth_refreshwith an outcome and reason and no token material.Behaviour changes to review
mcp_oauth_sessions.current_pair_used_atand, onmcp_oauth_retired_refresh_tokens,successor_token_hash,refresh_token_expires_atandreissued_at. All nullable, no backfill. The verifier schema manifest is refreshed in its own commit.invalid_grant, and one for the wrong resourceinvalid_target, in both cases without revoking and before the 30-second check. The hosted edition revokes on such a mismatch after 30 seconds. This keeps the existing self-hosted answers.The agent identity and the relay inbox position are untouched: recovery keeps the grant, and both are bound to the grant.
Checks
Local, on this head, one suite at a time:
tsc --noEmit: passtest/mcp-oauth-token-flow.test.ts: 36/36 (20 new cases: a lost response is re-issued, and re-issued again when that response is lost too; a retry racing a recovery or the rotation is refused without revoking; two concurrent recoveries; recovery refused once the successor pair was used, past 36 hours, past the retired token's own expiry, for another client id, and for a retired row from before the upgrade; only the exact successor session is re-issued on a grant with several sessions; re-issue keeps the refresh expiry; revoking a retired token ends its grant)test/mcp-oauth-sessions.test.ts: 10 pass, 1 skippedmcp-oauth-admission57/57,mcp-oauth-store11/11,mcp-oauth-http16/16,mcp-oauth-public-handlers5/5,mcp-oauth-departure4/4,mcp-oauth-oidc6/6,grok-routines21/21,migrations11/11,mcp61/61mcp-oauth-postgres(4) andcloud-import-verify(21) need a PostgreSQL URL and are skipped locally; CI runs them.One case in
test/mcp-oauth-sessions.test.tsneeds a PostgreSQL URL and is skipped locally, as on main.Held for the owner's merge decision.
🤖 Generated with Claude Code
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.Summary by cubic
Fixes MCP OAuth connections being revoked when a refresh response never reached the client.
Previously a refresh rotated the token pair before the response was written; if that response was lost and the client retried more than 30 seconds later with the refresh token it still held, the retry counted as token reuse and revoked the entire grant, forcing a reconnect.
Now each rotation records its exact successor token hash on the retired token, and bearer admission records first use of each pair. If the successor pair has never been used, a retired refresh token re-rotates that pair and returns the new pair on the same grant, for up to 36 hours after the first rotation and never past the retired token's own expiry. Re-issued pairs keep their refresh expiry, so recovery never extends the grant. A retry within 30 seconds of a rotation or recovery is refused without revoking; reuse after the successor pair was used still revokes the grant. Revoking a retired refresh token at the revocation endpoint now ends its grant. Each refresh logs one
mcp_oauth_refreshline with outcome and reason, never token material.Behavior changes
invalid_grant, one for the wrong resourceinvalid_target, without revoking — keeping the existing self-hosted answers.Written for commit 9c2d36a. Summary will update on new commits.