From 9ddad03891c508e2dc9a2aaa35bf83f29e77de30 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Mon, 21 Jul 2025 11:40:32 -0400 Subject: [PATCH] mcp: dedup wire type for image and audio (#149) Factor out the wire type into a top-level struct. --- mcp/content.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/mcp/content.go b/mcp/content.go index 04888de..8bf75f0 100644 --- a/mcp/content.go +++ b/mcp/content.go @@ -63,13 +63,7 @@ func (c *ImageContent) MarshalJSON() ([]byte, error) { if data == nil { data = []byte{} } - wire := struct { - Type string `json:"type"` - MIMEType string `json:"mimeType"` - Data []byte `json:"data"` - Meta Meta `json:"_meta,omitempty"` - Annotations *Annotations `json:"annotations,omitempty"` - }{ + wire := imageAudioWire{ Type: "image", MIMEType: c.MIMEType, Data: data, @@ -100,13 +94,7 @@ func (c AudioContent) MarshalJSON() ([]byte, error) { if data == nil { data = []byte{} } - wire := struct { - Type string `json:"type"` - MIMEType string `json:"mimeType"` - Data []byte `json:"data"` - Meta Meta `json:"_meta,omitempty"` - Annotations *Annotations `json:"annotations,omitempty"` - }{ + wire := imageAudioWire{ Type: "audio", MIMEType: c.MIMEType, Data: data, @@ -123,6 +111,15 @@ func (c *AudioContent) fromWire(wire *wireContent) { c.Annotations = wire.Annotations } +// Custom wire format to ensure required fields are always included, even when empty. +type imageAudioWire struct { + Type string `json:"type"` + MIMEType string `json:"mimeType"` + Data []byte `json:"data"` + Meta Meta `json:"_meta,omitempty"` + Annotations *Annotations `json:"annotations,omitempty"` +} + // ResourceLink is a link to a resource type ResourceLink struct { URI string