fix(auth): deduplicate concurrent token refresh requests#1813
Open
Aboudjem wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
fix(auth): deduplicate concurrent token refresh requests#1813Aboudjem wants to merge 2 commits intomodelcontextprotocol:mainfrom
Aboudjem wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
When multiple requests detect a 401 simultaneously, they each call auth() which races to refresh using the same refresh token. With OAuth servers that use rotating refresh tokens (e.g. Atlassian, Asana), this triggers RFC 6819 §5.2.2.3 replay detection — the second refresh invalidates the first's new token, revoking the entire token family. Add a Map-based singleton guard that stores the in-flight auth promise keyed by provider. Concurrent callers await the existing promise instead of starting a new refresh. The promise is cleared on resolve/reject so future refreshes proceed normally. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
auth()where concurrent 401 handlers each independently refresh the same token, causing rotating refresh tokens to be reused and triggering RFC 6819 §5.2.2.3 replay detectionMap<OAuthClientProvider, Promise<AuthResult>>— when a refresh is already in-flight for a provider, concurrent callers await the existing promise instead of starting a new oneProblem
When two or more requests for the same OAuth MCP server arrive in parallel:
auth()refreshAuthorization()with the same refresh tokenSolution
Store the in-flight
auth()promise in a module-levelMapkeyed by provider instance. If a concurrent call arrives for the same provider, it awaits the existing promise. The entry is cleaned up in afinallyblock so subsequent refreshes proceed normally.This is the same pattern used by
msal-browserandaxios-auth-refresh.Test plan
Closes #1760