You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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,
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
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.
216
264
217
265
## Security
218
266
219
-
<!-- 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
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
0 commit comments