Make Tool APIs future-proof by pushing generics into CallToolParams, and using CallToolParams throughout. This has several advantages: - We no longer need the awkward CallToolOptions type, which is almost but not quite like CallToolParams. - NewTool no longer needs to define its own handler type: it accepts a ToolHandler[TArgs]. This also means its signature is future-proof. Additionally, provide a `CallTool` helper (the analog of NewTool) to serve as a more strongly typed variant of `ClientSession.CallTool`. + design, protocol generation Change-Id: I80172a25bc50734dae84a541aa4776ed9161694e Reviewed-on: https://go-review.googlesource.com/c/tools/+/676495 Reviewed-by: Jonathan Amsterdam <jba@google.com> Auto-Submit: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
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"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
type AddParams struct {
|
|
X, Y int
|
|
}
|
|
|
|
func Add(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParams[AddParams]) (*mcp.CallToolResult, error) {
|
|
return &mcp.CallToolResult{
|
|
Content: []*mcp.Content{mcp.NewTextContent(fmt.Sprintf("%d", params.Arguments.X+params.Arguments.Y))},
|
|
}, nil
|
|
}
|
|
|
|
func ExampleSSEHandler() {
|
|
server := mcp.NewServer("adder", "v0.0.1", nil)
|
|
server.AddTools(mcp.NewTool("add", "add two numbers", Add))
|
|
|
|
handler := mcp.NewSSEHandler(func(*http.Request) *mcp.Server { return server })
|
|
httpServer := httptest.NewServer(handler)
|
|
defer httpServer.Close()
|
|
|
|
ctx := context.Background()
|
|
transport := mcp.NewSSEClientTransport(httpServer.URL)
|
|
client := mcp.NewClient("test", "v1.0.0", nil)
|
|
cs, err := client.Connect(ctx, transport)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer cs.Close()
|
|
|
|
res, err := mcp.CallTool(ctx, cs, &mcp.CallToolParams[map[string]any]{
|
|
Name: "add",
|
|
Arguments: map[string]any{"x": 1, "y": 2},
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(res.Content[0].Text)
|
|
|
|
// Output: 3
|
|
}
|