Rust项目管理工具

一个基于MCP协议的Rust项目管理服务器,提供全面的cargo命令行工具集成,用于构建、测试、依赖管理和项目维护。

  • by SignalWhisperer
  • Rust
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 SignalWhisperer/cargo-mcp

使用说明

货运MCP服务器

一个模型上下文协议(MCP)服务器,它提供了使用 cargo 命令行工具。

免责声明

整个存储库是在生成式人工智能的帮助下构建的,在整个开发过程中由人类监督。代码、文档和架构是在人工指导和人工智能实现之间协同开发的。

建筑

为了便于维护,服务器被组织成几个模块:

src/
├── main.rs              # Entry point
├── lib.rs               # Library exports
├── server.rs            # MCP server implementation
├── tools/
│   ├── mod.rs           # Tool module exports
│   ├── definitions.rs   # Main tool registry
│   ├── schemas.rs       # Common schema utilities
│   ├── build_tools.rs   # Build-related tools (check, build, clippy, fmt)
│   ├── execution_tools.rs # Execution tools (run, test, bench)
│   ├── dependency_tools.rs # Dependency management (add, remove, update, tree)
│   ├── project_tools.rs # Project management (new, init, clean, doc)
│   ├── registry_tools.rs # Registry operations (search, info, install, uninstall)
│   ├── utility_tools.rs # Utility tools (metadata, version)
│   └── executor.rs      # Command execution logic
├── types.rs             # Data structures and types
└── error.rs             # Error handling

特性

此MCP服务器提供按类别组织的全面货运工具:

构建工具

  • 检查 -分析代码而不生成可执行文件
  • 构建 -编译当前包
  • clippy -在当前包上运行Clippy lints
  • fmt -使用rustfmt格式化Rust代码

执行工具

  • 跑 -运行本地包的二进制文件或示例
  • 测试 -运行单元和集成测试
  • 长凳 -运行基准测试

依赖管理

  • 添加 -向Cargo.toml清单文件添加依赖项
  • 移除 -从Cargo.toml清单文件中删除依赖项
  • 更新 -更新本地锁文件中记录的依赖关系
  • 树 -显示依赖关系图的树形可视化

项目管理

  • 新 -在以下位置创建新的货物包
  • 初始化 -在现有目录中创建新的货物包
  • 清洁 -删除货物过去产生的工件
  • 文档 -构建此包及其依赖项的文档

注册表操作

  • 搜索 -在crates.io中搜索包裹
  • 信息 -在注册表中显示有关包的信息
  • 安装 -安装Rust二进制文件
  • 卸载 -删除Rust二进制文件

实用工具

  • 元数据 -以机器可读格式输出包的已解析依赖关系
  • 版本 -显示货物和锈迹的版本信息

安装

  1. 克隆此存储库
  2. 构建项目:
    cargo build --release
    
  3. 配置您的MCP客户端以使用服务器。配置示例:
    {
      "mcpServers": {
        "cargo-mcp": {
          "command": "/path/to/cargo-mcp/target/release/cargo-mcp",
          "args": []
        }
      }
    }
    

用法

服务器通过JSON-RPC在stdin/stdout上进行通信。它旨在与MCP兼容的客户端一起使用。

工具调用示例

检查项目

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "check",
    "arguments": {
      "working_directory": "/path/to/project",
      "all_targets": true
    }
  }
}

内置发布模式

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "build",
    "arguments": {
      "working_directory": "/path/to/project",
      "release": true
    }
  }
}

运行特定的二进制文件

{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "run",
    "arguments": {
      "working_directory": "/path/to/project",
      "bin": "my-binary"
    }
  }
}

格式代码

{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "tools/call",
  "params": {
    "name": "fmt",
    "arguments": {
      "working_directory": "/path/to/project"
    }
  }
}

运行Clippy并修复

{
  "jsonrpc": "2.0",
  "id": 5,
  "method": "tools/call",
  "params": {
    "name": "clippy",
    "arguments": {
      "working_directory": "/path/to/project",
      "fix": true,
      "allow_dirty": true
    }
  }
}

添加依赖项

{
  "jsonrpc": "2.0",
  "id": 6,
  "method": "tools/call",
  "params": {
    "name": "add",
    "arguments": {
      "working_directory": "/path/to/project",
      "dependency": "serde",
      "features": ["derive"]
    }
  }
}

搜索包裹

{
  "jsonrpc": "2.0",
  "id": 7,
  "method": "tools/call",
  "params": {
    "name": "search",
    "arguments": {
      "working_directory": "/path/to/project",
      "query": "tokio",
      "limit": 5
    }
  }
}

运行测试

{
  "jsonrpc": "2.0",
  "id": 8,
  "method": "tools/call",
  "params": {
    "name": "test",
    "arguments": {
      "working_directory": "/path/to/project",
      "release": true
    }
  }
}

刀具参数

公共参数

大多数工具支持以下常见参数:

  • working_directory (string,可选)-运行货物的工作目录
  • package (字符串,可选)-要操作的包
  • features (字符串数组,可选)-要激活的功能
  • all_features (布尔值,可选)-激活所有可用功能
  • no_default_features (布尔值,可选)-不激活默认功能
  • release (布尔值,可选)-使用发布配置文件
  • target (字符串,可选)-目标三元组

目标选择参数

用于检查、构建、剪切、测试和台架:

  • lib (boolean,可选)-仅此包的库
  • bin (string,可选)-仅指定的二进制文件
  • bins (布尔值,可选)-所有二进制文件
  • example (string,可选)-仅指定示例
  • examples (布尔值,可选)-所有示例
  • test (字符串,可选)-仅指定的测试目标
  • tests (布尔值,可选)-所有测试
  • bench (string,可选)-仅指定工作台目标
  • benches (布尔值,可选)-所有长凳
  • all_targets (布尔值,可选)-所有目标

