Skip to content

Fix middleware error responses failing outputSchema validation#446

Merged
QuentinAmbard merged 1 commit intomainfrom
fix/middleware-error-outputschema-validation
Apr 9, 2026
Merged

Fix middleware error responses failing outputSchema validation#446
QuentinAmbard merged 1 commit intomainfrom
fix/middleware-error-outputschema-validation

Conversation

@jacksandom
Copy link
Copy Markdown
Collaborator

Summary

  • Fix middleware error responses failing MCP SDK outputSchema validation
  • Raise ToolError instead of returning ToolResult for error cases so the MCP SDK sets isError=True, bypassing outputSchema validation
  • Update tests to assert ToolError is raised and fix incorrect CancelledError test

Context

PR #411 expanded the middleware to catch all exceptions and return them as ToolResult. Since ToolResult is a success-path object, the MCP SDK treats these as successful responses missing structuredContent and rejects them with:

Output validation error: outputSchema defined but no structured output returned

Before #411, general exceptions propagated to the MCP SDK which handled them correctly with isError=True. #411 attempted to set structured_content on error responses but reverted it because error dicts don't match tool-specific output schemas.

The fix raises ToolError (from fastmcp.exceptions) which propagates to the MCP SDK's exception handler → _make_error_result(str(e))CallToolResult(isError=True), bypassing validation entirely while preserving the structured JSON error payloads.

Test plan

  • All 5 middleware unit tests pass
  • Manually trigger a tool error (e.g. manage_jobs with action="bogus") and verify the actual error JSON is returned instead of the outputSchema validation error

@jacksandom jacksandom requested a review from QuentinAmbard April 9, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants