Skip to main content
Glama

本项目已归档

请考虑使用官方的 glab mcp

{
  "mcpServers": {
    "glab": {
      "type": "stdio",
      "command": "glab",
      "args": ["mcp", "serve"]
    }
  }
}

GitLab MCP 服务器

这是一个用于 GitLab 的生产级模型上下文协议 (MCP) 服务器,可与 IntelliJ IDEA 中的 GitHub Copilot 集成。它能从 git 远程仓库自动检测您的 GitLab 项目,通过智能轮询监控流水线和作业状态,并提供带有重试逻辑的可靠 API 集成。

状态: ✅ 完全验证 (35 个测试,通过率 100%)


快速入门

1. 安装依赖

# Runtime dependencies
pip install -r requirements.txt

# Development/test dependencies (optional)
pip install -r requirements-dev.txt

2. 配置环境

# Copy the example configuration
cp .env.example .env

# Edit .env with your GitLab credentials
# GITLAB_URL=https://your-gitlab-instance.com
# GITLAB_TOKEN=glpat-xxx

如何获取 GitLab 令牌:

  1. GitLab 设置 → 个人访问令牌 (Personal Access Tokens)

  2. 创建具有以下作用域的令牌:api, read_api, read_repository

  3. 将令牌值复制到 .env 文件中

3. 启动服务器

# Using the startup script
./run.sh

# Or directly
python -m src.server

预期输出:

2026-02-10 13:15:30,123 - src.server - INFO - Initializing GitLab MCP server for https://...
2026-02-10 13:15:30,456 - src.server - INFO - GitLab authentication successful
2026-02-10 13:15:30,789 - src.server - INFO - Tools registered successfully
2026-02-10 13:15:30,900 - src.server - INFO - GitLab MCP server started, listening on stdio

4. 在 IntelliJ IDEA 中配置

  1. 安装 GitHub Copilot 插件(如果尚未安装)

  2. 设置 → 工具 → GitHub Copilot → MCP 服务器

  3. 添加 MCP 服务器:

    • 类型: stdio

    • 命令: python -m src.server

    • 环境: 指向您的 .env 文件


Related MCP server: GitLab MCP Server

功能特性

✅ 自动项目检测

  • 无需指定项目路径

  • 从 git 远程 origin 自动检测

  • 支持 SSH 和 HTTPS URL

  • 支持嵌套的 GitLab 组

✅ 流水线状态监控

  • 实时流水线状态

  • 所有作业的详细信息和状态

  • 自动检测分支和提交

  • 人类可读的格式化输出

✅ 智能轮询作业状态

  • 每 2 秒轮询一次作业完成情况

  • 可配置超时时间(默认 30 秒)

  • 返回中间状态

  • 响应中包含轮询元数据

✅ 可靠的 API 集成

  • 3 次重试,带有指数退避(1秒、5秒、9秒)

  • 优雅地处理瞬时网络故障

  • 会话级项目 ID 缓存

  • 清晰的调试错误消息

✅ 支持自托管 GitLab

  • 适用于任何自托管的 GitLab 实例

  • 不依赖 gitlab.com

  • 完全的 API 兼容性


可用工具

check_pipeline_status

获取当前项目和分支的流水线状态

Input:  working_directory (string)
        Optional: branch (string), commit (string)
Output: Pipeline status report with all jobs

功能说明:

  • 自动检测:来自 git 仓库的项目、分支、提交

  • 返回:流水线 ID、状态、各作业的独立状态

  • 格式:人类可读的文本报告

  • 包含:时间、Web URL、阶段信息

示例:

# In Copilot context:
# "Check the pipeline status for this project"
# → Copilot calls: check_pipeline_status("/path/to/repo")

check_job_status

通过自动轮询检查特定作业状态

Input:  working_directory (string)
        job_name (string) OR job_id (integer)
Output: Job status report with polling metadata

功能说明:

  • 自动检测:来自当前分支/提交的项目、流水线

  • 搜索:按作业名称或数字作业 ID

  • 轮询:每 2 秒一次,直到完成(最长 30 秒)

  • 返回:作业状态、时间、日志 URL、轮询元数据

  • 元数据:is_polling, polling_timeout, polling_duration_seconds

