Add a logical client session type back to the SDK, to bring it closer to the design. Also, remove 'ConnectionOptions', which is better implemented using middleware, and reimplement logging as a LoggingTransport middleware, which is also added to the design. Change-Id: I908b4f837d513ffc60ae5b6171a31519cffafebc Reviewed-on: https://go-review.googlesource.com/c/tools/+/672795 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Sam Thanawalla <samthanawalla@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
51 lines
1.2 KiB
Go
51 lines
1.2 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 *AddParams) ([]*mcp.Content, error) {
|
|
return []*mcp.Content{
|
|
mcp.NewTextContent(fmt.Sprintf("%d", params.X+params.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 := cs.CallTool(ctx, "add", map[string]any{"x": 1, "y": 2}, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(res.Content[0].Text)
|
|
|
|
// Output: 3
|
|
}
|