What this thread does
Do first: install pydantic-ai or pip install "pydantic-ai-slim[mcp]", then register MCPToolset in toolsets.
Short answer
MCPToolset wraps the FastMCP client. An agent context opens and closes registered toolsets; a toolset context can share a connection across agents.
What to do
Inside an async function, with the HTTP server already listening:
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPToolset
agent = Agent('openai:gpt-6.1-sol', toolsets=[MCPToolset('http://localhost:8000/mcp')])
async with agent:
result = await agent.run('What is 7 plus 5?')
| Input / setting | Action |
|---|---|
| URL string | Streamable HTTP recommended; example http://localhost:8000/mcp |
URL ending /sse | SSE auto-detected; deprecated transport; other SSE paths require SSETransport |
| Python / Node.js script path | Runs through stdio |
| Explicit transport | StdioTransport, StreamableHttpTransport, or SSETransport |
| Local command example | StdioTransport(command='python', args=['mcp_server.py']) from fastmcp.client.transports |
| Prebuilt client / server | Accepts fastmcp.Client or an in-process FastMCP server |
async with agent | Manages all registered toolsets and stdio subprocess lifetime |
async with toolset | Manages a particular toolset; reusable across agents |
| Neither context | Opens and closes automatically as needed |
| Alternative install | uv add "pydantic-ai-slim[mcp]" |
Limits and open questions
The install selects FastMCP 4; FastMCP 3 is also supported. Version 4 modern mode lacks server-initiated sampling, elicitation, and log_level; use legacy mode for them, or log_handler to filter logs. Remote HTTP servers must be started separately. No fixed connection timeout is stated here.
Not for: PydanticAI provider-native MCP execution through the MCP capability.
Sources
- MCP Client, Pydantic.
- MCP Client canonical destination, Pydantic.
Checked
2026-11-17; read MCP Client; no command was run.
Started from a post by @Pydantic, packaged by the editor. Original post