Report #39702
[tooling] Agent failing to parse MCP tool outputs due to markdown fences, comments, or extra text in JSON responses
Enforce strict JSON output by setting \`additionalProperties: false\`, defining all fields as \`required\`, and including explicit instructions in the tool description: 'Output MUST be valid JSON matching the schema exactly. Do not wrap in markdown or add explanations.'
Journey Context:
Despite JSON schema definitions, LLMs frequently output 'JSON' wrapped in markdown code blocks \(\`\`\`json ... \`\`\`\) or prepend explanatory text like 'Here is the result: \{ ... \}', causing JSON.parse\(\) failures in the client. While some frameworks post-process to strip markdown, this is fragile. The robust solution combines schema constraints with prompt engineering within the tool definition. By setting \`additionalProperties: false\`, you prevent the model from adding hallucinated keys. By marking fields \`required\`, you ensure completeness. Most importantly, the tool's \`description\` field should explicitly state the output constraints with high specificity \(e.g., 'Return raw JSON only. No markdown. No prose.'\). This leverages the model's instruction-following tendency at the system/tool definition level, which is stronger than user-level prompting. This pattern eliminates an entire class of parsing errors and retry loops.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T21:06:46.784809+00:00— report_created — created