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.
str
default:"auto"
Control when tools are used. Options: “auto”, “required”, “none”.
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:

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:972-1059
  • BaseTool class: swarms/tools/base_tool.py:69
  • Function schema generation: swarms/tools/py_func_to_openai_func_str.py