Overview
TheToolAgent extends LlmAgent with function calling capabilities. It allows you to provide Python functions as tools that the LLM can intelligently call to complete tasks.
ToolAgent handles:
- Converting Python functions to FastMCP tools
- Tool schema generation for the LLM
- Automatic tool execution based on LLM requests
- Tool result formatting and history management
- Parallel and sequential tool execution
Key Features
- Function Tools: Convert any Python function to an LLM-callable tool
- FastMCP Integration: Use FastMCP tools directly
- Agent-as-Tool: Expose other agents as tools
- Tool Hooks: Lifecycle hooks for tool execution
- Parallel Execution: Run multiple tools concurrently
- Tool Filtering: Control which tools are exposed
- Result Handling: Automatic formatting of tool outputs
Architecture
Creating a Tool Agent
Basic Example
With FastMCP Tools
Tool Function Requirements
Function Signature
Tools can be sync or async functions with type hints:Type Hints
Use type hints for automatic schema generation:Docstrings
Docstrings are used for tool descriptions shown to the LLM:Managing Tools
Adding Tools Dynamically
Listing Available Tools
Calling Tools Directly
Tool Execution Loop
TheToolRunner manages the tool execution loop:
- LLM generates response with tool calls
- Agent executes requested tools (parallel or sequential)
- Tool results are formatted and added to history
- LLM generates next response with tool results
- Repeat until LLM returns final answer
Execution Flow
Tool Hooks
Tool hooks allow you to intercept and modify tool execution:Hook Example: Logging
Agents as Tools
Expose other agents as tools for delegation:Parallel Tool Execution
Tools are executed in parallel when beneficial:Parallel Execution Configuration
Fromfast_agent/constants.py:
Tool Result Formatting
Tool results are automatically formatted:Error Handling
Advanced Patterns
Stateful Tools
Context-Aware Tools
Best Practices
Tool Design
Tool Design
- Keep tools focused on single responsibilities
- Use clear, descriptive names and docstrings
- Provide type hints for all parameters
- Handle errors gracefully with meaningful messages
- Make tools idempotent when possible
Error Handling
Error Handling
- Validate inputs and raise clear errors
- Return error messages the LLM can understand
- Log errors for debugging
- Consider retry logic for transient failures
Performance
Performance
- Design tools for parallel execution
- Avoid blocking operations in sync tools
- Use async tools for I/O operations
- Cache expensive computations
- Monitor tool execution time
Security
Security
- Validate and sanitize all inputs
- Limit tool capabilities appropriately
- Never expose destructive operations without safeguards
- Use tool hooks for authorization checks
Common Patterns
Data Analysis Agent
API Integration Agent
Next Steps
MCP Agent
Connect to MCP servers for more tools
FastMCP
Create advanced FastMCP tools
Tool Hooks
Deep dive into tool execution hooks
Examples
See complete tool agent examples
