Repository navigation
Keep offline MCP grants working after their approving session expires - #2587
Merged
RhysSullivan merged 1 commit intoNov 22, 2026
Merged
Conversation
A refresh copied the approving browser session's id to the new access token, even after that session had expired. Token validation refuses a token whose session has expired, so the refresh answered 200 and the next MCP request answered 401. Every offline_access grant a browser session approved stopped about seven days after the session was last used. On Local, every grant from one server run stopped together. The refresh grant now names the session on new access and ID tokens only while it is live, as refresh token introspection already does. The rotated refresh token keeps its parent's session for back-channel logout. This is the change in Better Auth PR 12723, released in oauth-provider 1.7.26. usableGrants counts an unexpired refresh token whatever its session, to match. Fixes UsefulSoftwareCo#2550. Release-note: Fix: MCP clients signed in with offline access keep working after the browser session that approved them expires. Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Fixes #2550.
Problem
@better-auth/oauth-providerrecords the browser session that approved a grant on its tokens. A refresh copied that session to the new access token, even after the session had expired. Token validation refuses a token whose session has expired. Better Auth does this invalidateOpaqueAccessToken, andbearer-access.tsdoes it too. So the refresh answered 200, and the next MCP request with the new token answered 401.A browser session expires after seven days without use. Every
offline_accessgrant it approved stopped at that moment. On Local, consent is approved by an internal operator session, so every grant from one server run stopped together. Signing out does not cause this: it deletes the session, andonDelete: "set null"detaches the tokens.Change
patches/@better-auth%[email protected]).handleRefreshTokenGrantnames the session on the new access and ID tokens only while it is live. The check is the onevalidateRefreshTokenalready makes.createRefreshTokenkeeps the parent's session on the rotated refresh token, so deleting the session later still runs back-channel logout for it. This is the same change as better-auth/better-auth#12723, released in@better-auth/oauth-provider1.7.26. The hunks can go when Executor moves to 1.7.26 or later.patches/better-auth-refresh-expired-session.mdexplains the patch.usableGrants(packages/mcp-auth). It now counts a grant's unexpired, unrevoked refresh token whatever its session, because that token can now refresh. Access tokens still need a live session. Without this change, the connections list would hide grants that work.local-mcp-connections.spec.ts). These are two scenarios adapted from the reporter's fork (Local offline MCP grants keep working after the operator session's seven days shayaansultan/executor#1):Trade-off
A grant now outlives an expired browser session, as it already outlives a sign-out. Revoking the grant still ends it. An access token issued before the fix keeps naming the expired session and stays refused until the client refreshes.
Differences from the fix posted in the issue
The hunk posted in the issue also fixes the 401. It differs from upstream in one way: the rotated refresh token loses its session, so a later deletion of that session no longer reaches it for back-channel logout. This PR follows upstream so the patch can be dropped cleanly on upgrade.
Verification
bun run e2e:local --test-name 'Local offline MCP grants': both scenarios pass. With the patch hunks andusableGrantsreverted, and the tests kept, both fail atinitialize MCP clientafter the refresh.bun run pr:check --description:static,apps-versionandrelease-notespass.bun run formatleaves no changes.v2shares no history withmain, sopr:checkran withv2's base served asmain.descriptionstep (e2e:check-description). It reads CI's job table from.github/workflows, whichv2does not have. Thee2eblock below is for the change once it reachesmain.🤖 Generated with Claude Code