Skip to main content

What are Tools?

Tools are external functions and capabilities that extend what agents can do beyond text generation. While language models excel at reasoning and language tasks, tools enable agents to:
  • Access external data (APIs, databases, web search)
  • Perform computations (calculations, data analysis)
  • Take actions (send emails, create files, run code)
  • Interact with systems (databases, cloud services, IoT devices)
Think of tools as the “hands” of your agent - they transform language understanding into real-world actions.

Why Tools Matter

Language models alone are limited to generating text based on their training data. Tools unlock:

Current Information

Access real-time data via web search, APIs, and databases

Reliable Computation

Perform accurate calculations and data processing

External Actions

Interact with external systems and services

Domain Expertise

Integrate specialized knowledge and capabilities

How Tools Work

The tool execution lifecycle in Swarms:

Step-by-Step Process

  1. Schema Generation: Tools are converted to OpenAI function calling schema
  2. Tool Discovery: LLM sees available tools and their descriptions
  3. Tool Selection: LLM decides which tool(s) to use based on the task
  4. Parameter Extraction: LLM generates parameters for the tool
  5. Execution: Agent executes the tool with provided parameters
  6. Result Integration: Tool output is added to conversation context
  7. Response Generation: LLM uses tool results to generate final response

Creating Tools

Basic Python Functions

The simplest way to create a tool is with a Python function:
Requirements for Tool Functions:
  1. Type hints: All parameters and return values must have type annotations
  2. Docstrings: Clear documentation explaining what the tool does
  3. Parameter descriptions: Document each parameter in the docstring
Without these, the LLM cannot reliably use your tools!

Tool Best Practices

Tool Integration Patterns

Pattern 1: Direct Function Tools

Simple Python functions passed directly to the agent:

Pattern 2: BaseTool Class

For advanced tool management and validation, use the BaseTool class from swarms/tools/base_tool.py:
The BaseTool class provides:
  • Automatic schema conversion: Convert Python functions to OpenAI format
  • Validation: Check for documentation and type hints
  • Execution management: Parse LLM responses and execute appropriate tools
  • Error handling: Graceful error handling and logging
  • Caching: Performance optimization for repeated operations

Pattern 3: Pydantic Models as Tools

Use Pydantic models for structured data:

Pattern 4: MCP (Model Context Protocol)

MCP provides standardized tool integration via external servers:
MCP Benefits:
  • Standardized protocol for tool integration
  • Dynamic tool discovery
  • Multiple MCP server support
  • Built-in error handling

Pattern 5: Tools from External Libraries

Integrate tools from other libraries like LangChain:

Common Tool Examples

Web Search Tool

Database Query Tool

File Operations Tool

API Integration Tool

Code Execution Tool

Multi-Tool Agents

Agents can use multiple tools in combination:

Tool Execution Control

Control how tools are executed:

BaseTool API Reference

Key methods from swarms/tools/base_tool.py:

Schema Conversion

Tool Execution

Validation

Best Practices

Use descriptive parameter names and comprehensive type hints:
Provide clear descriptions that help the LLM understand when and how to use the tool:
Tools should handle errors gracefully and return informative messages:
  • Validate and sanitize all inputs
  • Limit file system access
  • Use sandboxing for code execution
  • Implement rate limiting for API calls
  • Never expose sensitive credentials in tool descriptions
  • Cache frequently used results
  • Use async operations when possible
  • Set appropriate timeouts
  • Limit output size for large responses

Advanced: Custom Tool Protocols

Implement custom tool loading and execution:

Next Steps

Agents

Learn how agents use tools for task execution

MCP Integration

Integrate standardized MCP tool servers

Tool Examples

Explore real-world tool implementations

BaseTool Reference

Complete API reference for BaseTool class