Skip to main content
Glama
shanksxz

GitHub Repository MCP Server

get-repo-structure

Retrieve the file and folder structure of a GitHub repository to understand its organization and contents for analysis or integration.

Instructions

Get the structure of a GitHub repository

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
ownerYesGitHub repository owner/organization name
repoYesGitHub repository name

Implementation Reference

  • The handler function for the 'get-repo-structure' tool. It retrieves all file paths from the repository using the getAllFiles helper, sorts them, and returns a formatted text response with the structure or an error message.
    async ({ owner, repo }) => {
      try {
        const allFiles = await getAllFiles(owner, repo);
    
        const fileStructure = allFiles
          .map(file => file.path)
          .sort()
          .join('\n');
    
        return {
          content: [
            {
              type: "text",
              text: `Repository Structure for ${owner}/${repo}:\n\n${fileStructure}`,
            },
          ],
        };
      } catch (error) {
        console.error("Error fetching repository structure:", error);
        return {
          content: [
            {
              type: "text",
              text: `Error fetching repository structure: ${error instanceof Error ? error.message : String(error)}`,
            },
          ],
        };
      }
    },
  • Input schema for the 'get-repo-structure' tool, defining 'owner' and 'repo' parameters using Zod.
    {
      owner: z.string().describe("GitHub repository owner/organization name"),
      repo: z.string().describe("GitHub repository name"),
    },
  • src/index.ts:200-236 (registration)
    Registration of the 'get-repo-structure' tool using server.tool(), including description, schema, and inline handler.
    server.tool(
      "get-repo-structure",
      "Get the structure of a GitHub repository",
      {
        owner: z.string().describe("GitHub repository owner/organization name"),
        repo: z.string().describe("GitHub repository name"),
      },
      async ({ owner, repo }) => {
        try {
          const allFiles = await getAllFiles(owner, repo);
    
          const fileStructure = allFiles
            .map(file => file.path)
            .sort()
            .join('\n');
    
          return {
            content: [
              {
                type: "text",
                text: `Repository Structure for ${owner}/${repo}:\n\n${fileStructure}`,
              },
            ],
          };
        } catch (error) {
          console.error("Error fetching repository structure:", error);
          return {
            content: [
              {
                type: "text",
                text: `Error fetching repository structure: ${error instanceof Error ? error.message : String(error)}`,
              },
            ],
          };
        }
      },
    );
  • Helper function getAllFiles that recursively traverses the repository directory structure to collect all file paths, used by the get-repo-structure handler.
    async function getAllFiles(owner: string, repo: string, path: string = ""): Promise<{ path: string, type: string }[]> {
      const contents = await getRepoContents(owner, repo, path);
      let allFiles: { path: string, type: string }[] = [];
    
      for (const item of contents) {
        if (item.type === 'file') {
          allFiles.push({
            path: item.path,
            type: 'file'
          });
        } else if (item.type === 'dir') {
          const subFiles = await getAllFiles(owner, repo, item.path);
          allFiles = [...allFiles, ...subFiles];
        }
      }
    
      return allFiles;
    }

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/shanksxz/gh-mcp-server'

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