MCP-Rig集成工具

MCP-Rig是一个Rust库,提供Model Context Protocol(MCP)与Rig的LLM代理框架之间的无缝集成,使MCP工具可在Rig代理中使用。

  • by tim-schultz
  • Rust
  • 本地部署
  • 更新于
在 GitHub 查看 tim-schultz/mcp-rig

使用说明

MCP钻机

MCP Rig是一个Rust机箱,它提供了模型上下文协议(MCP)和Rig的LLM代理框架之间的无缝集成。这个机箱允许您将MCP工具暴露给Rig代理,在这两个强大的系统之间架起一座桥梁。

概述

该集成通过调整MCP工具来实现Rig的 Tool 和 ToolEmbedding 特征,使其可以直接在Rig的代理框架中使用。这允许您:

  • 将现有的MCP工具暴露给LLM代理
  • 使用具有不同传输机制(stdio、SSE)的工具
  • 启用基于自然语言查询的工具语义检索
  • 在单个应用程序中管理多个MCP客户端

特性

  • MCP客户端管理:轻松创建和管理多个MCP客户端
  • 工具适配:MCP工具自动适应钻机工具界面
  • 运输选项:支持基于stdio和SSE的传输
  • 错误处理:MCP和钻机操作的全面错误处理
  • RAG兼容性:工具可用于检索增强生成

安装

将此添加到您的 Cargo.toml:

[dependencies]
mcp-rig = "0.1.0"
rig-core = "0.9.1"
tokio = { version = "1.32", features = ["full"] }

快速开始

下面是一个如何使用MCP Rig创建文件系统工具代理的简单示例:

use mcp_client::client::ClientInfo;
use mcp_rig::{setup_rig_with_mcp, McpConnectionManager};
use rig::providers::openai::Client as RigClient;
use std::collections::HashMap;
use std::env;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create the MCP connection manager
    let mut connection_manager = McpConnectionManager::with_timeout(Duration::from_secs(30));

    // Get current directory for filesystem access
    let current_dir = env::current_dir()?;
    let current_dir_str = current_dir.to_string_lossy().to_string();

    // Add a filesystem client
    connection_manager
        .add_stdio_client(
            "filesystem-client".to_string(),
            "npx",
            vec![
                "-y".to_string(),
                "@modelcontextprotocol/server-filesystem".to_string(),
                current_dir_str,
            ],
            HashMap::new(),
            ClientInfo {
                name: "rig-integration-filesystem".to_string(),
                version: "1.0.0".to_string(),
            },
        )
        .await?;

    // Initialize the Rig client
    let rig_client = RigClient::from_env();

    // Create the agent builder and model
    let agent_builder = rig_client.agent("gpt-4o").preamble(
        "You are a helpful assistant with access to filesystem tools."
    );
    let model = rig_client.completion_model("o3-mini");

    // Get the filesystem client
    let filesystem_client = connection_manager
        .get_client("filesystem-client")
        .ok_or("Filesystem client not found")?;

    // Set up the Rig agent with MCP tools
    let agent = setup_rig_with_mcp(filesystem_client, agent_builder, model).await?;

    // Use cli_chatbot to handle the interaction
    rig::cli_chatbot::cli_chatbot(agent).await?;

    Ok(())
}

高级用法

有关更高级的用法,请参阅 bin/advanced_filesystem_example.rs 示例,演示了:

  • 设置文件系统MCP客户端
  • 使用MCP工具配置Rig代理
  • 使用代理与文件系统交互
  • 通过代理创建和操作文件

MCP连接管理器

这 McpConnectionManager 提供了一个简化的界面,用于处理多个MCP客户端:

// Create a connection manager
let mut manager = McpConnectionManager::new();

// Add a Git client using stdio transport
manager.add_stdio_client(
    "git-client".to_string(),
    "uvx",
    vec!["mcp-server-git".to_string()],
    HashMap::new(),
    ClientInfo {
        name: "integration-git".to_string(),
        version: "1.0.0".to_string(),
    },
).await?;

// Add an Echo client using SSE transport
manager.add_sse_client(
    "echo-client".to_string(),
    "http://localhost:8000/sse",
    HashMap::new(),
    ClientInfo {
        name: "integration-echo".to_string(),
        version: "1.0.0".to_string(),
    },
).await?;

// Retrieve clients by ID
let git_client = manager.get_client("git-client");
let echo_client = manager.get_client("echo-client");

许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。