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

102 lines
2.6 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 (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func TestContent(t *testing.T) {
tests := []struct {
in *mcp.ContentBlock
want string // json serialization
}{
{mcp.NewTextContent("hello"), `{"type":"text","text":"hello"}`},
{
mcp.NewImageContent([]byte("a1b2c3"), "image/png"),
`{"type":"image","mimeType":"image/png","data":"YTFiMmMz"}`,
},
{
mcp.NewAudioContent([]byte("a1b2c3"), "audio/wav"),
`{"type":"audio","mimeType":"audio/wav","data":"YTFiMmMz"}`,
},
{
mcp.NewResourceContents(
mcp.NewTextResourceContents("file://foo", "text", "abc"),
),
`{"type":"resource","resource":{"uri":"file://foo","mimeType":"text","text":"abc"}}`,
},
{
mcp.NewResourceContents(
mcp.NewBlobResourceContents("file://foo", "image/png", []byte("a1b2c3")),
),
`{"type":"resource","resource":{"uri":"file://foo","mimeType":"image/png","blob":"YTFiMmMz"}}`,
},
}
for _, test := range tests {
got, err := json.Marshal(test.in)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(test.want, string(got)); diff != "" {
t.Errorf("json.Marshal(%v) mismatch (-want +got):\n%s", test.in, diff)
}
var out *mcp.ContentBlock
if err := json.Unmarshal(got, &out); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(test.in, out); diff != "" {
t.Errorf("json.Unmarshal(%q) mismatch (-want +got):\n%s", string(got), diff)
}
}
}
func TestResourceContents(t *testing.T) {
for _, tt := range []struct {
rc mcp.ResourceContents
want string // marshaled JSON
}{
{
mcp.ResourceContents{URI: "u", Text: "t"},
`{"uri":"u","text":"t"}`,
},
{
mcp.ResourceContents{URI: "u", MIMEType: "m", Text: "t"},
`{"uri":"u","mimeType":"m","text":"t"}`,
},
{
mcp.ResourceContents{URI: "u", Text: "", Blob: nil},
`{"uri":"u","text":""}`,
},
{
mcp.ResourceContents{URI: "u", Blob: []byte{}},
`{"uri":"u","blob":""}`,
},
{
mcp.ResourceContents{URI: "u", Blob: []byte{1}},
`{"uri":"u","blob":"AQ=="}`,
},
} {
data, err := json.Marshal(tt.rc)
if err != nil {
t.Fatal(err)
}
if got := string(data); got != tt.want {
t.Errorf("%#v:\ngot %s\nwant %s", tt.rc, got, tt.want)
}
var urc mcp.ResourceContents
if err := json.Unmarshal(data, &urc); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(tt.rc, urc); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
}
}