Skip to content

Commit 3c48177

Browse files
committed
mcp: validate required fields in ImageContent and AudioContent
ImageContent and AudioContent now return errors when required fields are missing during JSON marshaling, matching TypeScript schema requirements. ImageContent requires data and mimeType fields. AudioContent requires data and mimeType fields. Added comprehensive test coverage for validation error cases.
1 parent 2765d6f commit 3c48177

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

mcp/content.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ type ImageContent struct {
5858
}
5959

6060
func (c *ImageContent) MarshalJSON() ([]byte, error) {
61+
if len(c.Data) == 0 {
62+
return nil, errors.New("ImageContent missing data")
63+
}
64+
if c.MIMEType == "" {
65+
return nil, errors.New("ImageContent missing mimeType")
66+
}
6167
return json.Marshal(&wireContent{
6268
Type: "image",
6369
MIMEType: c.MIMEType,
@@ -83,6 +89,12 @@ type AudioContent struct {
8389
}
8490

8591
func (c AudioContent) MarshalJSON() ([]byte, error) {
92+
if len(c.Data) == 0 {
93+
return nil, errors.New("AudioContent missing data")
94+
}
95+
if c.MIMEType == "" {
96+
return nil, errors.New("AudioContent missing mimeType")
97+
}
8698
return json.Marshal(&wireContent{
8799
Type: "audio",
88100
MIMEType: c.MIMEType,

mcp/content_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func TestContent(t *testing.T) {
2626
&mcp.TextContent{Text: ""},
2727
`{"type":"text","text":""}`,
2828
},
29+
{
30+
&mcp.TextContent{},
31+
`{"type":"text","text":""}`,
32+
},
2933
{
3034
&mcp.TextContent{
3135
Text: "hello",
@@ -168,3 +172,44 @@ func TestEmbeddedResource(t *testing.T) {
168172
}
169173
}
170174
}
175+
176+
func TestContentMarshalErrors(t *testing.T) {
177+
tests := []struct {
178+
name string
179+
content mcp.Content
180+
wantErr string
181+
}{
182+
{
183+
name: "ImageContent missing data",
184+
content: &mcp.ImageContent{MIMEType: "image/png"},
185+
wantErr: "json: error calling MarshalJSON for type *mcp.ImageContent: ImageContent missing data",
186+
},
187+
{
188+
name: "ImageContent missing mimeType",
189+
content: &mcp.ImageContent{Data: []byte("test")},
190+
wantErr: "json: error calling MarshalJSON for type *mcp.ImageContent: ImageContent missing mimeType",
191+
},
192+
{
193+
name: "AudioContent missing data",
194+
content: &mcp.AudioContent{MIMEType: "audio/wav"},
195+
wantErr: "json: error calling MarshalJSON for type *mcp.AudioContent: AudioContent missing data",
196+
},
197+
{
198+
name: "AudioContent missing mimeType",
199+
content: &mcp.AudioContent{Data: []byte("test")},
200+
wantErr: "json: error calling MarshalJSON for type *mcp.AudioContent: AudioContent missing mimeType",
201+
},
202+
}
203+
204+
for _, tt := range tests {
205+
t.Run(tt.name, func(t *testing.T) {
206+
_, err := json.Marshal(tt.content)
207+
if err == nil {
208+
t.Fatal("expected error, got nil")
209+
}
210+
if got := err.Error(); got != tt.wantErr {
211+
t.Errorf("got error %q, want %q", got, tt.wantErr)
212+
}
213+
})
214+
}
215+
}

0 commit comments

Comments
 (0)