Skip to content

Commit c044068

Browse files
committed
docs: explain how to get TokenInfo
1 parent 28753c9 commit c044068

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

docs/client.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ func Example_roots() {
5656
if _, err := s.Connect(ctx, t1, nil); err != nil {
5757
log.Fatal(err)
5858
}
59-
if _, err := c.Connect(ctx, t2, nil); err != nil {
59+
60+
clientSession, err := c.Connect(ctx, t2, nil)
61+
if err != nil {
6062
log.Fatal(err)
6163
}
64+
defer clientSession.Close()
6265

6366
// ...and add a root. The server is notified about the change.
6467
c.AddRoots(&mcp.Root{URI: "file://b"})

docs/protocol.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ If [`RequireBearerTokenOptions.ResourceMetadataURL`](https://pkg.go.dev/github.c
254254
the middleware function sets the WWW-Authenticate header as required by the [Protected Resource
255255
Metadata spec](https://datatracker.ietf.org/doc/html/rfc9728).
256256

257+
Server handlers, such as tool handlers, can obtain the `TokenInfo` returned by the `TokenVerifier`
258+
from `req.Extra.TokenInfo`, where `req` is the handler's request. (For example, a
259+
[`CallToolRequest`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#CallToolRequest).)
260+
HTTP handlers wrapped by the `RequireBearerToken` middleware can obtain the `TokenInfo` from the context
261+
with [`auth.TokenInfoFromContext`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenInfoFromContext).
262+
263+
257264
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.
258265

259266
### Client

docs/server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func Example_prompts() {
7373
if err != nil {
7474
log.Fatal(err)
7575
}
76+
defer cs.Close()
7677

7778
// List the prompts.
7879
for p, err := range cs.Prompts(ctx, nil) {

docs/troubleshooting.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,21 @@ func ExampleLoggingTransport() {
2929
ctx := context.Background()
3030
t1, t2 := mcp.NewInMemoryTransports()
3131
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
32-
if _, err := server.Connect(ctx, t1, nil); err != nil {
32+
serverSession, err := server.Connect(ctx, t1, nil)
33+
if err != nil {
3334
log.Fatal(err)
3435
}
36+
defer serverSession.Wait()
3537

3638
client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
3739
var b bytes.Buffer
3840
logTransport := &mcp.LoggingTransport{Transport: t2, Writer: &b}
39-
if _, err := client.Connect(ctx, logTransport, nil); err != nil {
41+
clientSession, err := client.Connect(ctx, logTransport, nil)
42+
if err != nil {
4043
log.Fatal(err)
4144
}
45+
defer clientSession.Close()
46+
4247
// Sort for stability: reads are concurrent to writes.
4348
for _, line := range slices.Sorted(strings.SplitSeq(b.String(), "\n")) {
4449
fmt.Println(line)

internal/docs/protocol.src.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ If [`RequireBearerTokenOptions.ResourceMetadataURL`](https://pkg.go.dev/github.c
181181
the middleware function sets the WWW-Authenticate header as required by the [Protected Resource
182182
Metadata spec](https://datatracker.ietf.org/doc/html/rfc9728).
183183

184+
Server handlers, such as tool handlers, can obtain the `TokenInfo` returned by the `TokenVerifier`
185+
from `req.Extra.TokenInfo`, where `req` is the handler's request. (For example, a
186+
[`CallToolRequest`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#CallToolRequest).)
187+
HTTP handlers wrapped by the `RequireBearerToken` middleware can obtain the `TokenInfo` from the context
188+
with [`auth.TokenInfoFromContext`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenInfoFromContext).
189+
190+
184191
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.
185192

186193
### Client

0 commit comments

Comments
 (0)