Skip to main content
Glama

git_directory_structure

Clone a Git repository and visualize its directory structure in a tree format using the Git Repo Browser MCP server.

Instructions

Clone a Git repository and return its directory structure in a tree format.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
repo_urlYesThe URL of the Git repository

Implementation Reference

  • The core handler function that implements the git_directory_structure tool. It clones the repository using cloneRepo, generates the directory tree using getDirectoryTree, and returns the tree as text content or an error message.
    export async function handleGitDirectoryStructure({ repo_url }) { try { const repoPath = await cloneRepo(repo_url); const tree = await getDirectoryTree(repoPath); return { content: [ { type: "text", text: tree, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error: ${error.message}`, }, ], isError: true, }; } }
  • Defines the input schema and metadata for the git_directory_structure tool in the toolsList, specifying the required repo_url parameter.
    { name: "git_directory_structure", description: "Clone a Git repository and return its directory structure in a tree format.", inputSchema: { type: "object", properties: { repo_url: { type: "string", description: "The URL of the Git repository", }, }, required: ["repo_url"], }, },
  • src/server.js:900-900 (registration)
    Registers the handleGitDirectoryStructure function to the 'git_directory_structure' tool name in the handlersMap used by the MCP server.
    git_directory_structure: handleGitDirectoryStructure,
  • Helper function called by the handler to clone the Git repository to a temporary directory, reusing existing clones when possible.
    export async function cloneRepo(repoUrl) { // Create deterministic directory name based on repo URL const repoHash = crypto .createHash("sha256") .update(repoUrl) .digest("hex") .slice(0, 12); const tempDir = path.join(os.tmpdir(), `github_tools_${repoHash}`); // Check if directory exists and is a valid git repo if (await fs.pathExists(tempDir)) { try { const git = simpleGit(tempDir); const remotes = await git.getRemotes(true); if (remotes.length > 0 && remotes[0].refs.fetch === repoUrl) { // Pull latest changes await git.pull(); return tempDir; } } catch (error) { // If there's any error with existing repo, clean it up await fs.remove(tempDir); } } // Create directory and clone repository await fs.ensureDir(tempDir); try { await simpleGit().clone(repoUrl, tempDir); return tempDir; } catch (error) { // Clean up on error await fs.remove(tempDir); throw new Error(`Failed to clone repository: ${error.message}`); } }
  • Helper function called by the handler to recursively generate an ASCII art tree representation of the directory structure.
    export async function getDirectoryTree(dirPath, prefix = "") { let output = ""; const entries = await fs.readdir(dirPath); entries.sort(); for (let i = 0; i < entries.length; i++) { const entry = entries[i]; if (entry.startsWith(".git")) continue; const isLast = i === entries.length - 1; const currentPrefix = isLast ? "└── " : "├── "; const nextPrefix = isLast ? " " : "│ "; const entryPath = path.join(dirPath, entry); output += prefix + currentPrefix + entry + "\n"; const stats = await fs.stat(entryPath); if (stats.isDirectory()) { output += await getDirectoryTree(entryPath, prefix + nextPrefix); } } return output; }

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/bsreeram08/git-commands-mcp'

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