go-sdk/mcp
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Maciej Kisiel c9317fb5b7
all: client side OAuth support (#785)
This PR introduces client-side OAuth support.

# Overall design philosophy

The main new interface introduced to support client-side OAuth is the
`OAuthHandler`. Contrary to its counter-parts in some other SDKs, it is
intentionally small and generic so that it can support most of the OAuth
authorization flow variants. The main `mcp` package purposefully
delegates almost all auth handling to `OAuthHandler` implementations.
This way, the `mcp` package continues to be largely OAuth agnostic.

The `auth` package comes with a default `OAuthHandler` implementation –
`AuthorizationCodeHandler`, which implements the authorization flow
for the `authorization_code` grant type, as defined in the [MCP
specification](https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization).
It is expected that the `OAuthHandler` implementation granularity will
be roughly per grant type, as this parameter influences the
authorization flow the most.

# Detailed design

## `OAuthHandler` (auth/client.go)

The main interface for the integration consists of two methods:

```go
type OAuthHandler interface {
	// TokenSource returns a token source to be used for outgoing requests.
	// Returned token source might be nil. In that case, the transport will not
	// add any authorization headers to the request.
	TokenSource(context.Context) (oauth2.TokenSource, error)

	// Authorize is called when an HTTP request results in an error that may
	// be addressed by the authorization flow (currently 401 Unauthorized and 403 Forbidden).
	// It is responsible for performing the OAuth flow to obtain an access token.
	// The arguments are the request that failed and the response that was received for it.
	// The headers of the request are available, but the body will have already been consumed
	// when Authorize is called.
	// If the returned error is nil, TokenSource is expected to return a non-nil token source.
	// After a successful call to Authorize, the HTTP request will be retried by the transport.
	// The function is responsible for closing the response body.
	Authorize(context.Context, *http.Request, *http.Response) error
}
```

`StreamableClientTransport` accepts this interface as a field and uses
the `TokenSource` method to add an `Authorization: Bearer` header to
outgoing requests to the MCP server. If the request fails with `401:
Unauthorized` or `403: Forbidden`, the transport calls `Authorize` to
perform the authorization flow. If `Authorize` returns a non-nil error,
the request is retried once. `Authorize` errors do not result in
termination of the client session (unless they happen during
connection initialization), as some OAuth flows are multi-legged and it should be
acceptable to retry for example when the authorization grant was
received.

*Note:* There was already an `OAuthHandler` type defined as part of a
previous iteration of OAuth support. It was renamed to
`OAuthHandlerLegacy`. This is the only backwards incompatible API change
in the experimental client `auth` part (protected by the
`mcp_go_client_oauth` Go build tag). The cost of replacing previous
usages is deemed worthwhile to maintain clear naming, under the
assumption that the experimental client `auth` part was not widely used.

## `AuthorizationCodeHandler` (auth/authorization_code.go)

This is the `OAuthHandler` implementation that fulfills the [MCP
specification](https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization).
In particular, in supports:

- Protected Resource Metadata discovery and fetching
- Authorization Server Metadata discovery and fetching
- Client registration
  - OpenID Connect metadata document
  - Pre-registration
  - Dynamic Client Registration
- PKCE
- `client_secret_post`, `client_secret_basic` token endpoint
auth methods
- Automatic refresh of tokens

### Authorization Server redirect

As part of the flow for the `authorization_code` grant type, the client
needs to point the user to the authorization URL, so that they confirm
the access request. This design proposes that it will be supported via a
pluggable dependency that will initiate this process in an
application-specific way:

```go
type AuthorizationCodeHandler struct {
	...

	// AuthorizationCodeFetcher is a required function called to initiate the authorization flow.
	// It is responsible for opening the URL in a browser for the user to start the authorization process.
	// It should return the authorization code and state once the Authorization Server
	// redirects back to the RedirectURL.
	AuthorizationCodeFetcher func(ctx context.Context, args *AuthorizationArgs) (*AuthorizationResult, error)

	...
}
```

The function is supposed perform any application specific logic to handle the redirect
from the Authorization Server and return the resulting code and state.

## `oauthex` changes

Some additional/adjusted building blocks that may be useful when
creating `OAuthHandler` implementations are present in the `oauthex`
package:

- `auth_meta.go`: Authorization Server Metadata utilities:
  - `GetAuthServerMeta`: get Authorization Server Metadata from a URL
- `dcr.go`: Dynamic Client Registration utilities (unchanged)
- `resource_meta.go`: Protected Resource Metadata utilities:
- `GetProtectedResourceMetadata`: get Protected Resource Metadata from a
URL
  - `ParseWWWAuthenticate` – as per name (unchanged)
- [deprecated] `GetProtectedResourceMetadataFromID` and
`GetProtectedResourceMetadataFromHeader` in favor of more generic
`GetProtectedResourceMetadata`. They will be kept under the Go build tag
and removed one version of the SDK in the future.
2026-02-27 15:30:13 +01:00
..
testdata go.mod: upgrade to jsonschema v0.4.2 (#732) 2026-01-04 09:18:01 -05:00
capabilities_go125_test.go mcp: add Capabilities fields to ServerOptions and ClientOptions (#713) 2025-12-11 20:17:46 -05:00
client.go all: use case-sensitive JSON unmarshaling (#807) 2026-02-18 16:38:36 +01:00
client_example_test.go mcp: fix goroutine leaks in unit tests (#496) 2025-10-16 11:13:30 -04:00
client_list_test.go mcp: allow any value for schemas (#517) 2025-09-26 11:55:54 -04:00
client_test.go mcp: add Extensions field to capabilities per SEP-2133 (#794) 2026-02-17 16:10:07 +01:00
cmd.go mcp: speed up TestCommandTransportDuration 2025-09-12 12:00:29 -04:00
cmd_export_test.go mcp: speed up TestCommandTransportDuration 2025-09-12 12:00:29 -04:00
cmd_go125_test.go mcp: use synctest for timing-dependent tests (#756) 2026-02-10 10:40:11 +01:00
cmd_test.go mcp: use synctest for timing-dependent tests (#756) 2026-02-10 10:40:11 +01:00
conformance_go124_test.go internal/mcp: update protocol versions 2025-06-18 08:11:16 -07:00
conformance_go125_test.go internal/mcp: update protocol versions 2025-06-18 08:11:16 -07:00
conformance_test.go mcp: switch icon theme to named string type (#733) 2025-12-19 16:44:10 -05:00
content.go all: use case-sensitive JSON unmarshaling (#807) 2026-02-18 16:38:36 +01:00
content_nil_test.go mcp: add CallToolResult.getError (#481) 2025-09-18 08:08:23 -04:00
content_test.go mcp: switch icon theme to named string type (#733) 2025-12-19 16:44:10 -05:00
elicitation_go125_test.go mcp: use synctest for timing-dependent tests (#756) 2026-02-10 10:40:11 +01:00
elicitation_test.go mcp: use synctest for timing-dependent tests (#756) 2026-02-10 10:40:11 +01:00
error_test.go mcp: add Capabilities fields to ServerOptions and ClientOptions (#713) 2025-12-11 20:17:46 -05:00
event.go mcp: improve http transports error handling and make transport work with any size message (#734) 2026-02-05 12:59:37 +01:00
event_test.go mcp: improve http transports error handling and make transport work with any size message (#734) 2026-02-05 12:59:37 +01:00
features.go mcp: minor cleanup of some TODOs (#332) 2025-08-20 12:55:19 -04:00
features_test.go mcp: change tool handler design (#325) 2025-08-20 19:36:34 -04:00
logging.go mcp: use rand.Text and slog.DiscardHandler over internal implementation (#773) 2026-02-03 14:23:59 +01:00
mcp.go mcp: fix inaccurate package doc (#534) 2025-09-26 14:16:10 -04:00
mcp_example_test.go docs: add feature and troubleshooting documentation (#463) 2025-09-16 11:13:40 -04:00
mcp_go125_test.go mcp: use synctest for timing-dependent tests (#756) 2026-02-10 10:40:11 +01:00
mcp_test.go mcp: fix multi-select enum elicitation 2026-02-17 14:42:18 +01:00
prompt.go mcp: align PromptHandler args with other handlers (#348) 2025-08-22 10:44:05 -04:00
protocol.go all: use case-sensitive JSON unmarshaling (#807) 2026-02-18 16:38:36 +01:00
protocol_test.go mcp: implement sampling with tools (#699) 2026-02-17 12:07:08 +01:00
requests.go mcp: implement sampling with tools (#699) 2026-02-17 12:07:08 +01:00
resource.go mcp: finalize cleanup of Go 1.23, make Go version support explicit 2026-02-06 12:50:01 +01:00
resource_test.go mcp/Resource: Refactor URI Template Matching with uritemplate Library (#100) 2025-07-08 15:52:54 -04:00
sampling_test.go mcp: implement sampling with tools (#699) 2026-02-17 12:07:08 +01:00
schema_cache.go perf: add schema caching to avoid repeated reflection (#685) 2026-01-20 20:16:46 -05:00
schema_cache_test.go perf: add schema caching to avoid repeated reflection (#685) 2026-01-20 20:16:46 -05:00
server.go all: use case-sensitive JSON unmarshaling (#807) 2026-02-18 16:38:36 +01:00
server_example_test.go doc: document logging (#497) 2025-09-19 11:58:58 -04:00
server_test.go mcp: add Extensions field to capabilities per SEP-2133 (#794) 2026-02-17 16:10:07 +01:00
session.go mcp: support stateless streamable sessions (#277) 2025-08-11 16:43:46 -04:00
shared.go all: use case-sensitive JSON unmarshaling (#807) 2026-02-18 16:38:36 +01:00
shared_test.go all: remove ServerRequest[T] for concrete T (#342) 2025-08-21 12:10:53 -04:00
sse.go mcp: use rand.Text and slog.DiscardHandler over internal implementation (#773) 2026-02-03 14:23:59 +01:00
sse_example_test.go SSE: Add support for SSE handler options (#508) 2025-09-19 13:01:57 -04:00
sse_test.go mcp: add Allow header to 405 responses per RFC 9110 §15.5.6 (#757) 2026-01-22 09:43:45 +01:00
streamable.go all: client side OAuth support (#785) 2026-02-27 15:30:13 +01:00
streamable_bench_test.go mcp: don't persist failed streamable sessions (#605) 2025-10-27 16:29:16 -04:00
streamable_client.go mcp: export session missing error (#771) 2026-02-03 15:46:49 +01:00
streamable_client_auth_test.go all: client side OAuth support (#785) 2026-02-27 15:30:13 +01:00
streamable_client_test.go all: client side OAuth support (#785) 2026-02-27 15:30:13 +01:00
streamable_example_test.go mcp: fix goroutine leaks in unit tests (#496) 2025-10-16 11:13:30 -04:00
streamable_server.go mcp: simplify stream state management in streamable transport 2025-12-10 13:28:25 -05:00
streamable_test.go all: client side OAuth support (#785) 2026-02-27 15:30:13 +01:00
tool.go all: use case-sensitive JSON unmarshaling (#807) 2026-02-18 16:38:36 +01:00
tool_example_test.go mcp: allow any value for schemas (#517) 2025-09-26 11:55:54 -04:00
tool_test.go all: expose JSON-RPC error codes, and support URLElicitationRequired errors (#655) 2025-12-01 11:29:17 -05:00
transport.go all: use case-sensitive JSON unmarshaling (#807) 2026-02-18 16:38:36 +01:00
transport_example_test.go mcp: fix broken client root capabilities (#698) 2025-12-11 10:16:56 -05:00
transport_test.go fix: handle Windows CRLF in MCP client(#664) (#665) 2025-12-01 12:16:39 -05:00
util.go all: use case-sensitive JSON unmarshaling (#807) 2026-02-18 16:38:36 +01:00