eyaltoledano/claude-task-master · error · MCPError
Generated object does not match schema: ${validationError.me
Error message
Generated object does not match schema: ${validationError.message}. Generated: ${JSON.stringify(parsedObject, null, 2)} What it means
After parsing the model's JSON, doGenerateObject validates it against the provided schema; if validation fails it throws an MCPError containing the validator's message plus the full pretty-printed generated object. This means the model produced valid JSON that doesn't conform to the requested structure (missing fields, wrong types, extra/invalid values).
Source
Thrown at mcp-server/src/custom-sdk/language-model.js:173
// Validate against schema
try {
const validatedObject = schema.parse(parsedObject);
return {
object: validatedObject,
finishReason: result.finishReason || 'stop',
usage: {
promptTokens: result.usage?.inputTokens || 0,
completionTokens: result.usage?.outputTokens || 0,
totalTokens:
(result.usage?.inputTokens || 0) +
(result.usage?.outputTokens || 0)
},
rawResponse: response,
warnings: result.warnings
};
} catch (validationError) {
throw new MCPError(
`Generated object does not match schema: ${validationError.message}. Generated: ${JSON.stringify(parsedObject, null, 2)}`
);
}
} catch (error) {
throw mapMCPError(error);
}
}
/**
* Stream text generation using MCP session sampling
* Note: MCP may not support native streaming, so this may simulate streaming
* @param {object} options - Generation options
* @returns {AsyncIterable} Stream of generation chunks
*/
async doStream(options) {
try {
// For now, simulate streaming by chunking the complete response
// TODO: Implement native streaming if MCP supports itView on GitHub (pinned to c0c98d367c)
Solutions
- Make the schema lenient where possible (optional fields, defaults, unions) and re-prompt with explicit field requirements and an example output
- Retry the generation — schema-adherence failures are often stochastic; add 'Respond ONLY with JSON matching this exact schema' instructions
- Post-process/repair the object (fill defaults, coerce types) before validation, or use a repair/re-ask loop
Example fix
// before
const schema = z.object({ count: z.number() });
// after
const schema = z.object({ count: z.coerce.number().default(0) }); Defensive patterns
Strategy: try-catch
Validate before calling
// Pre-validate expected fields if you control the prompt/output contract
if (!jsonText.includes('"requiredField"')) console.warn('Response may be missing required fields'); Try / catch
try { obj = await model.doGenerateObject({ schema, prompt }); } catch (e) {
if (e.message.startsWith('Generated object does not match schema')) {
// retry with a stricter prompt or relax the schema
} else throw e;
} Prevention
- Include an explicit JSON example matching the schema in the prompt
- Keep schemas simple: avoid strict enums/formats the model may violate; mark non-essential fields optional
- Implement one retry-with-feedback loop: feed the validation error back to the model on failure
When it happens
Trigger: Model returns JSON whose shape fails schema validation — missing required properties, wrong types (string vs number), or values violating constraints — when calling doGenerateObject/generateObject through the MCP language model.
Common situations: Weak models hallucinating fields or omitting required ones; overly strict schemas (enums, formats) the model can't satisfy; schema/prompt mismatch after refactoring.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- Schema is required for object generation
- Failed to parse JSON response: ${parseError.message}. Respon
- MCP provider requires session object
- The MCP model function cannot be called with the new keyword
- MCP session must have client sampling capabilities
AI-assisted analysis of eyaltoledano/claude-task-master@c0c98d367c (2026-08-29).
Data as JSON: /api/errors/2cf616d8040dd58d.
Report an issue: GitHub.