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>
This commit is contained in:
parent
af4d138732
commit
bf608eeeae
1 changed files with 282 additions and 104 deletions
75
mcp/content_test.go
Normal file
75
mcp/content_test.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// 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
|
||||
}
|
||||
Loading…
Reference in a new issue