
MCP-Rig

2025.03.31
0
RustLLM 代理框架集成MCP 工具适配开发效率
MCP-Rig 是一个 Rust 库,用于无缝集成 Model Context Protocol (MCP) 和 Rig 的 LLM 代理框架。它允许将 MCP 工具暴露给 Rig 代理,作为这两个强大系统之间的桥梁。主要功能包括 MCP 客户端管理、工具适配、支持 stdio 和 SSE 传输、错误处理以及 RAG 兼容性。适用于需要将 MCP 工具集成到 LLM 代理框架中的开发场景。
View on GitHub
Overview
基本能力
产品定位
MCP-Rig 是一个用于集成 MCP 和 Rig LLM 代理框架的 Rust 库,旨在简化两者之间的交互。
核心功能
- MCP 客户端管理:轻松创建和管理多个 MCP 客户端
- 工具适配:自动将 MCP 工具适配到 Rig 的 Tool 接口
- 传输选项:支持 stdio 和 SSE 传输
- 错误处理:全面的 MCP 和 Rig 操作错误处理
- RAG 兼容性:工具可用于检索增强生成
适用场景
- 将 MCP 工具集成到 LLM 代理框架中
- 需要多种传输机制(stdio 或 SSE)的应用
- 基于自然语言查询的语义检索工具
- 在单个应用中管理多个 MCP 客户端
工具列表
- 文件系统工具:允许代理访问和操作文件系统
- Git 工具:提供 Git 版本控制功能
- Echo 工具:用于测试和调试的简单回显功能
常见问题解答
- 如何添加新的 MCP 客户端?使用
add_stdio_client
或add_sse_client
方法 - 如何获取特定客户端?使用
get_client
方法 - 支持哪些传输机制?支持 stdio 和 SSE
使用教程
使用依赖
安装前需要以下依赖: - Rust 编程语言 - Cargo 包管理器
安装教程
在 Cargo.toml
中添加以下依赖:
[dependencies]
mcp-rig = "0.1.0"
rig-core = "0.9.1"
tokio = { version = "1.32", features = ["full"] }
调试方式
运行示例代码进行调试:
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>> {
// 创建 MCP 连接管理器
let mut connection_manager = McpConnectionManager::with_timeout(Duration::from_secs(30));
// 获取当前目录
let current_dir = env::current_dir()?;
let current_dir_str = current_dir.to_string_lossy().to_string();
// 添加文件系统客户端
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?;
// 初始化 Rig 客户端
let rig_client = RigClient::from_env();
// 创建代理构建器和模型
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");
// 获取文件系统客户端
let filesystem_client = connection_manager
.get_client("filesystem-client")
.ok_or("Filesystem client not found")?;
// 设置带有 MCP 工具的 Rig 代理
let agent = setup_rig_with_mcp(filesystem_client, agent_builder, model).await?;
// 使用 cli_chatbot 处理交互
rig::cli_chatbot::cli_chatbot(agent).await?;
Ok(())
}