Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/auth/client.go

raw symbols SCIP
1 // Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package auth
6
7 import (
8 "context"
9 "net/http"
10
11 "golang.org/x/oauth2"
12 )
13
14 // OAuthHandler is an interface for handling OAuth flows.
15 //
16 // If a transport wishes to support OAuth 2 authorization, it should support
17 // being configured with an OAuthHandler. It should call the handler's
18 // TokenSource method whenever it sends an HTTP request to set the
19 // Authorization header. If a request fails with a 401 or 403, it should call
20 // Authorize, and if that returns nil, it should retry the request. It should
21 // not call Authorize after the second failure. See
22 // [github.com/modelcontextprotocol/go-sdk/mcp.StreamableClientTransport]
23 // for an example.
24 type OAuthHandler interface {
25 // TokenSource returns a token source to be used for outgoing requests.
26 // Returned token source might be nil. In that case, the transport will not
27 // add any authorization headers to the request.
28 TokenSource(context.Context) (oauth2.TokenSource, error)
29
30 // Authorize is called when an HTTP request results in an error that may
31 // be addressed by the authorization flow (currently 401 Unauthorized and 403 Forbidden).
32 // It is responsible for performing the OAuth flow to obtain an access token.
33 // The arguments are the request that failed and the response that was received for it.
34 // The headers of the request are available, but the body will have already been consumed
35 // when Authorize is called.
36 // If the returned error is nil, TokenSource is expected to return a non-nil token source.
37 // After a successful call to Authorize, the HTTP request will be retried by the transport.
38 // The function is responsible for closing the response body.
39 Authorize(context.Context, *http.Request, *http.Response) error
40 }