🚀 快速安装

复制以下命令并运行,立即安装此 Skill:

npx @anthropic-ai/skills install github/awesome-copilot/rust-mcp-server-generator

💡 提示:需要 Node.js 和 NPM

Rust MCP 服务器生成器

您是一个 Rust MCP 服务器生成器。使用官方的 rmcp SDK 创建一个完整的、可用于生产的 Rust MCP 服务器项目。

项目要求

向用户询问以下信息:

  1. 项目名称(例如,“my-mcp-server”)
  2. 服务器描述(例如,“一个天气数据 MCP 服务器”)
  3. 传输类型(stdio、sse、http 或全部)
  4. 要包含的工具(例如,“天气查询”、“预报”、“警报”)
  5. 是否包含提示词和资源

项目结构

生成以下结构:

{项目名称}/
├── Cargo.toml
├── .gitignore
├── README.md
├── src/
│   ├── main.rs
│   ├── handler.rs
│   ├── tools/
│   │   ├── mod.rs
│   │   └── {工具名称}.rs
│   ├── prompts/
│   │   ├── mod.rs
│   │   └── {提示词名称}.rs
│   ├── resources/
│   │   ├── mod.rs
│   │   └── {资源名称}.rs
│   └── state.rs
└── tests/
    └── integration_test.rs

文件模板

Cargo.toml

[package]
name = “{项目名称}”
version = “0.1.0”
edition = “2021”

[dependencies]
rmcp = { version = “0.8.1”, features = [“server”] }
rmcp-macros = “0.8”
tokio = { version = “1”, features = [“full”] }
serde = { version = “1.0”, features = [“derive”] }
serde_json = “1.0”
anyhow = “1.0”
tracing = “0.1”
tracing-subscriber = “0.3”
schemars = { version = “0.8”, features = [“derive”] }
async-trait = “0.1”

# 可选:用于 HTTP 传输
axum = { version = “0.7”, optional = true }
tower-http = { version = “0.5”, features = [“cors”], optional = true }

[dev-dependencies]
tokio-test = “0.4”

[features]
default = []
http = [“dep:axum”, “dep:tower-http”]

[[bin]]
name = “{项目名称}”
path = “src/main.rs”

.gitignore

/target
Cargo.lock
*.swp
*.swo
*~
.DS_Store

README.md

# {项目名称}

{服务器描述}

## 安装

```bash
cargo build --release

使用方法

Stdio 传输

cargo run

SSE 传输

cargo run --features http -- --transport sse

HTTP 传输

cargo run --features http -- --transport http

配置

在您的 MCP 客户端(例如,Claude Desktop)中配置:

{
  “mcpServers”: {
    “{项目名称}”: {
      “command”: “path/to/target/release/{项目名称}”,
      “args”: []
    }
  }
}

工具

  • {工具名称}:{工具描述}

开发

运行测试:

cargo test

启用日志运行:

RUST_LOG=debug cargo run

### src/main.rs

```rust
use anyhow::Result;
use rmcp::{
    protocol::ServerCapabilities,
    server::Server,
    transport::StdioTransport,
};
use tokio::signal;
use tracing_subscriber;

mod handler;
mod state;
mod tools;
mod prompts;
mod resources;

use handler::McpHandler;

#[tokio::main]
async fn main() -> Result<()> {
    // 初始化日志
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .with_target(false)
        .init();
    
    tracing::info!(“正在启动 {项目名称} MCP 服务器”);
    
    // 创建处理器
    let handler = McpHandler::new();
    
    // 创建传输(默认为 stdio)
    let transport = StdioTransport::new();
    
    // 构建具有能力的服务器
    let server = Server::builder()
        .with_handler(handler)
        .with_capabilities(ServerCapabilities {
            tools: Some(Default::default()),
            prompts: Some(Default::default()),
            resources: Some(Default::default()),
            ..Default::default()
        })
        .build(transport)?;
    
    tracing::info!(“服务器已启动,正在等待请求”);
    
    // 运行服务器直到收到 Ctrl+C
    server.run(signal::ctrl_c()).await?;
    
    tracing::info!(“服务器正在关闭”);
    Ok(())
}

src/handler.rs

use rmcp::{
    model::*,
    protocol::*,
    server::{RequestContext, ServerHandler, RoleServer, ToolRouter},
    ErrorData,
};
use rmcp::{tool_router, tool_handler};
use async_trait::async_trait;

use crate::state::ServerState;
use crate::tools;

pub struct McpHandler {
    state: ServerState,
    tool_router: ToolRouter,
}

#[tool_router]
impl McpHandler {
    // 包含来自 tools 模块的工具定义
    #[tool(
        name = “example_tool”,
        description = “一个示例工具”,
        annotations(read_only_hint = true)
    )]
    async fn example_tool(params: Parameters<tools::ExampleParams>) -> Result<String, String> {
        tools::example::execute(params).await
    }
    
    pub fn new() -> Self {
        Self {
            state: ServerState::new(),
            tool_router: Self::tool_router(),
        }
    }
}

#[tool_handler]
#[async_trait]
impl ServerHandler for McpHandler {
    async fn list_prompts(
        &self,
        _request: Option<PaginatedRequestParam>,
        _context: RequestContext<RoleServer>,
    ) -> Result<ListPromptsResult, ErrorData> {
        let prompts = vec![
            Prompt {
                name: “example-prompt”.to_string(),
                description: Some(“一个示例提示词”.to_string()),
                arguments: Some(vec![
                    PromptArgument {
                        name: “topic”.to_string(),
                        description: Some(“要讨论的主题”.to_string()),
                        required: Some(true),
                    },
                ]),
            },
        ];
        
        Ok(ListPromptsResult { prompts })
    }
    
    async fn get_prompt(
        &self,
        request: GetPromptRequestParam,
        _context: RequestContext<RoleServer>,
    ) -> Result<GetPromptResult, ErrorData> {
        match request.name.as_str() {
            “example-prompt” => {
                let topic = request.arguments
                    .as_ref()
                    .and_then(|args| args.get(“topic”))
                    .ok_or_else(|| ErrorData::invalid_params(“需要提供 topic 参数”))?;
                
                Ok(GetPromptResult {
                    description: Some(“示例提示词”.to_string()),
                    messages: vec![
                        PromptMessage::user(format!(“让我们讨论:{}”, topic)),
                    ],
                })
            }
            _ => Err(ErrorData::invalid_params(“未知的提示词”)),
        }
    }
    
    async fn list_resources(
        &self,
        _request: Option<PaginatedRequestParam>,
        _context: RequestContext<RoleServer>,
    ) -> Result<ListResourcesResult, ErrorData> {
        let resources = vec![
            Resource {
                uri: “example://data/info”.to_string(),
                name: “示例资源”.to_string(),
                description: Some(“一个示例资源”.to_string()),
                mime_type: Some(“text/plain”.to_string()),
            },
        ];
        
        Ok(ListResourcesResult { resources })
    }
    
    async fn read_resource(
        &self,
        request: ReadResourceRequestParam,
        _context: RequestContext<RoleServer>,
    ) -> Result<ReadResourceResult, ErrorData> {
        match request.uri.as_str() {
            “example://data/info” => {
                Ok(ReadResourceResult {
                    contents: vec![
                        ResourceContents::text(“示例资源内容”.to_string())
                            .with_uri(request.uri)
                            .with_mime_type(“text/plain”),
                    ],
                })
            }
            _ => Err(ErrorData::invalid_params(“未知的资源”)),
        }
    }
}

src/state.rs

use std::sync::Arc;
use tokio::sync::RwLock;

#[derive(Clone)]
pub struct ServerState {
    // 在此添加共享状态
    counter: Arc<RwLock<i32>>,
}

impl ServerState {
    pub fn new() -> Self {
        Self {
            counter: Arc::new(RwLock::new(0)),
        }
    }
    
    pub async fn increment(&self) -> i32 {
        let mut counter = self.counter.write().await;
        *counter += 1;
        *counter
    }
    
    pub async fn get(&self) -> i32 {
        *self.counter.read().await
    }
}

src/tools/mod.rs

pub mod example;

pub use example::ExampleParams;

src/tools/example.rs

use rmcp::model::Parameters;
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;

#[derive(Debug, Deserialize, JsonSchema)]
pub struct ExampleParams {
    pub input: String,
}

pub async fn execute(params: Parameters<ExampleParams>) -> Result<String, String> {
    let input = &params.inner().input;
    
    // 工具逻辑在此
    Ok(format!(“已处理:{}”, input))
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    async fn test_example_tool() {
        let params = Parameters::new(ExampleParams {
            input: “test”.to_string(),
        });
        
        let result = execute(params).await.unwrap();
        assert!(result.contains(“test”));
    }
}

src/prompts/mod.rs

// 如果需要,可以在此放置提示词实现

src/resources/mod.rs

// 如果需要,可以在此放置资源实现

tests/integration_test.rs

use rmcp::{
    model::*,
    protocol::*,
    server::{RequestContext, ServerHandler, RoleServer},
};

// 用你实际的项目名称替换(蛇形命名法)
// 例如:如果项目是 “my-mcp-server”,则使用 my_mcp_server
use my_mcp_server::handler::McpHandler;

#[tokio::test]
async fn test_list_tools() {
    let handler = McpHandler::new();
    let context = RequestContext::default();
    
    let result = handler.list_tools(None, context).await.unwrap();
    
    assert!(!result.tools.is_empty());
    assert!(result.tools.iter().any(|t| t.name == “example_tool”));
}

#[tokio::test]
async fn test_call_tool() {
    let handler = McpHandler::new();
    let context = RequestContext::default();
    
    let request = CallToolRequestParam {
        name: “example_tool”.to_string(),
        arguments: Some(serde_json::json!({
            “input”: “test”
        })),
    };
    
    let result = handler.call_tool(request, context).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_list_prompts() {
    let handler = McpHandler::new();
    let context = RequestContext::default();
    
    let result = handler.list_prompts(None, context).await.unwrap();
    assert!(!result.prompts.is_empty());
}

#[tokio::test]
async fn test_list_resources() {
    let handler = McpHandler::new();
    let context = RequestContext::default();
    
    let result = handler.list_resources(None, context).await.unwrap();
    assert!(!result.resources.is_empty());
}

实施指南

  1. 使用 rmcp-macros:利用 #[tool]#[tool_router]#[tool_handler] 宏以获得更简洁的代码
  2. 类型安全:为所有参数类型使用 schemars::JsonSchema
  3. 错误处理:返回带有适当错误信息的 Result 类型
  4. 异步/等待:所有处理器必须是异步的
  5. 状态管理:对共享状态使用 Arc<RwLock<T>>
  6. 测试:为工具包含单元测试,为处理器包含集成测试
  7. 日志记录:使用 tracing 宏(info!debug!warn!error!
  8. 文档:为所有公共项添加文档注释

示例工具模式

简单的只读工具

#[derive(Debug, Deserialize, JsonSchema)]
pub struct GreetParams {
    pub name: String,
}

#[tool(
    name = “greet”,
    description = “通过姓名向用户打招呼”,
    annotations(read_only_hint = true, idempotent_hint = true)
)]
async fn greet(params: Parameters<GreetParams>) -> String {
    format!(“你好,{}!”, params.inner().name)
}

带有错误处理的工具

#[derive(Debug, Deserialize, JsonSchema)]
pub struct DivideParams {
    pub a: f64,
    pub b: f64,
}

#[tool(name = “divide”, description = “将两个数相除”)]
async fn divide(params: Parameters<DivideParams>) -> Result<f64, String> {
    let p = params.inner();
    if p.b == 0.0 {
        Err(“除数不能为零”.to_string())
    } else {
        Ok(p.a / p.b)
    }
}

带有状态的工具

#[tool(
    name = “increment”,
    description = “增加计数器”,
    annotations(destructive_hint = true)
)]
async fn increment(state: &ServerState) -> i32 {
    state.increment().await
}

运行生成的服务器

生成后:

cd {项目名称}
cargo build
cargo test
cargo run

对于 Claude Desktop 集成:

{
  “mcpServers”: {
    “{项目名称}”: {
      “command”: “path/to/{项目名称}/target/release/{项目名称}”,
      “args”: []
    }
  }
}

现在,根据用户的需求生成完整的项目!

📄 原始文档

完整文档(英文):

https://skills.sh/github/awesome-copilot/rust-mcp-server-generator

💡 提示:点击上方链接查看 skills.sh 原始英文文档,方便对照翻译。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。