get_file_tree
Generate a detailed directory and file structure for GitHub repositories with customizable filters like depth, hidden files, extensions, and excluded paths using CodeCompass MCP.
Instructions
🌳 Get complete directory structure and file listing with filtering options. Focused on file system structure without content analysis.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| options | No | ||
| url | Yes | GitHub repository URL |
Implementation Reference
- src/tools/consolidated.ts:46-90 (schema)Tool schema definition including input schema, description, and parameters for get_file_tree{ name: 'get_file_tree', description: '🌳 Get complete directory structure and file listing with filtering options. Focused on file system structure without content analysis.', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'GitHub repository URL', }, options: { type: 'object', properties: { max_depth: { type: 'number', description: 'Maximum directory depth to traverse', default: 10, }, include_hidden: { type: 'boolean', description: 'Include hidden files and directories', default: false, }, file_extensions: { type: 'array', items: { type: 'string' }, description: 'Filter by file extensions (e.g., [".js", ".ts"])', }, exclude_paths: { type: 'array', items: { type: 'string' }, description: 'Paths to exclude from listing', default: ['node_modules', 'dist', 'build', '.git'], }, include_file_info: { type: 'boolean', description: 'Include file metadata (size, modified date)', default: true, }, }, }, }, required: ['url'], }, },
- src/index.ts:350-371 (handler)MCP tool handler function that extracts parameters, calls GitHub service getFileTree, formats success/error response with metadataasync function handleGetFileTree(args: any) { const { url, options = {} } = args; try { const tree = await githubService.getFileTree(url); const response = { file_tree: tree, metadata: { max_depth: options.max_depth || 10, include_hidden: options.include_hidden || false, total_files: tree.length, filtered_extensions: options.file_extensions || null, excluded_paths: options.exclude_paths || ['node_modules', 'dist', 'build', '.git'] } }; return formatToolResponse(createResponse(response, null, { tool: 'get_file_tree', url })); } catch (error) { return formatToolResponse(createResponse(null, error, { tool: 'get_file_tree', url })); } }
- src/services/github.ts:193-219 (handler)Core GitHub service method implementing file tree retrieval using GitHub Git Trees API, building hierarchical structureasync getFileTree(url: string, path?: string): Promise<FileNode[]> { const { owner, repo } = this.parseGitHubUrl(url); try { const { data: repoData } = await this.octokit.rest.repos.get({ owner, repo, }); const { data: treeData } = await this.octokit.rest.git.getTree({ owner, repo, tree_sha: repoData.default_branch, recursive: 'true', }); const fileTree = this.buildFileTree(treeData.tree); if (path) { return this.filterTreeByPath(fileTree, path); } return fileTree; } catch (error: any) { throw new Error(`Failed to fetch file tree: ${error.message}`); } }
- src/services/github.ts:488-527 (helper)Helper function that transforms flat GitHub git tree array into hierarchical FileNode tree structureprivate buildFileTree(gitTree: any[]): FileNode[] { const tree: FileNode[] = []; const pathMap = new Map(); // Sort by path to ensure proper ordering const sortedTree = gitTree.sort((a, b) => a.path.localeCompare(b.path)); for (const item of sortedTree) { const pathParts = item.path.split('/'); let currentLevel = tree; let currentPath = ''; for (let i = 0; i < pathParts.length; i++) { const part = pathParts[i]; currentPath = currentPath ? `${currentPath}/${part}` : part; let existingItem = currentLevel.find(node => node.name === part); if (!existingItem) { const isFile = i === pathParts.length - 1 && item.type === 'blob'; existingItem = { name: part, path: currentPath, type: isFile ? 'file' : 'directory', children: isFile ? undefined : [], size: isFile ? item.size : undefined, sha: item.sha, }; currentLevel.push(existingItem); } if (existingItem.children) { currentLevel = existingItem.children; } } } return tree; }
- src/index.ts:236-240 (registration)Tool registration via returning consolidatedTools list in ListToolsRequestHandlerserver.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: consolidatedTools, }; });
- src/index.ts:257-259 (registration)Dispatch registration in tool call switch statement mapping name to handler functioncase 'get_file_tree': result = await handleGetFileTree(args); break;