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. Each agent runs its tools through a ToolManager, available as agent.tool_manager. It builds the schemas, executes each call, retries failures, and prints tool activity.

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 every parameter without a default. A missing one raises TypeError when the agent is built. A missing return annotation only logs a warning.
  2. A docstring describing the function’s purpose. A missing one raises ValueError when the agent is built.
  3. Parameter descriptions the model can read. The whole docstring, Args: section included, becomes the tool description.

How the schema is built

The agent converts each callable to an OpenAI function schema when it is constructed: To give a single parameter its own description in the schema, use Annotated:

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:"False"
Defer tool schemas behind a searchable tool_search tool instead of sending every schema on every request. Off by default. When on, deferral activates if the agent has tools, an MCP connection, or max_loops="auto".
See Dynamic Tool Loading for how deferral, search ranking, and turn budgeting work.
With deferral active, a tool costs one extra round trip: the model must call tool_search before it can call the tool. With max_loops=1 it can search but never call what it found. Raise max_loops or leave dynamic_tools off.
bool
default:"True"
When True, print a Tool Execution Results panel with the time, each tool’s name and call id, and the full output. When False, print a short panel with just the tool names and the time. Nothing is printed when print_on=False.
int
default:"3"
How many times the batch of tool calls from one reply runs before the agent gives up. Each attempt runs the whole batch once, so calls that succeeded run again with it.
bool
default:"True"
After each tool batch, make one extra model call to summarize the output and add the summary to the conversation. Set to False to skip the extra call. A plain-text reply that calls no tool is never summarized.
When every attempt fails, the agent does not retry the model call. It records Tool execution failed after N attempts: ... in the conversation and moves to the next loop, so the model can read the error and try something else. Provider retries (retry_attempts) are not used up by a broken tool. See ToolManager for the details.

Adding tools after construction

add_tool, add_tools, remove_tool, and remove_tools change only agent.tools. The schemas, the executor, and the LLM are built once, at construction, so pass every tool to the constructor when you can. To rebuild after a change, see ToolManager.

Advanced Tool Patterns

Tool with State

Async Tools

BaseTool API

Swarms uses the BaseTool class internally to convert and run tools. Every agent keeps one as agent.tool_struct, and its ToolManager calls it for each batch of local tool calls. You can also use it on its own:

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:
In an agent with an integer max_loops, keep MCP servers and local tools on separate agents. When tools is set, every tool call in a reply goes to the local executor first, and an MCP tool name makes that batch fail. With max_loops="auto", MCP calls are routed to the MCP manager separately. See ToolManager.

Sub-agents in autonomous mode

With max_loops="auto", an agent can create sub-agents with the create_sub_agent tool. Each sub-agent gets the parent’s model_name, its tools list, and its print_on and verbose settings. A sub-agent with tools runs up to 5 loops, so it can call a tool and read the result. A sub-agent without tools runs 1 loop. MCP servers and handoffs are not passed down. See Autonomous mode.

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

ToolManager

How the agent sets up, runs, and retries its tools

Dynamic Tool Loading

Defer tool schemas behind a searchable catalog

Agent Skills

Learn about the Agent Skills system

Structured Outputs

Get structured responses from agents

Reference

  • Tool setup, execution, and retries: swarms/agents/tool_manager.py (ToolManager)
  • BaseTool class: swarms/tools/base_tool.py
  • Function schema generation: swarms/tools/py_func_to_openai_func_str.py