diff --git a/docs/client.md b/docs/client.md index cbc4db8f..2cbe082c 100644 --- a/docs/client.md +++ b/docs/client.md @@ -56,9 +56,12 @@ func Example_roots() { if _, err := s.Connect(ctx, t1, nil); err != nil { log.Fatal(err) } - if _, err := c.Connect(ctx, t2, nil); err != nil { + + clientSession, err := c.Connect(ctx, t2, nil) + if err != nil { log.Fatal(err) } + defer clientSession.Close() // ...and add a root. The server is notified about the change. c.AddRoots(&mcp.Root{URI: "file://b"}) diff --git a/docs/protocol.md b/docs/protocol.md index e3b9e8c2..c87e522b 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -254,6 +254,13 @@ If [`RequireBearerTokenOptions.ResourceMetadataURL`](https://pkg.go.dev/github.c the middleware function sets the WWW-Authenticate header as required by the [Protected Resource Metadata spec](https://datatracker.ietf.org/doc/html/rfc9728). +Server handlers, such as tool handlers, can obtain the `TokenInfo` returned by the `TokenVerifier` +from `req.Extra.TokenInfo`, where `req` is the handler's request. (For example, a +[`CallToolRequest`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#CallToolRequest).) +HTTP handlers wrapped by the `RequireBearerToken` middleware can obtain the `TokenInfo` from the context +with [`auth.TokenInfoFromContext`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenInfoFromContext). + + 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. ### Client diff --git a/docs/server.md b/docs/server.md index f59e2c7e..7d9320e7 100644 --- a/docs/server.md +++ b/docs/server.md @@ -73,6 +73,7 @@ func Example_prompts() { if err != nil { log.Fatal(err) } + defer cs.Close() // List the prompts. for p, err := range cs.Prompts(ctx, nil) { diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 0f990edc..38410ad5 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -29,16 +29,21 @@ func ExampleLoggingTransport() { ctx := context.Background() t1, t2 := mcp.NewInMemoryTransports() server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) - if _, err := server.Connect(ctx, t1, nil); err != nil { + serverSession, err := server.Connect(ctx, t1, nil) + if err != nil { log.Fatal(err) } + defer serverSession.Wait() client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil) var b bytes.Buffer logTransport := &mcp.LoggingTransport{Transport: t2, Writer: &b} - if _, err := client.Connect(ctx, logTransport, nil); err != nil { + clientSession, err := client.Connect(ctx, logTransport, nil) + if err != nil { log.Fatal(err) } + defer clientSession.Close() + // Sort for stability: reads are concurrent to writes. for _, line := range slices.Sorted(strings.SplitSeq(b.String(), "\n")) { fmt.Println(line) diff --git a/internal/docs/protocol.src.md b/internal/docs/protocol.src.md index 0abd5aa8..88c023cc 100644 --- a/internal/docs/protocol.src.md +++ b/internal/docs/protocol.src.md @@ -181,6 +181,13 @@ If [`RequireBearerTokenOptions.ResourceMetadataURL`](https://pkg.go.dev/github.c the middleware function sets the WWW-Authenticate header as required by the [Protected Resource Metadata spec](https://datatracker.ietf.org/doc/html/rfc9728). +Server handlers, such as tool handlers, can obtain the `TokenInfo` returned by the `TokenVerifier` +from `req.Extra.TokenInfo`, where `req` is the handler's request. (For example, a +[`CallToolRequest`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#CallToolRequest).) +HTTP handlers wrapped by the `RequireBearerToken` middleware can obtain the `TokenInfo` from the context +with [`auth.TokenInfoFromContext`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenInfoFromContext). + + 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. ### Client