Hub Tools Usage Guide¶
Changelog
New in version: 0.4.2
Standalone Package
Hub Tools Package Update: starting 0.4.14, hub tools have been moved to a separate package toolregistry-hub. You can install it via:
pip install toolregistry-hub
This allows you to use hub tools independently or alongside ToolRegistry.
Introduction¶
Hub encapsulates commonly used tools as methods of a class, including both static methods and instance methods, serving as ready-to-use tool groups. This design offers several advantages:
- Organization: Related tool methods are grouped in the same class for easier management and maintenance.
- Reusability: Pre-built tools can be imported and used directly without reimplementation.
- Consistency: All tools follow the same interface specification.
- Extensibility: New tool classes or methods can be easily added.
- Safety and Exception Handling: Encapsulated tools provide better control over security and handle exceptions more effectively compared to allowing models to directly execute commands.
Hub tools can be registered using the register() method with source="class". Refer to Registering Class-Based Python Tools for detailed instructions.
Example of Using Predefined Tools¶
Hub also provides predefined classes with ready-to-use methods. These tools can be easily registered and used without additional setup.
from toolregistry import ToolRegistry
from toolregistry.hub import BaseCalculator, Calculator, FileOps
# Create tool registry
registry = ToolRegistry()
# Register Calculator tools (with namespace)
registry.register(Calculator, source="class", namespace=True)
# Register FileOps tools (without namespace)
registry.register(FileOps, source="class")
# Get available tools list
print(registry.get_available_tools())
# Output: ['calculator-list_allowed_fns', 'calculator-help', 'calculator-evaluate', 'read_file', 'write_file', ...]
Using the namespace=True parameter during registration adds the class name as a namespace prefix to tool names, ensuring better organization and avoiding naming conflicts. For example:
This will register tools with names like base_calculator-add, base_calculator-subtract, etc.
Advantages of using namespace:
- Avoids naming conflicts between methods with the same names in different classes.
- More clearly identifies tool sources.
- Maintains naming consistency.