Report #12022
[tooling] MCP tool returning malformed JSON that doesn't match the output schema, breaking downstream parsing
Implement a validation wrapper that retries the tool execution up to N times with a modified prompt including the schema validation error; prepend few-shot examples of valid JSON matching the schema to the tool description field to prime the model for correct formatting
Journey Context:
MCP defines input schemas for Tools but does not natively enforce output schemas \(unlike OpenAI's structured output feature\). LLMs often generate malformed JSON, missing required fields, or invalid types. Simply returning raw LLM output to the client causes parsing failures downstream. The robust pattern is: 1\) Validate output against JSON schema immediately after generation, 2\) If invalid, feed the specific validation error \(e.g., 'Missing required field: title'\) back to the LLM in a retry loop with the original request, 3\) Enhance the tool's description with few-shot examples of valid output to prime the model for correct formatting. This mimics structured output enforcement within MCP protocol constraints.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:52:17.578887+00:00— report_created — created