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

Method handleFiles

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

8719 * Handle codegraph_files - get project file structure from the index
8720 */
8721 private async handleFiles(args: Record<string, unknown>): Promise<ToolResult> {
8722 const cg = this.getCodeGraph(args.projectPath as string | undefined);
8723 const pathFilter = args.path as string | undefined;
8724 const pattern = args.pattern as string | undefined;
8725 const format = (args.format as 'tree' | 'flat' | 'grouped' | 'package') || 'tree';
8726 const includeMetadata = args.includeMetadata !== false;
8727 const maxDepth = args.maxDepth != null ? clamp(args.maxDepth as number, 1, 20) : undefined;
8728
8729 // Get all files from the index
8730 const allFiles = cg.getFiles();
8731
8732 if (allFiles.length === 0) {
8733 return this.textResult('No files indexed. Run `codegraph index` first.');
8734 }
8735
8736 // Filter by path prefix. Stored paths are project-relative POSIX (e.g.
8737 // "src/foo.ts"), but agents commonly pass project-root variants like "/",
8738 // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading
8739 // "/", "./" or "\". Normalize all of those before matching so the agent
8740 // gets results instead of falling back to Read/Glob (see #426).
8741 const normalizedFilter = pathFilter
8742 ? pathFilter
8743 .replace(/\\/g, '/')
8744 .replace(/^(?:\.?\/+)+/, '')
8745 .replace(/^\.$/, '')
8746 .replace(/\/+$/, '')
8747 : '';
8748 let files = normalizedFilter
8749 ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/'))
8750 : allFiles;
8751
8752 // Filter by glob pattern
8753 if (pattern) {
8754 const regex = this.globToRegex(pattern);
8755 files = files.filter(f => regex.test(f.path));
8756 }
8757
8758 if (files.length === 0) {
8759 return this.textResult(`No files found matching the criteria.`);
8760 }
8761
8762 // Format output
8763 let output: string;
8764 switch (format) {
8765 case 'flat':
8766 output = this.formatFilesFlat(files, includeMetadata);
8767 break;
8768 case 'grouped':
8769 output = this.formatFilesGrouped(files, includeMetadata);
8770 break;
8771 case 'package':
8772 output = this.formatFilesByPackage(cg.getProjectRoot(), files, includeMetadata);
8773 break;
8774 case 'tree':
8775 default:
8776 output = this.formatFilesTree(files, includeMetadata, maxDepth);
8777 break;
8778 }

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