Skip to main content

Connect your AI assistant to GitLab via MCP

Project description

GitLab MR MCP

CI PyPI version Python Versions License: MIT Code style: black

Connect your AI assistant to GitLab. Ask questions like "List open merge requests", "Show me reviews for MR #123", "Get commit discussions for MR #456", or "Find merge requests for the feature branch" directly in your chat.

Table of Contents

Quick Setup

Installation

# Using pipx (recommended)
pipx install gitlab-mr-mcp

# Or using uv
uv tool install gitlab-mr-mcp

# Or using pip
pip install gitlab-mr-mcp

Note: Using pipx or uv tool is recommended as they automatically add the gitlab-mcp command to your PATH. If using pip install, ensure your Python scripts directory is in PATH, or use the full path to the command.

Get your GitLab token

  1. Go to GitLab โ†’ Settings โ†’ Access Tokens
  2. Create token with read_api scope (add api scope if you want write access)
  3. Copy the token

Configure your MCP client

Add to your MCP config (e.g., .cursor/mcp.json for Cursor):

{
  "mcpServers": {
    "gitlab-mcp": {
      "command": "gitlab-mcp",
      "env": {
        "GITLAB_URL": "https://gitlab.com",
        "GITLAB_ACCESS_TOKEN": "glpat-xxxxxxxxxxxxxxxxxxxx",
        "GITLAB_PROJECT_ID": "12345"
      }
    }
  }
}

Restart your MCP client and start asking GitLab questions!

What You Can Do

Once connected, try these commands in your chat:

  • "List open merge requests"
  • "Show me details for merge request 456"
  • "Get reviews and discussions for MR #123"
  • "Show me the test summary for MR #456"
  • "What tests failed in merge request #789?"
  • "Show me the pipeline for MR #456"
  • "Get the failed job logs for merge request #789"
  • "Show me commit discussions for MR #456"
  • "Get all comments on commits in merge request #789"
  • "Find merge requests for the feature/auth-improvements branch"
  • "Show me closed merge requests targeting main"
  • "Reply to discussion abc123 in MR #456 with 'Thanks for the feedback!'"
  • "Create a new review comment in MR #789 asking about the error handling"
  • "Resolve discussion def456 in MR #123"

Working with Review Comments

The enhanced review tools allow you to interact with merge request discussions:

  1. First, get the reviews to see discussion IDs:

    "Show me reviews for MR #123"
    
  2. Reply to specific discussions using the discussion ID:

    "Reply to discussion abc123 in MR #456 with 'I'll fix this in the next commit'"
    
  3. Create new discussion threads to start conversations:

    "Create a review comment in MR #789 asking 'Could you add error handling here?'"
    
  4. Resolve discussions when issues are addressed:

    "Resolve discussion def456 in MR #123"
    

Note: The get_merge_request_reviews tool now displays discussion IDs and note IDs in the output, making it easy to reference specific discussions when replying or resolving.

Working with Test Reports (Recommended for Test Failures)

GitLab provides two tools for checking test results - use the summary for quick checks, and the full report for detailed debugging:

Option 1: Test Summary (Fast & Lightweight) โšก

Use get_pipeline_test_summary for a quick overview:

"Show me the test summary for MR #123"
"How many tests passed in MR #456?"

What You Get:

  • ๐Ÿ“Š Pass/fail counts per test suite
  • โฑ๏ธ Total execution time
  • ๐ŸŽฏ Pass rate percentage
  • โšก Fast - doesn't include detailed error messages

Option 2: Full Test Report (Detailed) ๐Ÿ”

Use get_merge_request_test_report for detailed debugging:

"Show me the test report for MR #123"
"What tests failed in merge request #456?"

What You Get:

  • โœ… Specific test names that passed/failed
  • โŒ Error messages and stack traces
  • ๐Ÿ“ฆ Test suites organized by class/file
  • โฑ๏ธ Execution time for each test
  • ๐Ÿ“Š Pass rate and summary statistics
  • ๐Ÿ“„ File paths and line numbers

