Skip to main content

ToolRegistry

Struct ToolRegistry 

Source
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 both

Implementations§

Source§

impl ToolRegistry

Source

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.

Source

pub fn empty() -> Self

Create an empty registry ready to accept multiple protocols.

Use add_protocol() to register protocols.

Source

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?;
Source

pub fn remove_protocol(&mut self, protocol_name: &str)

Remove a protocol and all its tools from the registry.

Source

pub fn add_tool(&mut self, tool: Tool)

Insert or replace a tool definition (for manual tool registration).

Source

pub fn remove_tool(&mut self, name: &str) -> Option<Tool>

Remove a tool by name returning the owned entry if present.

Source

pub fn get_tool(&self, name: &str) -> Option<&Tool>

Borrow a tool by name.

Source

pub fn list_tools(&self) -> Vec<&ToolMetadata>

List metadata for registered tools (iteration order follows the underlying map).

Source

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.

Source

pub fn get_tool_protocol(&self, tool_name: &str) -> Option<&str>

Get which protocol handles a specific tool.

Source

pub fn list_protocols(&self) -> Vec<&str>

Get all registered protocol names.

Source

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.

Source

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.

Source

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");

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more