fix: skip outputSchema validation when CallToolResult.is_error=True#2508
Open
RanjithHiremath wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: skip outputSchema validation when CallToolResult.is_error=True#2508RanjithHiremath wants to merge 1 commit intomodelcontextprotocol:mainfrom
RanjithHiremath wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
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.
Resolves #2429.
When a tool function with an
output_schemareturnsCallToolResult(is_error=True),convert_result()was unconditionally callingoutput_model.model_validate(result.structured_content). For an error result,structured_contentisNone, somodel_validate(None)raises a PydanticValidationErrorthat replaces the tool's actual error message before it reaches the client.Skipping schema validation when
is_erroris true matches the resolution that landed in typescript-sdk#655.Motivation and Context
Closes #2429.
How Has This Been Tested?
Added
test_tool_call_result_with_is_error_skips_output_schema_validationintests/server/mcpserver/test_func_metadata.py. The test registers a function with an inferred output schema, returnsCallToolResult(content=[], is_error=True), and assertsconvert_result()returns the result unchanged. Without the fix the test fails withpydantic.ValidationError(becausemodel_validate(None)runs against a model with required fields); with the fix it passes.Verified locally:
uv run --frozen ruff check src tests— cleanuv run --frozen ruff format --check src tests— cleanuv run --frozen pyright— 0 errors./scripts/test— 100.00% coverage,strict-no-coverpassesBreaking Changes
None.
Types of changes
Checklist
Additional context
While reviewing, I noticed
src/mcp/client/session.py:336has the same bug pattern client-side: it raisesRuntimeError("Tool ... has an output schema but did not return structured content")without first checkingresult.is_error. I'm scoping that out of this PR (different surface, separate code path) — happy to file a follow-up issue once this is in.A
v1.xbackport will follow as a separate PR after this lands (src/mcp/server/fastmcp/utilities/func_metadata.py:115, same change with the v1.x camelCase fields).