Implement sending middleware. Sending middleware is useful for adding tracing, progress tokens, and so on. Change-Id: I0acab2550f0212d549c4adbcf1ebf425a9486df6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/676355 Reviewed-by: Sam Thanawalla <samthanawalla@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
29 lines
841 B
Go
29 lines
841 B
Go
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package mcp_test
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
var nextProgressToken atomic.Int64
|
|
|
|
// This middleware function adds a progress token to every outgoing request
|
|
// from the client.
|
|
func Example_progressMiddleware() {
|
|
c := mcp.NewClient("test", "v1", nil)
|
|
c.AddSendingMiddleware(addProgressToken[*mcp.ClientSession])
|
|
_ = c
|
|
}
|
|
|
|
func addProgressToken[S mcp.Session](h mcp.MethodHandler[S]) mcp.MethodHandler[S] {
|
|
return func(ctx context.Context, s S, method string, params mcp.Params) (result mcp.Result, err error) {
|
|
params.GetMeta().ProgressToken = nextProgressToken.Add(1)
|
|
return h(ctx, s, method, params)
|
|
}
|
|
}
|