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

Method handleFileView

src/mcp/tools.ts:8674–8847  ·  view source on GitHub ↗

* FILE READ MODE: resolve `fileArg` (path or basename) to an indexed file and * read it like the Read tool — its current on-disk source with line numbers, * narrowable with `offset`/`limit` exactly as Read's are — preceded by a * one-line blast-radius header (which files depend on it). `sym

(
    cg: CodeGraph,
    fileArg: string,
    opts: { offset?: number; limit?: number; symbolsOnly?: boolean } = {},
  )

Source from the content-addressed store, hash-verified

8672 * through validatePathWithinRoot (#527).
8673 */
8674 private async handleFileView(
8675 cg: CodeGraph,
8676 fileArg: string,
8677 opts: { offset?: number; limit?: number; symbolsOnly?: boolean } = {},
8678 ): Promise<ToolResult> {
8679 const normalize = (p: string) => p.replace(/\\/g, '/').replace(/^(?:\.?\/+)+/, '').replace(/\/+$/, '');
8680 const allFiles = cg.getFiles();
8681 if (allFiles.length === 0) return this.textResult('No files indexed. Run `codegraph index` first.');
8682
8683 // Resolve ONE spelling of the path against the index: exact, then
8684 // suffix-of-path, then substring — narrowing to a single file or handing
8685 // back the ambiguous set.
8686 const resolveOne = (want: string): { file?: (typeof allFiles)[number]; candidates: typeof allFiles } => {
8687 const wantLower = normalize(want).toLowerCase();
8688 let file = allFiles.find((f) => f.path.toLowerCase() === wantLower);
8689 let found: typeof allFiles = [];
8690 if (!file) {
8691 found = allFiles.filter((f) => f.path.toLowerCase().endsWith('/' + wantLower));
8692 if (found.length === 1) file = found[0];
8693 }
8694 if (!file && found.length === 0) {
8695 found = allFiles.filter((f) => f.path.toLowerCase().includes(wantLower));
8696 if (found.length === 1) file = found[0];
8697 }
8698 return { file, candidates: found };
8699 };
8700
8701 // Agents and humans paste file references WITH a line suffix — `a.ts:12`,
8702 // `a.ts:12-40`, `a.ts#L88`, `a.ts#L12-L40`. explore already strips exactly
8703 // these shapes (src/search/query-paths.ts); file-view used to treat them as
8704 // part of the filename and report an indexed file as missing (#1831).
8705 // The literal spelling is tried FIRST, so a file genuinely named `foo:12`
8706 // still resolves to itself; only when that finds nothing is the suffix
8707 // stripped, and then the range becomes the read window.
8708 const LINE_SUFFIX = /(?::(\d+)(?:-(\d+))?|#L(\d+)(?:-L?(\d+))?)$/;
8709 let { file: resolved, candidates } = resolveOne(fileArg);
8710 let shownArg = fileArg;
8711 if (!resolved) {
8712 const m = LINE_SUFFIX.exec(normalize(fileArg));
8713 const stripped = m ? fileArg.slice(0, fileArg.length - m[0].length) : '';
8714 const retry = stripped ? resolveOne(stripped) : undefined;
8715 if (m && retry && (retry.file || retry.candidates.length > 0)) {
8716 resolved = retry.file;
8717 candidates = retry.candidates;
8718 shownArg = stripped;
8719 const startLine = Number(m[1] ?? m[3]);
8720 const endRaw = m[2] ?? m[4];
8721 const endLine = endRaw === undefined ? undefined : Number(endRaw);
8722 if (Number.isFinite(startLine) && startLine > 0) {
8723 // An explicit offset/limit from the caller always wins over the
8724 // suffix. `:12` alone is a "start here" pointer (Read given only an
8725 // offset); `:12-40` pins both ends.
8726 opts = {
8727 ...opts,
8728 offset: opts.offset ?? startLine,
8729 limit: opts.limit ?? (endLine !== undefined && endLine >= startLine ? endLine - startLine + 1 : undefined),
8730 };
8731 }

Callers 1

handleNodeMethod · 0.95

Calls 15

textResultMethod · 0.95
truncateOutputMethod · 0.95
isTestPathFunction · 0.90
validatePathWithinRootFunction · 0.90
NumberInterface · 0.85
getFilesMethod · 0.80
mapMethod · 0.80
pushMethod · 0.80
hasMethod · 0.80
execMethod · 0.65
getNodesInFileMethod · 0.65
getProjectRootMethod · 0.65

Tested by

no test coverage detected