Skip to main content

Module protocol

Module protocol 

Source
Expand description

Tool protocol abstraction layer.

This module provides a flexible abstraction for connecting agents to various tool protocols. It supports multiple standards including MCP (Model Context Protocol), custom function calling, Memory persistence, and allows users to implement their own tool communication mechanisms.

§Architecture

Single Protocol (traditional):

Agent → ToolRegistry → ToolProtocol → Single Tool Source

Multi-Protocol (new in 0.5.0):

Agent → ToolRegistry → [Protocol1, Protocol2, Protocol3]
        (routing map)     ↓          ↓          ↓
                       Local      YouTube    GitHub
                       Tools      Server     Server

§Key Components

  • ToolProtocol trait: Define how tools are executed, discovered, and described
  • ToolRegistry: Single or multi-protocol tool aggregation with transparent routing
  • ToolMetadata: Tool identity, description, parameters
  • ToolParameter: Type-safe parameter definitions with validation
  • ToolResult: Structured tool execution results
  • Tool: Runtime tool instance bound to a protocol

§Single Protocol Example

use std::sync::Arc;
use mcp::protocol::{ToolMetadata, ToolProtocol, ToolRegistry, ToolResult};

struct MyProtocol;

#[async_trait::async_trait]
impl ToolProtocol for MyProtocol {
    async fn execute(
        &self,
        _tool_name: &str,
        _parameters: serde_json::Value,
    ) -> Result<ToolResult, Box<dyn std::error::Error + Send + Sync>> {
        Ok(ToolResult::success(serde_json::json!({"ok": true})))
    }
    async fn list_tools(&self) -> Result<Vec<ToolMetadata>, Box<dyn std::error::Error + Send + Sync>> {
        Ok(vec![ToolMetadata::new("demo", "Demo tool")])
    }
    async fn get_tool_metadata(&self, _: &str) -> Result<ToolMetadata, Box<dyn std::error::Error + Send + Sync>> {
        Ok(ToolMetadata::new("demo", "Demo tool"))
    }
    fn protocol_name(&self) -> &str { "demo" }
}

let protocol = Arc::new(MyProtocol);
let mut registry = ToolRegistry::new(protocol);
let _ = registry.discover_tools_from_primary().await;

§Multi-Protocol Example

use mcp::client::McpClientProtocol;
use mcp::protocol::ToolRegistry;

let mut registry = ToolRegistry::empty();
let _ = registry
    .add_protocol("remote", std::sync::Arc::new(McpClientProtocol::new("http://localhost:8080".to_string())))
    .await;

Structs§

Tool
A tool that can be used by agents
ToolDefinition
Provider-agnostic tool definition suitable for LLM native tool calling.
ToolMetadata
Metadata about a tool
ToolParameter
Defines a parameter for a tool
ToolRegistry
Registry for managing tools available to agents
ToolResult
Represents the result of a tool execution

Enums§

ToolError
Error types for tool operations
ToolParameterType
Defines the type of a tool parameter

Traits§

ToolProtocol
Trait for implementing tool execution protocols