From 4e6599184a765d1f5709753350e3fd930934f17d Mon Sep 17 00:00:00 2001 From: Wahaj Ahmed Date: Sat, 13 Jun 2026 01:40:39 +0200 Subject: [PATCH] fix: remove dead code path in _handle_call_tool and correct call_tool return type The isinstance(result, dict) branch in _handle_call_tool is unreachable because FuncMetadata.convert_result never returns a raw dict -- it returns CallToolResult, Sequence[ContentBlock], or a tuple of (Sequence[ContentBlock], dict[str, Any]). This change: - Removes the dead dict branch - Drops the now-unused import json - Corrects call_tool's return type annotation to reflect the actual return shapes Closes #2695 Co-authored-by: AI assistant (code review and diff structuring) Signed-off-by: Wahaj Ahmed --- src/mcp/server/mcpserver/server.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/mcp/server/mcpserver/server.py b/src/mcp/server/mcpserver/server.py index fdb69571d..f4c971ff7 100644 --- a/src/mcp/server/mcpserver/server.py +++ b/src/mcp/server/mcpserver/server.py @@ -4,7 +4,6 @@ import base64 import inspect -import json import re from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Sequence from contextlib import AbstractAsyncContextManager, asynccontextmanager @@ -322,14 +321,6 @@ async def _handle_call_tool( content=list(unstructured_content), # type: ignore[arg-type] structured_content=structured_content, # type: ignore[arg-type] ) - if isinstance(result, dict): # pragma: no cover - # TODO: this code path is unreachable — convert_result never returns a raw dict. - # The call_tool return type (Sequence[ContentBlock] | dict[str, Any]) is wrong - # and needs to be cleaned up. - return CallToolResult( - content=[TextContent(type="text", text=json.dumps(result, indent=2))], - structured_content=result, - ) return CallToolResult(content=list(result)) async def _handle_list_resources( @@ -399,7 +390,7 @@ async def list_tools(self) -> list[MCPTool]: async def call_tool( self, name: str, arguments: dict[str, Any], context: Context[LifespanResultT, Any] | None = None - ) -> Sequence[ContentBlock] | dict[str, Any]: + ) -> CallToolResult | Sequence[ContentBlock] | tuple[Sequence[ContentBlock], dict[str, Any]]: """Call a tool by name with arguments.""" if context is None: context = Context(mcp_server=self)