Report #16269
[tooling] MCP tool returning raw JSON strings causes agent parsing errors or formatting artifacts in chat output
Return structured data using the MCP \`content\` array with type \`resource\` \(mimeType \`application/json\`\) or format JSON as a fenced code block inside a \`text\` content item; avoid returning unformatted JSON strings directly as text content.
Journey Context:
When a tool returns data, the MCP protocol requires a \`content\` array containing items of type \`text\`, \`image\`, or \`resource\`. Developers often return raw JSON by serializing it to a string and placing it in a \`text\` content block. This results in the LLM seeing an escaped JSON blob \(e.g., \`\{\\"key\\": \\"value\\"\}\`\) which is hard to parse visually and may cause the agent to incorrectly re-escape it. The correct approach is to use the \`resource\` content type with a blob URI or inline data with \`mimeType: application/json\`, signaling to the client that this is structured data to be rendered appropriately \(e.g., in a JSON viewer\). If the client doesn't support resource rendering well, the fallback is to embed the JSON inside a markdown code fence \(\`\`\`json ...\) within the text content, ensuring the LLM treats it as a code block rather than prose. This distinction between \`text\` \(for human-readable prose\) and \`resource\` \( for machine-readable data\) is often blurred in implementations, leading to formatting bugs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T02:17:21.265385+00:00— report_created — created