MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / handleFiles

Method handleFiles

src/mcp/tools.ts:8894–8954  ·  view source on GitHub ↗

* Handle codegraph_files - get project file structure from the index

(args: Record<string, unknown>)

Source from the content-addressed store, hash-verified

8892 * Handle codegraph_files - get project file structure from the index
8893 */
8894 private async handleFiles(args: Record<string, unknown>): Promise<ToolResult> {
8895 const cg = this.getCodeGraph(args.projectPath as string | undefined);
8896 const pathFilter = args.path as string | undefined;
8897 const pattern = args.pattern as string | undefined;
8898 const format = (args.format as 'tree' | 'flat' | 'grouped' | 'package') || 'tree';
8899 const includeMetadata = args.includeMetadata !== false;
8900 const maxDepth = args.maxDepth != null ? clamp(args.maxDepth as number, 1, 20) : undefined;
8901
8902 // Get all files from the index
8903 const allFiles = cg.getFiles();
8904
8905 if (allFiles.length === 0) {
8906 return this.textResult('No files indexed. Run `codegraph index` first.');
8907 }
8908
8909 // Filter by path prefix. Stored paths are project-relative POSIX (e.g.
8910 // "src/foo.ts"), but agents commonly pass project-root variants like "/",
8911 // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading
8912 // "/", "./" or "\". Normalize all of those before matching so the agent
8913 // gets results instead of falling back to Read/Glob (see #426).
8914 const normalizedFilter = pathFilter
8915 ? pathFilter
8916 .replace(/\\/g, '/')
8917 .replace(/^(?:\.?\/+)+/, '')
8918 .replace(/^\.$/, '')
8919 .replace(/\/+$/, '')
8920 : '';
8921 let files = normalizedFilter
8922 ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/'))
8923 : allFiles;
8924
8925 // Filter by glob pattern
8926 if (pattern) {
8927 const regex = this.globToRegex(pattern);
8928 files = files.filter(f => regex.test(f.path));
8929 }
8930
8931 if (files.length === 0) {
8932 return this.textResult(`No files found matching the criteria.`);
8933 }
8934
8935 // Format output
8936 let output: string;
8937 switch (format) {
8938 case 'flat':
8939 output = this.formatFilesFlat(files, includeMetadata);
8940 break;
8941 case 'grouped':
8942 output = this.formatFilesGrouped(files, includeMetadata);
8943 break;
8944 case 'package':
8945 output = this.formatFilesByPackage(cg.getProjectRoot(), files, includeMetadata);
8946 break;
8947 case 'tree':
8948 default:
8949 output = this.formatFilesTree(files, includeMetadata, maxDepth);
8950 break;
8951 }

Callers 1

dispatchToolMethod · 0.95

Calls 11

getCodeGraphMethod · 0.95
textResultMethod · 0.95
globToRegexMethod · 0.95
formatFilesFlatMethod · 0.95
formatFilesGroupedMethod · 0.95
formatFilesByPackageMethod · 0.95
formatFilesTreeMethod · 0.95
truncateOutputMethod · 0.95
clampFunction · 0.90
getFilesMethod · 0.80
getProjectRootMethod · 0.65

Tested by

no test coverage detected