Crate mistralrs_mcp

Source
Expand description

Model Context Protocol (MCP) Client Implementation

This crate provides a comprehensive client implementation for the Model Context Protocol (MCP), enabling AI assistants to connect to and interact with external tools and resources through standardized server interfaces.

§Overview

The MCP client supports multiple transport protocols and provides automatic tool discovery, registration, and execution. It seamlessly integrates with tool calling systems, allowing MCP tools to be used alongside built-in tools.

§Features

  • Multiple Transport Protocols: HTTP, WebSocket, and Process-based connections
  • Automatic Tool Discovery: Discovers and registers tools from connected MCP servers
  • Bearer Token Authentication: Supports authentication for secured MCP servers
  • Concurrent Tool Execution: Handles multiple tool calls efficiently
  • Resource Access: Access to MCP server resources like files and data
  • Tool Naming Prefix: Avoid conflicts with customizable tool name prefixes

§Transport Protocols

§HTTP Transport

For MCP servers accessible via HTTP endpoints with JSON-RPC over HTTP. Supports both regular JSON responses and Server-Sent Events (SSE).

§WebSocket Transport

For real-time bidirectional communication with MCP servers over WebSocket. Ideal for interactive applications requiring low-latency tool calls.

§Process Transport

For local MCP servers running as separate processes, communicating via stdin/stdout using JSON-RPC messages.

§Example Usage

§Simple Configuration

use mistralrs_mcp::{McpClientConfig, McpServerConfig, McpServerSource, McpClient};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Simple configuration with minimal settings
    // Most fields use sensible defaults (enabled=true, UUID for id/prefix, no timeouts)
    let config = McpClientConfig {
        servers: vec![
            McpServerConfig {
                name: "Hugging Face MCP Server".to_string(),
                source: McpServerSource::Http {
                    url: "https://hf.co/mcp".to_string(),
                    timeout_secs: None,
                    headers: None,
                },
                bearer_token: Some("hf_xxx".to_string()),
                ..Default::default()
            },
        ],
        ..Default::default()
    };
     
    // Initialize MCP client
    let mut client = McpClient::new(config);
    client.initialize().await?;
     
    // Get tool callbacks for integration with model builder
    let tool_callbacks = client.get_tool_callbacks_with_tools();
    println!("Registered {} MCP tools", tool_callbacks.len());
     
    Ok(())
}

§Advanced Configuration

use mistralrs_mcp::{McpClientConfig, McpServerConfig, McpServerSource, McpClient};
use std::collections::HashMap;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Configure MCP client with multiple servers and custom settings
    let config = McpClientConfig {
        servers: vec![
            // HTTP server with Bearer token
            McpServerConfig {
                id: "web_search".to_string(),
                name: "Web Search MCP".to_string(),
                source: McpServerSource::Http {
                    url: "https://api.example.com/mcp".to_string(),
                    timeout_secs: Some(30),
                    headers: None,
                },
                enabled: true,
                tool_prefix: Some("web".to_string()),
                resources: None,
                bearer_token: Some("your-api-token".to_string()),
            },
            // WebSocket server
            McpServerConfig {
                id: "realtime_data".to_string(),
                name: "Real-time Data MCP".to_string(),
                source: McpServerSource::WebSocket {
                    url: "wss://realtime.example.com/mcp".to_string(),
                    timeout_secs: Some(60),
                    headers: None,
                },
                enabled: true,
                tool_prefix: Some("rt".to_string()),
                resources: None,
                bearer_token: Some("ws-token".to_string()),
            },
            // Process-based server
            McpServerConfig {
                id: "filesystem".to_string(),
                name: "Filesystem MCP".to_string(),
                source: McpServerSource::Process {
                    command: "mcp-server-filesystem".to_string(),
                    args: vec!["--root".to_string(), "/tmp".to_string()],
                    work_dir: None,
                    env: None,
                },
                enabled: true,
                tool_prefix: Some("fs".to_string()),
                resources: Some(vec!["file://**".to_string()]),
                bearer_token: None,
            },
        ],
        auto_register_tools: true,
        tool_timeout_secs: Some(30),
        max_concurrent_calls: Some(5),
    };
     
    // Initialize MCP client
    let mut client = McpClient::new(config);
    client.initialize().await?;
     
    // Get tool callbacks for integration with model builder
    let tool_callbacks = client.get_tool_callbacks_with_tools();
    println!("Registered {} MCP tools", tool_callbacks.len());
     
    Ok(())
}

Re-exports§

pub use client::McpClient;
pub use client::McpServerConnection;
pub use tools::CalledFunction;
pub use tools::Function;
pub use tools::Tool;
pub use tools::ToolCallback;
pub use tools::ToolCallbackWithTool;
pub use tools::ToolType;
pub use types::McpToolResult;

Modules§

client
tools
transport
types

Structs§

McpClientConfig
Configuration for MCP client integration
McpServerConfig
Configuration for an individual MCP server
McpToolInfo
Information about a tool discovered from an MCP server

Enums§

McpServerSource
Supported MCP server transport sources