Skip to content

Commit d5f724d

Browse files
committed
mcp: render blank text field in TextContent
The text field is required. Some clients fail to parse the response if the text field is omitted and a blank text field is allowed. Return the field even when it is blank by removing omitempty.
1 parent ba7a064 commit d5f724d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

mcp/content.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ type TextContent struct {
2525
}
2626

2727
func (c *TextContent) MarshalJSON() ([]byte, error) {
28-
return json.Marshal(&wireContent{
28+
// Custom wire format to ensure the required "text" field is always included, even when empty.
29+
wire := struct {
30+
Type string `json:"type"`
31+
Text string `json:"text"`
32+
Meta Meta `json:"_meta,omitempty"`
33+
Annotations *Annotations `json:"annotations,omitempty"`
34+
}{
2935
Type: "text",
3036
Text: c.Text,
3137
Meta: c.Meta,
3238
Annotations: c.Annotations,
33-
})
39+
}
40+
return json.Marshal(wire)
3441
}
3542

3643
func (c *TextContent) fromWire(wire *wireContent) {

mcp/content_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func TestContent(t *testing.T) {
2222
&mcp.TextContent{Text: "hello"},
2323
`{"type":"text","text":"hello"}`,
2424
},
25+
{
26+
&mcp.TextContent{Text: ""},
27+
`{"type":"text","text":""}`,
28+
},
2529
{
2630
&mcp.TextContent{
2731
Text: "hello",

0 commit comments

Comments
 (0)