Basic Tool Usage
Define a Simple Tool
Tools are Python functions with type hints and docstrings:Tool Requirements
Type Hints and Docstrings
For reliable tool execution, functions must have:- Type hints for all parameters and return value
- Docstring describing the function’s purpose
- Parameter descriptions in the docstring
Multiple Tools
Add Multiple Tools to an Agent
Real-World Tool Examples
Web Search Tool
Database Query Tool
File Operations Tool
Tool Configuration
Tool Schema Control
List[Callable]
default:"None"
List of tool functions to make available to the agent.
tool_choice is not an Agent constructor parameter. Whenever tools (or MCP tools) are configured, the agent always sends tool_choice="auto" to the underlying LLM call — passing tool_choice=... to Agent(...) is silently absorbed into **kwargs and has no effect.bool
default:"True"
Display tool execution results.
int
default:"3"
Number of retry attempts for failed tool executions.
Advanced Tool Patterns
Tool with State
Async Tools
BaseTool API
Swarms uses theBaseTool class internally to manage tools:
MCP Tools
Model Context Protocol (MCP) enables connecting to external tool servers:mcp_url:
Best Practices
1. Always Include Type Hints
2. Write Descriptive Docstrings
3. Handle Errors Gracefully
4. Use Specific Parameter Names
5. Return Structured Data When Possible
Next Steps
Agent Skills
Learn about the Agent Skills system
Structured Outputs
Get structured responses from agents
Reference
- Tool handling:
swarms/structs/agent.py:954-1038(tool_handling) - BaseTool class:
swarms/tools/base_tool.py:69 - Function schema generation:
swarms/tools/py_func_to_openai_func_str.py