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

Method handleFiles

src/mcp/tools.ts:9180–9240  ·  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

9178 * Handle codegraph_files - get project file structure from the index
9179 */
9180 private async handleFiles(args: Record<string, unknown>): Promise<ToolResult> {
9181 const cg = this.getCodeGraph(args.projectPath as string | undefined);
9182 const pathFilter = args.path as string | undefined;
9183 const pattern = args.pattern as string | undefined;
9184 const format = (args.format as 'tree' | 'flat' | 'grouped' | 'package') || 'tree';
9185 const includeMetadata = args.includeMetadata !== false;
9186 const maxDepth = args.maxDepth != null ? clamp(args.maxDepth as number, 1, 20) : undefined;
9187
9188 // Get all files from the index
9189 const allFiles = cg.getFiles();
9190
9191 if (allFiles.length === 0) {
9192 return this.textResult('No files indexed. Run `codegraph index` first.');
9193 }
9194
9195 // Filter by path prefix. Stored paths are project-relative POSIX (e.g.
9196 // "src/foo.ts"), but agents commonly pass project-root variants like "/",
9197 // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading
9198 // "/", "./" or "\". Normalize all of those before matching so the agent
9199 // gets results instead of falling back to Read/Glob (see #426).
9200 const normalizedFilter = pathFilter
9201 ? pathFilter
9202 .replace(/\\/g, '/')
9203 .replace(/^(?:\.?\/+)+/, '')
9204 .replace(/^\.$/, '')
9205 .replace(/\/+$/, '')
9206 : '';
9207 let files = normalizedFilter
9208 ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/'))
9209 : allFiles;
9210
9211 // Filter by glob pattern
9212 if (pattern) {
9213 const regex = this.globToRegex(pattern);
9214 files = files.filter(f => regex.test(f.path));
9215 }
9216
9217 if (files.length === 0) {
9218 return this.textResult(`No files found matching the criteria.`);
9219 }
9220
9221 // Format output
9222 let output: string;
9223 switch (format) {
9224 case 'flat':
9225 output = this.formatFilesFlat(files, includeMetadata);
9226 break;
9227 case 'grouped':
9228 output = this.formatFilesGrouped(files, includeMetadata);
9229 break;
9230 case 'package':
9231 output = this.formatFilesByPackage(cg.getProjectRoot(), files, includeMetadata);
9232 break;
9233 case 'tree':
9234 default:
9235 output = this.formatFilesTree(files, includeMetadata, maxDepth);
9236 break;
9237 }

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