Skip to content

Commit e56525a

Browse files
agaonkerclaude
andcommitted
Remove dead dict branch in _handle_call_tool and fix call_tool return type
`FuncMetadata.convert_result` only ever returns one of three shapes: a `CallToolResult`, a `Sequence[ContentBlock]`, or a `(unstructured_content, structured_content)` tuple. It never returns a raw `dict`, so the `isinstance(result, dict)` branch in `_handle_call_tool` was unreachable (already marked `# pragma: no cover` with a TODO). Remove the dead branch, correct `call_tool`'s return annotation to reflect the three real shapes, and drop the now-orphaned `json` import (used only inside the removed branch). Closes #2695 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent cf110e3 commit e56525a

1 file changed

Lines changed: 1 addition & 10 deletions

File tree

src/mcp/server/mcpserver/server.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import base64
66
import inspect
7-
import json
87
import re
98
from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Sequence
109
from contextlib import AbstractAsyncContextManager, asynccontextmanager
@@ -322,14 +321,6 @@ async def _handle_call_tool(
322321
content=list(unstructured_content), # type: ignore[arg-type]
323322
structured_content=structured_content, # type: ignore[arg-type]
324323
)
325-
if isinstance(result, dict): # pragma: no cover
326-
# TODO: this code path is unreachable — convert_result never returns a raw dict.
327-
# The call_tool return type (Sequence[ContentBlock] | dict[str, Any]) is wrong
328-
# and needs to be cleaned up.
329-
return CallToolResult(
330-
content=[TextContent(type="text", text=json.dumps(result, indent=2))],
331-
structured_content=result,
332-
)
333324
return CallToolResult(content=list(result))
334325

335326
async def _handle_list_resources(
@@ -399,7 +390,7 @@ async def list_tools(self) -> list[MCPTool]:
399390

400391
async def call_tool(
401392
self, name: str, arguments: dict[str, Any], context: Context[LifespanResultT, Any] | None = None
402-
) -> Sequence[ContentBlock] | dict[str, Any]:
393+
) -> CallToolResult | Sequence[ContentBlock] | tuple[Sequence[ContentBlock], dict[str, Any]]:
403394
"""Call a tool by name with arguments."""
404395
if context is None:
405396
context = Context(mcp_server=self)

0 commit comments

Comments
 (0)