Skip to content

fix: correct return type annotation for MCPServer.call_tool()#2159

Closed
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone:fix/call-tool-return-type
Closed

fix: correct return type annotation for MCPServer.call_tool()#2159
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone:fix/call-tool-return-type

Conversation

@giulio-leone
Copy link

Summary

Fixes #1251

The return type annotation for MCPServer.call_tool() was Sequence[ContentBlock] | dict[str, Any], but the actual return types from convert_result() are:

  • Sequence[ContentBlock] — when no output schema is defined
  • tuple[Sequence[ContentBlock], dict[str, Any]] — when an output schema is set
  • CallToolResult — when the tool function returns a CallToolResult directly

The dict[str, Any] variant was unreachable, as noted by an inline TODO comment in _handle_call_tool. This could mislead subclass authors into returning a bare dict from call_tool() overrides, which would hit the dead-code fallback path instead of being handled correctly.

Changes

  • Updated the return type on call_tool() to Sequence[ContentBlock] | tuple[Sequence[ContentBlock], dict[str, Any]] | CallToolResult
  • Removed the stale TODO comment that documented this exact issue

Test Results

475 server tests pass (2 consecutive clean runs).

The return type was annotated as Sequence[ContentBlock] | dict[str, Any]
but the actual return types from convert_result() are:
- Sequence[ContentBlock] (when no output schema)
- tuple[Sequence[ContentBlock], dict[str, Any]] (when output schema is set)
- CallToolResult (when tool returns CallToolResult directly)

The dict[str, Any] variant was unreachable, as noted by a TODO comment
in _handle_call_tool. Update the annotation to match the actual behavior
and remove the stale TODO.

Fixes modelcontextprotocol#1251

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Kludex
Copy link
Member

Kludex commented Feb 28, 2026

This is not the way to go, we need to drop the tuple on the return statement.

@Kludex Kludex closed this Feb 28, 2026
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.

Incorrect typing hint for FastMCP.call_tool

2 participants