internal/mcp: adjust content types

- Change the names of some content-related types to better match
  the spec. Most notably, rename Resource to EmbeddedResource,
  because the former has a different meaning in the spec.

- Use []byte intead of string where the spec says "base64-encoded data."

- Unexport EmbeddedResource.ToWire.

Change-Id: I9f65efaadadda1b3abc0e58c64a743af1a72852c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/671362
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Bypass: Jonathan Amsterdam <jba@google.com>
Commit-Queue: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Jonathan Amsterdam 2025-05-10 13:47:22 -04:00
commit 71265ba130
3 changed files with 72 additions and 62 deletions

View file

@ -15,6 +15,7 @@ import (
// //
// ToWire converts content to its jsonrpc2 wire format. // ToWire converts content to its jsonrpc2 wire format.
type Content interface { type Content interface {
// TODO: unexport this, and move the tests that use it to this package.
ToWire() protocol.Content ToWire() protocol.Content
} }
@ -29,64 +30,69 @@ func (c TextContent) ToWire() protocol.Content {
// ImageContent contains base64-encoded image data. // ImageContent contains base64-encoded image data.
type ImageContent struct { type ImageContent struct {
Data string Data []byte // base64-encoded
MimeType string MIMEType string
} }
func (c ImageContent) ToWire() protocol.Content { func (c ImageContent) ToWire() protocol.Content {
return protocol.Content{Type: "image", MIMEType: c.MimeType, Data: c.Data} return protocol.Content{Type: "image", MIMEType: c.MIMEType, Data: c.Data}
} }
// AudioContent contains base64-encoded audio data. // AudioContent contains base64-encoded audio data.
type AudioContent struct { type AudioContent struct {
Data string Data []byte
MimeType string MIMEType string
} }
func (c AudioContent) ToWire() protocol.Content { func (c AudioContent) ToWire() protocol.Content {
return protocol.Content{Type: "audio", MIMEType: c.MimeType, Data: c.Data} return protocol.Content{Type: "audio", MIMEType: c.MIMEType, Data: c.Data}
} }
// ResourceContent contains embedded resources. // ResourceContent contains embedded resources.
type ResourceContent struct { type ResourceContent struct {
Resource Resource Resource EmbeddedResource
} }
func (r ResourceContent) ToWire() protocol.Content { func (r ResourceContent) ToWire() protocol.Content {
res := r.Resource.ToWire() res := r.Resource.toWire()
return protocol.Content{Type: "resource", Resource: &res} return protocol.Content{Type: "resource", Resource: &res}
} }
type Resource interface { type EmbeddedResource interface {
ToWire() protocol.Resource toWire() protocol.ResourceContents
} }
type TextResource struct { // The {Text,Blob}ResourceContents types match the protocol definitions,
// but we represent both as a single type on the wire.
// A TextResourceContents is the contents of a text resource.
type TextResourceContents struct {
URI string URI string
MimeType string MIMEType string
Text string Text string
} }
func (r TextResource) ToWire() protocol.Resource { func (r TextResourceContents) toWire() protocol.ResourceContents {
return protocol.Resource{ return protocol.ResourceContents{
URI: r.URI, URI: r.URI,
MIMEType: r.MimeType, MIMEType: r.MIMEType,
Text: r.Text, Text: r.Text,
// Blob is nil, indicating this is a TextResourceContents.
} }
} }
type BlobResource struct { // A BlobResourceContents is the contents of a blob resource.
type BlobResourceContents struct {
URI string URI string
MimeType string MIMEType string
Blob string Blob []byte
} }
func (r BlobResource) ToWire() protocol.Resource { func (r BlobResourceContents) toWire() protocol.ResourceContents {
blob := r.Blob return protocol.ResourceContents{
return protocol.Resource{
URI: r.URI, URI: r.URI,
MIMEType: r.MimeType, MIMEType: r.MIMEType,
Blob: &blob, Blob: r.Blob,
} }
} }
@ -97,22 +103,22 @@ func ContentFromWireContent(c protocol.Content) Content {
case "text": case "text":
return TextContent{Text: c.Text} return TextContent{Text: c.Text}
case "image": case "image":
return ImageContent{Data: c.Data, MimeType: c.MIMEType} return ImageContent{Data: c.Data, MIMEType: c.MIMEType}
case "audio": case "audio":
return AudioContent{Data: c.Data, MimeType: c.MIMEType} return AudioContent{Data: c.Data, MIMEType: c.MIMEType}
case "resource": case "resource":
r := ResourceContent{} r := ResourceContent{}
if c.Resource != nil { if c.Resource != nil {
if c.Resource.Blob != nil { if c.Resource.Blob != nil {
r.Resource = BlobResource{ r.Resource = BlobResourceContents{
URI: c.Resource.URI, URI: c.Resource.URI,
MimeType: c.Resource.MIMEType, MIMEType: c.Resource.MIMEType,
Blob: *c.Resource.Blob, Blob: c.Resource.Blob,
} }
} else { } else {
r.Resource = TextResource{ r.Resource = TextResourceContents{
URI: c.Resource.URI, URI: c.Resource.URI,
MimeType: c.Resource.MIMEType, MIMEType: c.Resource.MIMEType,
Text: c.Resource.Text, Text: c.Resource.Text,
} }
} }

View file

@ -19,24 +19,24 @@ func TestContent(t *testing.T) {
}{ }{
{mcp.TextContent{Text: "hello"}, protocol.Content{Type: "text", Text: "hello"}}, {mcp.TextContent{Text: "hello"}, protocol.Content{Type: "text", Text: "hello"}},
{ {
mcp.ImageContent{Data: "a1b2c3", MimeType: "image/png"}, mcp.ImageContent{Data: []byte("a1b2c3"), MIMEType: "image/png"},
protocol.Content{Type: "image", Data: "a1b2c3", MIMEType: "image/png"}, protocol.Content{Type: "image", Data: []byte("a1b2c3"), MIMEType: "image/png"},
}, },
{ {
mcp.AudioContent{Data: "a1b2c3", MimeType: "audio/wav"}, mcp.AudioContent{Data: []byte("a1b2c3"), MIMEType: "audio/wav"},
protocol.Content{Type: "audio", Data: "a1b2c3", MIMEType: "audio/wav"}, protocol.Content{Type: "audio", Data: []byte("a1b2c3"), MIMEType: "audio/wav"},
}, },
{ {
mcp.ResourceContent{ mcp.ResourceContent{
Resource: mcp.TextResource{ Resource: mcp.TextResourceContents{
URI: "file://foo", URI: "file://foo",
MimeType: "text", MIMEType: "text",
Text: "abc", Text: "abc",
}, },
}, },
protocol.Content{ protocol.Content{
Type: "resource", Type: "resource",
Resource: &protocol.Resource{ Resource: &protocol.ResourceContents{
URI: "file://foo", URI: "file://foo",
MIMEType: "text", MIMEType: "text",
Text: "abc", Text: "abc",
@ -45,18 +45,18 @@ func TestContent(t *testing.T) {
}, },
{ {
mcp.ResourceContent{ mcp.ResourceContent{
Resource: mcp.BlobResource{ Resource: mcp.BlobResourceContents{
URI: "file://foo", URI: "file://foo",
MimeType: "text", MIMEType: "text",
Blob: "a1b2c3", Blob: []byte("a1b2c3"),
}, },
}, },
protocol.Content{ protocol.Content{
Type: "resource", Type: "resource",
Resource: &protocol.Resource{ Resource: &protocol.ResourceContents{
URI: "file://foo", URI: "file://foo",
MIMEType: "text", MIMEType: "text",
Blob: ptr("a1b2c3"), Blob: []byte("a1b2c3"),
}, },
}, },
}, },
@ -69,7 +69,3 @@ func TestContent(t *testing.T) {
} }
} }
} }
func ptr[T any](t T) *T {
return &t
}

View file

@ -9,27 +9,35 @@ import (
"fmt" "fmt"
) )
// The []byte fields below are marked omitzero, not omitempty:
// we want to marshal an empty byte slice.
// Content is the wire format for content. // Content is the wire format for content.
// // It represents the protocol types TextContent, ImageContent, AudioContent
// The Type field distinguishes the type of the content. // and EmbeddedResource.
// At most one of Text, MIMEType, Data, and Resource is non-zero. // The Type field distinguishes them. In the protocol, each type has a constant
// value for the field.
// At most one of Text, Data, and Resource is non-zero.
type Content struct { type Content struct {
Type string `json:"type"` Type string `json:"type"`
Text string `json:"text,omitempty"` Text string `json:"text,omitempty"`
MIMEType string `json:"mimeType,omitempty"` MIMEType string `json:"mimeType,omitempty"`
Data string `json:"data,omitempty"` Data []byte `json:"data,omitzero"`
Resource *Resource `json:"resource,omitempty"` Resource *ResourceContents `json:"resource,omitempty"`
Annotations *Annotations `json:"annotations,omitempty"`
} }
// Resource is the wire format for embedded resources. // A ResourceContents is either a TextResourceContents or a BlobResourceContents.
// See https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/schema/2025-03-26/schema.ts#L524-L551
// for the inheritance structure.
// If Blob is nil, this is a TextResourceContents; otherwise it's a BlobResourceContents.
// //
// The URI field describes the resource location. At most one of Text and Blob // The URI field describes the resource location.
// is non-zero. type ResourceContents struct {
type Resource struct { URI string `json:"uri,"`
URI string `json:"uri,"` MIMEType string `json:"mimeType,omitempty"`
MIMEType string `json:"mimeType,omitempty"` Text string `json:"text"`
Text string `json:"text"` Blob []byte `json:"blob,omitzero"`
Blob *string `json:"blob"` // blob is a pointer to distinguish empty from missing data
} }
func (c *Content) UnmarshalJSON(data []byte) error { func (c *Content) UnmarshalJSON(data []byte) error {