ToolRegistry: a library for easier function calling and tool use in LLMs
Project description
ToolRegistry
A Python library for managing and executing tools in a structured way.
Features
- Tool registration and management
- JSON Schema generation for tool parameters
- Tool execution and result handling
- Support for both synchronous and asynchronous tools
- Support native Python functions and class methods as tools
- Support multiple MCP transport methods: STDIO, streamable HTTP, SSE, WebSocket, FastMCP instance, etc.
- Support OpenAPI tools
Full Documentation
Full documentation is available at https://toolregistry.lab.oaklight.cn
API Changes (starting 0.4.4)
Previously, the method ToolRegistry.register_static_tools was used for registering static methods from classes. This has now been replaced by ToolRegistry.register_from_class. Similarly, ToolRegistry.register_mcp_tools has been replaced by ToolRegistry.register_from_mcp, and ToolRegistry.register_openapi_tools by ToolRegistry.register_from_openapi. All old methods are planned to be deprecated soon, so please migrate to the new interfaces as soon as possible. For backward compatibility, the old names remain as aliases to the new ones.
Installation
Basic Installation
Install the core package (requires Python >= 3.8):
pip install toolregistry
Installing with Extra Support Modules
Extra modules can be installed by specifying extras in brackets. For example, to install specific extra supports:
pip install toolregistry[mcp,openapi]
Below is a table summarizing available extra modules:
| Extra Module | Python Requirement | Example Command |
|---|---|---|
| mcp | Python >= 3.10 | pip install toolregistry[mcp] |
| openapi | Python >= 3.8 | pip install toolregistry[openapi] |
| langchain | Python >= 3.9 | pip install toolregistry[langchain] |
Examples
OpenAI Implementation
The openai_tool_usage_example.py shows how to integrate ToolRegistry with OpenAI's API.
Cicada Implementation
The cicada_tool_usage_example.py demonstrates how to use ToolRegistry with the Cicada MultiModalModel.
Basic Tool Invocation
This section demonstrates how to invoke a basic tool. Example:
from toolregistry import ToolRegistry
registry = ToolRegistry()
@registry.register
def add(a: float, b: float) -> float:
"""Add two numbers together."""
return a + b
available_tools = registry.get_available_tools()
print(available_tools) # ['add']
add_func = registry.get_callable('add')
print(type(add_func)) # <class 'function'>
add_result = add_func(1, 2)
print(add_result) # 3
add_func = registry['add']
print(type(add_func)) # <class 'function'>
add_result = add_func(4, 5)
print(add_result) # 9
For more usage examples, please refer to Documentation - Usage
MCP Integration
The ToolRegistry provides first-class support for MCP (Model Context Protocol) tools with multiple transport options:
# transport can be a URL string, script path, transport instance, or MCP instance.
transport = "https://mcphub.url/mcp" # Streamable HTTP MCP
transport = "http://localhost:8000/sse/test_group" # Legacy HTTP+SSE
transport = "examples/mcp_related/mcp_servers/math_server.py" # Local path
transport = {
"mcpServers": {
"make_mcp": {
"command": f"{Path.home()}/mambaforge/envs/toolregistry_dev/bin/python",
"args": [
f"{Path.home()}/projects/toolregistry/examples/mcp_related/mcp_servers/math_server.py"
],
"env": {},
}
}
} # MCP configuration dictionary example
transport = FastMCP(name="MyFastMCP") # FastMCP instance
transport = StreamableHttpTransport(url="https://mcphub.example.com/mcp", headers={"Authorization": "Bearer token"}) # Transport instance with custom headers
registry.register_from_mcp(transport)
# Get all tools' JSON, including MCP tools
tools_json = registry.get_tools_json()
OpenAPI Integration
ToolRegistry supports integration with OpenAPI for interacting with tools using a standardized API interface:
registry.register_from_openapi("http://localhost:8000/") # Providing base URL
registry.register_from_openapi("./openapi_spec.json", "http://localhost/") # Providing local OpenAPI specification file and base URL
# Get all tools' JSON, including OpenAPI tools
tools_json = registry.get_tools_json()
LangChain Integration
The LangChain integration module allows ToolRegistry to register and invoke LangChain tools seamlessly, supporting both synchronous and asynchronous calls.
from langchain_community.tools import ArxivQueryRun, PubmedQueryRun
from toolregistry import ToolRegistry
registry = ToolRegistry()
registry.register_from_langchain([ArxivQueryRun(), PubmedQueryRun()])
tools_json = registry.get_tools_json()
Note
When only providing a base URL, ToolRegistry will attempt a "best effort" auto-discovery to find the OpenAPI specification file (e.g., via http://<base_url>/openapi.json or http://<base_url>/swagger.json). If discovery fails, ensure the provided URL is correct or download the OpenAPI specification file yourself and register using the file + base URL method:
registry.register_from_openapi("./openapi_spec.json", "http://localhost/")
Registering Class Tools
Class tools are registered to ToolRegistry using the register_from_class method. This allows developers to extend the functionality of ToolRegistry by creating custom tool classes with reusable methods.
Example:
from toolregistry import ToolRegistry
class StaticExample:
@staticmethod
def greet(name: str) -> str:
return f"Hello, {name}!"
class InstanceExample:
def __init__(self, name: str):
self.name = name
def greet(self, name: str) -> str:
return f"Hello, {name}! I'm {self.name}."
registry = ToolRegistry()
registry.register_from_class(StaticExample, with_namespace=True)
print(registry.get_available_tools()) # ['static_example.greet']
print(registry["static_example.greet"]("Alice")) # Hello, Alice!
registry = ToolRegistry()
registry.register_from_class(InstanceExample("Bob"), with_namespace=True)
print(registry.get_available_tools()) # ['instance_example.greet']
print(registry["instance_example.greet"]("Alice")) # Hello, Alice! I'm Bob.
Hub Tools
Hub tools encapsulate commonly used functionalities as methods in classes. Examples of available hub tools include:
- Calculator: Basic arithmetic, scientific operations, statistical functions, financial calculations, and more.
- FileOps: File manipulation like diff generation, patching, verification, merging, and splitting.
- Filesystem: Comprehensive file system operations such as directory listing, file read/write, path normalization, and querying file attributes.
- UnitConverter: Extensive unit conversions such as temperature, length, weight, volume, etc.
- WebSearch: Web search functionality supporting multiple engines like SearxNG and Google.
To register hub tools:
from toolregistry import ToolRegistry
from toolregistry.hub import Calculator
registry = ToolRegistry()
registry.register_from_class(Calculator, with_namespace=True)
# Get available tools list
print(registry.get_available_tools())
# Output: ['Calculator.add', 'Calculator.subtract', ..., 'Calculator.multiply', ...]
Community Contribution
We welcome community contributions of new tool classes to ToolRegistry! If you have designs or implementations for other commonly used tools, feel free to submit them through a Pull Request on the GitHub Repository. Your contributions will help expand the diversity and usability of the tools.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file toolregistry-0.4.9.tar.gz.
File metadata
- Download URL: toolregistry-0.4.9.tar.gz
- Upload date:
- Size: 46.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a41435347ed4a57e6a4f4979ed17abc05db68abef65acea17645083ada8917cb
|
|
| MD5 |
64804f3bc9a5e842f6193fee1c849e15
|
|
| BLAKE2b-256 |
424bff320825b6faa1ce52fa0d0d8fd9f4801d06d47e722968d3a3956dd6e123
|
File details
Details for the file toolregistry-0.4.9-py3-none-any.whl.
File metadata
- Download URL: toolregistry-0.4.9-py3-none-any.whl
- Upload date:
- Size: 52.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3aadb799fd7a18ce341c218555f0319d0251979ffc4e6709889fdf767e50055e
|
|
| MD5 |
23f9b3066836ccb523060fa7ed38df6e
|
|
| BLAKE2b-256 |
b0240182896706045dcca92fce12335bd17498b80830dd096b2969e6057e842e
|