Add validate_output option for OpenAPI tools#3134
Conversation
When backends don't conform to their own OpenAPI spec, the MCP SDK's output validation rejects the response and the tool fails. This adds a validate_output parameter (default True) that replaces strict output schemas with a permissive one, keeping structured JSON flowing while making validation a no-op. Closes #2472
WalkthroughAdds a new optional parameter Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f7c4575952
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| "type": "object", | ||
| "additionalProperties": True, | ||
| } | ||
| if output_schema.get("x-fastmcp-wrap-result"): | ||
| permissive["x-fastmcp-wrap-result"] = True |
There was a problem hiding this comment.
Preserve wrapping when output validation is disabled
With validate_output=False, this replaces the extracted schema with a plain object schema and only carries x-fastmcp-wrap-result forward when it already existed. For endpoints whose spec says type: object, a backend that actually returns an array/primitive will not be wrapped, so OpenAPITool.run passes a non-dict structured_content and ToolResult raises structured_content must be a dict; the tool still fails instead of acting as a validation opt-out for response shape mismatches.
Useful? React with 👍 / 👎.
When validate_output=False and a backend returns an array where the
schema declared an object, the response slipped through unwrapped,
causing MCP protocol failures. Now non-dict structured content is
always wrapped in {"result": ...}.
When backends don't conform to their own OpenAPI response schemas, the MCP SDK's output validation rejects the response and the tool fails with "Output validation error." Users have no way to opt out — they're stuck if the backend is wrong.
This adds
validate_output(defaultTrue) toOpenAPIProviderandFastMCP.from_openapi(). When set toFalse, extracted output schemas are replaced with a permissive{"type": "object", "additionalProperties": true}that accepts any response. Structured JSON still flows to clients normally; only the strict schema checking is disabled. Thex-fastmcp-wrap-resultflag is preserved so non-object responses (arrays, primitives) continue to be wrapped correctly.For per-tool control,
mcp_component_fncan be used to selectively disable validation on specific endpoints.Closes #2472