Git MCP Server

by cyanheads
Verified

local-only server

The server can only run on the client’s local machine because it depends on local resources.

Integrations

  • Provides comprehensive Git operations including repository initialization, cloning, file staging, committing, branch management, tag operations, remote repository handling, and stash management, enabling LLMs to interact with Git repositories.

  • Supports interactions with GitHub repositories through Git operations like cloning from GitHub URLs, pushing to and pulling from GitHub remotes, enabling LLMs to manage code on GitHub.

GIT MCP 服务器

一个模型上下文协议 (MCP) 服务器,提供与 Git 仓库交互的工具。该服务器允许 AI 助手和 LLM 代理通过标准化接口管理仓库、分支、提交和文件,而无需直接访问文件系统或命令行。它将 Git 操作公开为 MCP 资源和工具,利用simple-git库实现核心功能,同时保持适当的安全边界。

目录

概述

主要功能:

  • 存储库管理:初始化、克隆和检查存储库状态
  • 分支操作:创建、列出、检出、删除和合并分支
  • 工作目录:暂存文件、提交更改、创建差异
  • 远程操作:添加远程、获取、拉取、推送
  • 高级 Git 命令:管理标签、存储更改、cherry-pick、rebase

架构与组件

核心系统架构:

核心组件:

  • MCP 服务器( server.ts :使用@modelcontextprotocol/sdk创建一个公开资源和工具的服务器。
  • Git 服务( services/git-service.tssimple-git库上的抽象层,为 Git 操作提供清晰的接口。
  • 资源( resources/ :通过具有一致 URI 模板的 MCP 资源公开 Git 数据(如状态、日志、文件内容)。
  • 工具( tools/ :通过具有明确定义的输入模式(使用 Zod 验证)的 MCP 工具公开 Git 操作(如提交、推送、拉取)。
  • 错误处理( services/error-service.ts :Git 和 MCP 操作的标准化错误处理和报告。
  • 入口点( index.ts :初始化并启动服务器,将其连接到标准 I/O 传输。

特征

资源访问

通过 MCP 资源公开 Git 存储库信息:

  • 存储库信息:访问基本 Git 存储库信息,包括当前分支、状态和参考详细信息
  • 存储库分支:列出存储库中带有当前分支指示符的所有分支
  • 远程存储库:列出所有已配置的远程存储库及其 URL
  • 存储库标签:列出存储库中的所有标签及其引用
  • 文件内容:访问给定 Git 引用的特定文件的内容
  • 目录列表:查看特定路径和引用的文件和目录列表
  • 差异:获取引用、未暂存更改或暂存更改之间的差异
  • 提交历史记录:查看包含作者、日期和消息信息的详细提交日志
  • 文件归因:查看逐行归因,显示每行最后修改了哪个提交
  • 提交详细信息:访问有关特定提交的详细信息,包括差异更改

Git 操作

通过 MCP 工具执行 Git 命令:

  • 存储库操作:初始化存储库、从远程克隆、检查存储库状态
  • 分支操作:创建分支、列出分支、检出、删除分支、合并
  • 工作目录操作:暂存文件、取消暂存文件、提交更改、创建差异
  • 远程操作:添加远程、列出远程、获取、拉取、推送
  • 高级操作:管理标签、存储更改、挑选提交、重新设置分支、重置、清理

安装

先决条件

  • Node.js 16 或更高版本
  • Git 已安装并可在 PATH 中使用

从 NPM 安装

npm install -g @cyanheads/git-mcp-server

从源安装

git clone https://github.com/cyanheads/git-mcp-server.git cd git-mcp-server npm install npm run build

用法

运行服务器

如果通过 NPM 全局安装:

git-mcp-server

如果从源运行:

node build/index.js

服务器使用模型上下文协议通过 stdin/stdout 进行通信,使其与任何 MCP 客户端兼容。

与克劳德的整合

添加到您的 Claude 配置文件(例如, cline_mcp_settings.jsonclaude_desktop_config.json ):

{ "mcpServers": { "git": { "command": "git-mcp-server", // Or the full path to build/index.js if not installed globally "args": [], "env": {}, "disabled": false, "autoApprove": [] // Configure auto-approval rules if desired } } }

与其他 MCP 客户端集成

使用 MCP 检查器测试服务器:

# If installed globally npx @modelcontextprotocol/inspector git-mcp-server # If running from source npx @modelcontextprotocol/inspector build/index.js

项目结构

代码库遵循模块化结构:

git-mcp-server/ ├── src/ │ ├── index.ts # Entry point: Initializes and starts the server │ ├── server.ts # Core MCP server implementation and setup │ ├── resources/ # MCP Resource implementations │ │ ├── descriptors.ts # Resource URI templates and descriptions │ │ ├── diff.ts # Diff-related resources (staged, unstaged, commit) │ │ ├── file.ts # File content and directory listing resources │ │ ├── history.ts # Commit history and blame resources │ │ ├── index.ts # Aggregates and registers all resources │ │ └── repository.ts # Repository info, branches, remotes, tags resources │ ├── services/ # Core logic and external integrations │ │ ├── error-service.ts # Centralized error handling utilities │ │ └── git-service.ts # Abstraction layer for simple-git operations │ ├── tools/ # MCP Tool implementations │ │ ├── advanced.ts # Advanced Git tools (tag, stash, cherry-pick, rebase, log, show) │ │ ├── branch.ts # Branch management tools (list, create, checkout, delete, merge) │ │ ├── index.ts # Aggregates and registers all tools │ │ ├── remote.ts # Remote interaction tools (add, list, fetch, pull, push) │ │ ├── repository.ts # Repository level tools (init, clone, status) │ │ └── workdir.ts # Working directory tools (add, reset, commit, diff, reset-commit, clean) │ ├── types/ # TypeScript type definitions │ │ └── git.ts # Custom types related to Git operations │ └── utils/ # Shared utility functions │ ├── global-settings.ts # Manages global working directory setting │ └── validation.ts # Input validation schemas (Zod) and helpers ├── build/ # Compiled JavaScript output ├── docs/ # Documentation files ├── logs/ # Log files (if any) ├── scripts/ # Helper scripts for development (e.g., clean, tree) ├── .env.example # Example environment variables ├── .gitignore # Git ignore rules ├── LICENSE # Project license file ├── package.json # Project metadata and dependencies ├── package-lock.json # Lockfile for dependencies ├── README.md # This file └── tsconfig.json # TypeScript compiler configuration

工具

Git MCP 服务器提供了一套全面的 Git 操作工具:

存储库操作

工具描述
git_init使用裸存储库选项在指定路径初始化新的 Git 存储库。
git_clone使用分支和深度选项将 Git 存储库从远程 URL 克隆到本地路径。
git_status获取 Git 存储库的当前状态,包括工作目录和暂存区更改。

分支机构运营

工具描述
git_branch_list列出存储库中的所有分支,并选择包含远程分支。
git_branch_create创建一个新分支,其中包含指定起点和自动签出的选项。
git_checkout检出分支、标记或提交,并在检出期间使用选项创建新分支。
git_branch_delete删除分支,并选择强制删除未合并的分支。
git_merge使用可自定义的提交消息和合并策略将分支合并到当前分支。

工作目录操作

工具描述
git_add暂存文件以供提交,支持单个文件或整个目录。
git_reset使用针对特定文件或所有暂存更改的选项从暂存区取消暂存文件。
git_commit使用可自定义的提交消息、作者信息和修改选项来提交阶段性更改。
git_diff_unstaged使用特定文件的选项获取工作目录中所有未暂存更改的差异。
git_diff_staged使用特定文件的选项获取索引中所有暂存更改的差异。
git_reset_commit使用硬、软或混合模式选项将存储库重置为特定引用。
git_clean使用目录和强制清理选项从工作树中删除未跟踪的文件。

远程操作

工具描述
git_remote_add添加一个具有名称和 URL 的新远程存储库。
git_remote_list列出所有已配置的远程存储库及其 URL。
git_fetch使用特定分支的选项从远程存储库获取更新。
git_pull使用变基策略选项从远程存储库中提取更改。
git_push将本地更改推送到远程存储库,并提供强制推送和上游跟踪选项。

高级操作

工具描述
git_tag_create创建一个带有带消息注释标签选项的新标签。
git_tag_list列出存储库中的所有标签及其引用。
git_stash_create使用未跟踪文件和描述的选项来存储工作目录中的更改。
git_stash_list列出存储库中的所有存储及其描述。
git_stash_apply应用存储的更改而不将其从存储列表中删除。
git_stash_pop应用存储的更改并将其从存储列表中删除。
git_cherry_pick将特定提交的更改应用到当前分支。
git_rebase使用交互模式选项将当前分支重新定位到另一个分支。
git_log获取具有可定制输出格式和深度的提交历史记录。
git_show显示有关特定提交的详细信息,包括差异更改。

资源

Git MCP 服务器通过标准 MCP 资源公开 Git 数据:

存储库资源

资源描述
git://repo/{repoPath}/info基本 Git 存储库信息,包括当前分支、状态和参考详细信息
git://repo/{repoPath}/branches带有当前分支指示符的存储库中所有分支的列表
git://repo/{repoPath}/remotes所有已配置的远程存储库及其 URL 的列表
git://repo/{repoPath}/tags存储库中所有标签及其引用的列表
git://repo/{repoPath}/file/{filePath}?ref={ref}返回给定 Git 引用的特定文件的内容
git://repo/{repoPath}/ls/{dirPath}?ref={ref}返回特定路径和引用的文件和目录列表
git://repo/{repoPath}/diff/{fromRef}/{toRef}?path={path}返回两个 Git 引用(提交、分支、标签)之间的差异
git://repo/{repoPath}/diff-unstaged?path={path}返回工作目录中所有未暂存更改的差异
git://repo/{repoPath}/diff-staged?path={path}返回索引中所有阶段性变化的差异
git://repo/{repoPath}/log?maxCount={maxCount}&file={file}返回包含作者、日期和消息详细信息的提交历史记录日志
git://repo/{repoPath}/blame/{filePath}返回逐行属性,显示每行最后修改的是哪个提交
git://repo/{repoPath}/commit/{commitHash}返回有关特定提交的详细信息,包括差异更改

发展

构建和测试

# Build the project npm run build # Watch for changes and rebuild automatically npm run watch # Test the server locally using the MCP inspector tool npm run inspector # Clean build artifacts npm run clean # Generate a file tree representation for documentation npm run tree # Clean and rebuild the project completely npm run rebuild

执照

Apache 许可证 2.0 - 有关详细信息,请参阅许可证


ID: e0hyslgby6