fix: accumulate OAuth scopes on 401/403 instead of overwriting#1657
Open
rechedev9 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: accumulate OAuth scopes on 401/403 instead of overwriting#1657rechedev9 wants to merge 1 commit intomodelcontextprotocol:mainfrom
rechedev9 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Replace raw `this._scope = scope` assignments with a `mergeScopes()` utility that unions existing and incoming scope strings. Prevents infinite re-authorization loops when an MCP server uses per-operation progressive authorization (RFC 6750 §3.1). Fixes modelcontextprotocol#1582
|
@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.
Summary
this._scope = scopeassignments with amergeScopes()utility that unions existing and incoming scope strings (space-separated,Set-based deduplication, insertion-order stable)StreamableHTTPClientTransport(lines 520, 553) andSSEClientTransport(lines 167, 281)Root cause
The 401/403 handlers overwrote
this._scopewith only the scope from the currentWWW-Authenticateheader. With per-operation scopes (e.g.,initfor initialize,mcp:tools:readfor tools/list), re-authorizing for the new scope dropped coverage of all prior scopes, causing an infinite loop between operations.Approach
A module-level unexported
mergeScopes(existing, incoming)function:Set<string>(deduplicates, preserves insertion order)undefinedwhen the result is empty (matches_scope?: stringsemantics)Duplicated in both transport files to keep each self-contained — no new shared modules or public API surface.
Testing
Added 8 regression tests:
streamableHttp.test.ts: mergeScopes edge cases, multi-scope accumulation across 401→403 sequences, circuit-breaker compatibilitysse.test.ts: scope accumulation in EventSource reconnect and send pathsAll 265 client tests pass. Typecheck and lint pass.
Fixes #1582