How Both Work:

  • Automatically fetch the latest pipeline for the merge request
  • Retrieve test data from that pipeline (uses GitLab's /pipelines/:pipeline_id/test_report or /test_report_summary API)

Example Output:

Test Report Summary:
Total: 45 tests | โœ… 42 passed | โŒ 3 failed | Pass Rate: 93.3%

โŒ Failed Tests:
  test_login_with_invalid_password (0.3s)
    Error: AssertionError: Expected 401, got 200
    File: tests/auth_test.py

Why Use This Instead of Job Logs?

  • ๐ŸŽฏ No noise: Only test results, no build/setup output
  • ๐Ÿ“Š Structured data: Easy for AI to understand and suggest fixes
  • ๐Ÿš€ Fast: Much smaller than full job logs
  • ๐Ÿ” Precise: Shows exact test names and error locations

Requirements:

Your CI must upload test results using artifacts:reports:junit in .gitlab-ci.yml:

test:
  script:
    - pytest --junitxml=report.xml
  artifacts:
    reports:
      junit: report.xml

Working with Pipeline Jobs and Logs

The pipeline tools provide a two-step workflow for debugging test failures:

Step 1: Get Pipeline Overview

Use get_merge_request_pipeline to see all jobs and their statuses:

"Show me the pipeline for MR #456"

What You Get:

  • Pipeline overview (status, duration, coverage)
  • All jobs grouped by status (failed, running, success)
  • Job IDs for each job (use these to fetch logs)
  • Direct links to view jobs in GitLab
  • Job-level timing and stage information

Step 2: Get Specific Job Logs

Use get_job_log with a job ID to fetch the actual output:

"Get the log for job 12345"
"Show me the output of job 67890"

What You Get:

  • Complete job output/trace
  • Log size and line count
  • Automatically truncated to last 15,000 characters for very long logs

Typical Workflow:

You: "Show me the pipeline for MR #123"
AI: "Pipeline failed. 2 jobs failed:
     - test-unit (Job ID: 12345)
     - test-integration (Job ID: 67890)"

You: "Get the log for job 12345"
AI: [Shows full test output with error details]

You: "Fix the failing test"
AI: [Analyzes the log and suggests fixes]

Why Two Tools?

  • Performance: Only fetch logs when needed (not all at once)
  • Flexibility: Check any job's log (failed, successful, or running)
  • Context Efficient: Avoid dumping huge logs unnecessarily

Working with Commit Discussions

The get_commit_discussions tool provides comprehensive insights into discussions and comments on individual commits within a merge request:

  1. View all commit discussions for a merge request:

    "Show me commit discussions for MR #123"
    
  2. Get detailed commit conversation history:

    "Get all comments on commits in merge request #456"
    

This tool is particularly useful for:

  • Code Review Tracking: See all feedback on specific commits
  • Discussion History: Understand the evolution of code discussions
  • Commit-Level Context: View comments tied to specific code changes
  • Review Progress: Monitor which commits have been discussed

Technical Implementation:

  • Uses /projects/:project_id/merge_requests/:merge_request_iid/commits to get all commits with proper pagination
  • Fetches ALL merge request discussions using /projects/:project_id/merge_requests/:merge_request_iid/discussions with pagination support
  • Filters discussions by commit SHA using position data to show commit-specific conversations
  • Handles both individual comments and discussion threads correctly

The output includes:

  • Summary of total commits and discussion counts
  • Individual commit details (SHA, title, author, date)
  • All discussions and comments for each commit with file positions
  • Complete conversation threads with replies
  • File positions for diff-related comments
  • Thread conversations with replies

Configuration Options

MCP Config (Recommended)

Configure environment variables directly in your MCP client config as shown in Quick Setup. This keeps project-specific settings with the project.

Environment Variables

Alternatively, set environment variables in your shell:

export GITLAB_PROJECT_ID=12345
export GITLAB_ACCESS_TOKEN=glpat-xxxxxxxxxxxxxxxxxxxx
export GITLAB_URL=https://gitlab.com

Find Your Project ID

  • Go to your GitLab project โ†’ Settings โ†’ General โ†’ Project ID
  • Or check the URL: https://gitlab.com/username/project (use the numeric ID)

Troubleshooting

Authentication Error: Verify your token has read_api permissions and is not expired.

Project Not Found: Double-check your project ID is correct (it's a number, not the project name).

Connection Issues: Make sure your GitLab URL is accessible and correct.

Script Not Found: Ensure the path in your MCP config points to the actual server location and the script is executable.

Tool Reference

Tool Description Parameters
list_merge_requests List merge requests state, target_branch, limit
get_merge_request_details Get MR details merge_request_iid
create_merge_request Create a new merge request source_branch, target_branch, title, etc.
update_merge_request Update an existing merge request merge_request_iid, title, assignees, etc.
get_pipeline_test_summary Get test summary (fast overview) merge_request_iid
get_merge_request_test_report Get detailed test failure reports merge_request_iid
get_merge_request_pipeline Get pipeline with all jobs merge_request_iid
get_job_log Get trace/output for specific job job_id
get_merge_request_reviews Get reviews/discussions merge_request_iid
get_commit_discussions Get discussions on commits merge_request_iid
get_branch_merge_requests Find MRs for branch branch_name
reply_to_review_comment Reply to existing discussion merge_request_iid, discussion_id, body
create_review_comment Create new discussion thread merge_request_iid, body
resolve_review_discussion Resolve/unresolve discussion merge_request_iid, discussion_id, resolved
list_project_members List project members (none)
list_project_labels List project labels (none)

Development

Project Structure

gitlab_mr_mcp/
โ”œโ”€โ”€ __init__.py          # Package version
โ”œโ”€โ”€ __main__.py          # Entry point for python -m
โ”œโ”€โ”€ server.py            # MCP server implementation
โ”œโ”€โ”€ config.py            # Configuration management
โ”œโ”€โ”€ gitlab_api.py        # GitLab API client
โ”œโ”€โ”€ utils.py             # Utility functions
โ”œโ”€โ”€ logging_config.py    # Logging configuration
โ””โ”€โ”€ tools/               # Tool implementations
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ list_merge_requests.py
    โ”œโ”€โ”€ get_merge_request_details.py
    โ”œโ”€โ”€ create_merge_request.py
    โ”œโ”€โ”€ update_merge_request.py
    โ””โ”€โ”€ ... (more tools)

Adding Tools

  1. Create new file in gitlab_mr_mcp/tools/ directory
  2. Add import and export to gitlab_mr_mcp/tools/__init__.py
  3. Add to list_tools() in gitlab_mr_mcp/server.py
  4. Add handler to call_tool() in gitlab_mr_mcp/server.py

Development Setup

  1. Install development dependencies:
uv pip install -e ".[dev]"
  1. Set up pre-commit hooks:
pre-commit install

This will automatically check and format your code for:

  • โœจ Trailing whitespace - auto-removed
  • ๐Ÿ“„ End-of-file issues - auto-fixed
  • ๐ŸŽจ Code formatting (black) - auto-formatted
  • ๐Ÿ“ฆ Import sorting (isort) - auto-organized
  • ๐Ÿ Python style (flake8) - linted with bugbear & print detection
  • ๐Ÿ”’ Security issues (bandit) - security checks
  • ๐Ÿ“‹ YAML/JSON formatting - validated
  1. Format all existing code (first time only):
# Install dependencies first if not already done
uv pip install -e ".[dev]"

# Format everything
black --line-length=120 .
isort --profile black --line-length=120 .
  1. Run pre-commit manually on all files:
pre-commit run --all-files

Running Tests

# Install dev dependencies
pip install -e ".[dev]"

# Run all tests
pytest

# Run with verbose output
pytest -v

Security Notes

  • Never commit access tokens to version control
  • Use project-specific tokens with minimal permissions (read_api scope)
  • Rotate tokens regularly
  • Store tokens in your MCP config (which should not be committed)

Support

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gitlab_mr_mcp-1.1.0.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gitlab_mr_mcp-1.1.0-py3-none-any.whl (39.8 kB view details)

Uploaded Python 3

File details

Details for the file gitlab_mr_mcp-1.1.0.tar.gz.

File metadata

  • Download URL: gitlab_mr_mcp-1.1.0.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gitlab_mr_mcp-1.1.0.tar.gz
Algorithm Hash digest
SHA256 950fee6bf4ff45da9c687ca0e31dc5534d80970bce8ec3506cd37a3c07b0f176
MD5 6f354691b06672146c8a61ae9e4f67c4
BLAKE2b-256 f36016889e776ea84789e191a2b556953c7ce16ed9b15f1eeb11f782eef00fbc

See more details on using hashes here.

Provenance

The following attestation bundles were made for gitlab_mr_mcp-1.1.0.tar.gz:

Publisher: publish.yml on amirsina-mandegari/gitlab-mr-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gitlab_mr_mcp-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: gitlab_mr_mcp-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 39.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gitlab_mr_mcp-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 74d915306b5232cf38b5c2a7d43d5f5bbe5a386e01b0e52968f4513597ab8d04
MD5 f541e5ae02af6129077f8c935b1073df
BLAKE2b-256 048d7bc09592bb68069a26951353577a076d0417d887f98cd636df14df7944ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for gitlab_mr_mcp-1.1.0-py3-none-any.whl:

Publisher: publish.yml on amirsina-mandegari/gitlab-mr-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page