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`.
This commit is contained in:
parent
ba7a06461a
commit
c6b229c855
2 changed files with 20 additions and 2 deletions
|
|
@ -2,6 +2,9 @@
|
||||||
// Use of this source code is governed by an MIT-style
|
// Use of this source code is governed by an MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// TODO(findleyr): update JSON marshalling of all content types to preserve required fields.
|
||||||
|
// (See [TextContent.MarshalJSON], which handles this for text content).
|
||||||
|
|
||||||
package mcp
|
package mcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -25,12 +28,19 @@ type TextContent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TextContent) MarshalJSON() ([]byte, error) {
|
func (c *TextContent) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(&wireContent{
|
// Custom wire format to ensure the required "text" field is always included, even when empty.
|
||||||
|
wire := struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
Meta Meta `json:"_meta,omitempty"`
|
||||||
|
Annotations *Annotations `json:"annotations,omitempty"`
|
||||||
|
}{
|
||||||
Type: "text",
|
Type: "text",
|
||||||
Text: c.Text,
|
Text: c.Text,
|
||||||
Meta: c.Meta,
|
Meta: c.Meta,
|
||||||
Annotations: c.Annotations,
|
Annotations: c.Annotations,
|
||||||
})
|
}
|
||||||
|
return json.Marshal(wire)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TextContent) fromWire(wire *wireContent) {
|
func (c *TextContent) fromWire(wire *wireContent) {
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,14 @@ func TestContent(t *testing.T) {
|
||||||
&mcp.TextContent{Text: "hello"},
|
&mcp.TextContent{Text: "hello"},
|
||||||
`{"type":"text","text":"hello"}`,
|
`{"type":"text","text":"hello"}`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
&mcp.TextContent{Text: ""},
|
||||||
|
`{"type":"text","text":""}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
&mcp.TextContent{},
|
||||||
|
`{"type":"text","text":""}`,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
&mcp.TextContent{
|
&mcp.TextContent{
|
||||||
Text: "hello",
|
Text: "hello",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue