MCP服务器工具

一个基于Rust的MCP服务器和客户端示例,使用SSE进行通信,并集成了一个简单的加法工具。

  • by RGGH
  • Rust
  • 本地部署
  • 8 星标
  • 更新于
在 GitHub 查看 RGGH/rig-mcp-server

使用说明

🔍 Rust MCP服务器+检查器示例:使用添加工具的SSE传输

信用:https://dev.to/joshmo_dev/using-model-context-protocol-with-rig-m7o

本项目演示了如何使用服务器发送事件(SSE)进行通信来设置MCP(模型上下文协议)服务器和客户端。它包括一个简单的工具,可以将两个数字相加并与 钻机 LLM提示代理。


🚀 入门指南

克隆此仓库并运行:

cargo run

在单独的终端中,使用以下命令启动MCP检查器:

npx @modelcontextprotocol/inspector sse http://127.0.0.1:3001/sse

您将看到如下输出:

Starting MCP inspector...
Proxy server listening on port 3000
New SSE connection
Query parameters: { transportType: 'sse', url: 'http://localhost:3001/sse' }
SSE transport: url=http://localhost:3001/sse, headers=
Connected to SSE transport
Connected MCP client to backing server transport
Created web app transport
Set up MCP proxy
🔍 MCP Inspector is up and running at http://localhost:5173 🚀

您现在可以在以下网址查看web界面 http://localhost:5173


🛠️ 特性

  • ✅ 使用以下命令设置自定义MCP服务器 ServerSseTransport
  • ✅ 将MCP客户端连接到服务器
  • ✅ 注册自定义工具: AddTool,它将两个数字相加
  • ✅ 通过MCP列出已注册的工具
  • ✅ 与RIG集成,并使用该工具提示LLM代理

🧠 代码概述

#[tool(
    name = "Add",
    description = "Adds two numbers together.",
    params(a = "The first number to add", b = "The second number to add")
)]
async fn add_tool(a: f64, b: f64) -> Result<ToolResponseContent> {
    Ok(tool_text_content!((a + b).to_string()))
}

这定义了 Add 在MCP服务器中注册的工具。

主要功能设置如下:

  • 追踪
  • MCP服务器和传输(SSE)
  • 初始化并列出可用工具的MCP客户端
  • 具有OpenAI后端的RIG代理,使用MCP工具

然后,代理运行提示:

let response = agent.prompt("Add 10 + 10").await;

🧪 样本输出

成功运行后,您将看到以下日志:

Initialized: Ok(...)
Tools: Ok([...])
Building RIG agent
Prompting RIG agent
Agent response: Some("20")

🧰 技术栈

  • 🦀 生锈 东京
  • 📡 SSE运输 mcp_core
  • 🔧 MCP服务器/客户端架构
  • 🤖 基于OpenAI模型的RIG代理
  • 🌐 MCP检查器web界面

📦 依赖项

确保你有这些在你的 Cargo.toml:

[dependencies]
tokio = { version = "1", features = ["full"] }
anyhow = "1"
serde_json = "1"
mcp_core = "..."
mcp_core_macros = "..."
rig = "..."

替换 ... 根据您的环境使用适当的版本。


📍 检查器GUI

访问 http://localhost:5173 查看MCP检查器UI并与之交互。


✅ 测试日志样本

Query parameters: { transportType: 'sse', url: 'http://localhost:3001/sse' }
Connected to SSE transport
Connected MCP client to backing server transport
Set up MCP proxy
Received message for sessionId cdd4a8be-57e2-44e3-9b81-3df300e86f22

📬 问题或反馈?

欢迎打开一个问题或开始讨论!