Skip to content

mcp: preserve nested content in sampling tool results - #1420

Open
po-et wants to merge 1 commit into
modelcontextprotocol:mainfrom
po-et:fix/nested-tool-result-content
Open

po-et wants to merge 1 commit into
modelcontextprotocol:mainfrom
po-et:fix/nested-tool-result-content

Conversation

@po-et

@po-et po-et commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

An empty tool output in a sampling conversation currently becomes {"type":"text"} inside ToolResultContent, losing the required text field. Empty image and audio fields are lost in the same way. Each child already marshals correctly, but ToolResultContent decodes that JSON into wireContent and marshals it again with omitempty. That intermediate decode can also round integers in child metadata before sending them.

Keep the child marshaler's JSON as json.RawMessage instead. Nil and empty content still emit [], and child marshaling errors still propagate. This change concerns outgoing sampling tool results; it does not change incoming number decoding.

The regression exercises CreateMessageWithTools over serialized in-memory transports using protocol 2025-11-25 and inspects the JSON received by the client. Five cases fail on the original source and pass with the fix, including empty text/image/audio, mixed content, and an exact metadata integer above 2^53. Empty arrays, embedded resources, annotations, and marshaling errors are controls. Sampling is deprecated in the newer protocol but remains supported by the SDK.

Local validation on macOS arm64: Go 1.26.9 full race tests, build and vet; Go 1.25.6 full tests; formatting and diff checks. The final test update was rerun on both versions, with race detection on 1.26. Staticcheck 0.8.1 passed; this is the local Go 1.26-compatible version, rather than CI's pinned 0.6.1. External conformance suites were not run.

AI assistance: Codex was used for investigation, implementation, tests, independent review, and this description.

@guglielmo-san

Copy link
Copy Markdown
Contributor

@po-et Thanks for the fix! main requires branches to be up to date before merging, and maintainers can't push to this branch. Could you click "Update branch" (there are no conflicts), or enable "Allow edits by maintainers" so we can do it?

This branch has not been deployed

No deployments
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