示例:

# In Copilot context:
# "Check the status of the 'test' job"
# → Copilot calls: check_job_status("/path/to/repo", job_name="test")

项目结构

gitlab-mcp/
├── src/
│   ├── __init__.py
│   ├── server.py              # MCP server entry point
│   ├── mcp_tools.py           # Tool definitions & logic
│   ├── gitlab_client.py       # GitLab API wrapper (retry logic, caching)
│   └── git_utils.py           # Git utilities (URL parsing, branch detection)
│
├── tests/                      # Comprehensive test suite
│   ├── test_gitlab_client.py  # 9 tests for API client
│   ├── test_git_utils.py      # 11 tests for git utilities
│   ├── test_mcp_tools.py      # 10 tests for tool logic
│   ├── test_server.py         # 5 tests for server initialization
│   └── conftest.py            # Pytest configuration
│
├── requirements.txt            # Runtime dependencies
├── requirements-dev.txt        # Test dependencies
├── .env.example               # Configuration template
├── pytest.ini                 # Pytest settings
├── run.sh                     # Startup script
└── README.md                  # This file

运行测试

快速测试运行

# Run all tests
python -m pytest tests/ -v

# Quick summary
python -m pytest tests/ -q

测试覆盖率

  • 总测试数: 35 (通过率 100% ✅)

  • 已测试模块: 全部 4 个核心模块

    • gitlab_client.py: 9 个测试(API 客户端、重试逻辑、缓存)

    • git_utils.py: 11 个测试(URL 解析、验证)

    • mcp_tools.py: 10 个测试(轮询、格式化、逻辑)

    • server.py: 5 个测试(初始化、配置)

运行特定测试

# Test GitLab client
python -m pytest tests/test_gitlab_client.py -v

# Test git utilities
python -m pytest tests/test_git_utils.py -v

# Test MCP tools
python -m pytest tests/test_mcp_tools.py -v

# Test server
python -m pytest tests/test_server.py -v

# Run with coverage
python -m pytest tests/ --cov=src --cov-report=html

配置

环境变量

创建包含以下内容的 .env 文件:

# Required
GITLAB_URL=https://your-gitlab-instance.com
GITLAB_TOKEN=glpat-your-token-here

# Optional
DEBUG=false  # Set to 'true' for verbose logging

重试逻辑配置

客户端会自动重试失败的 API 调用:

  • 总尝试次数: 3(初始 + 2 次重试)

  • 退避延迟: 1秒、5秒、9秒

  • 适用范围: 所有 GitLab API 调用

作业轮询配置

通过代码配置轮询行为:

# Default settings
_poll_job_status(client, project, job_name, job_id,
                timeout_seconds=30,    # Max wait time
                poll_interval=2.0)      # Check every 2 seconds

架构

┌─────────────────────────────────────────────┐
│  IntelliJ IDEA + GitHub Copilot Plugin      │
│  (IDE Client)                               │
└──────────────────┬──────────────────────────┘
                   │ (stdio transport)
                   │ (MCP Protocol)
                   │
┌──────────────────▼──────────────────────────┐
│  FastMCP Server (Python)                    │
│  ┌────────────────────────────────────────┐ │
│  │ MCP Tools                              │ │
│  │ • check_pipeline_status                │ │
│  │ • check_job_status (with polling)      │ │
│  └────────────────────────────────────────┘ │
│  ┌────────────────────────────────────────┐ │
│  │ GitLab Client                          │ │
│  │ • Session-based caching                │ │
│  │ • Retry logic (1s, 5s, 9s backoff)     │ │
│  │ • Pipeline/job/MR queries              │ │
│  └────────────────────────────────────────┘ │
│  ┌────────────────────────────────────────┐ │
│  │ Git Utilities                          │ │
│  │ • SSH/HTTPS URL parsing                │ │
│  │ • Branch/commit detection              │ │
│  │ • Repository validation                │ │
│  └────────────────────────────────────────┘ │
└──────────────────┬──────────────────────────┘
                   │ (HTTP REST API)
                   │
