!-lifecycle !+progress
()
| 58 | // !+progress |
| 59 | |
| 60 | func Example_progress() { |
| 61 | server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 62 | mcp.AddTool(server, &mcp.Tool{Name: "makeProgress"}, func(ctx context.Context, req *mcp.CallToolRequest, _ any) (*mcp.CallToolResult, any, error) { |
| 63 | if token := req.Params.GetProgressToken(); token != nil { |
| 64 | for i := range 3 { |
| 65 | params := &mcp.ProgressNotificationParams{ |
| 66 | Message: "frobbing widgets", |
| 67 | ProgressToken: token, |
| 68 | Progress: float64(i), |
| 69 | Total: 2, |
| 70 | } |
| 71 | req.Session.NotifyProgress(ctx, params) // ignore error |
| 72 | } |
| 73 | } |
| 74 | return &mcp.CallToolResult{}, nil, nil |
| 75 | }) |
| 76 | client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, &mcp.ClientOptions{ |
| 77 | ProgressNotificationHandler: func(_ context.Context, req *mcp.ProgressNotificationClientRequest) { |
| 78 | fmt.Printf("%s %.0f/%.0f\n", req.Params.Message, req.Params.Progress, req.Params.Total) |
| 79 | }, |
| 80 | }) |
| 81 | ctx := context.Background() |
| 82 | t1, t2 := mcp.NewInMemoryTransports() |
| 83 | if _, err := server.Connect(ctx, t1, nil); err != nil { |
| 84 | log.Fatal(err) |
| 85 | } |
| 86 | |
| 87 | session, err := client.Connect(ctx, t2, nil) |
| 88 | if err != nil { |
| 89 | log.Fatal(err) |
| 90 | } |
| 91 | defer session.Close() |
| 92 | if _, err := session.CallTool(ctx, &mcp.CallToolParams{ |
| 93 | Name: "makeProgress", |
| 94 | Meta: mcp.Meta{"progressToken": "abc123"}, |
| 95 | }); err != nil { |
| 96 | log.Fatal(err) |
| 97 | } |
| 98 | // Output: |
| 99 | // frobbing widgets 0/2 |
| 100 | // frobbing widgets 1/2 |
| 101 | // frobbing widgets 2/2 |
| 102 | } |
| 103 | |
| 104 | // !-progress |
| 105 |
nothing calls this directly
no test coverage detected
searching dependent graphs…