Git工具库

一个基于模型上下文协议(MCP)实现的Git操作工具库,提供包括状态检查、分支管理、提交历史查询等核心Git功能。

  • by lileeei
  • Rust
  • 本地部署
  • 8 星标
  • 更新于
在 GitHub 查看 lileeei/mcp-git-tools

使用说明

MCP Git工具

模型上下文协议(MCP)的Git工具实现。

中文文档

特性

此库提供了一组可以通过模型上下文协议调用的Git操作:

  • git_status -获取存储库的状态
  • git_branches -列出分支机构信息
  • git_log -使用灵活的过滤选项(按时间、作者、分支、计数)获取提交历史记录
  • git_commit -创建新提交
  • git_pull -从远程拉取更改
  • git_push -将更改推送到远程
  • git_diff -查看文件差异
  • git_add -将文件内容添加到暂存区
  • git_reset -将暂存区或工作树重置为指定状态
  • git_worktree_list -列出所有工作树
  • git_worktree_add -创建新工作台
  • git_worktree_remove -拆下工作台
  • git_worktree_prune -修剪过期的工作树文件

安装

# Clone the repository
git clone https://gitcode.com/lileeei/mcp-git-tools.git

# Navigate to the directory
cd mcp-git-tools

# Build
cargo build --release

用法

作为独立服务器运行

cargo run --release --bin mcp-git-server

这将启动一个MCP服务器,该服务器通过标准输入/输出与客户端交互。

使用MCP检查员进行测试

  1. 构建服务器:
cargo build --release
  1. 启动服务器:
./target/release/mcp-git-server
  1. 使用MCP检查器测试工具:
    • 下载MCP检查器:https://github.com/modelcontextprotocol/inspector
    • 点击“添加服务器”
    • 输入: /path/to/mcp-git-server/target/release/mcp-git-server
    • 点击“添加”
    • 使用检查器界面测试每个工具

建筑

该项目使用具有干净架构的现代RMCP SDK(v0.14.0):

核心组件

GitToolsHandler

  • 所有Git操作的中央处理程序结构
  • 实现 ServerHandler RMCP SDK的特性
  • 包含每个Git操作的方法

服务器处理程序实现

  • get_info() -提供服务器元数据和功能
  • list_tools() -列出所有可用工具及其模式
  • call_tool() -将工具调用路由到适当的处理程序方法

工具实现模式

每个工具都作为一种方法实现 GitToolsHandler:

impl GitToolsHandler {
    #[tool(description = "Get the status of a git repository")]
    pub async fn git_status(
        &self,
        repo_path: String,
    ) -> Result<CallToolResult> {
        // Implementation
    }
}

参数处理

所有工具参数都使用派生宏自动生成模式:

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct GitStatusParams {
    repo_path: String,
}

这提供了:

  • 自动生成JSON模式
  • 类型安全参数解析
  • 无手动序列化/反序列化代码

运输

使用RMCP stdio() 用于标准输入/输出通信的传输。

工具详细信息

git_status

获取存储库的状态。

参数:

  • repo_path -Git存储库的路径

退货:

{
  "status": ["M file1.txt", "?? file2.txt"],
  "is_clean": false
}

git_牧场

列出所有分支。

参数:

  • repo_path -Git存储库的路径

退货:

{
  "branches": ["* main", "develop", "feature/new-feature"],
  "current": "main"
}

git_log

使用灵活的过滤选项获取提交历史记录。

参数:

  • repo_path -Git存储库路径
  • max_count -(可选)要返回的最大提交数
  • branch -(可选)分支机构名称
  • since -(可选)开始日期(例如,“2023-01-01”、“1周前”、“昨天”)
  • until -(可选)结束日期(例如,“2023-01-31”、“今天”)
  • author -(可选)按特定作者筛选(匹配姓名或电子邮件)

退货:

{
  "commits": [
    {
      "hash": "abcd1234",
      "author": "User Name",
      "author_date": "Mon Aug 1 10:00:00 2023 +0800",
      "message_title": "Initial commit"
    }
  ]
}

示例:

  • 获取最近10次提交: {"repo_path": ".", "max_count": 10}
  • 获取上周的提交: {"repo_path": ".", "since": "1 week ago"}
  • 按作者获取提交: {"repo_path": ".", "author": "John"}
  • 从某个时间范围获取提交: {"repo_path": ".", "since": "2023-01-01", "until": "2023-12-31"}

git_提交

创建一个新的commit。

参数:

  • repo_path -Git存储库的路径
  • message -提交消息
  • all -(可选)是否自动暂存修改后的文件

退货:

{
  "success": true,
  "hash": "abcd1234",
  "message": "feat: Add new feature",
  "output": "[main abcd1234] feat: Add new feature\n 1 file changed, 10 insertions(+), 2 deletions(-)"
}

git_pull

