|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import base64 |
| 4 | +from asyncio import Lock |
| 5 | +from contextlib import AsyncExitStack |
| 6 | +from dataclasses import KW_ONLY, dataclass |
| 7 | +from pathlib import Path |
| 8 | +from typing import TYPE_CHECKING, Any, Literal |
| 9 | + |
| 10 | +from pydantic import AnyUrl |
| 11 | +from typing_extensions import Self, assert_never |
| 12 | + |
| 13 | +from pydantic_ai import messages |
| 14 | +from pydantic_ai.exceptions import ModelRetry |
| 15 | +from pydantic_ai.tools import AgentDepsT, RunContext, ToolDefinition |
| 16 | +from pydantic_ai.toolsets import AbstractToolset |
| 17 | +from pydantic_ai.toolsets.abstract import ToolsetTool |
| 18 | + |
| 19 | +try: |
| 20 | + from fastmcp.client import Client |
| 21 | + from fastmcp.client.transports import ClientTransport |
| 22 | + from fastmcp.exceptions import ToolError |
| 23 | + from fastmcp.mcp_config import MCPConfig |
| 24 | + from fastmcp.server import FastMCP |
| 25 | + from mcp.server.fastmcp import FastMCP as FastMCP1Server |
| 26 | + from mcp.types import ( |
| 27 | + AudioContent, |
| 28 | + BlobResourceContents, |
| 29 | + ContentBlock, |
| 30 | + EmbeddedResource, |
| 31 | + ImageContent, |
| 32 | + ResourceLink, |
| 33 | + TextContent, |
| 34 | + TextResourceContents, |
| 35 | + Tool as MCPTool, |
| 36 | + ) |
| 37 | + |
| 38 | + from pydantic_ai.mcp import TOOL_SCHEMA_VALIDATOR |
| 39 | + |
| 40 | +except ImportError as _import_error: |
| 41 | + raise ImportError( |
| 42 | + 'Please install the `fastmcp` package to use the FastMCP server, ' |
| 43 | + 'you can use the `fastmcp` optional group — `pip install "pydantic-ai-slim[fastmcp]"`' |
| 44 | + ) from _import_error |
| 45 | + |
| 46 | + |
| 47 | +if TYPE_CHECKING: |
| 48 | + from fastmcp.client.client import CallToolResult |
| 49 | + |
| 50 | + |
| 51 | +FastMCPToolResult = messages.BinaryContent | dict[str, Any] | str | None |
| 52 | + |
| 53 | +ToolErrorBehavior = Literal['model_retry', 'error'] |
| 54 | + |
| 55 | +UNKNOWN_BINARY_MEDIA_TYPE = 'application/octet-stream' |
| 56 | + |
| 57 | + |
| 58 | +@dataclass(init=False) |
| 59 | +class FastMCPToolset(AbstractToolset[AgentDepsT]): |
| 60 | + """A FastMCP Toolset that uses the FastMCP Client to call tools from a local or remote MCP Server. |
| 61 | +
|
| 62 | + The Toolset can accept a FastMCP Client, a FastMCP Transport, or any other object which a FastMCP Transport can be created from. |
| 63 | +
|
| 64 | + See https://gofastmcp.com/clients/transports for a full list of transports available. |
| 65 | + """ |
| 66 | + |
| 67 | + client: Client[Any] |
| 68 | + """The FastMCP client to use.""" |
| 69 | + |
| 70 | + _: KW_ONLY |
| 71 | + |
| 72 | + tool_error_behavior: Literal['model_retry', 'error'] |
| 73 | + """The behavior to take when a tool error occurs.""" |
| 74 | + |
| 75 | + max_retries: int |
| 76 | + """The maximum number of retries to attempt if a tool call fails.""" |
| 77 | + |
| 78 | + _id: str | None |
| 79 | + |
| 80 | + def __init__( |
| 81 | + self, |
| 82 | + client: Client[Any] |
| 83 | + | ClientTransport |
| 84 | + | FastMCP |
| 85 | + | FastMCP1Server |
| 86 | + | AnyUrl |
| 87 | + | Path |
| 88 | + | MCPConfig |
| 89 | + | dict[str, Any] |
| 90 | + | str, |
| 91 | + *, |
| 92 | + max_retries: int = 1, |
| 93 | + tool_error_behavior: Literal['model_retry', 'error'] = 'model_retry', |
| 94 | + id: str | None = None, |
| 95 | + ) -> None: |
| 96 | + if isinstance(client, Client): |
| 97 | + self.client = client |
| 98 | + else: |
| 99 | + self.client = Client[Any](transport=client) |
| 100 | + |
| 101 | + self._id = id |
| 102 | + self.max_retries = max_retries |
| 103 | + self.tool_error_behavior = tool_error_behavior |
| 104 | + |
| 105 | + self._enter_lock: Lock = Lock() |
| 106 | + self._running_count: int = 0 |
| 107 | + self._exit_stack: AsyncExitStack | None = None |
| 108 | + |
| 109 | + @property |
| 110 | + def id(self) -> str | None: |
| 111 | + return self._id |
| 112 | + |
| 113 | + async def __aenter__(self) -> Self: |
| 114 | + async with self._enter_lock: |
| 115 | + if self._running_count == 0: |
| 116 | + self._exit_stack = AsyncExitStack() |
| 117 | + await self._exit_stack.enter_async_context(self.client) |
| 118 | + |
| 119 | + self._running_count += 1 |
| 120 | + |
| 121 | + return self |
| 122 | + |
| 123 | + async def __aexit__(self, *args: Any) -> bool | None: |
| 124 | + async with self._enter_lock: |
| 125 | + self._running_count -= 1 |
| 126 | + if self._running_count == 0 and self._exit_stack: |
| 127 | + await self._exit_stack.aclose() |
| 128 | + self._exit_stack = None |
| 129 | + |
| 130 | + return None |
| 131 | + |
| 132 | + async def get_tools(self, ctx: RunContext[AgentDepsT]) -> dict[str, ToolsetTool[AgentDepsT]]: |
| 133 | + async with self: |
| 134 | + mcp_tools: list[MCPTool] = await self.client.list_tools() |
| 135 | + |
| 136 | + return { |
| 137 | + tool.name: _convert_mcp_tool_to_toolset_tool(toolset=self, mcp_tool=tool, retries=self.max_retries) |
| 138 | + for tool in mcp_tools |
| 139 | + } |
| 140 | + |
| 141 | + async def call_tool( |
| 142 | + self, name: str, tool_args: dict[str, Any], ctx: RunContext[AgentDepsT], tool: ToolsetTool[AgentDepsT] |
| 143 | + ) -> Any: |
| 144 | + async with self: |
| 145 | + try: |
| 146 | + call_tool_result: CallToolResult = await self.client.call_tool(name=name, arguments=tool_args) |
| 147 | + except ToolError as e: |
| 148 | + if self.tool_error_behavior == 'model_retry': |
| 149 | + raise ModelRetry(message=str(e)) from e |
| 150 | + else: |
| 151 | + raise e |
| 152 | + |
| 153 | + # If we have structured content, return that |
| 154 | + if call_tool_result.structured_content: |
| 155 | + return call_tool_result.structured_content |
| 156 | + |
| 157 | + # Otherwise, return the content |
| 158 | + return _map_fastmcp_tool_results(parts=call_tool_result.content) |
| 159 | + |
| 160 | + |
| 161 | +def _convert_mcp_tool_to_toolset_tool( |
| 162 | + toolset: FastMCPToolset[AgentDepsT], |
| 163 | + mcp_tool: MCPTool, |
| 164 | + retries: int, |
| 165 | +) -> ToolsetTool[AgentDepsT]: |
| 166 | + """Convert an MCP tool to a toolset tool.""" |
| 167 | + return ToolsetTool[AgentDepsT]( |
| 168 | + tool_def=ToolDefinition( |
| 169 | + name=mcp_tool.name, |
| 170 | + description=mcp_tool.description, |
| 171 | + parameters_json_schema=mcp_tool.inputSchema, |
| 172 | + metadata={ |
| 173 | + 'meta': mcp_tool.meta, |
| 174 | + 'annotations': mcp_tool.annotations.model_dump() if mcp_tool.annotations else None, |
| 175 | + 'output_schema': mcp_tool.outputSchema or None, |
| 176 | + }, |
| 177 | + ), |
| 178 | + toolset=toolset, |
| 179 | + max_retries=retries, |
| 180 | + args_validator=TOOL_SCHEMA_VALIDATOR, |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +def _map_fastmcp_tool_results(parts: list[ContentBlock]) -> list[FastMCPToolResult] | FastMCPToolResult: |
| 185 | + """Map FastMCP tool results to toolset tool results.""" |
| 186 | + mapped_results = [_map_fastmcp_tool_result(part) for part in parts] |
| 187 | + |
| 188 | + if len(mapped_results) == 1: |
| 189 | + return mapped_results[0] |
| 190 | + |
| 191 | + return mapped_results |
| 192 | + |
| 193 | + |
| 194 | +def _map_fastmcp_tool_result(part: ContentBlock) -> FastMCPToolResult: |
| 195 | + if isinstance(part, TextContent): |
| 196 | + return part.text |
| 197 | + elif isinstance(part, ImageContent | AudioContent): |
| 198 | + return messages.BinaryContent(data=base64.b64decode(part.data), media_type=part.mimeType) |
| 199 | + elif isinstance(part, EmbeddedResource): |
| 200 | + if isinstance(part.resource, BlobResourceContents): |
| 201 | + return messages.BinaryContent( |
| 202 | + data=base64.b64decode(part.resource.blob), |
| 203 | + media_type=part.resource.mimeType or UNKNOWN_BINARY_MEDIA_TYPE, |
| 204 | + ) |
| 205 | + elif isinstance(part.resource, TextResourceContents): |
| 206 | + return part.resource.text |
| 207 | + else: |
| 208 | + assert_never(part.resource) |
| 209 | + elif isinstance(part, ResourceLink): |
| 210 | + # ResourceLink is not yet supported by the FastMCP toolset as reading resources is not yet supported. |
| 211 | + raise NotImplementedError( |
| 212 | + 'ResourceLink is not supported by the FastMCP toolset as reading resources is not yet supported.' |
| 213 | + ) |
| 214 | + else: |
| 215 | + assert_never(part) |
0 commit comments