Function tools
The simplest way to add tools is using Python functions:The function docstring and type hints are used to generate the tool schema for the LLM.
Multiple tools
Async tools
Tools can be async functions:MCP servers
Model Context Protocol (MCP) servers provide reusable tool integrations:Code execution
UseCodeExecutorAgent to execute code safely:
AgentTool
Wrap an agent as a tool for another agent:Custom tool classes
For more control, create a custom tool class:Tool execution control
Max iterations
Control how many tool-calling rounds the agent can make:Reflect on tool use
Have the agent reflect on tool results before responding:Tool security
Use Docker for code execution
Use Docker for code execution
Always use
DockerCommandLineCodeExecutor for untrusted code execution to provide isolation.Validate inputs
Validate inputs
Use Pydantic models to validate and sanitize tool inputs.
Limit tool capabilities
Limit tool capabilities
Give agents only the tools they need. Don’t provide file system access unless required.
Set timeouts
Set timeouts
Use timeouts for tools that make network requests or long-running operations.
Best practices
- Clear descriptions - Write detailed docstrings. The LLM uses them to understand when to call the tool.
- Type hints - Always use type hints. They’re used to generate the tool schema.
- Error handling - Handle errors gracefully in your tools:
- Structured outputs - For complex data, return structured formats (JSON, Pydantic models).
Next steps
MCP Servers
Deep dive into MCP server integration
Tool Integration Guide
Learn advanced tool patterns
Code Execution
See code execution in action
Web Browsing
Web browsing with MCP servers