Skip to main content
Glama

compare_branches

Compare differences between two branches or commits in a GitLab project to analyze changes before merging or deploying.

Instructions

比較兩個分支或 commit 的差異

Args: project_id: 專案 ID 或路徑 from_ref: 起始分支/commit to_ref: 目標分支/commit

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
project_idYes
from_refYes
to_refYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The tool handler that exposes `compare_branches` as an MCP tool and processes the response to format the user output.
    @mcp.tool()
    def compare_branches(project_id: int | str, from_ref: str, to_ref: str) -> str:
        """比較兩個分支或 commit 的差異
    
        Args:
            project_id: 專案 ID 或路徑
            from_ref: 起始分支/commit
            to_ref: 目標分支/commit
        """
        try:
            client = get_client()
            data = client.compare_branches(project_id, from_ref, to_ref)
    
            commits = data.get("commits", [])
            diffs = data.get("diffs", [])
    
            lines = [f"比較 {from_ref} → {to_ref}:"]
            lines.append(f"Commits: {len(commits)} 個 | 檔案變更: {len(diffs)} 個\n")
    
            if commits:
                lines.append("--- Commits ---")
                for c in commits[:20]:  # 限制最多顯示 20 個 commits
                    lines.append(f"- {c.get('short_id', 'N/A')} {c.get('title', 'N/A')}")
                if len(commits) > 20:
                    lines.append(f"... 還有 {len(commits) - 20} 個 commits")
    
            if diffs:
                lines.append("\n--- 檔案變更 ---")
                for d in diffs:
                    new_path = d.get("new_path", "")
                    if d.get("new_file"):
                        lines.append(f"  [新增] {new_path}")
                    elif d.get("deleted_file"):
                        lines.append(f"  [刪除] {new_path}")
                    elif d.get("renamed_file"):
                        lines.append(f"  [重新命名] {d.get('old_path', '')} → {new_path}")
                    else:
                        lines.append(f"  [修改] {new_path}")
    
            return "\n".join(lines)
        except GitLabAPIError as e:
            return f"比較分支失敗: {str(e)}"
  • The GitLab API client method that performs the network request to compare branches.
    def compare_branches(
        self, project_id: int | str, from_ref: str, to_ref: str
    ) -> dict:
        """GET /projects/:id/repository/compare"""
        pid = self._resolve_project_id(project_id)
        params = {"from": from_ref, "to": to_ref}
        return self._get_json(f"/projects/{pid}/repository/compare", params=params)
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It states the tool compares differences but doesn't disclose behavioral traits like what format the output takes (e.g., diff format, summary statistics), whether it's read-only (implied but not explicit), rate limits, or authentication needs. For a comparison tool with zero annotation coverage, this is inadequate.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded with the core purpose in the first sentence. The parameter list is structured but could be more integrated. There's no wasted text, though it lacks depth in usage or behavior.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (3 parameters, no annotations, but has an output schema), the description is minimally adequate. The output schema likely covers return values, reducing the need for output details in the description. However, it misses key context like comparison scope (e.g., file-level, commit-level) and behavioral traits, leaving gaps.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, so the schema provides no parameter details. The description adds minimal semantics by listing parameters with brief labels ('專案 ID 或路徑', '起始分支/commit', '目標分支/commit'), but doesn't explain formats, constraints, or examples. It partially compensates for the schema gap but leaves key details unclear.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose as '比較兩個分支或 commit 的差異' (compare differences between two branches or commits), which is a specific verb+resource combination. It distinguishes itself from siblings like 'get_branch' or 'list_branches' by focusing on comparison rather than retrieval. However, it doesn't explicitly differentiate from 'get_merge_request_changes' which might also involve comparisons.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites, context (e.g., for code review or deployment checks), or exclusions. Given siblings like 'get_merge_request_changes' that might overlap, this lack of differentiation is a significant gap.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

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/snowild/gitlab-mcp'

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