Skip to main content
Glama
razorback16

MCP Git Repo Browser

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
NameRequiredDescriptionDefault
repo_urlYesThe URL of the Git repository

Implementation Reference

  • 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'],
      },
    },
  • 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'],
    },
  • 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}`);
      }
    }
  • 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;
    }

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/razorback16/mcp-git-repo-browser'

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