Skip to content

fix: [v1.x backport] skip output schema validation when tool returns isError=True#2467

Open
kimsehwan96 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
kimsehwan96:fix/output-schema-skip-validation-on-error-v1x
Open

fix: [v1.x backport] skip output schema validation when tool returns isError=True#2467
kimsehwan96 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
kimsehwan96:fix/output-schema-skip-validation-on-error-v1x

Conversation

@kimsehwan96
Copy link
Copy Markdown

Backport of #2466 to v1.x branch.

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

Motivation and Context

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

This was already fixed in the TypeScript SDK via modelcontextprotocol/typescript-sdk#655. This PR backports the equivalent fix to the Python SDK v1.x branch.

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(isError=True) with unstructured error content, and verifies the result is returned unchanged without raising a validation error.
  • All 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 isError=True were previously broken, 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.isError:

Fixes #2429

@kimsehwan96 kimsehwan96 force-pushed the fix/output-schema-skip-validation-on-error-v1x branch from 9494c17 to d75eb72 Compare April 18, 2026 09:23
Backport of the main branch fix to v1.x.

When a tool with an inferred output_schema returns
CallToolResult(isError=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-v1x branch from d75eb72 to a3e240c 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.

1 participant