Skip to content

Commit 0a6216e

Browse files
committed
docs: add auth and security sections
1 parent 02f0b25 commit 0a6216e

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

docs/protocol.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
1. [Custom transports](#custom-transports)
99
1. [Concurrency](#concurrency)
1010
1. [Authorization](#authorization)
11+
1. [Server](#server)
12+
1. [Client](#client)
1113
1. [Security](#security)
14+
1. [Confused Deputy](#confused-deputy)
15+
1. [Token Passthrough](#token-passthrough)
16+
1. [Session Hijacking](#session-hijacking)
1217
1. [Utilities](#utilities)
1318
1. [Cancellation](#cancellation)
1419
1. [Ping](#ping)
@@ -232,11 +237,60 @@ for more background.
232237

233238
## Authorization
234239

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.
236264

237265
## Security
238266

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) happens on the MCP client. At present we don't provide client-side OAuth support.
273+
274+
275+
### Token Passthrough
276+
277+
The [mitigation](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#mitigation-2) is the responsibility of the
278+
[`TokenVerifier`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenVerifier)
279+
provided to
280+
[`RequireBearerToken`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerToken).
281+
282+
### Session Hijacking
283+
284+
The [mitigations](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#mitigation-3) are as follows:
285+
286+
- _Verify all inbound requests_. The [`RequireBearerToken`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerToken)
287+
middleware function will verify all HTTP requests that it receives. It is the
288+
user's responsibility to wrap that function around all handlers in their server.
289+
290+
- _Secure session IDs_. This SDK generates cryptographically secure session IDs by default.
291+
292+
- _Binding session IDs to user information_. This is an application requirement, out of scope
293+
for the SDK.
240294

241295
## Utilities
242296

internal/docs/protocol.src.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,60 @@ for more background.
164164

165165
## Authorization
166166

167-
<!-- TODO -->
167+
### Server
168+
169+
To write an MCP server that performs authorization,
170+
use [`RequireBearerToken`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerToken).
171+
This function is middleware that wraps an HTTP handler, such as the one returned
172+
by [`NewStreamableHTTPHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#NewStreamableHTTPHandler), to provide support for verifying bearer tokens.
173+
The middleware function checks every request for an Authorization header with a bearer token,
174+
and invokes the
175+
[`TokenVerifier`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenVerifier)
176+
passed to `RequireBearerToken` to parse the token and perform validation.
177+
The middleware function checks expiration and scopes (if they are provided in
178+
[`RequireBearerTokenOptions.Scopes`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerTokenOptions.Scopes)), so the
179+
`TokenVerifer` doesn't have to.
180+
If [`RequireBearerTokenOptions.ResourceMetadataURL`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerTokenOptions.ResourceMetadataURL) is set and verification fails,
181+
the middleware function sets the WWW-Authenticate header as required by the [Protected Resource
182+
Metadata spec](https://datatracker.ietf.org/doc/html/rfc9728).
183+
184+
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.
185+
186+
### Client
187+
188+
Client-side OAuth is implemented by setting
189+
[`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)
190+
Additional support is forthcoming.
168191

169192
## Security
170193

171-
<!-- TODO -->
194+
Here we discuss the mitigations described under
195+
the MCP spec's [Security Best Practices](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices) section, and how we handle them.
196+
197+
### Confused Deputy
198+
199+
The [mitigation](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#mitigation) happens on the MCP client. At present we don't provide client-side OAuth support.
200+
201+
202+
### Token Passthrough
203+
204+
The [mitigation](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#mitigation-2) is the responsibility of the
205+
[`TokenVerifier`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenVerifier)
206+
provided to
207+
[`RequireBearerToken`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerToken).
208+
209+
### Session Hijacking
210+
211+
The [mitigations](https://modelcontextprotocol.io/specification/2025-06-18/basic/security_best_practices#mitigation-3) are as follows:
212+
213+
- _Verify all inbound requests_. The [`RequireBearerToken`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#RequireBearerToken)
214+
middleware function will verify all HTTP requests that it receives. It is the
215+
user's responsibility to wrap that function around all handlers in their server.
216+
217+
- _Secure session IDs_. This SDK generates cryptographically secure session IDs by default.
218+
219+
- _Binding session IDs to user information_. This is an application requirement, out of scope
220+
for the SDK.
172221

173222
## Utilities
174223

0 commit comments

Comments
 (0)