GitLab REST API服务器

基于fastmcp构建的GitLab REST API v4服务器,优化了令牌使用效率,适用于自动化代码管理和CI/CD流程。

  • by LoindP
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 LoindP/gitLab_mcp_loind

使用说明

GitLab MCP服务器

用于GitLab REST API v4的MCP服务器使用 fastmcp。优化以最小化令牌使用。

先决条件

对于最终用户(Claude/Codex):

  • ✅ Python 3.10+安装在您的系统上
  • ✅ GitLab个人访问令牌

就是这样! 无需手动安装-其他一切都是自动的。

运作原理

当您运行服务器时 npx,Node.js启动器 自动地:

  1. ✅ 检查Python -寻找 python3 或 python 在你的路径中
  2. ✅ 创建虚拟环境 -In ~/.cache/gitlab-mcp-server/venv
  3. ✅ 安装依赖项 - fastmcp 从 requirements.txt
  4. ✅ 缓存一切 -快速后续发射

首次运行:~10-30秒(一次性设置) 下一次跑步:即时(使用缓存的venv)

如果找不到Python

如果未安装Python 3.10+,启动器将显示操作系统的说明:

  • macOS: brew install python@3.11 或从python.org下载
  • Linux: sudo apt install python3 或 sudo yum install python3
  • 视窗:从python.org下载或 winget install Python.Python.3.11

您不需要手动安装 fastmcp 或者创建virtualenv! 发射器可以处理这个问题。

配置

克劳德代码

使用CLI添加服务器:

claude mcp add gitlab --transport stdio \
  --env GITLAB_TOKEN=your-gitlab-token \
  --env GITLAB_URL=https://gitlab.com \
  --env GITLAB_TIMEOUT=30 \
  -- npx -y git+https://github.com/YOUR_ORG/YOUR_REPO.git#main

或添加到 ~/.claude.json:

{
  "mcpServers": {
    "gitlab": {
      "command": "npx",
      "args": ["-y", "git+https://github.com/YOUR_ORG/YOUR_REPO.git#main"],
      "env": {
        "GITLAB_TOKEN": "your-gitlab-token",
        "GITLAB_URL": "https://gitlab.com",
        "GITLAB_TIMEOUT": "30"
      }
    }
  }
}

法典

增添 ~/.codex/config.toml:

[mcp_servers.gitlab]
command = "npx"
args = ["-y", "git+https://github.com/YOUR_ORG/YOUR_REPO.git#main"]
startup_timeout_sec = 60
enabled = true

[mcp_servers.gitlab.env]
GITLAB_TOKEN = "your-gitlab-token"
GITLAB_URL = "https://gitlab.com"
GITLAB_TIMEOUT = "30"
MCP_TRANSPORT = "stdio"

注: 替换 YOUR_ORG/YOUR_REPO 使用您的GitHub存储库路径(例如。, myorg/gitlab-mcp-server).

环境变量

变量 必填 默认 描述
GITLAB_TOKEN 是 - GitLab个人访问令牌
GITLAB_URL 没有 https://gitlab.com GitLab实例URL
GITLAB_TIMEOUT 没有 30 API超时(秒)
MCP_TRANSPORT 没有 stdio 传输协议
GITLAB_MCP_CACHE_DIR 没有 ~/.cache virtualenv的自定义缓存目录

可用工具

此服务器将20多个GitLab操作作为MCP工具公开:

用户

  • gitlab_whoami -获取当前用户配置文件并验证凭据

项目

  • gitlab_list_projects -列出GitLab项目(支持搜索、分页)
  • gitlab_get_project -按ID或路径获取单个项目详细信息

分支

  • gitlab_list_branches -列出项目中的存储库分支

合并请求

  • gitlab_list_merge_requests -列出合并请求(按状态筛选:打开、合并、关闭)
  • gitlab_get_merge_request -获取单个合并请求详细信息
  • gitlab_create_merge_request -创建新的合并请求
  • gitlab_merge_merge_request -合并合并请求
  • gitlab_list_merge_request_notes -列出合并请求的注释
  • gitlab_create_merge_request_note -向合并请求添加注释
  • gitlab_list_merge_request_discussions -列出合并请求中的讨论线程
  • gitlab_create_merge_request_discussion_note -回复讨论帖子

问题

  • gitlab_list_issues -列出项目中的问题(按状态筛选:打开、关闭)
  • gitlab_create_issue -创建新问题
  • gitlab_list_issue_notes -列出对某个问题的评论
  • gitlab_create_issue_note -向问题添加评论

管道和CI/CD

  • gitlab_list_pipelines -列出CI/CD管道(按参考、状态过滤)
  • gitlab_get_pipeline -获取单个管道详细信息
  • gitlab_create_pipeline -触发新管道
  • gitlab_list_pipeline_jobs -列出管道中的作业
  • gitlab_get_job -获取单个作业详细信息
  • gitlab_play_job -开始手动作业
  • gitlab_retry_job -重试失败的作业
  • gitlab_cancel_job -取消正在运行/挂起的作业

所有工具都支持GitLab.com和自托管GitLab实例 GITLAB_URL 环境变量。

对于开发者

如果您正在本地开发或测试:

# Clone the repo
git clone https://github.com/YOUR_ORG/YOUR_REPO.git
cd YOUR_REPO

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate  # or .venv\Scripts\activate on Windows

# Install dependencies
pip install -r requirements.txt

# Set environment variables
export GITLAB_TOKEN='your-gitlab-token'
export GITLAB_URL='https://gitlab.com'

# Run the server directly
python3 gitlab_mcp_server.py

# Or test via Node.js launcher
node bin/gitlab-mcp-server.js

终端用户注意事项:你不需要这样做!npx启动器自动处理一切。