Skip to content

fix: skip output schema validation when tool returns is_error=True#2466

Open
kimsehwan96 wants to merge 1 commit intomodelcontextprotocol:mainfrom
kimsehwan96:fix/output-schema-skip-validation-on-error
Open

fix: skip output schema validation when tool returns is_error=True#2466
kimsehwan96 wants to merge 1 commit intomodelcontextprotocol:mainfrom
kimsehwan96:fix/output-schema-skip-validation-on-error

Conversation

@kimsehwan96
Copy link
Copy Markdown

Skip output schema validation for error results in convert_result().

Motivation and Context

When a tool with an inferred output_schema returns CallToolResult(is_error=True), the SDK's convert_result() calls model_validate(None) unconditionally, raising a pydantic ValidationError that replaces the intended error message (e.g., "Division by zero" becomes a pydantic schema error).

This was already fixed in the TypeScript SDK via modelcontextprotocol/typescript-sdk#655, but the equivalent fix has not been applied to the Python SDK.

How Has This Been Tested?

  • Added regression test test_tool_call_result_annotated_is_error_skips_validation that creates a tool with an output schema, returns CallToolResult(is_error=True) with unstructured error content, and verifies the result is returned unchanged without raising a validation error.
  • All 34 existing test_func_metadata.py tests pass.
  • ruff check and ruff format clean.

Breaking Changes

None. This only changes behavior for the error path — tools returning is_error=True were previously broken (pydantic error replaced the intended message), so no working code is affected.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

One-line fix matching the TypeScript SDK approach:

-            if self.output_schema is not None:
+            if self.output_schema is not None and not result.is_error:

A separate backport PR for v1.x will follow.

Fixes #2429

When a tool with an inferred output_schema returns
CallToolResult(is_error=True), the SDK's convert_result() calls
model_validate(None) unconditionally, raising a pydantic error that
replaces the intended error message.

Skip output schema validation when the result signals an error,
matching the TypeScript SDK fix (modelcontextprotocol/typescript-sdk#655).

Fixes modelcontextprotocol#2429
@kimsehwan96 kimsehwan96 force-pushed the fix/output-schema-skip-validation-on-error branch from 223f5dc to 979f326 Compare April 18, 2026 09:49
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.

[v1.x] Server-side outputSchema validation blocks tool error reporting (isError: true)

1 participant