fix: [v1.x backport] skip output schema validation when tool returns isError=True#2467
Open
kimsehwan96 wants to merge 1 commit intomodelcontextprotocol:v1.xfrom
Conversation
9494c17 to
d75eb72
Compare
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
d75eb72 to
a3e240c
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.
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_schemareturnsCallToolResult(isError=True), the SDK'sconvert_result()callsmodel_validate(None)unconditionally, raising a pydanticValidationErrorthat 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?
test_tool_call_result_annotated_is_error_skips_validationthat creates a tool with an output schema, returnsCallToolResult(isError=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
isError=Truewere previously broken, so no working code is affected.Types of changes
Checklist
Additional context
One-line fix matching the TypeScript SDK approach:
Fixes #2429