|
1 | 1 | <!-- Autogenerated by weave; DO NOT EDIT --> |
2 | 2 | # Support for the MCP base protocol |
3 | 3 |
|
4 | | - 1. [Lifecycle](#lifecycle) |
5 | | - 1. [Transports](#transports) |
6 | | - 1. [Authorization](#authorization) |
7 | | - 1. [Security](#security) |
8 | | - 1. [Utilities](#utilities) |
| 4 | +1. [Lifecycle](#lifecycle) |
| 5 | +1. [Transports](#transports) |
| 6 | +1. [Authorization](#authorization) |
| 7 | +1. [Security](#security) |
| 8 | +1. [Utilities](#utilities) |
9 | 9 |
|
10 | 10 | ## Lifecycle |
11 | 11 |
|
@@ -215,8 +215,77 @@ server has observed it (see [concurrency](#concurrency)). |
215 | 215 |
|
216 | 216 | ### Ping |
217 | 217 |
|
218 | | -<!-- TODO --> |
| 218 | +[Ping](https://modelcontextprotocol.io/specification/2025-06-18/basic/utilities/ping) |
| 219 | +support is symmetrical for client and server. |
| 220 | + |
| 221 | +To initiate a ping, call |
| 222 | +[`ClientSession.Ping`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Ping) |
| 223 | +or |
| 224 | +[`ServerSession.Ping`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.Ping). |
| 225 | + |
| 226 | +To have the client or server session automatically ping its peer, and close the |
| 227 | +session if the ping fails, set |
| 228 | +[`ClientOptions.KeepAlive`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.KeepAlive) |
| 229 | +or |
| 230 | +[`ServerOptions.KeepAlive`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.KeepAlive). |
219 | 231 |
|
220 | 232 | ### Progress |
221 | 233 |
|
222 | | -<!-- TODO --> |
| 234 | +[Progress](https://modelcontextprotocol.io/specification/2025-06-18/basic/utilities/progress) |
| 235 | +reporting is possible by reading the progress token from request metadata and |
| 236 | +calling either |
| 237 | +[`ClientSession.NotifyProgress`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.NotifyProgress) |
| 238 | +or |
| 239 | +[`ServerSession.NotifyProgress`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.NotifyProgress). |
| 240 | +To listen to progress notifications, set |
| 241 | +[`ClientOptions.ProgressNotificationHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.ProgressNotificationHandler) |
| 242 | +or |
| 243 | +[`ServerOptions.ProgressNotificationHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.ProgressNotificationHandler). |
| 244 | + |
| 245 | +Issue #460 discusses some potential ergonomic improvements to this API. |
| 246 | + |
| 247 | +```go |
| 248 | +func Example_progress() { |
| 249 | + server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 250 | + mcp.AddTool(server, &mcp.Tool{Name: "makeProgress"}, func(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) { |
| 251 | + token, ok := req.Params.GetMeta()["progressToken"] |
| 252 | + if ok { |
| 253 | + for i := range 3 { |
| 254 | + params := &mcp.ProgressNotificationParams{ |
| 255 | + Message: fmt.Sprintf("progress %d", i), |
| 256 | + ProgressToken: token, |
| 257 | + Progress: float64(i), |
| 258 | + } |
| 259 | + req.Session.NotifyProgress(ctx, params) // ignore error |
| 260 | + } |
| 261 | + } |
| 262 | + return &mcp.CallToolResult{}, nil, nil |
| 263 | + }) |
| 264 | + client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, &mcp.ClientOptions{ |
| 265 | + ProgressNotificationHandler: func(_ context.Context, req *mcp.ProgressNotificationClientRequest) { |
| 266 | + fmt.Println(req.Params.Message) |
| 267 | + }, |
| 268 | + }) |
| 269 | + ctx := context.Background() |
| 270 | + t1, t2 := mcp.NewInMemoryTransports() |
| 271 | + if _, err := server.Connect(ctx, t1, nil); err != nil { |
| 272 | + log.Fatal(err) |
| 273 | + } |
| 274 | + |
| 275 | + session, err := client.Connect(ctx, t2, nil) |
| 276 | + if err != nil { |
| 277 | + log.Fatal(err) |
| 278 | + } |
| 279 | + defer session.Close() |
| 280 | + if _, err := session.CallTool(ctx, &mcp.CallToolParams{ |
| 281 | + Name: "makeProgress", |
| 282 | + Meta: mcp.Meta{"progressToken": "abc123"}, |
| 283 | + }); err != nil { |
| 284 | + log.Fatal(err) |
| 285 | + } |
| 286 | + // Output: |
| 287 | + // progress 0 |
| 288 | + // progress 1 |
| 289 | + // progress 2 |
| 290 | +} |
| 291 | +``` |
0 commit comments