Skip to main content
Glama

Git MCP Server

Git MCP 服务器

MCP(模型上下文协议)服务器,提供与 Git 存储库交互的工具。使 LLM 和 AI 代理能够通过 MCP 标准执行 Git 操作,例如克隆、提交、推送、拉取、分支、差异、日志、状态等。

该服务器基于cyanheads/mcp-ts-template构建,遵循模块化架构:

开发人员注意事项:此存储库包含一个.clinerules文件,该文件可作为您的 LLM 编码代理的开发人员备忘单,并提供代码库模式、文件位置和代码片段的快速参考。

目录

|概述|特点|安装|

|配置|项目结构|

|工具|资源|开发|许可证|

概述

通过无缝 Git 集成增强您的 AI 代理和开发工具!

Git MCP 服务器充当桥梁,允许理解模型上下文协议 (MCP) 的应用程序(MCP 客户端) - 如高级 AI 助手 (LLM)、IDE 扩展或自定义脚本 - 直接安全地与本地 Git 存储库进行交互。

您的工具可以利用此服务器来执行以下操作,而无需使用复杂的脚本或手动 CLI:

  • 自动化 Git 工作流程:克隆存储库、创建分支、暂存更改、提交工作、推送更新以及以编程方式管理标签。
  • 获得存储库洞察:无需离开主机应用程序即可检查状态、查看日志、差异更改和检查 Git 对象。
  • 将 Git 集成到 AI 驱动的开发中:使 LLM 能够将版本控制作为其编码或重构任务的一部分进行管理。

该服务器基于强大的mcp-ts-template构建,提供了一种通过 MCP 标准公开 Git 功能的标准化、安全且高效的方式。它通过使用 Node.js 的child_process模块安全地执行系统上安装的标准git命令行工具来实现这一点,从而确保兼容性并充分利用 Git 的全部功能。

特征

核心实用程序(来自模板)

利用mcp-ts-template提供的强大实用程序:

  • 日志记录:结构化、可配置的日志记录(文件轮换、控制台、MCP 通知),并带有敏感数据编辑。
  • 错误处理:集中错误处理、标准化错误类型( McpError )和自动日志记录。
  • 配置:环境变量加载( dotenv )。
  • 输入验证/清理:使用zod进行模式验证和自定义清理逻辑(对于路径至关重要)。
  • 请求上下文:通过唯一的请求 ID 来跟踪和关联操作。
  • 类型安全:由 TypeScript 和 Zod 模式强制执行的强类型。
  • HTTP 传输选项:内置 Express 服务器,支持 SSE、会话管理和 CORS。

