返回市场
mcp-git工具

mcp-git工具

作者:lileeei7 星标更新:2025-05-27

项目介绍

MCP Git Tools

用于模型上下文协议(MCP)的Git工具集成库。

中文文档

特性

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

  • git_status - 获取仓库的状态
  • git_branches - 列出分支信息
  • git_log - 获取提交历史
  • git_time_filtered_log - 获取特定时间范围内的提交
  • git_commit - 创建新的提交
  • git_pull - 从远程拉取更改
  • git_push - 将更改推送到远程
  • git_diff - 查看文件差异
  • git_add - 将文件内容添加到暂存区
  • git_reset - 将暂存区或工作树重置到指定状态

安装

# 克隆仓库
git clone https://github.com/lileeei/mcp-git-tools.git

# 进入目录
cd mcp-git-tools

# 构建
cargo build

使用

作为独立服务器运行

cargo run --bin mcp-git-server

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

在MCP客户端中使用

use mcp_client::{
    client::{ClientCapabilities, ClientInfo, McpClient},
    StdioTransport, Transport, McpService,
};
use std::collections::HashMap;
use std::time::Duration;

// 创建到Git工具服务器的连接
let transport = StdioTransport::new(
    "path/to/mcp-git-server", 
    vec![], 
    HashMap::new()
);

// 启动传输
let handle = transport.start().await?;
let service = McpService::with_timeout(handle, Duration::from_secs(10));
let mut client = McpClient::new(service);

// 初始化客户端
client.initialize(
    ClientInfo {
        name: "my-client".into(),
        version: "1.0.0".into(),
    },
    ClientCapabilities::default(),
).await?;

// 调用git_status工具
let status = client
    .call_tool("git_status", serde_json::json!({ "repo_path": "/path/to/repo" }))
    .await?;

println!("Git状态: {:?}", status);

集成到自己的MCP服务器

use mcp_git_tools::register_git_tools;
use mcp_server::McpServerBuilder;

// 创建服务器
let mut builder = McpServerBuilder::new("my-server", "1.0.0");

// 注册Git工具
register_git_tools(&mut builder);

// 添加其他工具...

// 构建服务器
let server = builder.build();

工具详情

git_status

获取仓库的状态。

参数:

  • repo_path - Git仓库路径

返回值:

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

git_branches

列出所有分支。

参数:

  • repo_path - Git仓库路径

返回值:

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

git_log

获取提交历史。

参数:

  • repo_path - Git仓库路径
  • max_count - (可选)要返回的最大提交数
  • branch - (可选)分支名称

返回值:

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

git_time_filtered_log

获取特定时间范围内的提交,可选地按作者和分支过滤。

参数:

  • repo_path - Git仓库路径
  • since - 开始日期(例如:"2023-01-01","1 week ago","yesterday")
  • until - (可选)结束日期(例如:"2023-01-31","today")
  • author - (可选)按特定作者过滤
  • branch - (可选)分支名称

返回值:

{
  "commits": [
    {
      "hash": "abcd1234",
      "author": "User Name",
      "date": "Mon Aug 1 10:00:00 2023 +0800",
      "message": "Initial commit"
    }
  ],
  "filters": {
    "since": "1 week ago",
    "until": "today",
    "author": "User Name",
    "branch": "main"
  }
}

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 - (可选)要比较的提交

返回值:

{
  "diff": "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\n Line 2\n+New line\n Line 3"
}

git_add

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

参数:

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

返回值:

{
  "success": true,
  "message": "文件已成功暂存",
  "status": ["M file1.txt", "A file2.txt"]
}

git_reset

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

参数:

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

返回值:

{
  "success": true,
  "message": "文件已成功取消暂存",
  "status": ["?? file1.txt", "?? file2.txt"]
}

许可证

MIT许可证