Skip to content

Commit 9571502

Browse files
Update call_tool return type and remove unreachable code
Refactor call_tool return type to include CallToolResult and tuple.
1 parent 3eb5799 commit 9571502

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

src/mcp/server/mcpserver/server.py

Lines changed: 10 additions & 11 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,8 +390,16 @@ 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]:
403-
"""Call a tool by name with arguments."""
393+
) -> CallToolResult | Sequence[ContentBlock] | tuple[Sequence[ContentBlock], dict[str, Any]]:
394+
"""Call a tool by name with arguments.
395+
396+
The return type reflects what ``FuncMetadata.convert_result`` produces:
397+
398+
- a :class:`CallToolResult` if the tool function returned one directly;
399+
- a ``Sequence[ContentBlock]`` for tools without an output schema;
400+
- a ``(unstructured_content, structured_content)`` tuple for tools with
401+
an output schema.
402+
"""
404403
if context is None:
405404
context = Context(mcp_server=self)
406405
return await self._tool_manager.call_tool(name, arguments, context, convert_result=True)

0 commit comments

Comments
 (0)