Skip to content

Keep offline MCP grants working after their approving session expires - #2587

Merged
RhysSullivan merged 1 commit into
UsefulSoftwareCo:v2from
hemaaanth:fix/refresh-expired-session-2550
Nov 22, 2026
Merged

RhysSullivan merged 1 commit into
UsefulSoftwareCo:v2from
hemaaanth:fix/refresh-expired-session-2550

Conversation

@hemaaanth

Copy link
Copy Markdown
Contributor

Fixes #2550.

Problem

@better-auth/oauth-provider records 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 in validateOpaqueAccessToken, and bearer-access.ts does 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_access grant 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, and onDelete: "set null" detaches the tokens.

Change

  • Patch (patches/@better-auth%[email protected]). handleRefreshTokenGrant names the session on the new access and ID tokens only while it is live. The check is the one validateRefreshToken already makes. createRefreshToken keeps 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-provider 1.7.26. The hunks can go when Executor moves to 1.7.26 or later. patches/better-auth-refresh-expired-session.md explains 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.
  • E2E (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):
    • The server clock moves forward 8 days. The test refreshes twice and lists tools after each refresh.
    • The approving session is expired directly in storage. The server starts, and the test refreshes and lists tools. This covers grants that are already dead when the fix is deployed.

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 and usableGrants reverted, and the tests kept, both fail at initialize MCP client after the refresh.
  • bun run pr:check --description: static, apps-version and release-notes pass. bun run format leaves no changes.
  • v2 shares no history with main, so pr:check ran with v2's base served as main.
  • Not run: the description step (e2e:check-description). It reads CI's job table from .github/workflows, which v2 does not have. The e2e block below is for the change once it reaches main.
local-mcp-connections.spec.ts

🤖 Generated with Claude Code

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

[bug] v2: Local offline MCP grants stop working seven days after the server starts, while refresh still succeeds

2 participants