|
| 1 | +# FastMCP Client |
| 2 | + |
| 3 | +[FastMCP](https://gofastmcp.com/) is a higher-level MCP framework that bills itself as "The fast, Pythonic way to build MCP servers and clients." It supports additional capabilities on top of the MCP specification like [Tool Transformation](https://gofastmcp.com/patterns/tool-transformation), [OAuth](https://gofastmcp.com/clients/auth/oauth), and more. |
| 4 | + |
| 5 | +As an alternative to Pydantic AI's standard [`MCPServer` MCP client](client.md) built on the [MCP SDK](https://github.com/modelcontextprotocol/python-sdk), you can use the [`FastMCPToolset`][pydantic_ai.toolsets.fastmcp.FastMCPToolset] [toolset](../toolsets.md) that leverages the [FastMCP Client](https://gofastmcp.com/clients/) to connect to local and remote MCP servers, whether or not they're built using [FastMCP Server](https://gofastmcp.com/servers/). |
| 6 | + |
| 7 | +Note that it does not yet support integration elicitation or sampling, which are supported by the [standard `MCPServer` client](client.md). |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +To use the `FastMCPToolset`, you will need to install [`pydantic-ai-slim`](../install.md#slim-install) with the `fastmcp` optional group: |
| 12 | + |
| 13 | +```bash |
| 14 | +pip/uv-add "pydantic-ai-slim[fastmcp]" |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +A `FastMCPToolset` can then be created from: |
| 20 | + |
| 21 | +- A FastMCP Server: `#!python FastMCPToolset(fastmcp.FastMCP('my_server'))` |
| 22 | +- A FastMCP Client: `#!python FastMCPToolset(fastmcp.Client(...))` |
| 23 | +- A FastMCP Transport: `#!python FastMCPToolset(fastmcp.StdioTransport(command='uvx', args=['mcp-run-python', 'stdio']))` |
| 24 | +- A Streamable HTTP URL: `#!python FastMCPToolset('http://localhost:8000/mcp')` |
| 25 | +- An HTTP SSE URL: `#!python FastMCPToolset('http://localhost:8000/sse')` |
| 26 | +- A Python Script: `#!python FastMCPToolset('my_server.py')` |
| 27 | +- A Node.js Script: `#!python FastMCPToolset('my_server.js')` |
| 28 | +- A JSON MCP Configuration: `#!python FastMCPToolset({'mcpServers': {'my_server': {'command': 'uvx', 'args': ['mcp-run-python', 'stdio']}}})` |
| 29 | + |
| 30 | +If you already have a [FastMCP Server](https://gofastmcp.com/servers) in the same codebase as your Pydantic AI agent, you can create a `FastMCPToolset` directly from it and save agent a network round trip: |
| 31 | + |
| 32 | +```python |
| 33 | +from fastmcp import FastMCP |
| 34 | + |
| 35 | +from pydantic_ai import Agent |
| 36 | +from pydantic_ai.toolsets.fastmcp import FastMCPToolset |
| 37 | + |
| 38 | +fastmcp_server = FastMCP('my_server') |
| 39 | +@fastmcp_server.tool() |
| 40 | +async def add(a: int, b: int) -> int: |
| 41 | + return a + b |
| 42 | + |
| 43 | +toolset = FastMCPToolset(fastmcp_server) |
| 44 | + |
| 45 | +agent = Agent('openai:gpt-5', toolsets=[toolset]) |
| 46 | + |
| 47 | +async def main(): |
| 48 | + result = await agent.run('What is 7 plus 5?') |
| 49 | + print(result.output) |
| 50 | + #> The answer is 12. |
| 51 | +``` |
| 52 | + |
| 53 | +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ |
| 54 | + |
| 55 | +Connecting your agent to a Streamable HTTP MCP Server is as simple as: |
| 56 | + |
| 57 | +```python |
| 58 | +from pydantic_ai import Agent |
| 59 | +from pydantic_ai.toolsets.fastmcp import FastMCPToolset |
| 60 | + |
| 61 | +toolset = FastMCPToolset('http://localhost:8000/mcp') |
| 62 | + |
| 63 | +agent = Agent('openai:gpt-5', toolsets=[toolset]) |
| 64 | +``` |
| 65 | + |
| 66 | +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ |
| 67 | + |
| 68 | +You can also create a `FastMCPToolset` from a JSON MCP Configuration: |
| 69 | + |
| 70 | +```python |
| 71 | +from pydantic_ai import Agent |
| 72 | +from pydantic_ai.toolsets.fastmcp import FastMCPToolset |
| 73 | + |
| 74 | +mcp_config = { |
| 75 | + 'mcpServers': { |
| 76 | + 'time_mcp_server': { |
| 77 | + 'command': 'uvx', |
| 78 | + 'args': ['mcp-run-python', 'stdio'] |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +toolset = FastMCPToolset(mcp_config) |
| 84 | + |
| 85 | +agent = Agent('openai:gpt-5', toolsets=[toolset]) |
| 86 | +``` |
| 87 | + |
| 88 | +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ |
0 commit comments