构建特定参数

检查和构建:

  • profile (字符串,可选)-使用指定的配置文件构建工件
  • message_format (string,可选)-错误格式(human、short、json等)
  • workspace (boolean,可选)-在工作区中构建所有包
  • exclude (字符串数组,可选)-从操作中排除包

Clippy特定参数

  • fix (布尔值,可选)-自动应用lint建议
  • allow_dirty (boolean,可选)-即使工作目录发生更改,也能修复代码
  • allow_staged (boolean,可选)-即使工作目录有阶段性更改,也能修复代码

测试特定参数

  • exact (boolean,可选)-完全匹配过滤器,而不是按子字符串匹配
  • ignored (boolean,可选)-运行忽略的测试
  • include_ignored (boolean,可选)-运行忽略和未忽略的测试
  • jobs (整数,可选)-并行作业数
  • nocapture (布尔值,可选)-不捕获stdout/stderr
  • test_threads (整数,可选)-运行测试的线程数

依赖关系管理参数

添加参数

  • dependency (string,必填)-要添加的依赖项
  • dev (boolean,可选)-添加为开发依赖项
  • build (boolean,可选)-添加为构建依赖项
  • optional (boolean,可选)-将依赖关系标记为可选
  • rename (string,可选)-重命名依赖关系
  • path (string,可选)-本地机箱的文件系统路径
  • git (字符串,可选)-Git存储库位置
  • branch (string,可选)-Git分支
  • tag (字符串,可选)-Git标签
  • rev (string,可选)-Git引用
  • default_features (布尔值,可选)-重新启用默认功能
  • registry (字符串,可选)-要使用的注册表

删除参数

  • dependency (string,必填)-要删除的依赖项
  • dev (boolean,可选)-作为开发依赖项删除
  • build (boolean,可选)-删除构建依赖项

更新参数

  • aggressive (布尔值,可选)-强制更新所有依赖项
  • dry_run (boolean,可选)-实际上不写入锁文件
  • precise (字符串,可选)-更新到此版本
  • workspace (boolean,可选)-更新工作区中的所有包

树参数

  • duplicates (boolean,可选)-仅显示多个版本的依赖关系
  • edges (字符串,可选)-要显示的依赖关系类型
  • format (string,可选)-打印依赖项的格式字符串
  • invert (字符串数组,可选)-反转依赖关系图
  • no_dedupe (布尔值,可选)-重复共享依赖关系
  • prefix (字符串,可选)-如何显示树
  • prune (字符串数组,可选)-从显示中修剪包
  • depth (整数,可选)-最大显示深度
  • charset (字符串,可选)-字符集(utf8,ascii)

项目管理参数

new/init参数

  • path (string,new需要)-新包的路径
  • name (字符串,可选)-包名称
  • bin_template (布尔值,可选)-使用二进制模板
  • lib_template (布尔值,可选)-使用库模板
  • edition (字符串,可选)-要设置的版本
  • registry (字符串,可选)-要使用的注册表

doc参数

  • open (布尔值,可选)-在浏览器中打开文档
  • no_deps (boolean,可选)-不为依赖关系构建文档
  • document_private_items (布尔值,可选)-记录私人项目
  • jobs (整数,可选)-并行作业数

注册表参数

搜索参数

  • query (字符串,必填)-搜索查询
  • limit (整数,可选)-限制结果(默认值:10,最大值:100)
  • registry (字符串,可选)-要使用的注册表

信息参数

  • query (字符串,必填)-包名称
  • registry (字符串,可选)-要使用的注册表

安装参数

  • query (字符串,可选)-要安装的包名称
  • version (字符串,可选)-指定版本
  • git_url (字符串,可选)-要从中安装的Git URL
  • branch_install (string,可选)-Git分支
  • tag_install (字符串,可选)-Git标签
  • rev_install (string,可选)-Git提交
  • path_install (字符串,可选)-本地路径
  • bin_install (字符串,可选)-仅安装指定的二进制文件
  • bins_install (boolean,可选)-安装所有二进制文件
  • example_install (字符串,可选)-仅安装指定示例
  • examples_install (布尔值,可选)-安装所有示例
  • force (布尔值,可选)-强制覆盖
  • no_track (布尔值,可选)-不保存跟踪信息
  • locked (布尔值,可选)-断言Cargo.lock不变
  • root (字符串,可选)-安装目录
  • registry (字符串,可选)-要使用的注册表
  • index (字符串,可选)-注册表索引
  • list (boolean,可选)-列出已安装的软件包

卸载参数

  • query (字符串,必填)-要卸载的包名称
  • bin_install (字符串,可选)-仅卸载指定的二进制文件
  • root (字符串,可选)-要卸载的目录

公用工程参数

元数据参数

  • no_deps (boolean,可选)-不获取依赖项
  • format_version (整数,可选)-格式版本

协议支持

此服务器实现MCP协议版本2024-11-05,并支持:

  • initialize -服务器初始化
  • tools/list -列出可用工具
  • tools/call -执行货物指令

发展

为该项目做出贡献:

  1. 克隆存储库
  2. 进行更改
  3. 测试用 cargo check 和 cargo build
  4. 格式化代码 cargo fmt
  5. 运行lints与 cargo clippy

错误处理

当cargo命令失败时,服务器会提供详细的错误消息,包括cargo进程的stdout和stderr输出。