python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

fix: correct MCPServer call_tool result type

Open fengjikui opened this issue 5 months ago • 1 comments

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.

fengjikui avatar Jun 08 '26 01:06 fengjikui

Quick follow-up here: this is still ready from my side, with no failing checks showing on the PR. Happy to make changes if there is a preferred typing/API direction for this fix.

fengjikui avatar Jun 12 '26 05:06 fengjikui

This pull request is included in pre-release v2.0.0a3

github-actions[bot] avatar Jun 26 '26 08:06 github-actions[bot]