Skip to content

一个使用 Go 语言实现的模型上下文协议(MCP)服务器,为大型语言模型提供 Git 仓库交互和自动化功能。支持状态查询、差异对比、提交管理、分支操作等完整的 Git 工作流,可与 Claude Desktop 等 AI 工具无缝集成。

License

Notifications You must be signed in to change notification settings

pengcunfu/go-mcp-git

Repository files navigation

go-mcp-git: Go 语言实现的 Git MCP 服务器

English | 中文

概述

一个使用 Go 语言实现的模型上下文协议(Model Context Protocol)服务器,用于 Git 仓库交互和自动化。该服务器提供工具,让大型语言模型能够读取、搜索和操作 Git 仓库。

这是原始 Python mcp-server-git 的 Go 语言移植版本,提供了更好的性能和更简单的部署。

工具列表

基础操作

  1. git_status - 显示工作树状态
  2. git_init - 新增 初始化新的Git仓库
  3. git_add - 将文件内容添加到暂存区
  4. git_commit - 将更改记录到仓库
  5. git_reset - 取消暂存所有已暂存的更改

分支管理

  1. git_branch - 列出 Git 分支
  2. git_create_branch - 创建新分支
  3. git_checkout - 切换分支

差异和日志

  1. git_diff_unstaged - 显示工作目录中尚未暂存的更改
  2. git_diff_staged - 显示已暂存待提交的更改
  3. git_diff - 显示分支或提交之间的差异
  4. git_log - 显示提交日志,支持可选的日期过滤
  5. git_show - 显示提交的内容

远程操作

  1. git_push - 新增 推送更改到远程仓库
  2. git_list_repositories - 新增 列出目录中的Git仓库

标签管理

  1. git_create_tag - 新增 创建Git标签(支持轻量级和注释标签)
  2. git_delete_tag - 新增 删除Git标签
  3. git_list_tags - 新增 列出Git标签(支持模式过滤)
  4. git_push_tags - 新增 推送标签到远程仓库

高级功能

  1. git_raw_command - 新增 直接执行原始Git命令(绕过shell包装问题)

安装

使用 Go 安装

go install github.com/pengcunfu/go-mcp-git@latest

从源码构建

git clone https://github.com/pengcunfu/go-mcp-git.git
cd go-mcp-git
go build -o go-mcp-git ./cmd/server

使用方法

基本用法

go-mcp-git --repository /path/to/git/repo

配置用户信息

go-mcp-git --user-name "pengcunfu" --user-email "3173484026@qq.com"

完整参数

go-mcp-git --repository /path/to/git/repo --user-name "pengcunfu" --user-email "3173484026@qq.com" --verbose

命令行参数说明

  • --repository, -r: 指定Git仓库路径(可选,支持自动检测)
  • --user-name, -u: 设置Git提交时使用的用户名
  • --user-email, -e: 设置Git提交时使用的邮箱地址
  • --verbose, -v: 启用详细日志输出(可重复使用增加详细程度)

智能路径解析

repo_path参数现在是可选的! MCP服务器会智能地解析仓库路径:

路径解析优先级

  1. 提供的路径 - 如果指定了repo_path参数
  2. 服务器配置 - 启动时通过--repository参数配置的默认路径
  3. 自动检测 - 从当前工作目录向上查找Git仓库
  4. 当前目录 - 最后回退到当前工作目录

支持的路径格式

// 绝对路径
{"repo_path": "/absolute/path/to/repository"}

// 相对路径
{"repo_path": "../parent-repo"}
{"repo_path": "./current-dir"}

// 特殊符号
{"repo_path": "."}     // 当前目录
{"repo_path": ".."}    // 父目录

// 省略参数(自动检测)
{}  // 自动查找Git仓库

使用示例

// 最简单的用法 - 自动检测当前Git仓库
{
  "command": "git status"
}

// 指定相对路径
{
  "repo_path": "../other-project"
}

// 使用服务器配置的默认路径
// 启动: go-mcp-git --repository /path/to/main/repo
{}  // 将使用 /path/to/main/repo

git_raw_command 工具特别说明

git_raw_command 工具是专门为解决在某些环境(如Windsurf IDE)中Git命令被shell包装导致的引号转义问题而设计的。

问题场景: 当执行包含引号的Git命令时,例如:

git tag -a v0.0.1 -m "发布v0.0.1版本 - 初始MCP Git服务器实现"

在PowerShell环境中可能被错误包装为:

Invoke-Expression "git tag -a v0.0.1 -m "发布v0.0.1版本 - 初始MCP Git服务器实现""

这会导致引号转义错误和命令执行失败。

解决方案: 使用 git_raw_command 工具可以直接执行原始Git命令,绕过shell包装:

{
  "repo_path": "/path/to/repository",
  "command": "git tag -a v0.0.1 -m \"发布v0.0.1版本 - 初始MCP Git服务器实现\""
}

支持的命令示例:

  • git tag -a v1.0.0 -m "Release version 1.0.0"
  • git commit --amend -m "Updated commit message"
  • git push origin --tags
  • git config user.name "Your Name"

新增工具使用示例

标签管理

// 创建注释标签
{
  "repo_path": "/path/to/repository",
  "tag_name": "v1.0.0",
  "message": "Release version 1.0.0",
  "annotated": true
}

// 列出所有标签
{
  "repo_path": "/path/to/repository"
}

// 推送特定标签
{
  "repo_path": "/path/to/repository",
  "remote": "origin",
  "tag_name": "v1.0.0"
}

// 推送所有标签
{
  "repo_path": "/path/to/repository",
  "remote": "origin"
}

仓库初始化和推送

// 初始化新仓库
{
  "repo_path": "/path/to/new/repository",
  "bare": false
}

// 推送到远程仓库
{
  "repo_path": "/path/to/repository",
  "remote": "origin",
  "tags": true
}

仓库发现

// 递归搜索Git仓库
{
  "search_path": "/path/to/search",
  "recursive": true
}

配置

与 Claude Desktop 一起使用

方式1:自动检测(推荐)

{
  "mcpServers": {
    "go-mcp-git": {
      "command": "D:\\Tools\\MCP\\go-mcp-git\\go-mcp-git.exe"
    }
  }
}

服务器将自动检测当前工作目录中的Git仓库

方式2:指定默认仓库

{
  "mcpServers": {
    "go-mcp-git": {
      "command": "D:\\Tools\\MCP\\go-mcp-git\\go-mcp-git.exe",
      "args": [
        "--repository",
        "D:\\Projects\\my-main-project"
      ]
    }
  }
}

所有操作默认使用指定的仓库路径

方式3:配置用户信息(推荐)

{
  "mcpServers": {
    "go-mcp-git": {
      "command": "D:\\Tools\\MCP\\go-mcp-git\\go-mcp-git.exe",
      "args": [
        "--user-name", "pengcunfu",
        "--user-email", "3173484026@qq.com"
      ]
    }
  }
}

配置Git提交时使用的用户名和邮箱

方式4:完整配置

{
  "mcpServers": {
    "go-mcp-git": {
      "command": "D:\\Tools\\MCP\\go-mcp-git\\go-mcp-git.exe",
      "args": [
        "--repository", "D:\\Projects\\main-repo",
        "--user-name", "pengcunfu",
        "--user-email", "3173484026@qq.com",
        "--verbose"
      ]
    }
  }
}

完整配置:指定仓库路径、用户信息和详细日志

许可证

本 MCP 服务器采用 Apache 2.0 许可证。

About

一个使用 Go 语言实现的模型上下文协议(MCP)服务器,为大型语言模型提供 Git 仓库交互和自动化功能。支持状态查询、差异对比、提交管理、分支操作等完整的 Git 工作流,可与 Claude Desktop 等 AI 工具无缝集成。

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published