git_directory_structure
Clone a Git repository and display its directory structure in a tree format to visualize project organization and file hierarchy.
Instructions
Clone a Git repository and return its directory structure in a tree format.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_url | Yes | The URL of the Git repository |
Implementation Reference
- src/index.js:155-178 (handler)The main handler function for the 'git_directory_structure' tool. It clones the specified Git repository using cloneRepo helper, generates the directory tree using getDirectoryTree helper, and returns the tree as text content.async 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, }; } }
- src/index.js:104-117 (registration)Tool registration in the ListTools response, including name, description, and input schema.{ 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/index.js:107-116 (schema)Input schema definition for the git_directory_structure tool, specifying the required repo_url parameter.inputSchema: { type: 'object', properties: { repo_url: { type: 'string', description: 'The URL of the Git repository', }, }, required: ['repo_url'], },
- src/index.js:17-49 (helper)Helper function to clone the Git repository to a temporary directory, reusing if already cloned.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) { 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}`); } }
- src/index.js:52-75 (helper)Helper function to recursively generate a tree-formatted string of the directory structure, excluding .git.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; }