mcp/examples: moves middleware examples into example folder (#356)
For better organization.
This commit is contained in:
parent
b891d95361
commit
d16ce9c25f
1 changed files with 5 additions and 15 deletions
30
examples/client/middleware/main.go
Normal file
30
examples/client/middleware/main.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// 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 main
|
||||
|
||||
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 main() {
|
||||
c := mcp.NewClient(&mcp.Implementation{Name: "test"}, nil)
|
||||
c.AddSendingMiddleware(addProgressToken[*mcp.ClientSession])
|
||||
}
|
||||
|
||||
func addProgressToken[S mcp.Session](h mcp.MethodHandler) mcp.MethodHandler {
|
||||
return func(ctx context.Context, method string, req mcp.Request) (result mcp.Result, err error) {
|
||||
if rp, ok := req.GetParams().(mcp.RequestParams); ok {
|
||||
rp.SetProgressToken(nextProgressToken.Add(1))
|
||||
}
|
||||
return h(ctx, method, req)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue