|
8 | 8 | 1. [Custom transports](#custom-transports) |
9 | 9 | 1. [Concurrency](#concurrency) |
10 | 10 | 1. [Authorization](#authorization) |
| 11 | + 1. [Server](#server) |
| 12 | + 1. [Client](#client) |
11 | 13 | 1. [Security](#security) |
| 14 | + 1. [Confused Deputy](#confused-deputy) |
| 15 | + 1. [Token Passthrough](#token-passthrough) |
| 16 | + 1. [Session Hijacking](#session-hijacking) |
12 | 17 | 1. [Utilities](#utilities) |
13 | 18 | 1. [Cancellation](#cancellation) |
14 | 19 | 1. [Ping](#ping) |
@@ -232,11 +237,67 @@ for more background. |
232 | 237 |
|
233 | 238 | ## Authorization |
234 | 239 |
|
235 | | -<!-- TODO --> |
| 240 | +### Server |
| 241 | + |
| 242 | +To write an MCP server that performs authorization, |
| 243 | +use [`RequireBearerToken`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerToken). |
| 244 | +This function is middleware that wraps an HTTP handler, such as the one returned |
| 245 | +by [`NewStreamableHTTPHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#NewStreamableHTTPHandler), to provide support for verifying bearer tokens. |
| 246 | +The middleware function checks every request for an Authorization header with a bearer token, |
| 247 | +and invokes the |
| 248 | +[`TokenVerifier`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenVerifier) |
| 249 | + passed to `RequireBearerToken` to parse the token and perform validation. |
| 250 | +The middleware function checks expiration and scopes (if they are provided in |
| 251 | +[`RequireBearerTokenOptions.Scopes`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerTokenOptions.Scopes)), so the |
| 252 | +`TokenVerifer` doesn't have to. |
| 253 | +If [`RequireBearerTokenOptions.ResourceMetadataURL`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerTokenOptions.ResourceMetadataURL) is set and verification fails, |
| 254 | +the middleware function sets the WWW-Authenticate header as required by the [Protected Resource |
| 255 | +Metadata spec](https://datatracker.ietf.org/doc/html/rfc9728). |
| 256 | + |
| 257 | +The [_auth middleware example_](https://github.com/modelcontextprotocol/go-sdk/tree/main/examples/server/auth-middleware) shows how to implement authorization for both JWT tokens and API keys. |
| 258 | + |
| 259 | +### Client |
| 260 | + |
| 261 | +Client-side OAuth is implemented by setting |
| 262 | +[`StreamableClientTransport.HTTPClient`](https://pkg.go.dev/github.com/modelcontextprotocol/[email protected]/mcp#StreamableClientTransport.HTTPClient) to a custom [`http.Client`](https://pkg.go.dev/net/http#Client) |
| 263 | +Additional support is forthcoming; see #493. |
236 | 264 |
|
237 | 265 | ## Security |
238 | 266 |
|
239 | | -<!-- TODO --> |
| 267 | +Here we discuss the mitigations described under |
| 268 | +the MCP spec's [Security Best Practices](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices) section, and how we handle them. |
| 269 | + |
| 270 | +### Confused Deputy |
| 271 | + |
| 272 | +The [mitigation](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#mitigation), obtaining user consent for dynamically registered clients, |
| 273 | +happens on the MCP client. At present we don't provide client-side OAuth support. |
| 274 | + |
| 275 | + |
| 276 | +### Token Passthrough |
| 277 | + |
| 278 | +The [mitigation](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#mitigation-2), accepting only tokens that were issued for the server, depends on the structure |
| 279 | +of tokens and is the responsibility of the |
| 280 | +[`TokenVerifier`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenVerifier) |
| 281 | +provided to |
| 282 | +[`RequireBearerToken`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerToken). |
| 283 | + |
| 284 | +### Session Hijacking |
| 285 | + |
| 286 | +The [mitigations](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#mitigation-3) are as follows: |
| 287 | + |
| 288 | +- _Verify all inbound requests_. The [`RequireBearerToken`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerToken) |
| 289 | +middleware function will verify all HTTP requests that it receives. It is the |
| 290 | +user's responsibility to wrap that function around all handlers in their server. |
| 291 | + |
| 292 | +- _Secure session IDs_. This SDK generates cryptographically secure session IDs by default. |
| 293 | +If you create your own with |
| 294 | +[`ServerOptions.GetSessionID`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.GetSessionID), it is your responsibility to ensure they are secure. |
| 295 | +If you are using Go 1.24 or above, |
| 296 | +we recommend using [`crypto/rand.Text`](https://pkg.go.dev/crypto/rand#Text) |
| 297 | + |
| 298 | +- _Binding session IDs to user information_. This is an application requirement, out of scope |
| 299 | +for the SDK. You can create your own session IDs by setting |
| 300 | +[`ServerOptions.GetSessionID`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.GetSessionID). |
240 | 301 |
|
241 | 302 | ## Utilities |
242 | 303 |
|
|
0 commit comments