Skip to content

Tool Registry

The tool registry provides a centralized way to register, store, and retrieve tools. It supports both individual tool registration and batch registration of multiple tools.

Features

  • Register individual tools or batches of tools
  • Retrieve tools by name
  • List all registered tools
  • Singleton pattern for global access

rllm.tools.registry

ToolRegistry

A registry for tools that handles registration, retrieval, and management.

Source code in rllm/tools/registry.py
class ToolRegistry:
    """
    A registry for tools that handles registration, retrieval, and management.
    """

    _instance = None
    _initialized = False

    def __new__(cls):
        """
        Implement singleton pattern to ensure there's only one registry instance.
        """
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self):
        """
        Initialize the registry if it hasn't been initialized yet.
        """
        if not ToolRegistry._initialized:
            self._tools: dict[str, type[Tool]] = {}
            ToolRegistry._initialized = True

    def register(self, name: str, tool_cls: type[Tool]) -> None:
        """
        Register a tool with the registry.

        Args:
            name: The name to register the tool under
            tool_cls: The tool class to register
        """
        if not issubclass(tool_cls, Tool):
            raise TypeError(f"Tool class must be a subclass of Tool, got {tool_cls}")

        self._tools[name] = tool_cls

    def register_all(self, tools_dict: dict[str, type[Tool]]) -> None:
        """
        Register multiple tools at once.

        Args:
            tools_dict: A dictionary mapping names to tool classes
        """
        for name, tool_cls in tools_dict.items():
            self.register(name, tool_cls)

    def get(self, name: str) -> type[Tool] | None:
        """
        Get a tool class by name.

        Args:
            name: The name of the tool to retrieve

        Returns:
            The tool class if found, None otherwise
        """
        return self._tools.get(name)

    def instantiate(self, name: str, *args, **kwargs) -> Tool | None:
        """
        Instantiate a tool by name.

        Args:
            name: The name of the tool to instantiate
            *args: Positional arguments to pass to the tool constructor
            **kwargs: Keyword arguments to pass to the tool constructor

        Returns:
            An instance of the tool if found, None otherwise
        """
        tool_cls = self.get(name)
        if tool_cls is None:
            return None
        return tool_cls(*args, **kwargs)

    def list_tools(self) -> list[str]:
        """
        List all registered tool names.

        Returns:
            A list of tool names
        """
        return list(self._tools.keys())

    def clear(self) -> None:
        """
        Clear all registered tools.
        """
        self._tools.clear()

    def unregister(self, name: str) -> bool:
        """
        Unregister a tool by name.

        Args:
            name: The name of the tool to unregister

        Returns:
            True if the tool was unregistered, False if it wasn't found
        """
        if name in self._tools:
            del self._tools[name]
            return True
        return False

    def __contains__(self, name: str) -> bool:
        """
        Check if a tool is registered.

        Args:
            name: The name of the tool to check

        Returns:
            True if the tool is registered, False otherwise
        """
        return name in self._tools

    def __getitem__(self, name: str) -> type[Tool]:
        """
        Get a tool class by name using dictionary-like syntax.

        Args:
            name: The name of the tool to retrieve

        Returns:
            The tool class

        Raises:
            KeyError: If the tool is not found
        """
        if name not in self._tools:
            raise KeyError(f"Tool '{name}' not found in registry")
        return self._tools[name]

    def __setitem__(self, name: str, tool_cls: type[Tool]) -> None:
        """
        Register a tool using dictionary-like syntax.

        Args:
            name: The name to register the tool under
            tool_cls: The tool class to register
        """
        self.register(name, tool_cls)

    def __iter__(self):
        """
        Iterate over tool names.

        Returns:
            An iterator over tool names
        """
        return iter(self._tools)

    def __len__(self) -> int:
        """
        Get the number of registered tools.

        Returns:
            The number of registered tools
        """
        return len(self._tools)

    def to_dict(self) -> dict[str, type[Tool]]:
        """
        Convert the registry to a dictionary.

        Returns:
            A dictionary mapping tool names to tool classes
        """
        return self._tools

__new__

__new__()

Implement singleton pattern to ensure there's only one registry instance.

Source code in rllm/tools/registry.py
def __new__(cls):
    """
    Implement singleton pattern to ensure there's only one registry instance.
    """
    if cls._instance is None:
        cls._instance = super().__new__(cls)
    return cls._instance

__init__

__init__()

Initialize the registry if it hasn't been initialized yet.

Source code in rllm/tools/registry.py
def __init__(self):
    """
    Initialize the registry if it hasn't been initialized yet.
    """
    if not ToolRegistry._initialized:
        self._tools: dict[str, type[Tool]] = {}
        ToolRegistry._initialized = True

register

register(name: str, tool_cls: type[Tool]) -> None

Register a tool with the registry.

Parameters:

Name Type Description Default
name str

The name to register the tool under

required
tool_cls type[Tool]

The tool class to register

required
Source code in rllm/tools/registry.py
def register(self, name: str, tool_cls: type[Tool]) -> None:
    """
    Register a tool with the registry.

    Args:
        name: The name to register the tool under
        tool_cls: The tool class to register
    """
    if not issubclass(tool_cls, Tool):
        raise TypeError(f"Tool class must be a subclass of Tool, got {tool_cls}")

    self._tools[name] = tool_cls

register_all

register_all(tools_dict: dict[str, type[Tool]]) -> None

Register multiple tools at once.

Parameters:

Name Type Description Default
tools_dict dict[str, type[Tool]]

