Base Tool¶
The BaseTool class serves as the foundation for all tool implementations in the Bedrock Swarm framework. It defines the core interface and shared functionality that all tools must implement.
Class Documentation¶
bedrock_swarm.tools.base.BaseTool(name: str, description: str)
¶
Bases: ABC
Base class for tools that can be used by agents.
All tools must implement: - name: Tool name - description: Tool description - get_schema: Method to get JSON schema - execute: Method to execute the tool
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Tool name
TYPE:
|
description
|
Tool description
TYPE:
|
Source code in src/bedrock_swarm/tools/base.py
Attributes¶
name: str
abstractmethod
property
¶
Get tool name.
description: str
abstractmethod
property
¶
Get tool description.
Functions¶
get_schema() -> Dict[str, Any]
abstractmethod
¶
execute(**kwargs: Any) -> str
¶
Execute the tool with given parameters.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Tool parameters
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Tool execution result
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ToolError
|
If tool execution fails |
Source code in src/bedrock_swarm/tools/base.py
_execute_impl(**kwargs: Any) -> str
abstractmethod
¶
Execute the tool implementation.
This method should be implemented by subclasses to provide the actual tool functionality. The base class handles parameter validation.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Tool parameters
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Tool execution result
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ToolError
|
If tool execution fails |
Source code in src/bedrock_swarm/tools/base.py
Usage Example¶
from bedrock_swarm.tools.base import BaseTool
class CustomTool(BaseTool):
def __init__(self):
super().__init__(
name="custom_tool",
description="A custom tool implementation"
)
def _execute_impl(self, **kwargs):
# Custom implementation
pass
Core Features¶
The base tool provides:
- Name and Description: Tool identification
- Input Validation: Common validation methods
- Error Handling: Standardized error handling
- Documentation: Built-in documentation support
- Type Safety: TypeScript interface definitions
Common Methods¶
All tool implementations inherit these methods:
invoke: Abstract method for tool executionvalidate_input: Input validation methodget_description: Returns tool descriptionget_name: Returns tool name
Error Handling¶
The base tool includes error handling for:
- Invalid inputs
- Missing parameters
- Type mismatches
- Resource errors
Implementation Guidelines¶
When implementing a new tool:
- Inherit from
BaseTool - Implement required abstract methods
- Add comprehensive input validation
- Include error handling
- Document all parameters and return values