Repository navigation
Conversation
Fixes modelcontextprotocol#1423. With an OAuthHandler configured, Client.Connect tries server/discover first and falls back to initialize on any non-modern error. When server/discover stayed unauthorized, the fallback initialize received 401 and ran OAuthHandler.Authorize a second time. Handle the two cases separately: - If OAuthHandler.Authorize itself returns an error during server/discover (e.g. consent declined), wrap it in an unexported authorizeError type (whose Error() and Unwrap() preserve the original error unchanged) so Connect can return immediately rather than falling back to initialize. - If OAuthHandler.Authorize succeeds, share an atomic handshake flag across Connect's handshake requests via the context so a fallback initialize reuses the newly acquired TokenSource credentials without invoking Authorize again. Close the initial 401/403 response body before retrying the request. TestStreamableClientOAuth_AuthorizeOncePerConnect covers Authorize failure, rejected granted token, method-level 403 on server/discover with 200 on initialize, and legacy server fallback. go test ./..., -race on mcp, go vet, and staticcheck v0.8.2 pass. AI-assisted: implementation and tests drafted with an AI assistant; reviewed and tested locally. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… context Replace authorizedContextKey on Client.Connect's context with a handshakeAuthorized boolean on streamableClientConn so Write skips a second OAuthHandler.Authorize call when a handshake method (server/discover or initialize) has already authorized. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
guglielmo-san
requested changes
Dec 3, 2026
| if (resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden) && c.oauthHandler != nil { | ||
| isHandshake := requestMethod == methodDiscover || requestMethod == methodInitialize | ||
| if (resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden) && c.oauthHandler != nil && | ||
| (!isHandshake || !c.handshakeAuthorized.Load()) { |
Contributor
There was a problem hiding this comment.
Suggested change
| (!isHandshake || !c.handshakeAuthorized.Load()) { | |
| !(requestMethod == methodInitialize && c.discoverAuthorized.Load()) { |
Comment on lines
+2640
to
2642
| if isHandshake { | ||
| c.handshakeAuthorized.Store(true) | ||
| } |
Contributor
There was a problem hiding this comment.
Suggested change
| if isHandshake { | |
| c.handshakeAuthorized.Store(true) | |
| } | |
| if requestMethod == methodDiscover { | |
| c.discoverAuthorized.Store(true) | |
| } |
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.
Fixes #1423
When
Client.Connectwith anOAuthHandlertriesserver/discoverfirst and falls back toinitialize, a server that rejects both requests with401/403currently triggersOAuthHandler.Authorizetwice in a singleConnectcall.This implements the two-case handling described by @guglielmo-san on #1423:
OAuthHandler.Authorizereturns an error duringserver/discover(e.g. consent declined or token exchange failed):streamableClientConn.Writewraps the handler error in an unexportedauthorizeError(preservingError()andUnwrap()unchanged), andClient.Connectcloses the session and returns immediately instead of falling back toinitialize.OAuthHandler.Authorizesucceeds duringserver/discover, and the retried request still fails (including with401/403):streamableClientConnrecordshandshakeAuthorizedonceAuthorizesucceeds for a handshake method (server/discoverorinitialize), so the fallbackinitializeis still attempted with the newly acquired token inTokenSourcewithout invokingOAuthHandler.Authorizea second time ifinitializealso returns401/403.Also closes the initial
401/403response body before retryingdoRequest()afterAuthorizesucceeds.Tests:
TestStreamableClientOAuth_AuthorizeOncePerConnectverifies thatAuthorizeruns at most once across four cases (Authorize fails,granted token rejected,discover forbidden, initialize allowed, andlegacy server).go test -race ./...,go vet ./..., andstaticcheckv0.8.2 pass.AI disclosure: drafted with AI assistance; I reviewed and tested the change locally.