MCPcopy Index your code
hub / github.com/modelcontextprotocol/go-sdk / Example_progress

Function Example_progress

mcp/mcp_example_test.go:60–102  ·  view source on GitHub ↗

!-lifecycle !+progress

()

Source from the content-addressed store, hash-verified

58// !+progress
59
60func 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

Callers

nothing calls this directly

Calls 10

ConnectMethod · 0.95
ConnectMethod · 0.95
NewServerFunction · 0.92
AddToolFunction · 0.92
NewClientFunction · 0.92
NewInMemoryTransportsFunction · 0.92
CallToolMethod · 0.80
GetProgressTokenMethod · 0.65
CloseMethod · 0.65
NotifyProgressMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…