Skip to main content
Glama

list_repository_tree

Browse and list files and directories in a GitLab repository to explore project structure, with options for specific paths, branches, and recursive viewing.

Instructions

瀏覽 Repository 檔案結構

Args: project_id: 專案 ID 或路徑 path: 目錄路徑(預設為根目錄) ref: 分支或標籤(預設為預設分支) recursive: 是否遞迴列出子目錄

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
project_idYes
pathNo
refNo
recursiveNo

Implementation Reference

  • The MCP tool registration and handler function for `list_repository_tree`.
    @mcp.tool()
    def list_repository_tree(project_id: int | str, path: str = "",
                             ref: str = None, recursive: bool = False) -> str:
        """瀏覽 Repository 檔案結構
    
        Args:
            project_id: 專案 ID 或路徑
            path: 目錄路徑(預設為根目錄)
            ref: 分支或標籤(預設為預設分支)
            recursive: 是否遞迴列出子目錄
        """
        try:
            client = get_client()
            items = client.list_repository_tree(project_id, path=path, ref=ref,
                                                 recursive=recursive, per_page=100)
            if not items:
                return "此路徑下沒有檔案"
    
            path_display = path or "/"
            lines = [f"Repository 檔案結構({path_display}):\n"]
    
            # 排序:目錄在前,檔案在後
            dirs = [i for i in items if i.get("type") == "tree"]
            files = [i for i in items if i.get("type") == "blob"]
    
            for d in dirs:
                lines.append(f"📁 {d['name']}/")
            for f in files:
                lines.append(f"📄 {f['name']}")
  • The actual GitLab API client implementation of `list_repository_tree` called by the MCP tool.
    def list_repository_tree(
        self,
        project_id: int | str,
        path: str = "",
        ref: str = None,
        recursive: bool = False,
        page: int = 1,
        per_page: int = 20,
    ) -> list[dict]:
        """GET /projects/:id/repository/tree"""
        pid = self._resolve_project_id(project_id)
        params = {"page": page, "per_page": per_page}
        if path:
            params["path"] = path
        if ref:
            params["ref"] = ref
        if recursive:
            params["recursive"] = True
        return self._get_json(f"/projects/{pid}/repository/tree", 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