Git操作MCP服务器

一个用于本地Git操作的Python MCP服务器,提供与Claude Code和其他MCP客户端的原生工具集成。

  • by rriesco
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 rriesco/git-mcp-server

使用说明

Git MCP服务器

Python MCP(模型上下文协议)服务器,用于本地git操作,提供与Claude Code和其他MCP客户端的本地工具集成。

特性

  • 6 Git工具:提交、分支、推送、拉取、状态和同步操作
  • 本地MCP集成:与Claude Code无缝协作
  • 约定式提交:带有类型前缀的强制提交消息格式
  • 分支命名:强制公约(问题-N-描述、特征-*等)
  • 令牌身份验证:用于远程操作的自动GitHub令牌注入

安装

来自PyPI

pip install git-mcp-server
# or with uv
uvx git-mcp-server

来源

git clone https://github.com/rriesco/git-mcp-server.git
cd git-mcp-server
uv sync

配置

环境变量

变量 必填 描述
GITHUB_TOKEN 用于推/拉 用于经过身份验证的git操作的GitHub个人访问令牌

Claude代码配置

添加到MCP配置(~/.config/claude-code/mcp-config.json):

{
  "mcpServers": {
    "git-manager": {
      "type": "stdio",
      "command": "uvx",
      "args": ["git-mcp-server"],
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}"
      }
    }
  }
}

可用工具

工具 说明
git_create_branch 使用命名约定创建和签出新分支
git_commit 使用Claude归因创建常规承诺
git_push 推送提交到远程,并进行上游跟踪
git_pull 从远程拉取提交
git_status 获取当前分支、跟踪信息和文件更改
git_sync_with_main 将当前分支与主分支同步(合并或重基)

用法示例

创建分支

result = git_create_branch(
    issue_number=42,
    description="add-feature-x"
)
# Creates: issue-42-add-feature-x

提交更改

result = git_commit(
    type="feat",
    message="implement user authentication"
)
# Creates: feat: implement user authentication
#
# Co-Authored-By: Claude <noreply@anthropic.com>

检查状态

result = git_status()
# Returns: {branch, tracking, ahead, behind, staged, modified, untracked, clean}

与主同步

result = git_sync_with_main(
    main_branch="main",
    strategy="merge"  # or "rebase"
)

提交类型

这 git_commit 该工具强制使用传统的提交类型:

类型 描述
feat 新功能
fix Bug修复
docs 仅文档
style 代码样式(格式、分号等)
refactor 代码重构
perf 性能改进
test 添加或修复测试
build 构建系统或依赖关系
ci CI/CD配置
chore 维护任务
revert 还原以前的提交

分支命名

这 git_create_branch 该工具强制执行命名约定:

  • issue-<N>-<description> -关于GitHub的问题
  • feature-<description> -对于没有问题的功能
  • fix-<description> -用于修复错误
  • refactor-<description> -用于重构

发展

先决条件

  • Python>=3.10
  • uv(推荐)或pip
  • Git

设置

git clone https://github.com/rriesco/git-mcp-server.git
cd git-mcp-server
uv sync

运行测试

# Unit tests only (fast)
uv run pytest -m "not integration" -v

# Integration tests (creates real git repos in temp directories)
uv run pytest -m integration -v

# All tests with coverage
uv run pytest --cov=git_mcp_server --cov-report=term-missing

类型检查

uv run mypy src/git_mcp_server --strict

建筑

Claude Code / MCP Client
      |
      | MCP Protocol (stdio)
      v
┌─────────────────────────────┐
│  Python FastMCP Server      │
│  - Tool Registry            │
│  - GitPython Client         │
│  - Error Handling           │
│  - Type Validation          │
└─────────────┬───────────────┘
              |
              | GitPython
              v
        Local Git Repo

项目结构

git-mcp-server/
├── src/git_mcp_server/
│   ├── server.py              # Server entry point
│   ├── tools/
│   │   ├── branch.py          # Branch operations
│   │   ├── commit.py          # Commit operations
│   │   ├── remote.py          # Push/pull operations
│   │   ├── status.py          # Status queries
│   │   └── sync.py            # Sync with main
│   └── utils/
│       ├── git_client.py      # Singleton Repo instance
│       └── errors.py          # Structured error handling
└── tests/
    ├── test_*.py              # Unit tests
    └── integration/           # Integration tests

许可证

MIT许可证-请参阅 许可证 了解详情。

贡献

欢迎投稿!请在提交PR之前阅读投稿指南。

  1. 克隆该仓库
  2. 创建要素分支
  3. 通过测试进行更改
  4. 提交拉取请求

链接