Overview
Theswarms.tools module provides a comprehensive toolkit for function calling, schema conversion, and tool management. It enables seamless integration with OpenAI-style function calling, MCP (Model Context Protocol) tools, and Pydantic-based schema validation.
BaseTool
A comprehensive tool management system for function calling, schema conversion, and execution.Constructor
bool
default:"None"
Enable detailed logging output
List[type[BaseModel]]
default:"None"
List of Pydantic models to manage
bool
default:"None"
Enable automatic validation checks
bool
default:"None"
Enable automatic tool execution
List[Callable]
default:"None"
List of callable functions to manage
str
default:"None"
System prompt for tool operations
Dict[str, Callable]
default:"None"
Mapping of function names to callables
Methods
func_to_dict
Convert a callable function to OpenAI function calling schema dictionary.Callable
required
The function to convert
Dict[str, Any]
OpenAI function calling schema dictionary
FunctionSchemaError: If function schema conversion failsToolValidationError: If function validation fails
base_model_to_dict
Convert a Pydantic BaseModel to OpenAI function calling schema.type[BaseModel]
required
The Pydantic model class to convert
bool
default:"False"
Whether to return string output format
Union[dict[str, Any], str]
OpenAI function calling schema dictionary or JSON string
execute_tool
Execute a tool based on a response string.str
required
JSON response string containing tool execution details
Callable
Result of the tool execution
ToolValidationError: If response validation failsToolExecutionError: If tool execution failsToolNotFoundError: If specified tool is not found
convert_funcs_into_tools
Convert all functions in the tools list into OpenAI function calling format.ToolValidationError: If tools are not properly configuredToolDocumentationError: If functions lack required documentationToolTypeHintError: If functions lack required type hints
execute_tool_by_name
Search for a tool by name and execute it with the provided response.str
required
The name of the tool to execute
str
required
JSON response string containing execution parameters
Any
The result of executing the tool
Tool Decorator
A decorator function that generates an OpenAI function schema from a Python function.str
default:"None"
The name of the OpenAI function
str
default:"None"
The description of the OpenAI function
bool
default:"True"
Whether to return the schema as a dictionary
bool
default:"True"
Enable verbose logging
bool
default:"False"
Whether to return the schema as a string
bool
default:"False"
Whether to return the schema as YAML
Utility Functions
get_openai_function_schema_from_func
Convert a Python function to OpenAI function calling schema.base_model_to_openai_function
Convert a Pydantic BaseModel to OpenAI function schema.scrape_tool_func_docs
Extract documentation from a tool function.tool_find_by_name
Find a tool by name in a list of tools.MCP Tools Integration
Functions for integrating with Model Context Protocol (MCP) servers.get_mcp_tools_sync
Synchronously retrieve tools from an MCP server.aget_mcp_tools
Asynchronously retrieve tools from an MCP server.execute_tool_call_simple
Execute a simple tool call on an MCP server.get_tools_for_multiple_mcp_servers
Retrieve tools from multiple MCP servers.Exceptions
The tools module defines several custom exceptions:BaseToolError
Base exception class for all BaseTool related errors.ToolValidationError
Raised when tool validation fails.ToolExecutionError
Raised when tool execution fails.ToolNotFoundError
Raised when a requested tool is not found.FunctionSchemaError
Raised when function schema conversion fails.ToolDocumentationError
Raised when tool documentation is missing or invalid.ToolTypeHintError
Raised when tool type hints are missing or invalid.Best Practices
- Always add type hints: Functions must have type hints for reliable schema generation
- Include docstrings: Comprehensive docstrings improve tool descriptions
- Validate inputs: Use Pydantic models for complex input validation
- Handle errors: Wrap tool execution in try-catch blocks
- Use caching: BaseTool caches expensive operations for performance
- Enable verbose mode: During development, enable verbose logging to debug issues