Tool Repositories
Relevant source files
The following files were used as context for generating this wiki page:Tool Repositories are a core component of the UTCP client architecture, responsible for the storage and management of Tool definitions and their associated CallTemplate configurations. They provide a persistent, thread-safe, and efficient mechanism for looking up and retrieving tools that have been discovered and registered from various sources. The system is designed to be extensible, allowing developers to implement different storage backends, such as in-memory dictionaries, databases, or file systems, by adhering to a common interface.
The default implementation provided is the InMemToolRepository, which stores all tool and manual data in memory. This makes the client lightweight and easy to set up for most use cases. The repository is a crucial part of the tool lifecycle, from registration via a UtcpManual to retrieval during a call_tool operation.
Sources: CLAUDE.md, core/src/utcp/interfaces/concurrent_tool_repository.py:1-7, 19-24
Architecture and Core Concepts
The Tool Repository architecture is defined by the ConcurrentToolRepository abstract base class. This interface establishes a contract for all repository implementations, ensuring they provide a standard set of asynchronous methods for managing tools and manuals. This pluggable design allows the UTCP client's storage mechanism to be swapped out as needed.
Sources: core/src/utcp/interfaces/concurrent_tool_repository.py:19-24, README.md
Class Diagram
This diagram shows the abstract ConcurrentToolRepository and its concrete implementation, InMemToolRepository.
Data Relationships
The repository manages the relationships between UtcpManual, Tool, and CallTemplate objects. A CallTemplate is used to fetch a UtcpManual, which contains a list of Tool definitions.
ConcurrentToolRepository Interface
The ConcurrentToolRepository is an abstract base class that defines the standard API for all tool repository implementations. All methods are asynchronous to support a wide range of storage backends.
Sources: core/src/utcp/interfaces/concurrent_tool_repository.py:19-35
| Method | Description |
|---|---|
save_manual(manual_call_template, manual) | Saves a UtcpManual and its associated CallTemplate. |
remove_manual(manual_name) | Removes a manual and all its associated tools. |
remove_tool(tool_name) | Removes a single tool from the repository. |
get_tool(tool_name) | Retrieves a single tool by its unique name. |
get_tools() | Retrieves all tools stored in the repository. |
get_tools_by_manual(manual_name) | Retrieves all tools belonging to a specific manual. |
get_manual(manual_name) | Retrieves a UtcpManual by its name. |
get_manuals() | Retrieves all manuals in the repository. |
get_manual_call_template(name) | Retrieves a CallTemplate by its name. |
get_manual_call_templates() | Retrieves all CallTemplate objects. |
Sources: core/src/utcp/interfaces/concurrent_tool_repository.py:37-119
InMemToolRepository Implementation
The python-utcp library provides a default, thread-safe, in-memory implementation of the ConcurrentToolRepository interface. It uses dictionaries to store manuals, tools, and call templates.
Sources: core/src/utcp/implementations/in_mem_tool_repository.py
Internal Structure
_manuals: A dictionary mapping a manual's name to theUtcpManualobject._tools: A dictionary mapping a tool's unique namespaced name (e.g.,manual_name.tool_name) to theToolobject._manual_call_templates: A dictionary mapping a manual's name to itsCallTemplate._lock: An instance ofAsyncRWLockto manage concurrent read/write access.
Sources: core/src/utcp/implementations/in_mem_tool_repository.py:21-25
Thread Safety
To ensure data consistency during concurrent operations, InMemToolRepository uses an AsyncRWLock. This custom lock allows multiple concurrent readers but ensures that write operations have exclusive access.
- Read operations (e.g.,
get_tool,get_tools) acquire areader_lock. - Write operations (e.g.,
save_manual,remove_manual) acquire awriter_lock.
This prevents race conditions where one coroutine might read data while another is in the middle of modifying it.
Sources: core/src/utcp/implementations/in_mem_tool_repository.py:32, 70, core/src/utcp/python_specific_tooling/async_rwlock.py
The following sequence diagram illustrates how a write lock is acquired during a save_manual operation.
Configuration and Serialization
The tool repository is configured when initializing the UtcpClient. The configuration can be specified in a dictionary or a JSON file. The tool_repository_type field determines which implementation to use. The default is "in_memory".
Sources: README.md, core/tests/client/test_utcp_client.py:217-229
An example config.json entry:
{
"tool_repository": {
"tool_repository_type": "in_memory"
}
}
Sources: README.md
The ConcurrentToolRepositoryConfigSerializer is responsible for deserializing this configuration into the correct repository object. It maintains a mapping of tool_repository_type strings to their corresponding serializer implementations.
Sources: core/src/utcp/interfaces/concurrent_tool_repository.py:121-134
This flowchart shows the deserialization process.
Summary
Tool Repositories are a foundational, extensible component in the UTCP client for managing the lifecycle of tools. They provide a thread-safe abstraction for storing and retrieving tool definitions, with a default in-memory implementation that is suitable for most applications. The use of an abstract interface and a serializer pattern makes the system flexible and allows for custom storage backends to be integrated seamlessly.