|
4 | 4 |
|
5 | 5 | import base64 |
6 | 6 | import inspect |
7 | | -import json |
8 | 7 | import re |
9 | 8 | from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Sequence |
10 | 9 | from contextlib import AbstractAsyncContextManager, asynccontextmanager |
@@ -322,14 +321,6 @@ async def _handle_call_tool( |
322 | 321 | content=list(unstructured_content), # type: ignore[arg-type] |
323 | 322 | structured_content=structured_content, # type: ignore[arg-type] |
324 | 323 | ) |
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 | | - ) |
333 | 324 | return CallToolResult(content=list(result)) |
334 | 325 |
|
335 | 326 | async def _handle_list_resources( |
@@ -399,8 +390,16 @@ async def list_tools(self) -> list[MCPTool]: |
399 | 390 |
|
400 | 391 | async def call_tool( |
401 | 392 | 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 | + """ |
404 | 403 | if context is None: |
405 | 404 | context = Context(mcp_server=self) |
406 | 405 | return await self._tool_manager.call_tool(name, arguments, context, convert_result=True) |
|
0 commit comments