从远程拉取更改。

参数:

  • repo_path -Git存储库的路径
  • remote -(可选)远程名称,默认为“origin”
  • branch -(可选)分支机构名称

退货:

{
  "success": true,
  "remote": "origin",
  "output": "Updating abcd1234..efgh5678\nFast-forward\n file1.txt | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)"
}

git_push

将更改推送到远程。

参数:

  • repo_path -Git存储库的路径
  • remote -(可选)远程名称,默认为“origin”
  • branch -(可选)分支机构名称
  • force -(可选)是否强制推动

退货:

{
  "success": true,
  "remote": "origin",
  "output": "To github.com:user/repo.git\n   abcd1234..efgh5678  main -> main"
}

使用 git_diff

查看文件差异。

参数:

  • repo_path -Git存储库的路径
  • path -(可选)文件或目录的路径
  • staged -(可选)是否显示阶段性更改
  • commit -(可选)承诺进行比较

退货:

{
  "changes": "diff --git a/file.txt b/file.txt\nindex 1234567..abcdefg 100644\n--- a/file.txt\n+++ b/file.txt\n@@ -1,3 +1,4 @@\n Line 1\nLine 2\nLine 3\n+New line\nLine 4"
}

git_add

将文件内容添加到暂存区。

参数:

  • repo_path -Git存储库的路径
  • path -要添加的路径或要匹配的模式。使用“。“对于所有文件。
  • update -(可选)是否更新,而不是添加
  • all -(可选)是否添加所有更改,包括未跟踪的文件

退货:

{
  "success": true,
  "message": "Files staged successfully",
  "status": ["M file1.txt", "A file2.txt"]
}

git_reset

将暂存区或工作树重置为指定状态。

参数:

  • repo_path -Git存储库的路径
  • path -要重置的路径或要匹配的模式。使用“。“对于所有文件。
  • hard -(可选)是否执行硬重置(警告:丢弃所有本地更改)
  • target -(可选)要重置的提交或分支(默认为HEAD)

退货:

{
  "success": true,
  "message": "Files unstaged successfully",
  "status": ["?? file1.txt", "?? file2.txt"]
}

git_worktree_list

列出存储库中的所有工作树。

参数:

  • repo_path -Git存储库的路径

退货:

{
  "worktrees": [
    {
      "id": "worktree-name",
      "path": "/path/to/worktree",
      "locked": false,
      "lock_reason": null
    }
  ]
}

git_worktree_add

创建新的工作台。

参数:

  • repo_path -Git存储库的路径
  • path -新工作台的路径
  • branch -(可选)要创建的分支名称
  • commit_ref -(可选)提交引用以进行签出
  • checkout -(可选)是否签出文件,默认为true

退货:

{
  "success": true,
  "path": "/path/to/new/worktree",
  "branch": "feature-branch",
  "output": "Preparing worktree (checking out 'feature-branch')"
}

git_worktree_remove

拆下工作台。

参数:

  • repo_path -Git存储库的路径
  • path -要删除的工作台路径
  • force -(可选)即使工作台很脏,也要强行拆卸

退货:

{
  "success": true,
  "path": "/path/to/worktree",
  "force": false,
  "output": "Removing worktrees/feature-branch"
}

git_worktree_pune

修剪$GIT_DIR/工作树中的工作树文件。

参数:

  • repo_path -Git存储库的路径
  • verbose -(可选)报告所有删除
  • dry_run -(可选)不删除,仅显示

退货:

{
  "success": true,
  "verbose": false,
  "dry_run": false,
  "output": "Pruning worktrees/feature-branch"
}

发展

项目结构

mcp-git-tools/
├── Cargo.toml          # Dependencies and project configuration
├── src/
│   ├── lib.rs          # Core library with GitToolsHandler
│   └── bin/
│       └── server.rs   # Server entry point
├── tests/
│   └── lib_tests.rs   # Unit tests

建筑

# Debug build
cargo build

# Release build
cargo build --release

# Run tests
cargo test

# Run server
cargo run --release --bin mcp-git-server

添加新工具

要添加新的Git工具,请执行以下操作:

  1. 在中添加参数结构体 src/lib.rs:
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct GitMyToolParams {
    repo_path: String,
    // other parameters...
}
  1. 在中添加该方法 GitToolsHandler impl块:
#[tool(description = "Description of your tool")]
pub async fn git_my_tool(
    &self,
    repo_path: String,
    // other parameters...
) -> Result<CallToolResult> {
    // Implementation
}
  1. 更新 list_tools() 将新工具包含在返回列表中的方法

依赖项

  • rmcp v0.14.0-官方RMCP SDK
  • tokio -异步运行时
  • serde -序列化框架
  • serde_json -JSON实现
  • anyhow -错误处理
  • tracing -日志框架
  • tempfile -测试工具

许可证

MIT许可证