Git 操作

  • 直接 Git CLI 执行:通过 Node.js child_process安全地执行标准git命令行工具与 Git 交互,确保完全兼容并可访问 Git 的功能。
  • 全面的命令覆盖:将各种 Git 命令作为 MCP 工具公开(参见工具部分)。
  • 存储库交互:支持状态检查、分支、暂存、提交、获取、拉取、推送、差异、日志记录、重置、标记等。
  • 工作目录管理:允许设置和清除特定于会话的工作目录,以便在多个 Git 操作中保持上下文持久性。
  • 安全功能:包括检查并要求对潜在破坏性操作(如git cleangit reset --hard进行明确确认。
  • 提交签名:支持使用 GPG 或 SSH 签名已验证的提交,通过GIT_SIGN_COMMITS环境变量和服务器端 Git 配置进行控制。包含一个可选工具参数,用于在签名失败时回退到未签名的提交。

安装

先决条件

通过 npm 安装

  1. 全局安装包:
    npm install @cyanheads/git-mcp-server

从源安装

  1. 克隆存储库:
    git clone https://github.com/cyanheads/git-mcp-server.git cd git-mcp-server
  2. 安装依赖项:
    npm install
  3. 构建项目:
    npm run build (or `npm run rebuild`)
    这会将dist/目录中的 TypeScript 代码编译为 JavaScript,并使入口点可执行。

配置

环境变量

使用环境变量配置服务器。在项目根目录下创建一个.env文件(从.env.example复制),或者在你的环境中设置它们。

多变的描述默认
MCP_TRANSPORT_TYPE传输机制: stdiohttpstdio
MCP_HTTP_PORTHTTP 服务器的端口(如果MCP_TRANSPORT_TYPE=http )。如果端口繁忙,则重试下一个端口。3010
MCP_HTTP_HOSTHTTP 服务器的主机地址(如果MCP_TRANSPORT_TYPE=http )。127.0.0.1
MCP_ALLOWED_ORIGINS以逗号分隔的 CORS 允许来源列表(如果MCP_TRANSPORT_TYPE=http )。(没有任何)
MCP_LOG_LEVEL日志级别( debuginfonoticewarningerrorcritalertemerg )。继承自模板。info
GIT_SIGN_COMMITS设置为"true"即可启用对git_commit工具所作提交的签名尝试。需要服务器端 Git/key 设置(见下文)。false
MCP_AUTH_SECRET_KEY用于签署/验证身份验证令牌的密钥(如果将来启用身份验证则需要)。''

MCP 客户端设置

添加到您的 MCP 客户端设置(例如, cline_mcp_settings.json ):

{ "mcpServers": { "git-mcp-server": { // Use a descriptive name "command": "node", // Use node to run the script "args": ["/path/to/your/git-mcp-server/dist/index.js"], // Absolute path to the built entry point "env": { // "MCP_TRANSPORT_TYPE": "http", // Optional: if using http // "MCP_HTTP_PORT": "3010", // Optional: if using http and non-default port // "GIT_SIGN_COMMITS": "true" // Optional: Enable commit signing attempts if server is configured }, "disabled": false, "autoApprove": [] // Configure auto-approval rules if desired } } }

项目结构

代码库遵循src/目录中的模块化结构:

src/ ├── index.ts # Entry point: Initializes and starts the server ├── config/ # Configuration loading (env vars, package info) │ └── index.ts ├── mcp-server/ # Core MCP server logic and capability registration │ ├── server.ts # Server setup, capability registration │ ├── transports/ # Transport handling (stdio, http) │ ├── resources/ # MCP Resource implementations (currently none) │ └── tools/ # MCP Tool implementations (subdirs per tool) ├── types-global/ # Shared TypeScript type definitions └── utils/ # Common utility functions (logger, error handler, etc.)

有关详细的文件树,请运行npm run tree或查看docs/tree.md

工具

Git MCP 服务器提供了一套用于与 Git 存储库交互的工具,可通过模型上下文协议调用。

工具名称描述关键论点
git_add暂存指定的文件或模式。path?files?
git_branch管理分支(列出、创建、删除、重命名、显示当前)。path?modebranchName?newBranchName?startPoint?force?all?remote?
git_checkout切换分支或恢复工作树文件。path?branchOrPathnewBranch? force?
git_cherry_pick应用现有提交引入的更改。path?commitRefmainline?strategy?noCommit? signoff?
git_clean删除未跟踪的文件。需要force: truepath?forcedryRun? directories?ignored?
git_clear_working_dir清除会话特定的工作目录。(没有任何)
git_clone将存储库克隆到指定的绝对路径。repositoryUrltargetPathbranch?depth?quiet?
git_commit提交阶段性变更。支持作者覆盖和签名控制。path?messageauthor?allowEmpty?amend?forceUnsignedOnFailure?
git_diff显示提交、工作树等之间的变化。path?commit1? commit2? ?、 staged?file?
git_fetch从其他存储库下载对象和引用。path?remote?prune?tags?all?
git_init在指定的绝对路径下初始化一个新的 Git 仓库。初始分支默认为“main”。pathinitialBranch? bare?quiet?
git_log显示提交日志。path?maxCount?author?since?until?branchOrFile?
git_merge将指定分支合并到当前分支。path?branchcommitMessage?noFf?squash?abort?
git_pull从另一个存储库或本地分支获取并集成。path?remote?branch?rebase?ffOnly?
git_push使用本地引用更新远程引用。path?remote?branch?remoteBranch? ?、 force?forceWithLease?setUpstream? tags?delete?
git_rebase在另一个基本提示之上重新应用提交。path?mode?upstream?branch?interactive?strategy?strategyOption?onto?
git_remote管理远程存储库(列出、添加、删除、显示)。path?modename?url?
git_reset将当前 HEAD 重置为指定状态。支持软、混合、硬三种模式。请谨慎使用“hard”path?mode?commit?
git_set_working_dir设置默认工作目录。如果不存在,可以选择初始化 repo。需要绝对路径。pathvalidateGitRepo?initializeIfNotPresent?
git_show显示有关 Git 对象(提交、标签等)的信息。path?reffilePath?
git_stash管理存储的更改(列出、应用、弹出、删除、保存)。path?modestashRef? message?
git_status获取存储库状态(分支、暂存、修改、未跟踪的文件)。path?
git_tag管理标签(列表、创建注释/轻量级、删除)。path?modetagName?message?commitRef? annotate?
git_worktree管理 Git 工作树(列出、添加、删除、移动、修剪)。path?modeworktreePath? commitish?newBranch? force?detach?newPath? verbose?dryRun? expire?

注意:如果通过git_set_working_dir设置,大多数工具的path参数默认为会话的工作目录。

资源

此版本(v2.0.12)中未实现 MCP 资源。

此版本重点关注基于最新mcp-ts-template和 MCP SDK v1.12.0 重构的 Git 工具实现。此前可用的资源功能在本次重大更新中被暂时移除。

如果您需要 MCP 资源访问(例如,直接通过服务器读取文件内容),请使用稳定的**v1.2.4 版本**

未来的开发可能会在后续版本中重新引入资源功能。

**注意:**此版本 (v2.0.0) 重点基于最新的 MCP SDK 重构和更新了核心 Git 工具。MCP 资源功能尚未在此版本中实现。如需访问资源,请使用v1.2.4 版本

发展

构建和测试

# Build the project (compile TS to JS in dist/ and make executable) npm run build # Test the server locally using the MCP inspector tool (stdio transport) npm run inspector # Test the server locally using the MCP inspector tool (http transport) npm run inspector:http # Clean build artifacts (runs scripts/clean.ts) npm run clean # Generate a file tree representation for documentation (runs scripts/tree.ts) npm run tree # Clean build artifacts and then rebuild the project npm run rebuild # Start the server using stdio (default) npm start # Or explicitly: npm run start:stdio # Start the server using HTTP transport npm run start:http

执照

该项目根据 Apache License 2.0 获得许可 - 有关详细信息,请参阅LICENSE文件。


Install Server
A
security – no known vulnerabilities
A
license - permissive license
A
quality - confirmed to work

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

模型上下文协议服务器使大型语言模型能够通过强大的 API 与 Git 存储库交互,支持存储库初始化、克隆、文件暂存、提交和分支管理等操作。

  1. 目录
    1. 概述
      1. 特征
        1. 核心实用程序(来自模板)
        2. Git 操作
      2. 安装
        1. 先决条件
        2. 通过 npm 安装
        3. 从源安装
      3. 配置
        1. 环境变量
        2. MCP 客户端设置
      4. 项目结构
        1. 工具
          1. 资源
            1. 发展
              1. 构建和测试
            2. 执照

              Related MCP Servers

              • -
                security
                A
                license
                -
                quality
                A Model Context Protocol server for Git repository interaction and automation. This server provides tools to read, search, and manipulate Git repositories via Large Language Models.
                Last updated -
                12
                57,877
                TypeScript
                MIT License
              • A
                security
                F
                license
                A
                quality
                A Model Context Protocol server that enables AI models to interact with GitHub's API, allowing for repository creation and management with descriptions, topics, and website URLs through natural language commands.
                Last updated -
                1
                JavaScript
              • A
                security
                A
                license
                A
                quality
                A Model Context Protocol server that enables interaction with GitLab accounts to manage repositories, merge requests, code reviews, and CI/CD pipelines through natural language.
                Last updated -
                41
                2
                TypeScript
                MIT License
                • Apple
              • A
                security
                A
                license
                A
                quality
                A Model Context Protocol server that helps large language models process code repositories by providing file tree generation, code merging, and code analysis capabilities.
                Last updated -
                3
                14
                JavaScript
                MIT License

              View all related MCP servers

              MCP directory API

              We provide all the information about MCP servers via our MCP API.

              curl -X GET 'https://glama.ai/api/mcp/v1/servers/cyanheads/git-mcp-server'

              If you have feedback or need assistance with the MCP directory API, please join our Discord server