Skip to content

Commit c6b229c

Browse files
authored
mcp: render blank text field in TextContent instead of omitempty (#91)
The text field is required, but current implementation omits the text field when it is a blank string. Some clients fail to parse the response if the text field is omitted. A blank text result is a valid MCP response. Claude desktop error message: ``` ClaudeAiToolResultRequest.content.0.text.text: Field required ``` The solution is to return the field even when it is blank by removing `omitempty`.
1 parent ba7a064 commit c6b229c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

mcp/content.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by an MIT-style
33
// license that can be found in the LICENSE file.
44

5+
// TODO(findleyr): update JSON marshalling of all content types to preserve required fields.
6+
// (See [TextContent.MarshalJSON], which handles this for text content).
7+
58
package mcp
69

710
import (
@@ -25,12 +28,19 @@ type TextContent struct {
2528
}
2629

2730
func (c *TextContent) MarshalJSON() ([]byte, error) {
28-
return json.Marshal(&wireContent{
31+
// Custom wire format to ensure the required "text" field is always included, even when empty.
32+
wire := struct {
33+
Type string `json:"type"`
34+
Text string `json:"text"`
35+
Meta Meta `json:"_meta,omitempty"`
36+
Annotations *Annotations `json:"annotations,omitempty"`
37+
}{
2938
Type: "text",
3039
Text: c.Text,
3140
Meta: c.Meta,
3241
Annotations: c.Annotations,
33-
})
42+
}
43+
return json.Marshal(wire)
3444
}
3545

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

mcp/content_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ 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+
},
29+
{
30+
&mcp.TextContent{},
31+
`{"type":"text","text":""}`,
32+
},
2533
{
2634
&mcp.TextContent{
2735
Text: "hello",

0 commit comments

Comments
 (0)