Skip to content

fix: correct MCPServer call_tool result type#2816

Open
fengjikui wants to merge 1 commit into
modelcontextprotocol:mainfrom
fengjikui:fix-mcpserver-call-tool-result-shapes
Open

fix: correct MCPServer call_tool result type#2816
fengjikui wants to merge 1 commit into
modelcontextprotocol:mainfrom
fengjikui:fix-mcpserver-call-tool-result-shapes

Conversation

@fengjikui

Copy link
Copy Markdown

Summary

Fixes #2695.

MCPServer.call_tool() currently advertises Sequence[ContentBlock] | dict[str, Any], but the convert_result=True path never returns a raw dict. It can return only:

  • a direct CallToolResult,
  • a Sequence[ContentBlock] for unstructured tools, or
  • a (content, structured_content) tuple for structured tools.

This PR removes the unreachable raw-dict branch in _handle_call_tool(), drops the now-unused json import, and introduces a ToolResult type alias so the public annotation and handler implementation share the same result shape.

Tests

  • uv run --frozen pytest tests/server/mcpserver/test_server.py -q
  • uv run --frozen pytest tests/server/mcpserver/test_server.py::TestServerTools::test_call_tool_returns_declared_result_shapes -q
  • uv 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-cover
  • uv run --frozen ruff format --check .
  • uv run --frozen ruff check .
  • uv run --frozen pyright
  • uvx --from pre-commit pre-commit run markdownlint --files docs/migration.md
  • uvx --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.py

AI 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.

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.

Dead code path in MCPServer._handle_call_tool and incorrect call_tool return type

1 participant