fix(server): don't raise when tool_use is not followed by tool_result#2962
Open
Bartok9 wants to merge 1 commit into
Open
fix(server): don't raise when tool_use is not followed by tool_result#2962Bartok9 wants to merge 1 commit into
Bartok9 wants to merge 1 commit into
Conversation
Closes modelcontextprotocol#2960 Per SEP-1577, tool_result blocks MUST be preceded by a tool_use block, but there is no requirement that a tool_use MUST be followed by a tool_result. A plain text response after a tool_use is valid. The ID-matching check in validate_tool_use_result_messages ran whenever the previous message contained a tool_use, regardless of whether the last message actually had any tool_result blocks. With no tool_result blocks present, tool_result_ids was empty and never matched the non-empty tool_use_ids, raising a false 'ids ... do not match' ValueError. Gate the check on has_tool_results so it only runs when the last message actually contains tool_result blocks. Adds a regression test that fails without the fix.
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.
Summary
validate_tool_use_result_messages()no longer raises a falseValueErrorwhen atool_useblock is followed by a plain (non-tool_result) response.Motivation
Closes #2960.
Per SEP-1577:
tool_resultblocks MUST be preceded by atool_useblock ✅ (already checked)tool_useMUST be followed by atool_result.The ID-matching block at the end of the function ran whenever the previous message contained a
tool_use, regardless of whether the last message actually contained anytool_resultblocks:When the last message has no
tool_resultblocks,tool_result_idsis empty and never equals the non-emptytool_use_ids, so a valid conversation raisedValueError: ids of tool_result blocks and tool_use blocks from previous message do not match.Fix
Gate the ID-matching check on
has_tool_resultsso it only runs when the last message actually containstool_resultblocks:This is a one-line change; all existing structural checks (mixed content, missing preceding tool_use, ID mismatch when results ARE present) are unaffected.
Verification
uv run pytest tests/server/test_validation.py— 16 passeduv run ruff check/ruff format --check— cleanReal behavior proof
Regression test
test_validate_tool_use_result_messages_no_error_when_tool_use_not_followed_by_tool_resultreproduces the exact case from the issue (assistant emits text + tool_use; user replies with plain text). It fails without the fix and passes with it: