go-sdk/mcp/client_list_test.go
Jonathan Amsterdam 32cf71de3a
mcp: change tool handler design (#325)
Change the design of tool handlers by removing genericity from the common path.
- `ToolHandler` gets the args as a `json.RawMessage`. Using Server.AddTool does no unmarshaling or schema validation.
- `ToolHandlerFor`, installed with `AddTool`, are the only generic pieces.

TODO:
uncomment and fix some tests
2025-08-20 19:36:34 -04:00

131 lines
4.1 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"
"iter"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func TestList(t *testing.T) {
ctx := context.Background()
clientSession, serverSession, server := createSessions(ctx)
defer clientSession.Close()
defer serverSession.Close()
t.Run("tools", func(t *testing.T) {
var wantTools []*mcp.Tool
for _, name := range []string{"apple", "banana", "cherry"} {
tt := &mcp.Tool{Name: name, Description: name + " tool"}
mcp.AddTool(server, tt, SayHi)
is, err := jsonschema.For[SayHiParams](nil)
if err != nil {
t.Fatal(err)
}
tt.InputSchema = is
wantTools = append(wantTools, tt)
}
t.Run("list", func(t *testing.T) {
res, err := clientSession.ListTools(ctx, nil)
if err != nil {
t.Fatal("ListTools() failed:", err)
}
if diff := cmp.Diff(wantTools, res.Tools, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
t.Fatalf("ListTools() mismatch (-want +got):\n%s", diff)
}
})
t.Run("iterator", func(t *testing.T) {
testIterator(t, clientSession.Tools(ctx, nil), wantTools)
})
})
t.Run("resources", func(t *testing.T) {
var wantResources []*mcp.Resource
for _, name := range []string{"apple", "banana", "cherry"} {
r := &mcp.Resource{URI: "http://" + name}
wantResources = append(wantResources, r)
server.AddResource(r, nil)
}
t.Run("list", func(t *testing.T) {
res, err := clientSession.ListResources(ctx, nil)
if err != nil {
t.Fatal("ListResources() failed:", err)
}
if diff := cmp.Diff(wantResources, res.Resources, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
t.Fatalf("ListResources() mismatch (-want +got):\n%s", diff)
}
})
t.Run("iterator", func(t *testing.T) {
testIterator(t, clientSession.Resources(ctx, nil), wantResources)
})
})
t.Run("templates", func(t *testing.T) {
var wantResourceTemplates []*mcp.ResourceTemplate
for _, name := range []string{"apple", "banana", "cherry"} {
rt := &mcp.ResourceTemplate{URITemplate: "http://" + name + "/{x}"}
wantResourceTemplates = append(wantResourceTemplates, rt)
server.AddResourceTemplate(rt, nil)
}
t.Run("list", func(t *testing.T) {
res, err := clientSession.ListResourceTemplates(ctx, nil)
if err != nil {
t.Fatal("ListResourceTemplates() failed:", err)
}
if diff := cmp.Diff(wantResourceTemplates, res.ResourceTemplates, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
t.Fatalf("ListResourceTemplates() mismatch (-want +got):\n%s", diff)
}
})
t.Run("ResourceTemplatesIterator", func(t *testing.T) {
testIterator(t, clientSession.ResourceTemplates(ctx, nil), wantResourceTemplates)
})
})
t.Run("prompts", func(t *testing.T) {
var wantPrompts []*mcp.Prompt
for _, name := range []string{"apple", "banana", "cherry"} {
p := &mcp.Prompt{Name: name, Description: name + " prompt"}
wantPrompts = append(wantPrompts, p)
server.AddPrompt(p, testPromptHandler)
}
t.Run("list", func(t *testing.T) {
res, err := clientSession.ListPrompts(ctx, nil)
if err != nil {
t.Fatal("ListPrompts() failed:", err)
}
if diff := cmp.Diff(wantPrompts, res.Prompts, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
t.Fatalf("ListPrompts() mismatch (-want +got):\n%s", diff)
}
})
t.Run("iterator", func(t *testing.T) {
testIterator(t, clientSession.Prompts(ctx, nil), wantPrompts)
})
})
}
func testIterator[T any](t *testing.T, seq iter.Seq2[*T, error], want []*T) {
t.Helper()
var got []*T
for x, err := range seq {
if err != nil {
t.Fatalf("iteration failed: %v", err)
}
got = append(got, x)
}
if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
t.Fatalf("mismatch (-want +got):\n%s", diff)
}
}
func testPromptHandler(context.Context, *mcp.ServerSession, *mcp.GetPromptParams) (*mcp.GetPromptResult, error) {
panic("not implemented")
}