┌──────────────────▼──────────────────────────┐
│  Self-Hosted GitLab Instance                │
│  (or gitlab.com)                            │
└─────────────────────────────────────────────┘

故障排除

配置问题

"GITLAB_URL environment variable is not set"

  • 验证 .env 文件是否存在:ls -la .env

  • 检查 .env 是否包含 GITLAB_URL:grep GITLAB_URL .env

  • 确保运行服务器时 .env 位于工作目录中

"GITLAB_TOKEN environment variable is not set"

  • GITLAB_TOKEN 添加到 .env

  • 令牌格式:glpat-xxx (GitLab 个人访问令牌)

  • 验证令牌是否具有正确的作用域:api, read_api, read_repository

"GitLab authentication successful" 但工具失败

  • 检查 GitLab 实例是否可访问:curl -H "PRIVATE-TOKEN: $TOKEN" $GITLAB_URL/api/v4/user

  • 验证令牌是否具有正确的作用域

  • 检查防火墙/网络对 GitLab 实例的访问权限

Git 问题

"Not a git repository"

  • 确保您位于 git 仓库中:git remote -v

  • 支持的远程格式:

    • git@gitlab.host:group/project.git

    • https://gitlab.host/group/project.git

    • https://gitlab.host/group/project (不带 .git)

    • http://gitlab.host/group/project (HTTP, 非 HTTPS)

"Unable to parse git remote URL"

  • 检查 git 远程格式:git remote -v

  • SSH 和 HTTPS 都必须采用标准的 GitLab 格式

  • 支持嵌套组:company/team/project

流水线/作业问题

"No pipeline found for branch"

  • 验证分支是否已推送:git push

  • 检查 GitLab 中是否配置了流水线触发器

  • 尝试使用明确的提交 SHA:check_pipeline_status(dir, commit="abc123")

"Job not found: test"

  • 验证作业名称是否完全匹配(区分大小写)

  • 检查流水线是否有作业(可能为空)

  • 列出作业:check_pipeline_status(dir) 以查看所有作业

作业轮询超时(30 秒)

  • 作业未在 2 分钟窗口内启动

  • 可以重新运行工具以检查当前状态

  • 即使超时,工具也会返回最后已知的状态

调试模式

启用详细日志记录:

# In .env
DEBUG=true

# Or as environment variable
DEBUG=true python -m src.server

在工具调用期间检查日志以获取详细的错误消息。


验证与测试

测试结果

============================= 35 passed in 12.73s ===============================
✅ test_git_utils.py         (11 tests)
✅ test_gitlab_client.py      (9 tests)
✅ test_mcp_tools.py         (10 tests)
✅ test_server.py             (5 tests)

测试内容

  • ✅ 使用模拟响应的 GitLab API 客户端

  • ✅ 重试逻辑和指数退避

  • ✅ 项目 ID 缓存机制

  • ✅ Git URL 解析(SSH、HTTPS、嵌套组)

  • ✅ 带超时的作业轮询

  • ✅ 响应格式化

  • ✅ 服务器初始化和配置

  • ✅ 错误处理和验证

无需真实 GitLab 实例的测试

所有测试均使用模拟的 GitLab API(无需真实的 API 调用):

python -m pytest tests/ -v

性能

典型响应时间

  • 首次 API 调用: 1-3 秒(取决于网络)

  • 后续调用: <500ms(缓存的项目 ID)

  • 作业轮询: 2 秒间隔

  • 总测试套件: ~13 秒

缓存策略

  • 项目 ID: 每个服务器会话缓存

  • 重置: 重启服务器会清除缓存

  • 优势: 减少重复操作的 API 调用次数


实现细节

重试逻辑

Attempt 1: Immediate call
  ↓ (fails)
Wait 1 second
Attempt 2: Retry
  ↓ (fails)
Wait 5 seconds
Attempt 3: Final retry
  ↓ (fails)
Raise GitLabClientError

URL 解析示例

SSH:   git@gitlab.com:group/project.git          → group/project
HTTPS: https://gitlab.com/group/project.git      → group/project
HTTPS: https://gitlab.com/group/project          → group/project
SSH:   git@host:company/team/subteam/project.git → company/team/subteam/project

