mcp: don't omit required fields in ImageContent and AudioContent (#95)
Update `ImageContent` and `AudioContent` to use an inline wire format to ensure that required fields are not omitted during JSON marshaling, matching TypeScript schema requirements. This follows the TextContent approach. - `ImageContent` requires `data` and `mimeType` fields. - `AudioContent` requires `data` and `mimeType` fields.
This commit is contained in:
parent
d437c81cbe
commit
dced3e4503
2 changed files with 46 additions and 6 deletions
|
|
@ -58,13 +58,25 @@ type ImageContent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ImageContent) MarshalJSON() ([]byte, error) {
|
func (c *ImageContent) 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: "image",
|
Type: "image",
|
||||||
MIMEType: c.MIMEType,
|
MIMEType: c.MIMEType,
|
||||||
Data: c.Data,
|
Data: data,
|
||||||
Meta: c.Meta,
|
Meta: c.Meta,
|
||||||
Annotations: c.Annotations,
|
Annotations: c.Annotations,
|
||||||
})
|
}
|
||||||
|
return json.Marshal(wire)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ImageContent) fromWire(wire *wireContent) {
|
func (c *ImageContent) fromWire(wire *wireContent) {
|
||||||
|
|
@ -83,13 +95,25 @@ type AudioContent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c AudioContent) MarshalJSON() ([]byte, error) {
|
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",
|
Type: "audio",
|
||||||
MIMEType: c.MIMEType,
|
MIMEType: c.MIMEType,
|
||||||
Data: c.Data,
|
Data: data,
|
||||||
Meta: c.Meta,
|
Meta: c.Meta,
|
||||||
Annotations: c.Annotations,
|
Annotations: c.Annotations,
|
||||||
})
|
}
|
||||||
|
return json.Marshal(wire)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AudioContent) fromWire(wire *wireContent) {
|
func (c *AudioContent) fromWire(wire *wireContent) {
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,14 @@ func TestContent(t *testing.T) {
|
||||||
},
|
},
|
||||||
`{"type":"image","mimeType":"image/png","data":"YTFiMmMz"}`,
|
`{"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{
|
&mcp.ImageContent{
|
||||||
Data: []byte("a1b2c3"),
|
Data: []byte("a1b2c3"),
|
||||||
|
|
@ -61,6 +69,14 @@ func TestContent(t *testing.T) {
|
||||||
},
|
},
|
||||||
`{"type":"audio","mimeType":"audio/wav","data":"YTFiMmMz"}`,
|
`{"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{
|
&mcp.AudioContent{
|
||||||
Data: []byte("a1b2c3"),
|
Data: []byte("a1b2c3"),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue