pub struct ToolRegistry { /* private fields */ }Expand description
Registry for managing tools available to agents
Supports single or multiple tool protocols, enabling agents to transparently access tools from multiple sources (local functions, MCP servers, etc.)
§Single Protocol
use cloudllm::tool_protocol::ToolRegistry;
use cloudllm::tool_protocols::CustomToolProtocol;
use std::sync::Arc;
let protocol = Arc::new(CustomToolProtocol::new());
let registry = ToolRegistry::new(protocol);§Multiple Protocols
use cloudllm::tool_protocol::ToolRegistry;
use cloudllm::tool_protocols::{CustomToolProtocol, McpClientProtocol};
use std::sync::Arc;
let mut registry = ToolRegistry::empty();
// Add local tools
registry.add_protocol(
"local",
Arc::new(CustomToolProtocol::new())
).await.ok();
// Add remote MCP server
registry.add_protocol(
"youtube",
Arc::new(McpClientProtocol::new("http://youtube-mcp:8081".to_string()))
).await.ok();
// Agent transparently accesses bothImplementations§
Source§impl ToolRegistry
impl ToolRegistry
Sourcepub fn new(protocol: Arc<dyn ToolProtocol>) -> Self
pub fn new(protocol: Arc<dyn ToolProtocol>) -> Self
Build a registry powered by a single protocol implementation.
This is the traditional single-protocol mode. Use empty() and add_protocol()
for multi-protocol support.
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Create an empty registry ready to accept multiple protocols.
Use add_protocol() to register protocols.
Sourcepub async fn add_protocol(
&mut self,
protocol_name: &str,
protocol: Arc<dyn ToolProtocol>,
) -> Result<(), Box<dyn Error + Send + Sync>>
pub async fn add_protocol( &mut self, protocol_name: &str, protocol: Arc<dyn ToolProtocol>, ) -> Result<(), Box<dyn Error + Send + Sync>>
Register a protocol and discover its tools.
§Arguments
protocol_name- Unique identifier for this protocol (e.g., “local”, “youtube”, “github”)protocol- The ToolProtocol implementation
§Tool Discovery
This method calls protocol.list_tools() to discover available tools
and automatically registers them in the registry.
§Conflicts
If a tool with the same name already exists, it will be replaced. The new protocol’s tool takes precedence.
§Example
use cloudllm::tool_protocol::ToolRegistry;
use cloudllm::tool_protocols::McpClientProtocol;
use std::sync::Arc;
let mut registry = ToolRegistry::empty();
registry.add_protocol(
"memory_server",
Arc::new(McpClientProtocol::new("http://localhost:8080".to_string()))
).await?;Sourcepub fn remove_protocol(&mut self, protocol_name: &str)
pub fn remove_protocol(&mut self, protocol_name: &str)
Remove a protocol and all its tools from the registry.
Sourcepub fn add_tool(&mut self, tool: Tool)
pub fn add_tool(&mut self, tool: Tool)
Insert or replace a tool definition (for manual tool registration).
Sourcepub fn remove_tool(&mut self, name: &str) -> Option<Tool>
pub fn remove_tool(&mut self, name: &str) -> Option<Tool>
Remove a tool by name returning the owned entry if present.
Sourcepub fn list_tools(&self) -> Vec<&ToolMetadata>
pub fn list_tools(&self) -> Vec<&ToolMetadata>
List metadata for registered tools (iteration order follows the underlying map).
Sourcepub async fn discover_tools_from_primary(
&mut self,
) -> Result<(), Box<dyn Error + Send + Sync>>
pub async fn discover_tools_from_primary( &mut self, ) -> Result<(), Box<dyn Error + Send + Sync>>
Discover tools from the primary protocol (for single-protocol registries).
This is useful after registering tools with the protocol to populate the registry.
For multi-protocol registries, use add_protocol() instead.
Sourcepub fn get_tool_protocol(&self, tool_name: &str) -> Option<&str>
pub fn get_tool_protocol(&self, tool_name: &str) -> Option<&str>
Get which protocol handles a specific tool.
Sourcepub fn list_protocols(&self) -> Vec<&str>
pub fn list_protocols(&self) -> Vec<&str>
Get all registered protocol names.
Sourcepub async fn execute_tool(
&self,
tool_name: &str,
parameters: Value,
) -> Result<ToolResult, Box<dyn Error + Send + Sync>>
pub async fn execute_tool( &self, tool_name: &str, parameters: Value, ) -> Result<ToolResult, Box<dyn Error + Send + Sync>>
Execute a named tool with serialized parameters.
Sourcepub fn protocol(&self) -> Option<&Arc<dyn ToolProtocol>>
pub fn protocol(&self) -> Option<&Arc<dyn ToolProtocol>>
Borrow the primary protocol implementation (for single-protocol mode).
Returns None if registry was created with empty() or has multiple protocols.
Sourcepub fn to_tool_definitions(&self) -> Vec<ToolDefinition>
pub fn to_tool_definitions(&self) -> Vec<ToolDefinition>
Returns all registered tools as ToolDefinitions ready to pass to the LLM.
Iterates over every tool in the registry, calling
ToolMetadata::to_tool_definition on each, and returns the results as a Vec.
An empty Vec is returned when no tools have been registered.
§Example
use cloudllm::tool_protocol::{ToolRegistry, Tool, ToolMetadata, ToolParameter, ToolParameterType};
use cloudllm::tool_protocols::CustomToolProtocol;
use std::sync::Arc;
let protocol = Arc::new(CustomToolProtocol::new());
let mut registry = ToolRegistry::new(protocol.clone());
let tool = Tool::new("calculator", "Evaluates math", protocol);
registry.add_tool(tool);
let defs = registry.to_tool_definitions();
assert_eq!(defs.len(), 1);
assert_eq!(defs[0].name, "calculator");