.agents/skills/building-pydantic-ai-agents/references/TOOLS-CORE.md
Read this file when the user wants to add function tools, organize toolsets, connect MCP servers, or use explicit common search tools.
Use @agent.tool_plain for pure functions and @agent.tool for tools that need RunContext.
import random
from pydantic_ai import Agent, RunContext
agent = Agent('google-gla:gemini-3-flash-preview', deps_type=str)
@agent.tool_plain
def roll_dice() -> str:
return str(random.randint(1, 6))
@agent.tool
def get_player_name(ctx: RunContext[str]) -> str:
return ctx.deps
Use Tool(fn) when tools are defined outside the agent file or shared between agents.
Default choices:
@agent.tool when the tool needs deps, usage, retry count, or message history@agent.tool_plain when the tool is a plain functionTool(...) in tools=[...] when the tool should be reusable across agentsFunctionToolset when multiple related tools should be managed as a groupUse toolsets when the user has multiple related tools or wants cross-cutting behavior applied to a group.
Examples:
FunctionToolset for a bundle of Python toolsRoute this to @agent.tool with RunContext.
Useful RunContext fields include:
ctx.depsctx.usagectx.messagesctx.retryAttach an MCP server as a toolset on the agent.
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio
server = MCPServerStdio('python', args=['mcp_server.py'], timeout=10)
agent = Agent('openai:gpt-5.2', toolsets=[server])
async def main():
async with agent:
result = await agent.run('What is the weather in Paris?')
print(result.output)
Default transport choices:
MCPServerStdio for local subprocess serversMCPServerStreamableHTTP for HTTP serversMCPServerSSE still exists, but Streamable HTTP is the better default.
Use common tools when the user wants explicit search tools rather than provider-adaptive capabilities.
from pydantic_ai import Agent
from pydantic_ai.common_tools.duckduckgo import duckduckgo_search_tool
agent = Agent(
'openai:gpt-5.2',
tools=[duckduckgo_search_tool()],
instructions='Search DuckDuckGo for the given query and return the results.',
)
Good default split:
WebSearch() capability when the user wants model-agnostic search with builtin fallbackduckduckgo_search_tool() / Tavily / Exa when the user explicitly wants those engines as tools