fix: deduplicate concurrent OAuth refresh token exchanges#1787
Open
claygeo wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: deduplicate concurrent OAuth refresh token exchanges#1787claygeo wants to merge 1 commit intomodelcontextprotocol:mainfrom
claygeo wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
When multiple parallel requests receive 401 responses, each independently calls onUnauthorized -> handleOAuthUnauthorized -> refreshAuthorization with the same refresh token. OAuth providers using rotating refresh tokens (Atlassian, Asana, per RFC 6819 5.2.2.3) detect the second use as a replay attack and revoke the entire token family, logging the user out. The fix adds promise coalescing in adaptOAuthProvider: the first 401 handler stores its refresh promise, and all concurrent 401s await the same promise instead of initiating separate refresh exchanges. The promise is cleared after completion (success or failure) so future token refreshes proceed normally. Closes modelcontextprotocol#1760
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
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.
Problem
When multiple parallel MCP requests receive 401 responses simultaneously, each independently calls
onUnauthorized→handleOAuthUnauthorized→refreshAuthorizationwith the same refresh token. OAuth providers using rotating refresh tokens (Atlassian, Asana, and others per RFC 6819 §5.2.2.3) detect the second use of a refresh token as a replay attack and revoke the entire token family, logging the user out.How it happens
There is no concurrency guard in
adaptOAuthProviderorhandleOAuthUnauthorized— each 401 handler runs independently.Solution
Add promise coalescing in
adaptOAuthProvider(): the firstonUnauthorizedcall stores its refresh promise. All concurrent 401 handlers await the same promise instead of initiating separate refresh exchanges. The promise is cleared after completion (success or failure) so future refreshes proceed normally.This is the standard pattern for deduplicating concurrent token refresh operations, used by libraries like
axios-auth-refresh,msal-browser, and Apollo Client.Changes
packages/client/src/client/auth.ts— AddedinflightRefreshpromise coalescing inadaptOAuthProvider()Test Plan
refreshAuthorizationcall made → both requests retry with new tokenCloses #1760