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

Method handleSearch

src/mcp/tools.ts:3365–3399  ·  view source on GitHub ↗

* Handle codegraph_search

(args: Record<string, unknown>)

Source from the content-addressed store, hash-verified

3363 * Handle codegraph_search
3364 */
3365 private async handleSearch(args: Record<string, unknown>): Promise<ToolResult> {
3366 const query = this.validateString(args.query, 'query');
3367 if (typeof query !== 'string') return query;
3368
3369 const cg = this.getCodeGraph(args.projectPath as string | undefined);
3370 const rawKind = args.kind as string | undefined;
3371 // The schema enum says 'type' (what agents naturally reach for); the
3372 // NodeKind is 'type_alias'. Without the mapping, kind: "type" silently
3373 // matched nothing — a filter value we advertise must work.
3374 const kind = rawKind === 'type' ? 'type_alias' : rawKind;
3375 const rawLimit = Number(args.limit) || 10;
3376 const limit = clamp(rawLimit, 1, 100);
3377
3378 const results = cg.searchNodes(query, {
3379 limit,
3380 kinds: kind ? [kind as NodeKind] : undefined,
3381 });
3382
3383 if (results.length === 0) {
3384 return this.textResult(`No results found for "${query}"`);
3385 }
3386
3387 // Down-rank generated files within the FTS-returned set so a search
3388 // for "Send" surfaces the hand-written keeper before .pb.go stubs
3389 // that share the name. Stable: only reorders generated vs. not.
3390 const isGen = cg.generatedFilePredicate(results.map((r) => r.node.filePath));
3391 const ranked = [...results].sort((a, b) => {
3392 const aGen = isGen(a.node.filePath) ? 1 : 0;
3393 const bGen = isGen(b.node.filePath) ? 1 : 0;
3394 return aGen - bGen;
3395 });
3396
3397 const formatted = this.formatSearchResults(ranked);
3398 return this.answerResult(cg, this.truncateOutput(formatted), ranked.map(r => r.node.filePath));
3399 }
3400
3401 /**
3402 * Group symbol matches into DISTINCT DEFINITIONS — one group per

Callers 1

dispatchToolMethod · 0.95

Calls 11

validateStringMethod · 0.95
getCodeGraphMethod · 0.95
textResultMethod · 0.95
formatSearchResultsMethod · 0.95
answerResultMethod · 0.95
truncateOutputMethod · 0.95
clampFunction · 0.90
NumberInterface · 0.85
mapMethod · 0.80
searchNodesMethod · 0.65

Tested by

no test coverage detected