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

Method handleFileView

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

8423 * through validatePathWithinRoot (#527).
8424 */
8425 private async handleFileView(
8426 cg: CodeGraph,
8427 fileArg: string,
8428 opts: { offset?: number; limit?: number; symbolsOnly?: boolean } = {},
8429 ): Promise<ToolResult> {
8430 const normalize = (p: string) => p.replace(/\\/g, '/').replace(/^(?:\.?\/+)+/, '').replace(/\/+$/, '');
8431 const allFiles = cg.getFiles();
8432 if (allFiles.length === 0) return this.textResult('No files indexed. Run `codegraph index` first.');
8433
8434 // Resolve ONE spelling of the path against the index: exact, then
8435 // suffix-of-path, then substring — narrowing to a single file or handing
8436 // back the ambiguous set.
8437 const resolveOne = (want: string): { file?: (typeof allFiles)[number]; candidates: typeof allFiles } => {
8438 const wantLower = normalize(want).toLowerCase();
8439 let file = allFiles.find((f) => f.path.toLowerCase() === wantLower);
8440 let found: typeof allFiles = [];
8441 if (!file) {
8442 found = allFiles.filter((f) => f.path.toLowerCase().endsWith('/' + wantLower));
8443 if (found.length === 1) file = found[0];
8444 }
8445 if (!file && found.length === 0) {
8446 found = allFiles.filter((f) => f.path.toLowerCase().includes(wantLower));
8447 if (found.length === 1) file = found[0];
8448 }
8449 return { file, candidates: found };
8450 };
8451
8452 // Agents and humans paste file references WITH a line suffix — `a.ts:12`,
8453 // `a.ts:12-40`, `a.ts#L88`, `a.ts#L12-L40`. explore already strips exactly
8454 // these shapes (src/search/query-paths.ts); file-view used to treat them as
8455 // part of the filename and report an indexed file as missing (#1831).
8456 // The literal spelling is tried FIRST, so a file genuinely named `foo:12`
8457 // still resolves to itself; only when that finds nothing is the suffix
8458 // stripped, and then the range becomes the read window.
8459 const LINE_SUFFIX = /(?::(\d+)(?:-(\d+))?|#L(\d+)(?:-L?(\d+))?)$/;
8460 let { file: resolved, candidates } = resolveOne(fileArg);
8461 let shownArg = fileArg;
8462 if (!resolved) {
8463 const m = LINE_SUFFIX.exec(normalize(fileArg));
8464 const stripped = m ? fileArg.slice(0, fileArg.length - m[0].length) : '';
8465 const retry = stripped ? resolveOne(stripped) : undefined;
8466 if (m && retry && (retry.file || retry.candidates.length > 0)) {
8467 resolved = retry.file;
8468 candidates = retry.candidates;
8469 shownArg = stripped;
8470 const startLine = Number(m[1] ?? m[3]);
8471 const endRaw = m[2] ?? m[4];
8472 const endLine = endRaw === undefined ? undefined : Number(endRaw);
8473 if (Number.isFinite(startLine) && startLine > 0) {
8474 // An explicit offset/limit from the caller always wins over the
8475 // suffix. `:12` alone is a "start here" pointer (Read given only an
8476 // offset); `:12-40` pins both ends.
8477 opts = {
8478 ...opts,
8479 offset: opts.offset ?? startLine,
8480 limit: opts.limit ?? (endLine !== undefined && endLine >= startLine ? endLine - startLine + 1 : undefined),
8481 };
8482 }

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