作业轮询行为

Initial check: Get job status immediately
  ↓
If terminal state (success/failed/canceled/skipped): Return
  ↓
If not started: Polling loop
  ├─ Check every 2 seconds
  ├─ Max 30 seconds total
  └─ Return with polling_timeout flag if timeout occurs

支持的 Git 仓库

自托管 GitLab 实例(任何版本) ✅ gitlab.com (公共 GitLab) ✅ 嵌套组 (company/team/project/...) ✅ SSH 和 HTTPS 远程仓库

❌ 不支持:GitHub、Bitbucket 等(仅限 GitLab)


下一步做什么

1. 本地测试

# Test git utilities
python -c "
from src.git_utils import get_project_path_from_working_dir
print(get_project_path_from_working_dir('.'))
"

2. 测试 GitLab 连接

python -c "
import os
from dotenv import load_dotenv
from src.gitlab_client import GitLabClient
load_dotenv()
client = GitLabClient(os.getenv('GITLAB_URL'), os.getenv('GITLAB_TOKEN'))
client.gl.auth()
print('✓ GitLab auth successful')
"

3. 启动服务器

./run.sh
# Then configure in IntelliJ IDEA GitHub Copilot plugin

4. 与 Copilot 一起使用

在安装了 Copilot 的 IntelliJ IDEA 中:

  • "Check the pipeline status"

  • "What's the status of the test job?"

  • "Show me the latest pipeline"


贡献

要添加测试或功能:

  1. tests/ 目录中创建测试文件

  2. 使用模拟 GitLab API:patch('src.gitlab_client.gitlab.Gitlab')

  3. 运行测试:python -m pytest tests/ -v

  4. 确保在提交前所有测试通过


依赖项

运行时

  • fastmcp>=2.14.0 - 模型上下文协议服务器

  • python-gitlab>=4.0.0 - GitLab API 客户端

  • python-dotenv>=1.0.0 - 环境变量加载

  • GitPython>=3.1.0 - Git 操作

开发/测试

  • pytest>=8.0.0 - 测试框架

  • requests-mock>=1.11.0 - HTTP 模拟(可选)


实现状态

功能

状态

测试

流水线状态监控

✅ 完成

4

作业状态查询

✅ 完成

5

作业轮询

✅ 完成

4

Git URL 解析

✅ 完成

8

重试逻辑

✅ 完成

1

错误处理

✅ 完成

3

服务器初始化

✅ 完成

5

配置验证

✅ 完成

5


支持

如有问题或疑问:

  1. 启用调试日志: 在 .env 中设置 DEBUG=true

  2. 检查日志: 在工具调用期间查看服务器输出

  3. 验证设置: 遵循上述故障排除部分

  4. 查看测试: 查看 tests/ 以获取使用示例

  5. 检查 git 远程: git remote -v 必须是有效的 GitLab URL


许可证

[在此处添加您的许可证]


最后验证: 2026年2月10日 测试套件: 35/35 通过 ✅ 状态: 生产就绪 🚀

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables AI clients to manage GitLab pipelines through natural language commands. Supports triggering pipelines, checking status, listing pipelines, viewing jobs, and canceling pipelines across multiple GitLab instances.
    107 npm
    ISC
  • F
    license
    A
    quality
    C
    maintenance
    Connects AI assistants to GitLab to interact with merge requests, reviews, discussions, pipelines, and test results through natural language queries. Supports viewing MR details, responding to comments, checking test summaries, and analyzing job logs.
    12
    2
    -
  • F
    license
    Not graded
    quality
    Not graded
    maintenance
    Connects AI assistants to GitLab projects, enabling natural language queries for merge requests, code reviews, test results, pipelines, and discussions. Supports viewing MR details, responding to comments, and analyzing CI/CD job logs.
    -
  • A
    license
    Not graded
    quality
    C
    maintenance
    Integrates GitLab with AI assistants to manage merge requests, analyze CI/CD pipelines, and create Architecture Decision Records. It enables seamless code searching, pipeline triggering, and deployment management through the Model Context Protocol.
    1
    MIT