A dictionary mapping names to tool classes

required
Source code in rllm/tools/registry.py
def register_all(self, tools_dict: dict[str, type[Tool]]) -> None:
    """
    Register multiple tools at once.

    Args:
        tools_dict: A dictionary mapping names to tool classes
    """
    for name, tool_cls in tools_dict.items():
        self.register(name, tool_cls)

get

get(name: str) -> type[Tool] | None

Get a tool class by name.

Parameters:

Name Type Description Default
name str

The name of the tool to retrieve

required

Returns:

Type Description
type[Tool] | None

The tool class if found, None otherwise

Source code in rllm/tools/registry.py
def get(self, name: str) -> type[Tool] | None:
    """
    Get a tool class by name.

    Args:
        name: The name of the tool to retrieve

    Returns:
        The tool class if found, None otherwise
    """
    return self._tools.get(name)

instantiate

instantiate(name: str, *args, **kwargs) -> Tool | None

Instantiate a tool by name.

Parameters:

Name Type Description Default
name str

The name of the tool to instantiate

required
*args

Positional arguments to pass to the tool constructor

()
**kwargs

Keyword arguments to pass to the tool constructor

{}

Returns:

Type Description
Tool | None

An instance of the tool if found, None otherwise

Source code in rllm/tools/registry.py
def instantiate(self, name: str, *args, **kwargs) -> Tool | None:
    """
    Instantiate a tool by name.

    Args:
        name: The name of the tool to instantiate
        *args: Positional arguments to pass to the tool constructor
        **kwargs: Keyword arguments to pass to the tool constructor

    Returns:
        An instance of the tool if found, None otherwise
    """
    tool_cls = self.get(name)
    if tool_cls is None:
        return None
    return tool_cls(*args, **kwargs)

list_tools

list_tools() -> list[str]

List all registered tool names.

Returns:

Type Description
list[str]

A list of tool names

Source code in rllm/tools/registry.py
def list_tools(self) -> list[str]:
    """
    List all registered tool names.

    Returns:
        A list of tool names
    """
    return list(self._tools.keys())

clear

clear() -> None

Clear all registered tools.

Source code in rllm/tools/registry.py
def clear(self) -> None:
    """
    Clear all registered tools.
    """
    self._tools.clear()

unregister

unregister(name: str) -> bool

Unregister a tool by name.

Parameters:

Name Type Description Default
name str

The name of the tool to unregister

required

Returns:

Type Description
bool

True if the tool was unregistered, False if it wasn't found

Source code in rllm/tools/registry.py
def unregister(self, name: str) -> bool:
    """
    Unregister a tool by name.

    Args:
        name: The name of the tool to unregister

    Returns:
        True if the tool was unregistered, False if it wasn't found
    """
    if name in self._tools:
        del self._tools[name]
        return True
    return False

__contains__

__contains__(name: str) -> bool

Check if a tool is registered.

Parameters:

Name Type Description Default
name str

The name of the tool to check

required

Returns:

Type Description
bool

True if the tool is registered, False otherwise

Source code in rllm/tools/registry.py
def __contains__(self, name: str) -> bool:
    """
    Check if a tool is registered.

    Args:
        name: The name of the tool to check

    Returns:
        True if the tool is registered, False otherwise
    """
    return name in self._tools

__getitem__

__getitem__(name: str) -> type[Tool]

Get a tool class by name using dictionary-like syntax.

Parameters:

Name Type Description Default
name str

The name of the tool to retrieve

required

Returns:

Type Description
type[Tool]

The tool class

Raises:

Type Description
KeyError

If the tool is not found

Source code in rllm/tools/registry.py
def __getitem__(self, name: str) -> type[Tool]:
    """
    Get a tool class by name using dictionary-like syntax.

    Args:
        name: The name of the tool to retrieve

    Returns:
        The tool class

    Raises:
        KeyError: If the tool is not found
    """
    if name not in self._tools:
        raise KeyError(f"Tool '{name}' not found in registry")
    return self._tools[name]

__setitem__

__setitem__(name: str, tool_cls: type[Tool]) -> None

Register a tool using dictionary-like syntax.

Parameters:

Name Type Description Default
name str

The name to register the tool under

required
tool_cls type[Tool]

The tool class to register

required
Source code in rllm/tools/registry.py
def __setitem__(self, name: str, tool_cls: type[Tool]) -> None:
    """
    Register a tool using dictionary-like syntax.

    Args:
        name: The name to register the tool under
        tool_cls: The tool class to register
    """
    self.register(name, tool_cls)

__iter__

__iter__()

Iterate over tool names.

Returns:

Type Description

An iterator over tool names

Source code in rllm/tools/registry.py
def __iter__(self):
    """
    Iterate over tool names.

    Returns:
        An iterator over tool names
    """
    return iter(self._tools)

__len__

__len__() -> int

Get the number of registered tools.

Returns:

Type Description
int

The number of registered tools

Source code in rllm/tools/registry.py
def __len__(self) -> int:
    """
    Get the number of registered tools.

    Returns:
        The number of registered tools
    """
    return len(self._tools)

to_dict

to_dict() -> dict[str, type[Tool]]

Convert the registry to a dictionary.

Returns:

Type Description
dict[str, type[Tool]]

A dictionary mapping tool names to tool classes

Source code in rllm/tools/registry.py
def to_dict(self) -> dict[str, type[Tool]]:
    """
    Convert the registry to a dictionary.

    Returns:
        A dictionary mapping tool names to tool classes
    """
    return self._tools