Skip to content

fix(core): reuse MCP OAuth refresh answers for spent tokens - #53275

Closed
rekram1-node wants to merge 1 commit into
devfrom
oauth-refresh-reuse
Closed

rekram1-node wants to merge 1 commit into
devfrom
oauth-refresh-reuse

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Summary

Refresh tokens rotate: once the token server answers a refresh, the presented refresh token is spent. Concurrent refreshes of the same token already share one request, but that request was forgotten as soon as the answer arrived, before the new tokens were saved. A connection sharing the credential that read the stored token in that window sent the spent token itself and got a 429 or invalid_grant:

  • A 429 makes the SDK abandon the refresh, and the server ends up needs_auth.
  • invalid_grant asks us to drop the credential. The guard against dropping a newer token does not help when the new token has not been saved yet, so the user has to sign in again.

This hits hardest when access tokens expire together and every connection refreshes at once.

Fix. A successful refresh answer is now kept for 60 seconds, keyed by the spent token. A later refresh with that token gets the same answer, including the new tokens, without contacting the server. Failed answers are forgotten immediately, so a retry still reaches the server.

This covers every connection in one process. Coordinating refreshes across processes that share credential storage is out of scope.

Verification

  • Checked against a local token server that rotates refresh tokens and rejects reuse:
    • Two concurrent refreshes share one request.
    • A later refresh with the spent token gets the saved answer without contacting the server.
    • A refresh with the new token reaches the server.
    • A failed refresh is not reused, and its retry reaches the server.
  • bun test test/mcp.test.ts test/mcp-oauth.test.ts and bun run check pass.

@Antonio-Faure

Copy link
Copy Markdown

While investigating #60612 I wrote a focused regression test for this: two sequential refreshes with the spent token against a server that rejects reuse, asserting one network request and the same rotated response. My now-closed #60896 had it in packages/core/test/mcp-oauth.test.ts (visible in its diff). Happy to open a small test-only PR against your branch, or paste the test here, if useful.

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants