English | 中文
一个使用 Go 语言实现的模型上下文协议(Model Context Protocol)服务器,用于 Git 仓库交互和自动化。该服务器提供工具,让大型语言模型能够读取、搜索和操作 Git 仓库。
这是原始 Python mcp-server-git 的 Go 语言移植版本,提供了更好的性能和更简单的部署。
git_status- 显示工作树状态git_init- 新增 初始化新的Git仓库git_add- 将文件内容添加到暂存区git_commit- 将更改记录到仓库git_reset- 取消暂存所有已暂存的更改
git_branch- 列出 Git 分支git_create_branch- 创建新分支git_checkout- 切换分支
git_diff_unstaged- 显示工作目录中尚未暂存的更改git_diff_staged- 显示已暂存待提交的更改git_diff- 显示分支或提交之间的差异git_log- 显示提交日志,支持可选的日期过滤git_show- 显示提交的内容
git_push- 新增 推送更改到远程仓库git_list_repositories- 新增 列出目录中的Git仓库
git_create_tag- 新增 创建Git标签(支持轻量级和注释标签)git_delete_tag- 新增 删除Git标签git_list_tags- 新增 列出Git标签(支持模式过滤)git_push_tags- 新增 推送标签到远程仓库
git_raw_command- 新增 直接执行原始Git命令(绕过shell包装问题)
go install github.com/pengcunfu/go-mcp-git@latestgit clone https://github.com/pengcunfu/go-mcp-git.git
cd go-mcp-git
go build -o go-mcp-git ./cmd/servergo-mcp-git --repository /path/to/git/repogo-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服务器会智能地解析仓库路径:
- 提供的路径 - 如果指定了
repo_path参数 - 服务器配置 - 启动时通过
--repository参数配置的默认路径 - 自动检测 - 从当前工作目录向上查找Git仓库
- 当前目录 - 最后回退到当前工作目录
// 绝对路径
{"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/repogit_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 --tagsgit 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
}{
"mcpServers": {
"go-mcp-git": {
"command": "D:\\Tools\\MCP\\go-mcp-git\\go-mcp-git.exe"
}
}
}服务器将自动检测当前工作目录中的Git仓库
{
"mcpServers": {
"go-mcp-git": {
"command": "D:\\Tools\\MCP\\go-mcp-git\\go-mcp-git.exe",
"args": [
"--repository",
"D:\\Projects\\my-main-project"
]
}
}
}所有操作默认使用指定的仓库路径
{
"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提交时使用的用户名和邮箱
{
"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 许可证。