rust创建MCP Server
参考文档:
https://www.shuttle.dev/blog/2025/07/18/how-to-build-a-stdio-mcp-server-in-rust
https://mcpcat.io/guides/building-mcp-server-rust/
MCP
https://modelcontextprotocol.io/overview
MCP(Model Context Protocol)定义了AI模型使用外部工具或资源方法的协议,这样可以扩展AI的应用场景。Cursor,Claude,VS Code的Cline插件,CherryStudio这些模型客户端都可以作为MCP客户端,通过MCP协议,从MCP Server获取资源,工具。
传输类型
MCP协议目前的传输类型有:
- stdio (standard input/output)标准输入输出流,主要在本地使用,可以访问本地文件系统,执行命令,访问数据库等
- SSE (Server-Sent-Events) 服务发送事件,运行在云服务器上,通过websockets连接
标准输入输出数据传输
通过stdin接收请求,通过stdout发送响应。这种模式在命令行工具、脚本集成和进程间通信(IPC)使用。
- 标准输入 (stdin): 程序读取输入数据的流(文件描述符0)
- 标准输出 (stdout): 程序写入输出数据的流(文件描述符1)
- 标准错误 (stderr): 程序写入错误信息的流(文件描述符2)
一个程序使用标准输入输出数据传输流程:
- 服务程序启动后,以阻塞模式从stdin读取数据
- 其他程序向服务程序的stdin写入数据,数据格式通常为JSON-RPC请求
- 服务程序解析读取的json数据做对应的处理
- 服务程序将应答封装为JSON-RPC数据,写入stdout
MCP Server基本工作流程
- AI客户端根据MCP Server获取它所能提供的工具、资源、提示词信息
- 模型根据上下文决定使用哪些工具或资源
- MCP客户端根据AI模型决策的工具向MCP Server发送对应工具或资源请求
- MCP Server处理请求
- MCP Server返回结果给客户端
- AI模型把返回的结果应用在上下文中
创建一个查询DNS的MCP Server
这个MCP Server因为是本地使用使用stdio传输就可以
创建工程
cargo new github-dns-mcp-server
,创建一个工程目录和默认的main.rs文件添加工程依赖
1
2
3
4
5
6
7[dependencies]
tokio = { version = "1", features = ["full"] }
rmcp = { version = "0.3", features = ["server", "transport-io"] }
serde = { version = "1", features = ["derive"] }
reqwest = "0.12"
anyhow = "1.0"
schemars = "1.0"tokio
处理异步操作rmcp
MCP官方提供的Rust Model Context Protocol SDKserde
序列化和反序列化MCP协议传输的 JSON-RPC (JSON Remote Procedure Call) 数据reqwest
创建给 DNS lookup API (HackerTarget)的HTTP请求anyhow
用来错误处理schemars
用来生成 JSON schema
实现服务功能
新建一个dns_mcp.rs
文件实现主要逻辑功能,具体宏的说明
1 | use rmcp::{ |
main.rs中启动服务
1 | use anyhow::Result; |
使用MCP Server
Cline中配置
vs code的Cline插件配置文件C:\Users\xxx\AppData\Roaming\Code\User\globalStorage\saoudrizwan.claude-dev\settings\cline_mcp_settings.json
中配置MCP Server,因为就是普通的可执行程序,所以执行的命令就是编译生成的exe文件。
开发过程中直接用编译出来的路径,实际应用可以放在系统环境变量中,例如使用cargo install --path .
工程会编译release版本,并把编译出来的exe文件拷贝到Cargo的bin目录中,这样系统任何地方都可以访问。
1 | { |
实际应用
在Cline的聊天框中询问 “tell me the dns records for github”
目前我用的Qwen3-Coder模型会如下回答:
The user wants to find the DNS records for github. I can see from the environment details that there’s a connected MCP server called “github-dns” that has a tool called “dns_lookup” which can perform DNS lookup for a domain name. This is exactly what I need.
The dns_lookup tool requires a “domain” parameter, and I can infer that “github” refers to “github.com” which is the standard domain for GitHub.
I’m currently in PLAN MODE, so I need to use the plan_mode_respond tool to acknowledge this request and explain my approach.
I can help you find the DNS records for GitHub using the available MCP tools. I see there’s a connected DNS lookup tool that can perform this task.
My approach will be:
- Use the dns_lookup tool from the github-dns MCP server to look up the DNS records for github.com
- Present the results to you
Since you asked for DNS records for “github”, I’ll assume you mean “github.com” - is that correct? Or did you have a different GitHub-related domain in mind?
在点击了Act模式后,Cline会运行MCP Server配置的github-dns-mcp-server.exe。通过Process Explorer可以看到github-dns-mcp-server.exe的父进程是VS Code。