compare_branches
Compare two branches in a GitLab project to see differences. Specify project ID, source branch, and target branch.
Instructions
Compare two branches.
Args:
project_id: GitLab project ID
from_branch: Source branch
to_branch: Target branch
token: GitLab Personal Access Token (optional)
ctx: MCP context (automatically injected)Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes | ||
| from_branch | Yes | ||
| to_branch | Yes | ||
| token | No | ||
| ctx | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The actual handler function for the 'compare_branches' tool. It calls the GitLab API endpoint /projects/{project_id}/repository/compare?from={from_branch}&to={to_branch}, then formats the result showing commit count, files changed, and recent commits.
async def compare_branches(project_id: int, from_branch: str, to_branch: str, token: str = None, ctx=None) -> str: """Compare two branches. Args: project_id: GitLab project ID from_branch: Source branch to_branch: Target branch token: GitLab Personal Access Token (optional) ctx: MCP context (automatically injected) """ endpoint = f"/projects/{project_id}/repository/compare?from={from_branch}&to={to_branch}" data = await make_gitlab_request(endpoint, ctx=ctx, token=token) if isinstance(data, dict) and "error" in data: return f"Error: {data['error']}" commits = data.get('commits', []) diffs = data.get('diffs', []) result = f"Comparing {from_branch} to {to_branch}:\n" result += f"Commits: {len(commits)}\n" result += f"Files changed: {len(diffs)}\n\n" if commits: result += "Recent commits:\n" for commit in commits[:5]: result += f"• {commit['short_id']}: {commit['title']}\n" return result - gitlab_clone_mcp_server/server.py:862-863 (registration)The @mcp.tool() decorator registers the compare_branches function as an MCP tool.
@mcp.tool() async def compare_branches(project_id: int, from_branch: str, to_branch: str, token: str = None, ctx=None) -> str: