Skip to content

fix(validation): only match tool_use/tool_result IDs when last message has tool_result#2981

Open
syf2211 wants to merge 1 commit into
modelcontextprotocol:mainfrom
syf2211:fix/2960-validate-tool-use-false-positive
Open

fix(validation): only match tool_use/tool_result IDs when last message has tool_result#2981
syf2211 wants to merge 1 commit into
modelcontextprotocol:mainfrom
syf2211:fix/2960-validate-tool-use-false-positive

Conversation

@syf2211

@syf2211 syf2211 commented Jun 26, 2026

Copy link
Copy Markdown

Summary

Fix a false positive in validate_tool_use_result_messages() when a previous tool_use block is followed by a plain text response without any tool_result blocks.

Motivation

Per SEP-1577, tool_result blocks must be preceded by a tool_use block, but there is no requirement that tool_use must be followed by tool_result. A plain text user reply after an assistant tool_use is valid.

Fixes #2960

Changes

  • Move the tool_use/tool_result ID matching check inside the existing if has_tool_results: block in src/mcp/server/validation.py
  • Add regression test reproducing the issue scenario

Tests

uv run pytest tests/server/test_validation.py -v

Result: 16 passed

Also ran:

uv run ruff check src/mcp/server/validation.py tests/server/test_validation.py

Result: All checks passed

Notes

AI assistance was used to implement this fix. The change is minimal (3-line scope move) and aligns with the root cause described in the issue.

Disclosure: This PR was prepared with AI assistance and reviewed before submission.

…e has tool_result

Fixes modelcontextprotocol#2960

Move the ID-matching check inside the has_tool_results block so
validate_tool_use_result_messages() does not raise when a previous
tool_use is followed by a plain text response without tool_result.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

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.

[Bug] validate_tool_use_result_messages raises false positive when tool_use is not followed by tool_result

2 participants