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

Method handleCallers

src/mcp/tools.ts:3407–3491  ·  view source on GitHub ↗

* Handle codegraph_callers

(args: Record<string, unknown>)

Source from the content-addressed store, hash-verified

3405 * Handle codegraph_callers
3406 */
3407 private async handleCallers(args: Record<string, unknown>): Promise<ToolResult> {
3408 const symbol = this.validateString(args.symbol, 'symbol');
3409 if (typeof symbol !== 'string') return symbol;
3410
3411 const cg = this.getCodeGraph(args.projectPath as string | undefined);
3412 const limit = clamp((args.limit as number) || 20, 1, 100);
3413 const fileFilter = typeof args.file === 'string' ? args.file : undefined;
3414
3415 const allMatches = this.findAllSymbols(cg, symbol);
3416 if (allMatches.nodes.length === 0) {
3417 return this.textResult(`Symbol "${symbol}" not found in the codebase${allMatches.note}`);
3418 }
3419
3420 const { groups, filteredOut } = this.groupDefinitions(allMatches.nodes, fileFilter);
3421 const answerPaths = new Set(groups.flat().map(node => node.filePath));
3422 const filterNote = filteredOut
3423 ? `\n\n> **Note:** no definition of "${symbol}" matches file "${fileFilter}" — showing all definitions instead.`
3424 : '';
3425
3426 // Every site of each caller (#2909): one row per caller, at the lines its
3427 // calls sit on, and the symbol itself when it calls itself.
3428 const collect = (defNodes: Node[]) => {
3429 const callers: Node[] = [];
3430 const labels = new Map<string, string>();
3431 const sites = new Map<string, Edge[]>();
3432 const recursive = new Set<string>();
3433 for (const node of defNodes) {
3434 for (const c of cg.getCallSites(node.id, 'callers')) {
3435 const known = sites.get(c.node.id);
3436 if (known) {
3437 known.push(...c.edges);
3438 continue;
3439 }
3440 sites.set(c.node.id, [...c.edges]);
3441 if (c.recursive) recursive.add(c.node.id);
3442 callers.push(c.node);
3443 answerPaths.add(c.node.filePath);
3444 const label = this.edgeLabel(c.edges[0]!);
3445 if (label) labels.set(c.node.id, label);
3446 }
3447 }
3448 const locations = new Map(callers.map((n) => [n.id, this.callSiteLocation(n, sites.get(n.id) ?? [])]));
3449 return { callers, labels, locations, recursive };
3450 };
3451
3452 // Single definition (or same-file overloads): the familiar flat list.
3453 if (groups.length === 1) {
3454 const { callers, labels, locations, recursive } = collect(groups[0]!);
3455 if (callers.length === 0) {
3456 return this.answerResult(cg, `No callers found for "${symbol}"${allMatches.note}${filterNote}`, answerPaths);
3457 }
3458 // A successful `file` narrowing makes the multi-symbol aggregation note
3459 // stale — suppress it.
3460 const note = fileFilter && !filteredOut ? '' : allMatches.note;
3461 // Say when the cap cut the list (#1639, #1674): a truncated answer with
3462 // no marker reads as the complete set, and an agent under-counts from it.
3463 const cut = callers.length > limit
3464 ? `\n\n> Showing ${limit} of ${callers.length} callers; pass \`limit\` (up to 100) to widen.`

Callers 1

dispatchToolMethod · 0.95

Calls 15

validateStringMethod · 0.95
getCodeGraphMethod · 0.95
findAllSymbolsMethod · 0.95
textResultMethod · 0.95
groupDefinitionsMethod · 0.95
answerResultMethod · 0.95
formatNodeListMethod · 0.95
truncateOutputMethod · 0.95
definitionHeadingMethod · 0.95
formatNodeListEntryMethod · 0.95
clampFunction · 0.90
mapMethod · 0.80

Tested by

no test coverage detected