Skip to main content
Glama

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
NameRequiredDescriptionDefault
project_idYes
searchNo
pageNo
per_pageNo

Implementation Reference

  • 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)}"
  • 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)

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