Skip to content

mcp: don't omit required fields in ImageContent and AudioContent #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions mcp/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,25 @@ type ImageContent struct {
}

func (c *ImageContent) MarshalJSON() ([]byte, error) {
return json.Marshal(&wireContent{
// Custom wire format to ensure required fields are always included, even when empty.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these two functions are almost identical. Factor out into a single function. It looks to me that that function's args are the type (image or audio) and the data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can if you think it's important.

Two copies of something doesn't feel like a strong deduplication signal to me given that it will split the currently very clear schema mapping into a function that abstracts away half of the type.

If you think this is a blocker, I'll refactor, since I'd prefer the sdk is correct regardless of the code inside.

data := c.Data
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"`
}{
Type: "image",
MIMEType: c.MIMEType,
Data: c.Data,
Data: data,
Meta: c.Meta,
Annotations: c.Annotations,
})
}
return json.Marshal(wire)
}

func (c *ImageContent) fromWire(wire *wireContent) {
Expand All @@ -83,13 +95,25 @@ type AudioContent struct {
}

func (c AudioContent) MarshalJSON() ([]byte, error) {
return json.Marshal(&wireContent{
// Custom wire format to ensure required fields are always included, even when empty.
data := c.Data
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"`
}{
Type: "audio",
MIMEType: c.MIMEType,
Data: c.Data,
Data: data,
Meta: c.Meta,
Annotations: c.Annotations,
})
}
return json.Marshal(wire)
}

func (c *AudioContent) fromWire(wire *wireContent) {
Expand Down
16 changes: 16 additions & 0 deletions mcp/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func TestContent(t *testing.T) {
},
`{"type":"image","mimeType":"image/png","data":"YTFiMmMz"}`,
},
{
&mcp.ImageContent{MIMEType: "image/png", Data: []byte{}},
`{"type":"image","mimeType":"image/png","data":""}`,
},
{
&mcp.ImageContent{Data: []byte("test")},
`{"type":"image","mimeType":"","data":"dGVzdA=="}`,
},
{
&mcp.ImageContent{
Data: []byte("a1b2c3"),
Expand All @@ -61,6 +69,14 @@ func TestContent(t *testing.T) {
},
`{"type":"audio","mimeType":"audio/wav","data":"YTFiMmMz"}`,
},
{
&mcp.AudioContent{MIMEType: "audio/wav", Data: []byte{}},
`{"type":"audio","mimeType":"audio/wav","data":""}`,
},
{
&mcp.AudioContent{Data: []byte("test")},
`{"type":"audio","mimeType":"","data":"dGVzdA=="}`,
},
{
&mcp.AudioContent{
Data: []byte("a1b2c3"),
Expand Down
Loading