Skip to main content
Swarms provides a comprehensive tool system that allows agents to execute custom functions, interact with APIs, and perform specialized tasks. The framework supports multiple tool integration patterns with automatic schema generation and validation.

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:
  1. Type hints for every parameter without a default. A missing one raises TypeError when the agent is built.
  2. A docstring describing the function. A missing one raises ValueError. The whole docstring becomes the tool description.
A return type annotation is recommended; without one, a warning is logged.
Inside the agent, tool calls are run by 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

The BaseTool 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:
Each schema is in OpenAI tool format, so you can pass it to an agent with 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 the ToolStorage 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 Hints
Missing Documentation

Next 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