go-sdk/mcp/content_test.go
Rob Findley bf608eeeae internal/mcp: clean up handling of content
Add support for all content types, and formalize the conversion of
content to and from the wire format (as a discriminated union).

Change-Id: I93678ce98c02176a524d2c82425fb1267ad68bf0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/669135
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
2025-05-01 19:15:30 -07:00

75 lines
1.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 (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/modelcontextprotocol/go-sdk/internal/protocol"
)
func TestContent(t *testing.T) {
tests := []struct {
in mcp.Content
want protocol.Content
}{
{mcp.TextContent{Text: "hello"}, protocol.Content{Type: "text", Text: "hello"}},
{
mcp.ImageContent{Data: "a1b2c3", MimeType: "image/png"},
protocol.Content{Type: "image", Data: "a1b2c3", MIMEType: "image/png"},
},
{
mcp.AudioContent{Data: "a1b2c3", MimeType: "audio/wav"},
protocol.Content{Type: "audio", Data: "a1b2c3", MIMEType: "audio/wav"},
},
{
mcp.ResourceContent{
Resource: mcp.TextResource{
URI: "file://foo",
MimeType: "text",
Text: "abc",
},
},
protocol.Content{
Type: "resource",
Resource: &protocol.Resource{
URI: "file://foo",
MIMEType: "text",
Text: "abc",
},
},
},
{
mcp.ResourceContent{
Resource: mcp.BlobResource{
URI: "file://foo",
MimeType: "text",
Blob: "a1b2c3",
},
},
protocol.Content{
Type: "resource",
Resource: &protocol.Resource{
URI: "file://foo",
MIMEType: "text",
Blob: ptr("a1b2c3"),
},
},
},
}
for _, test := range tests {
got := test.in.ToWire()
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("ToWire mismatch (-want +got):\n%s", diff)
}
}
}
func ptr[T any](t T) *T {
return &t
}