list_branches
Retrieve and filter branches from a GitLab project using search parameters and pagination controls.
Instructions
列出專案的分支
Args: project_id: 專案 ID 或路徑 search: 搜尋關鍵字 page: 頁碼 per_page: 每頁筆數
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes | ||
| search | No | ||
| page | No | ||
| per_page | No |
Implementation Reference
- src/gitlab_mcp/server.py:762-798 (handler)The MCP tool handler for 'list_branches'. This function is registered with @mcp.tool() and calls the GitLabClient to fetch branch information.
@mcp.tool() def list_branches(project_id: int | str, search: str = None, page: int = 1, per_page: int = 20) -> str: """列出專案的分支 Args: project_id: 專案 ID 或路徑 search: 搜尋關鍵字 page: 頁碼 per_page: 每頁筆數 """ try: client = get_client() branches = client.list_branches(project_id, search=search, page=page, per_page=per_page) if not branches: return "找不到符合條件的分支" lines = [f"找到 {len(branches)} 個分支:\n"] for b in branches: flags = [] if b.get("default"): flags.append("預設") if b.get("protected"): flags.append("受保護") if b.get("merged"): flags.append("已合併") flag_str = f" ({', '.join(flags)})" if flags else "" commit = b.get("commit", {}) lines.append( f"- {b['name']}{flag_str}" f"\n 最新 commit: {commit.get('short_id', 'N/A')} {commit.get('title', 'N/A')}" ) return "\n".join(lines) except GitLabAPIError as e: return f"列出分支失敗: {str(e)}" - src/gitlab_mcp/gitlab_client.py:372-384 (handler)The underlying client method that performs the API request to list repository branches from GitLab.
def list_branches( self, project_id: int | str, search: str = None, page: int = 1, per_page: int = 20, ) -> list[dict]: """GET /projects/:id/repository/branches""" pid = self._resolve_project_id(project_id) params = {"page": page, "per_page": per_page} if search: params["search"] = search return self._get_json(f"/projects/{pid}/repository/branches", params=params)