go-sdk/mcp/protocol_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

69 lines
1.7 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 (
"encoding/json"
"maps"
"testing"
)
func TestParamsMeta(t *testing.T) {
// Verify some properties of the Meta field of Params structs.
// We use CallToolParams for the test, but the Meta setup of all params types
// is identical so they should all behave the same.
toJSON := func(x any) string {
data, err := json.Marshal(x)
if err != nil {
t.Fatal(err)
}
return string(data)
}
meta := map[string]any{"m": 1}
// You can set the embedded Meta field to a literal map.
p := &CallToolParams{
Meta: meta,
Name: "name",
}
// The Meta field marshals properly when it's present.
if g, w := toJSON(p), `{"_meta":{"m":1},"name":"name"}`; g != w {
t.Errorf("got %s, want %s", g, w)
}
// ... and when it's absent.
p2 := &CallToolParams{Name: "n"}
if g, w := toJSON(p2), `{"name":"n"}`; g != w {
t.Errorf("got %s, want %s", g, w)
}
// The GetMeta and SetMeta functions work as expected.
if g := p.GetMeta(); !maps.Equal(g, meta) {
t.Errorf("got %+v, want %+v", g, meta)
}
meta2 := map[string]any{"x": 2}
p.SetMeta(meta2)
if g := p.GetMeta(); !maps.Equal(g, meta2) {
t.Errorf("got %+v, want %+v", g, meta2)
}
// The GetProgressToken and SetProgressToken methods work as expected.
if g := p.GetProgressToken(); g != nil {
t.Errorf("got %v, want nil", g)
}
p.SetProgressToken("t")
if g := p.GetProgressToken(); g != "t" {
t.Errorf("got %v, want `t`", g)
}
// You can set a progress token to an int, int32 or int64.
p.SetProgressToken(int(1))
p.SetProgressToken(int32(1))
p.SetProgressToken(int64(1))
}