pub struct CustomToolProtocol { /* private fields */ }Expand description
Custom function-calling tool adapter
This adapter allows you to register Rust functions as tools that agents can use. It’s useful for quick prototyping and simple tool implementations.
§Example
use cloudllm::tool_protocols::CustomToolProtocol;
use cloudllm::tool_protocol::{ToolResult, ToolMetadata, ToolParameter, ToolParameterType};
use std::sync::Arc;
let mut adapter = CustomToolProtocol::new();
// Register a synchronous tool
adapter.register_tool(
ToolMetadata::new("add", "Adds two numbers")
.with_parameter(
ToolParameter::new("a", ToolParameterType::Number).required()
)
.with_parameter(
ToolParameter::new("b", ToolParameterType::Number).required()
),
Arc::new(|params| {
let a = params["a"].as_f64().unwrap_or(0.0);
let b = params["b"].as_f64().unwrap_or(0.0);
Ok(ToolResult::success(serde_json::json!({"result": a + b})))
})
);Implementations§
Source§impl CustomToolProtocol
impl CustomToolProtocol
Sourcepub async fn register_tool(
&self,
metadata: ToolMetadata,
function: ToolFunction,
)
pub async fn register_tool( &self, metadata: ToolMetadata, function: ToolFunction, )
Register a synchronous tool function.
Subsequent calls will overwrite any existing tool with the same name.
Sourcepub async fn register_async_tool(
&self,
metadata: ToolMetadata,
function: AsyncToolFunction,
)
pub async fn register_async_tool( &self, metadata: ToolMetadata, function: AsyncToolFunction, )
Register an asynchronous tool function.
Sourcepub async fn unregister_tool(&self, name: &str)
pub async fn unregister_tool(&self, name: &str)
Remove a tool from the adapter.
Trait Implementations§
Source§impl Default for CustomToolProtocol
impl Default for CustomToolProtocol
Source§impl ToolProtocol for CustomToolProtocol
impl ToolProtocol for CustomToolProtocol
Source§fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
tool_name: &'life1 str,
parameters: JsonValue,
) -> Pin<Box<dyn Future<Output = Result<ToolResult, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
tool_name: &'life1 str,
parameters: JsonValue,
) -> Pin<Box<dyn Future<Output = Result<ToolResult, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Execute a tool with the given parameters
Source§fn list_tools<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Vec<ToolMetadata>, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn list_tools<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Vec<ToolMetadata>, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Get metadata about available tools
Source§fn get_tool_metadata<'life0, 'life1, 'async_trait>(
&'life0 self,
tool_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<ToolMetadata, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_tool_metadata<'life0, 'life1, 'async_trait>(
&'life0 self,
tool_name: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<ToolMetadata, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Get metadata about a specific tool
Source§fn protocol_name(&self) -> &str
fn protocol_name(&self) -> &str
Protocol identifier (e.g., “mcp”, “custom”, “openai-functions”)
Source§fn initialize<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn initialize<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Initialize/connect to the tool protocol
Source§fn shutdown<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn shutdown<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Cleanup/disconnect from the tool protocol
Source§fn list_resources<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Vec<ResourceMetadata>, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn list_resources<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<Vec<ResourceMetadata>, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
List available resources (MCP Resource support) Read more
Source§fn read_resource<'life0, 'life1, 'async_trait>(
&'life0 self,
uri: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn read_resource<'life0, 'life1, 'async_trait>(
&'life0 self,
uri: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<String, Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Read the content of a resource by URI (MCP Resource support) Read more
Source§fn supports_resources(&self) -> bool
fn supports_resources(&self) -> bool
Check if this protocol supports resources
Auto Trait Implementations§
impl Freeze for CustomToolProtocol
impl !RefUnwindSafe for CustomToolProtocol
impl Send for CustomToolProtocol
impl Sync for CustomToolProtocol
impl Unpin for CustomToolProtocol
impl !UnwindSafe for CustomToolProtocol
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more