Skip to main content
Tools extend agent capabilities by allowing them to call external functions, APIs, and services. Swarms supports OpenAI-style function calling with automatic schema generation.

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:
  1. Type hints for all parameters and return value
  2. Docstring describing the function’s purpose
  3. 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 the BaseTool class internally to manage tools:

MCP Tools

Model Context Protocol (MCP) enables connecting to external tool servers:
Simpler alternative for a single server — pass the URL directly with 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