fix: skip output schema validation when tool returns is_error=True#2466
Open
kimsehwan96 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: skip output schema validation when tool returns is_error=True#2466kimsehwan96 wants to merge 1 commit intomodelcontextprotocol:mainfrom
kimsehwan96 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
9 tasks
87ef671 to
223f5dc
Compare
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
223f5dc to
979f326
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Skip output schema validation for error results in
convert_result().Motivation and Context
When a tool with an inferred
output_schemareturnsCallToolResult(is_error=True), the SDK'sconvert_result()callsmodel_validate(None)unconditionally, raising a pydanticValidationErrorthat 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?
test_tool_call_result_annotated_is_error_skips_validationthat creates a tool with an output schema, returnsCallToolResult(is_error=True)with unstructured error content, and verifies the result is returned unchanged without raising a validation error.test_func_metadata.pytests pass.ruff checkandruff formatclean.Breaking Changes
None. This only changes behavior for the error path — tools returning
is_error=Truewere previously broken (pydantic error replaced the intended message), so no working code is affected.Types of changes
Checklist
Additional context
One-line fix matching the TypeScript SDK approach:
A separate backport PR for
v1.xwill follow.Fixes #2429