fix: correct MCPServer call_tool result type#2816
Merged
Kludex merged 5 commits intoJun 20, 2026
Merged
Conversation
This comment was marked as spam.
This comment was marked as spam.
Move the conversion of the internal tool-result shapes (bare content sequence and (content, structured_content) tuple) out of the low-level handler and into call_tool, so the public API has a single CallToolResult return type. Type the conversion chain with a ToolResult alias and convert_result overloads keyed on the convert_result flag, removing the cast and type: ignore the previous approach needed.
ToolManager.call_tool forwards convert_result as a bool, never a literal, so the Literal-keyed overloads never resolved. The overloads that matter are on ToolManager.call_tool, which MCPServer.call_tool calls with convert_result=True.
Have FuncMetadata.convert_result return a CallToolResult instead of the intermediate (content, structured_content) tuple / bare content sequence shapes. MCPServer.call_tool becomes a passthrough, and the ToolResult alias plus the convert_result overloads on ToolManager.call_tool are no longer needed.
maxisbey
approved these changes
Jun 20, 2026
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
Fixes #2695.
MCPServer.call_tool()currently advertisesSequence[ContentBlock] | dict[str, Any], but theconvert_result=Truepath never returns a raw dict. It can return only:CallToolResult,Sequence[ContentBlock]for unstructured tools, or(content, structured_content)tuple for structured tools.This PR removes the unreachable raw-dict branch in
_handle_call_tool(), drops the now-unusedjsonimport, and introduces aToolResulttype alias so the public annotation and handler implementation share the same result shape.Tests
uv run --frozen pytest tests/server/mcpserver/test_server.py -quv run --frozen pytest tests/server/mcpserver/test_server.py::TestServerTools::test_call_tool_returns_declared_result_shapes -quv run --frozen coverage erase && uv run --frozen coverage run -m pytest tests/server/mcpserver/test_server.py && uv run --frozen coverage combine && uv run --frozen coverage report --include='src/mcp/server/mcpserver/server.py' --fail-under=0 && UV_FROZEN=1 uv run --frozen strict-no-coveruv run --frozen ruff format --check .uv run --frozen ruff check .uv run --frozen pyrightuvx --from pre-commit pre-commit run markdownlint --files docs/migration.mduvx --from pre-commit pre-commit run end-of-file-fixer --files docs/migration.md src/mcp/server/mcpserver/server.py tests/server/mcpserver/test_server.pyAI assistance disclosure
I used an AI coding assistant to help trace the result shapes and draft the patch. I reviewed the final diff and ran the validation commands above locally.