go-sdk/mcp/features_test.go
Jonathan Amsterdam 3da831b0c9
mcp: move to 2025-06-18 schema (#8)
* mcp: move to 2025-06-18 schema

Add types and fields for the latest MCP schema.

The main challenge here was dealing with the presence of _meta fields
on many types, like Tool, Prompt and Resource.

- Distinguish between the _meta field of a request's params, which
  includes a progress token, from the _meta field on other types,
  which is simply a map.

- Redefine GetMeta to return a pointer to the map. We wanted to avoid
  that, but I don't see a clean way to do so. An alternative would be
  to have GetMeta return a map, and SetMeta to set it.

- Provide GetProgressToken for requests only (not notifications or other
  types).

- Still to do: deal with the meta and annotation fields of ContentBlock,
  which are currently duplicated for Resource and ResourceContents.

* Meta and progress token redesign
2025-06-25 09:48:33 -04:00

76 lines
2.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
import (
"context"
"slices"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/modelcontextprotocol/go-sdk/jsonschema"
)
type SayHiParams struct {
Name string `json:"name"`
}
func SayHi(ctx context.Context, cc *ServerSession, params *CallToolParamsFor[SayHiParams]) (*CallToolResultFor[any], error) {
return &CallToolResultFor[any]{
Content: []*ContentBlock{
NewTextContent("Hi " + params.Name),
},
}, nil
}
func TestFeatureSetOrder(t *testing.T) {
toolA := NewServerTool("apple", "apple tool", SayHi).Tool
toolB := NewServerTool("banana", "banana tool", SayHi).Tool
toolC := NewServerTool("cherry", "cherry tool", SayHi).Tool
testCases := []struct {
tools []*Tool
want []*Tool
}{
{[]*Tool{toolA, toolB, toolC}, []*Tool{toolA, toolB, toolC}},
{[]*Tool{toolB, toolC, toolA}, []*Tool{toolA, toolB, toolC}},
{[]*Tool{toolA, toolC}, []*Tool{toolA, toolC}},
{[]*Tool{toolA, toolA, toolA}, []*Tool{toolA}},
{[]*Tool{}, nil},
}
for _, tc := range testCases {
fs := newFeatureSet(func(t *Tool) string { return t.Name })
fs.add(tc.tools...)
got := slices.Collect(fs.all())
if diff := cmp.Diff(got, tc.want, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
t.Errorf("expected %v, got %v, (-want +got):\n%s", tc.want, got, diff)
}
}
}
func TestFeatureSetAbove(t *testing.T) {
toolA := NewServerTool("apple", "apple tool", SayHi).Tool
toolB := NewServerTool("banana", "banana tool", SayHi).Tool
toolC := NewServerTool("cherry", "cherry tool", SayHi).Tool
testCases := []struct {
tools []*Tool
above string
want []*Tool
}{
{[]*Tool{toolA, toolB, toolC}, "apple", []*Tool{toolB, toolC}},
{[]*Tool{toolA, toolB, toolC}, "banana", []*Tool{toolC}},
{[]*Tool{toolA, toolB, toolC}, "cherry", nil},
}
for _, tc := range testCases {
fs := newFeatureSet(func(t *Tool) string { return t.Name })
fs.add(tc.tools...)
got := slices.Collect(fs.above(tc.above))
if diff := cmp.Diff(got, tc.want, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" {
t.Errorf("expected %v, got %v, (-want +got):\n%s", tc.want, got, diff)
}
}
}