Skip to content

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: str

description

Tool description

TYPE: str

Source code in src/bedrock_swarm/tools/base.py
def __init__(self, name: str, description: str) -> None:
    """Initialize the tool and validate schema.

    Args:
        name (str): Tool name
        description (str): Tool description
    """
    self._name = name
    self._description = description
    schema = self.get_schema()
    validate_tool_schema(self.name, schema)

Attributes

name: str abstractmethod property

Get tool name.

description: str abstractmethod property

Get tool description.

Functions

get_schema() -> Dict[str, Any] abstractmethod

Get JSON schema for the tool.

RETURNS DESCRIPTION
Dict[str, Any]

Dict[str, Any]: Tool schema

Source code in src/bedrock_swarm/tools/base.py
@abstractmethod
def get_schema(self) -> Dict[str, Any]:
    """Get JSON schema for the tool.

    Returns:
        Dict[str, Any]: Tool schema
    """
    pass
execute(**kwargs: Any) -> str

Execute the tool with given parameters.

PARAMETER DESCRIPTION
**kwargs

Tool parameters

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
str

Tool execution result

TYPE: str

RAISES DESCRIPTION
ToolError

If tool execution fails

Source code in src/bedrock_swarm/tools/base.py
def execute(self, **kwargs: Any) -> str:
    """Execute the tool with given parameters.

    Args:
        **kwargs: Tool parameters

    Returns:
        str: Tool execution result

    Raises:
        ToolError: If tool execution fails
    """
    try:
        validate_tool_parameters(self.get_schema(), **kwargs)
        return self._execute_impl(**kwargs)
    except Exception as e:
        if isinstance(e, ToolError):
            raise
        raise ToolError(str(e))
_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: Any DEFAULT: {}

RETURNS DESCRIPTION
str

Tool execution result

TYPE: str

RAISES DESCRIPTION
ToolError

If tool execution fails

Source code in src/bedrock_swarm/tools/base.py
@abstractmethod
def _execute_impl(self, **kwargs: Any) -> str:
    """Execute the tool implementation.

    This method should be implemented by subclasses to provide the actual
    tool functionality. The base class handles parameter validation.

    Args:
        **kwargs: Tool parameters

    Returns:
        str: Tool execution result

    Raises:
        ToolError: If tool execution fails
    """
    pass

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:

  1. Name and Description: Tool identification
  2. Input Validation: Common validation methods
  3. Error Handling: Standardized error handling
  4. Documentation: Built-in documentation support
  5. Type Safety: TypeScript interface definitions

Common Methods

All tool implementations inherit these methods:

  • invoke: Abstract method for tool execution
  • validate_input: Input validation method
  • get_description: Returns tool description
  • get_name: Returns tool name

Error Handling

The base tool includes error handling for:

  1. Invalid inputs
  2. Missing parameters
  3. Type mismatches
  4. Resource errors

Implementation Guidelines

When implementing a new tool:

  1. Inherit from BaseTool
  2. Implement required abstract methods
  3. Add comprehensive input validation
  4. Include error handling
  5. Document all parameters and return values