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
| Name | Required | Description | Default |
|---|---|---|---|
| owner | Yes | GitHub repository owner/organization name | |
| repo | Yes | GitHub repository name |
Implementation Reference
- src/index.ts:207-235 (handler)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)}`, }, ], }; } },
- src/index.ts:203-206 (schema)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)}`, }, ], }; } }, );
- src/index.ts:60-77 (helper)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; }