|
5 | 5 | import base64 |
6 | 6 | import inspect |
7 | 7 | import re |
8 | | -from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Sequence |
| 8 | +from collections.abc import AsyncIterator, Awaitable, Callable, Iterable |
9 | 9 | from contextlib import AbstractAsyncContextManager, asynccontextmanager |
10 | | -from typing import Any, Generic, Literal, TypeAlias, TypeVar, cast, overload |
| 10 | +from typing import Any, Generic, Literal, TypeVar, overload |
11 | 11 |
|
12 | 12 | import anyio |
13 | 13 | import pydantic_core |
|
50 | 50 | CompleteRequestParams, |
51 | 51 | CompleteResult, |
52 | 52 | Completion, |
53 | | - ContentBlock, |
54 | 53 | GetPromptRequestParams, |
55 | 54 | GetPromptResult, |
56 | 55 | Icon, |
|
75 | 74 |
|
76 | 75 | _CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) |
77 | 76 |
|
78 | | -ToolResult: TypeAlias = CallToolResult | Sequence[ContentBlock] | tuple[Sequence[ContentBlock], dict[str, Any]] |
79 | | - |
80 | 77 |
|
81 | 78 | class Settings(BaseSettings, Generic[LifespanResultT]): |
82 | 79 | """MCPServer settings. |
@@ -310,20 +307,11 @@ async def _handle_call_tool( |
310 | 307 | ) -> CallToolResult: |
311 | 308 | context = Context(request_context=ctx, mcp_server=self) |
312 | 309 | try: |
313 | | - result = await self.call_tool(params.name, params.arguments or {}, context) |
| 310 | + return await self.call_tool(params.name, params.arguments or {}, context) |
314 | 311 | except MCPError: |
315 | 312 | raise |
316 | 313 | except Exception as e: |
317 | 314 | return CallToolResult(content=[TextContent(type="text", text=str(e))], is_error=True) |
318 | | - if isinstance(result, CallToolResult): |
319 | | - return result |
320 | | - if isinstance(result, tuple) and len(result) == 2: |
321 | | - unstructured_content, structured_content = cast(tuple[Sequence[ContentBlock], dict[str, Any]], result) |
322 | | - return CallToolResult( |
323 | | - content=list(unstructured_content), |
324 | | - structured_content=structured_content, |
325 | | - ) |
326 | | - return CallToolResult(content=list(result)) |
327 | 315 |
|
328 | 316 | async def _handle_list_resources( |
329 | 317 | self, ctx: ServerRequestContext[LifespanResultT], params: PaginatedRequestParams | None |
@@ -392,18 +380,17 @@ async def list_tools(self) -> list[MCPTool]: |
392 | 380 |
|
393 | 381 | async def call_tool( |
394 | 382 | self, name: str, arguments: dict[str, Any], context: Context[LifespanResultT, Any] | None = None |
395 | | - ) -> ToolResult: |
396 | | - """Call a tool by name with arguments. |
397 | | -
|
398 | | - Returns: |
399 | | - The tool result converted for the low-level handler: |
400 | | - - a `CallToolResult` returned directly by the tool, |
401 | | - - a sequence of content blocks for unstructured tools, or |
402 | | - - a `(content, structured_content)` tuple for tools with structured output. |
403 | | - """ |
| 383 | + ) -> CallToolResult: |
| 384 | + """Call a tool by name with arguments.""" |
404 | 385 | if context is None: |
405 | 386 | context = Context(mcp_server=self) |
406 | | - return await self._tool_manager.call_tool(name, arguments, context, convert_result=True) |
| 387 | + result = await self._tool_manager.call_tool(name, arguments, context, convert_result=True) |
| 388 | + if isinstance(result, CallToolResult): |
| 389 | + return result |
| 390 | + if isinstance(result, tuple): |
| 391 | + content, structured_content = result |
| 392 | + return CallToolResult(content=list(content), structured_content=structured_content) |
| 393 | + return CallToolResult(content=list(result)) |
407 | 394 |
|
408 | 395 | async def list_resources(self) -> list[MCPResource]: |
409 | 396 | """List all available resources.""" |
|
0 commit comments