Repository navigation
Conversation
…T SSE 401 StreamableHTTPClientTransport._startOrAuthSse() went straight to _authThenStart() on a 401, so auth() and a later finishAuth() ran discovery without the resource_metadata URL the server advertised. When protected resource metadata is only reachable through that URL, discovery falls back to the MCP server origin as the authorization server, and the authorization redirect and token request go there. Store resourceMetadataUrl and scope first, as send() and SSEClientTransport already do on v1.x and as main does since modelcontextprotocol#1710. Fixes modelcontextprotocol#1450 Co-Authored-By: Claude <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 17e1349 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
This branch has not been deployed
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.
Thanks for keeping v1.x patched alongside v2 (1.32.1 today), and for #1710, which already fixed this on
main.Fixes #1450 on v1.x. When the 401 comes from the GET SSE request, v1.x ignores the
WWW-Authenticateheader, so discovery can fall back to the MCP server origin instead of the authorization server named in the advertisedresource_metadata. This backports the GET-path lines from #1710.What
ff07b00auth()options after a GET 401 withresource_metadataandscoperesourceMetadataUrl: undefined,scope: undefinedhttps://mcp.example.com/authorizehttps://auth.example.com/oauth2/authorizefinishAuth()token requestPOST https://mcp.example.com/tokenPOST https://auth.example.com/oauth2/tokensend())WWW-AuthenticateHow
_startOrAuthSse()now storesresourceMetadataUrlandscopebefore calling_authThenStart(). It is the guarded blockSSEClientTransportalready uses on v1.x (src/client/sse.tsL183-186) and the one #1710 added to this method onmain(e563e63b).scopeis assigned the waysend()does it on v1.x; the scope union onmainbelongs to v2's step-up flow. No other file changes besides the test and a changeset.start()makes no request on v1.x, so this branch is reached fromresumeStream(),send()with aresumptionToken, the standalone GET afternotifications/initialized, and SSE reconnects (all four measured below). If the first 401 a transport sees comes from one of those, discovery runs without the advertised URL.I found no other open PR for this on v1.x; #1472 targeted
mainand was closed in favor of #1710.Tests
One regression test next to the existing 403 upscoping test, in the same style (spy on
auth, assert its options). It goes throughresumeStream()becausestart()alone never fetches on v1.x.End-to-end repro (real
auth(), mocked network, not committed)The GET returns
401withWWW-Authenticate: Bearer resource_metadata="https://auth.example.com/.well-known/oauth-protected-resource/tenant-a". That document nameshttps://auth.example.comas the authorization server. Every other URL returns 404. The client callsresumeStream('evt-1'), thenfinishAuth('code-123').The other three entry points, with
authspied. The 401 carriesresource_metadata="https://auth.example.com/prm", scope="s1". Forafter-initialized,notifications/initializedgets a 202 and the GET that follows gets the 401. Forreconnect, that GET returns an SSE stream that closes and the reconnect GET gets the 401.Header-less GET 401 after a POST 401 that carried
resource_metadata="https://auth.example.com/prm", scope="a", same output before and after:AI disclosure: Claude (via Claude Code) assisted with the reproduction, the patch and this description.