Overview
The Swarms tool system provides:- Automatic Schema Generation: Convert Python functions to OpenAI-compatible tool schemas
- Type Safety: Full type hint support with validation
- Tool Registry: Centralized management of available tools
- Pydantic Integration: Use Pydantic models for structured tool inputs
- Batch Execution: Execute multiple tools concurrently
- Error Handling: Comprehensive error handling and validation
Quick Start
Basic Tool Usage
Add a simple function as a tool:Tools must have:
- Type hints for every parameter without a default. A missing one raises
TypeErrorwhen the agent is built. - A docstring describing the function. A missing one raises
ValueError. The whole docstring becomes the tool description.
ToolManager (agent.tool_manager). It builds the schemas, runs each call, retries failures up to tool_retry_attempts times, and records the results.
BaseTool Class
Core Tool Management
TheBaseTool class provides comprehensive tool management:
BaseTool is the standalone converter and executor; every agent keeps one as agent.tool_struct. It is not the same class as the agent’s ToolManager, which drives the whole tool cycle and uses BaseTool to run local calls.Multiple Tools
Manage multiple tools with validation:Pydantic Model Tools
Structured Tool Inputs
Use Pydantic models for complex tool schemas:Agent(tools_list_dictionary=[user_schema, search_schema]). A field without a Field(description=...) takes its description from the model docstring’s Args: section, with wrapped lines joined.
Multiple Pydantic Models
Tool Registry
Centralized Tool Management
Use theToolStorage registry for managing tools:
Batch Tool Registration
Tool Execution
Execute Tools Dynamically
Execute by Name
execute_tool_by_name returns a JSON string with results and summary keys.
Advanced Patterns
Tool Validation
Validate tools before execution:Dynamic Tool Generation
Generated functions need their own name and docstring. An f-string in the docstring position is not a docstring, so set__doc__ and __name__ explicitly:
Tool Composition
Combine multiple tools into workflows:Best Practices
Documentation
Always include comprehensive docstrings with parameter descriptions
Type Hints
Use type hints for all function parameters and return values
Error Handling
Implement robust error handling in your tools
Validation
Validate inputs using Pydantic models for complex schemas
Common Patterns
API Integration Tool
Database Tool
File Processing Tool
Troubleshooting
Common Issues
Missing Type HintsNext Steps
ToolManager
How an agent sets up, runs, and retries its tools
MCP Integration
Connect to MCP servers for more tools
Marketplace
Share and discover prompts on the marketplace