go-sdk/mcp/example_progress_test.go
Jonathan Amsterdam 6c6243c0b0
mcp: NewClient and NewServer take Implementation (#113)
Change the name and version arguments to NewClient and NewServer to a
`*Implementation`, to future-proof against the spec.

Fixes #109.
2025-07-10 16:57:38 -04:00

31 lines
878 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(testImpl, 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) {
if rp, ok := params.(mcp.RequestParams); ok {
rp.SetProgressToken(nextProgressToken.Add(1))
}
return h(ctx, s, method, params)
}
}