Repository navigation
mcp: authorize at most once when Connect falls back to initialize - #1467
Closed
guglielmo-san wants to merge 1 commit into
Closed
guglielmo-san wants to merge 1 commit into
guglielmo-san wants to merge 1 commit into
Conversation
With an OAuthHandler, Client.Connect sends server/discover first and falls back to initialize on any non-modern error. When server/discover stayed unauthorized, the fallback initialize got the same 401 and ran OAuthHandler.Authorize a second time: a second consent prompt for one Connect. Following the approach agreed on the issue: - If Authorize returns an error during server/discover, Connect returns that error instead of falling back, since the fallback would only run the authorization flow again. - If Authorize succeeds but server/discover still fails, including with 401 or 403, Connect still falls back to initialize, which carries the credentials just acquired (a server may authorize per method). The transport doesn't call Authorize again during the handshake. Fixes #1423
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.
With an
OAuthHandler,Client.Connectsendsserver/discoverfirst and falls back toinitializeon any non-modern error. Whenserver/discoverstayed unauthorized, the fallbackinitializegot the same 401 and ranOAuthHandler.Authorizea second time. Withauth.AuthorizationCodeHandler, that is a second full authorization flow and consent prompt for a singleConnect.This follows the approach discussed on #1423:
Authorizereturns an error duringserver/discover(e.g. the user declines consent):Connectreturns that error instead of falling back, since the fallback would only run the authorization flow again. The transport wrapsAuthorizeerrors with an unexported sentinel soConnectcan tell them apart from a legacy server.Authorizesucceeds butserver/discoverstill fails, including with 401/403:Connectstill falls back toinitialize, which carries the credentials just acquired, since a server may authorize per method.Connectshares a flag across its handshake requests through the context, and the transport doesn't callAuthorizeagain once it has succeeded during the handshake.Unlike #1425, a server that rejects
server/discoverwith 403 but allowsinitializestill connects.Tests:
TestStreamableClientOAuth_AuthorizeOncePerConnectruns a real server behind bearer authorization and checksAuthorizeruns exactly once in four cases:Authorizefails (noinitializesent, the error wraps the handler's error), the granted token is rejected (initializetried with it, no secondAuthorize),server/discoverforbidden butinitializeallowed (connects), and a legacy server (connects). The first two fail onmain.go test -race ./...and staticcheck pass. The client conformance suite (--suite all, latest release) gives the same failing scenarios asmain; theauth/resource-mismatchandauth/2025-03-26-oauth-metadata-backcompatscenarios no longer repeat the authorization flow afterserver/discover.AI assistance was used to prepare this change.
Fixes #1423