Skip to content

mcp: render blank text field in TextContent instead of omitempty #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

martinemde
Copy link
Contributor

@martinemde martinemde commented Jul 3, 2025

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.

@@ -25,12 +25,19 @@ type TextContent struct {
}

func (c *TextContent) MarshalJSON() ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this oversight.
The same fix should apply to other content types, but you do not need to do that in this CL--we appreciate the fix as it is. However, could you please add

// TODO(findleyr): update JSON marshalling of all content types to preserve required fields.
// (See [TextContent.MarshalJSON], which handles this for text content).

near the top of content.go to address this bug for other content types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered whether other types needed it, and I wasn't really sure. Maybe semantically not omitting the empty response is better.

My logic was that an empty string is technically still a string, but is empty data still an image?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the source of truth here is the machine readable schema, which states that "data" is required:
https://github.com/modelcontextprotocol/modelcontextprotocol/blob/6601f565d1e40f840dbabb7679c5ddad45324efb/schema/2025-06-18/schema.json#L794

Of course, we need to be careful that we don't print JSON null.
A TODO is sufficient for now, and I'll fix when I get back (today and tomorrow are Google holidays).

Thanks again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #95 which addresses 2 other types in a slightly different way. I left this PR the same and added the TODO.

@martinemde
Copy link
Contributor Author

Added one additional test where Text is not set to ensure we don't have any null values. Looks good.

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.
@martinemde martinemde force-pushed the martinemde/dont-omit-blank-text-content branch from 3c48177 to 70221a8 Compare July 4, 2025 16:32
@findleyr
Copy link
Contributor

findleyr commented Jul 4, 2025

Added one additional test where Text is not set to ensure we don't have any null values. Looks good.

Good idea, thanks.

@findleyr findleyr merged commit c6b229c into modelcontextprotocol:main Jul 4, 2025
3 checks passed
@martinemde martinemde deleted the martinemde/dont-omit-blank-text-content branch July 4, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants