Native Integration¶
This section documents the native Python class integration capabilities of the ToolRegistry library.
Architecture Overview¶
The Native integration enables direct registration of Python class methods as tools within the ToolRegistry framework. This integration provides a seamless way to convert existing Python classes into callable tools:
Core Components¶
-
ClassToolIntegration: The main integration class that handles class method registration
-
Automatically detects static vs instance methods
- Handles class instantiation when needed
-
Manages namespace assignment for organized tool hierarchy
-
Method Registration Logic: Intelligent registration that adapts to different class patterns
- Static methods are registered directly from the class
- Instance methods trigger automatic class instantiation
- Mixed method types are handled with appropriate error messages
Design Philosophy¶
- Zero Configuration: Minimal setup required to register Python classes
- Intelligent Detection: Automatic detection of method types and instantiation needs
- Namespace Management: Automatic namespace generation based on class names
- Error Transparency: Clear error messages for common integration issues
Key Features¶
- Automatic Method Discovery: Scans classes for public callable methods
- Smart Instantiation: Handles both static and instance method registration
- Namespace Support: Automatic namespace generation from class names
- MRO Traversal: The
traverse_mroparameter controls whether inherited methods from parent classes are included via Method Resolution Order traversal. WhenTrue(default), methods inherited from all parent classes (excludingobject) are also included, with subclass methods taking priority. WhenFalse, only methods defined directly on the class are registered. - Error Handling: Clear error messages for problematic class structures
- Async Support: Full compatibility with async/await patterns
- Reflection-Based: Uses Python's introspection capabilities for method discovery
Registration Patterns¶
Static Method Classes¶
Methods are registered directly without instantiation.
Instance Method Classes¶
class FileManager:
def __init__(self, base_path: str):
self.base_path = base_path
def read_file(self, filename: str) -> str:
# Implementation
Class is automatically instantiated and instance methods are registered.
Mixed Method Classes¶
Automatically detected and handled appropriately.
Inherited Method Registration¶
By default (traverse_mro=True), methods inherited from parent classes are also registered:
class BaseTools:
@staticmethod
def base_method(x: int) -> int:
return x * 2
class DerivedTools(BaseTools):
@staticmethod
def derived_method(x: int) -> int:
return x * 3
# Default behavior (traverse_mro=True): both base_method and derived_method are registered
# With traverse_mro=False: only derived_method is registered
API Reference¶
ClassToolIntegration¶
Handles integration with Python classes for method registration.
toolregistry.integrations.native.integration.ClassToolIntegration ¶
Initialize with a ToolRegistry instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ToolRegistry
|
The tool registry to register methods with. |
required |
traverse_mro
|
bool
|
Whether to traverse the MRO (Method Resolution Order)
to include inherited methods. When True (default), methods from
parent classes are also included (excluding |
True
|
register_class_methods ¶
register_class_methods(cls_or_instance: type | object, namespace: bool | str = False, constructor_kwargs: dict | None = None) -> None
Register all methods from a class or instance as tools.
If a class is provided
- If all public methods are static, they are registered directly.
- Otherwise, the class is instantiated and its public callable methods are registered.
If an instance is provided: - Its public callable methods are registered directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls_or_instance
|
Union[Type, object]
|
The class or instance to scan for methods. |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace. - If False, no namespace is used. - If True, the namespace is derived from the class name. - If a string is provided, it is used as the namespace. Defaults to False. |
False
|
constructor_kwargs
|
dict | None
|
Keyword arguments forwarded to the class
constructor when cls_or_instance is a class that needs
to be instantiated. Ignored when a pre-built instance is
passed. Defaults to |
None
|
register_class_methods_async
async
¶
register_class_methods_async(cls_or_instance: type | object, namespace: bool | str = False, constructor_kwargs: dict | None = None) -> None
Async implementation to register tools from a class.
Currently, this is implemented synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls_or_instance
|
Union[Type, object]
|
The class or instance to scan for methods. |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace. - If False, no namespace is used. - If True, the namespace is derived from the class name. - If a string is provided, it is used as the namespace. Defaults to False. |
False
|
constructor_kwargs
|
dict | None
|
Keyword arguments forwarded to the class
constructor when cls_or_instance is a class that needs
to be instantiated. Ignored when a pre-built instance is
passed. Defaults to |
None
|
Module Overview¶
Native Module¶
The main native integration module.
toolregistry.integrations.native ¶
ClassToolIntegration ¶
Initialize with a ToolRegistry instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ToolRegistry
|
The tool registry to register methods with. |
required |
traverse_mro
|
bool
|
Whether to traverse the MRO (Method Resolution Order)
to include inherited methods. When True (default), methods from
parent classes are also included (excluding |
True
|
register_class_methods ¶
register_class_methods(cls_or_instance: type | object, namespace: bool | str = False, constructor_kwargs: dict | None = None) -> None
Register all methods from a class or instance as tools.
If a class is provided
- If all public methods are static, they are registered directly.
- Otherwise, the class is instantiated and its public callable methods are registered.
If an instance is provided: - Its public callable methods are registered directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls_or_instance
|
Union[Type, object]
|
The class or instance to scan for methods. |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace. - If False, no namespace is used. - If True, the namespace is derived from the class name. - If a string is provided, it is used as the namespace. Defaults to False. |
False
|
constructor_kwargs
|
dict | None
|
Keyword arguments forwarded to the class
constructor when cls_or_instance is a class that needs
to be instantiated. Ignored when a pre-built instance is
passed. Defaults to |
None
|
register_class_methods_async
async
¶
register_class_methods_async(cls_or_instance: type | object, namespace: bool | str = False, constructor_kwargs: dict | None = None) -> None
Async implementation to register tools from a class.
Currently, this is implemented synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls_or_instance
|
Union[Type, object]
|
The class or instance to scan for methods. |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace. - If False, no namespace is used. - If True, the namespace is derived from the class name. - If a string is provided, it is used as the namespace. Defaults to False. |
False
|
constructor_kwargs
|
dict | None
|
Keyword arguments forwarded to the class
constructor when cls_or_instance is a class that needs
to be instantiated. Ignored when a pre-built instance is
passed. Defaults to |
None
|
Native Utils¶
Utility functions for native integration.
toolregistry.integrations.native.utils ¶
get_all_static_methods ¶
get_all_static_methods(cls_or_instance: type | object, skip_list: list[str] | None = None, include_list: list[str] | None = None) -> list[str]
Returns a list of all valid public static methods of a class or its instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls_or_instance
|
Union[Type, object]
|
The class type or instance from which static methods will be retrieved. |
required |
skip_list
|
Optional[List[str]]
|
A list of method names to explicitly skip. |
None
|
include_list
|
Optional[List[str]]
|
A list of method names to explicitly include. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List[str]: A list of names of all valid public static methods in the class. |
Example
class Example:
@staticmethod
def static_method_one():
pass
@staticmethod
def static_method_two():
pass
get_all_static_methods(Example, skip_list=["static_method_two"])
# ['static_method_one']
get_all_static_methods(Example, include_list=["static_method_two", "static_method_three"])
# ['static_method_two']