What is Pydantic AI?
Pydantic AI is a production-grade Python agent framework from the creators of Pydantic. It emphasises type safety, testability, and structured outputs. Its MCP client is built directly into the library — no extra packages needed. This makes it the cleanest way to use MCP tools in Python agents.
Prerequisites
- Python 3.9+
- Node.js 18+
- Agent MCP Studio at agentmcp.studio
Step-by-Step Setup
Install Pydantic AI
pip install pydantic-ai
Start bridge.js
curl -O https://agentmcp.studio/bridge.js && npm install ws node bridge.js wss://agentmcp.studio/api/relay/YOUR-UUID
Open the studio → Settings → MCP Relay Bridge → click Connect.
-
Use MCP tools in your agent
import asyncio from pydantic_ai import Agent from pydantic_ai.mcp import MCPServerStdio # Define the MCP server connection mcp_server = MCPServerStdio( command="node", args=[ "/path/to/bridge.js", "wss://agentmcp.studio/api/relay/YOUR-UUID" ] ) # Create an agent with MCP tools agent = Agent( "claude-3-5-sonnet-20241022", mcp_servers=[mcp_server] ) async def main(): async with agent.run_mcp_servers(): result = await agent.run( "Use the fetch_webpage tool to get the content of example.com " "and summarise it in 3 bullet points." ) print(result.data) asyncio.run(main())Pydantic AI's
run_mcp_servers()context manager starts bridge.js, connects to the relay, and makes all your browser tools available to the agent automatically.
Structured Outputs + MCP Tools
Pydantic AI's biggest advantage is type-safe structured outputs. Combine this with your browser MCP tools for powerful data extraction pipelines:
from pydantic import BaseModel
class SalesReport(BaseModel):
top_products: list[str]
total_revenue: float
date_range: str
agent = Agent(
"claude-3-5-sonnet-20241022",
result_type=SalesReport,
mcp_servers=[mcp_server]
)
async with agent.run_mcp_servers():
result = await agent.run(
"Query the sales database and return a structured report"
)
report: SalesReport = result.data # Fully typed!
Frequently Asked Questions
Yes — PydanticAI has built-in MCP support via MCPServerStdio and MCPServerHTTP. Use MCPServerStdio("node", ["bridge.js", "wss://..."]) and PydanticAI automatically discovers and type-validates Agent MCP Studio tools.
PydanticAI uses Pydantic's type validation for all tool inputs and outputs — including MCP tool arguments. This means your Agent MCP Studio tools get automatic input validation and clear error messages when wrong data types are passed.
PydanticAI reads JSON Schema from MCP tool definitions and automatically creates Pydantic models for validation. Your Agent MCP Studio tools' input schemas become Pydantic-validated at call time — no extra code needed.
PydanticAI supports OpenAI, Anthropic, Google Gemini, Groq, Ollama, and more via a unified model interface. MCP tools work with any model that has function calling support — Claude and GPT-4o are recommended.
Yes — PydanticAI is async-first. All MCP tool calls are awaitable. Use async with MCPServerStdio(...) as mcp: agent = Agent(model, mcp_servers=[mcp]) and run agents with await agent.run() in any async context.