Files
codegraph/__tests__/mcp-tool-allowlist.test.ts
T
Colby MchenryandGitHub 68eaf0dbd8 feat(mcp): codegraph_explore as the sole primary tool + store coverage + overload disambiguation (#647)
## Summary

Completes the explore-overhaul arc: `codegraph_explore` becomes the single primary tool an agent reaches for, and its coverage + output shape are tuned so flow/architecture questions resolve with near-zero Read/Grep.

### What changed
- **explore is the sole primary tool** — removed `codegraph_context` (the fuzzy-input Read-trigger) and `codegraph_trace` (under-picked by agents); explore already surfaces the call flow among the symbols you name. A plain natural-language question now works as the query.
- **Store/handler coverage** — functions defined inside object literals (Zustand `create((set, get) => ({ … }))`, Redux/Pinia/MobX, exported handler/route maps) are indexed as real symbols, including calls through `useStore.getState().fn()` and destructured `const { fn } = useStore.getState()`. A general AST rule, not a per-lib hack.
- **Overload disambiguation** — explore leads with the *right* definition when a method name is overloaded across types (a PascalCase type token in the query biases to that type's own def); `codegraph_node` returns *every* overload's body in one call, with an optional `file`/`line` selector to pin one.
- **Method-atomic render** — explore never returns half a method; at the size budget it drops whole methods/files (and lists what it dropped) instead of truncating a body mid-method.
- **Native-read-shaped output** — per-call output is capped to ~24K with a 25K hard ceiling and concentrated into ~150–250-line flow windows, mirroring how the agent natively reads; repo size scales the *call* budget, not the per-call size (a larger response just gets externalized to a file the host Reads back).
- **Blast radius** folded into explore (dependents + covering tests, locations only).

### Benchmark (refreshed on this build)
Re-validated the 7-repo A/B on 2026-06-02 (Opus 4.8, effort=high, median of 4). WITH arm re-measured on this build, WITHOUT reused:

**~16% cheaper · 47% fewer tokens · 22% faster · 58% fewer tool calls** — 0 file reads on 6 of 7 repos (Gin ~1).

The arc trades larger, cache-heavy explore responses for guaranteed near-zero reads, so cost/token margins soften vs the prior build (Excalidraw and Tokio land at cost break-even) while time and tool-calls stay clear wins everywhere — consistent with the project's stated optimization target (latency + tool-calls, not token cost).

### Validation
- Full suite green: **1112 passed, 2 skipped**.
- 28/28 plain WITH runs across the 7 README repos completed clean; reads median 0 on 6/7.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-06-02 10:15:27 -05:00

59 lines
2.3 KiB
TypeScript

/**
* CODEGRAPH_MCP_TOOLS allowlist — lets an operator (or an A/B harness) trim the
* exposed MCP tool surface without touching the client config. Inert when unset.
* Filtering happens in ListTools (getTools) and is enforced again on execute().
*/
import { describe, it, expect, afterEach } from 'vitest';
import { ToolHandler } from '../src/mcp/tools';
const ENV = 'CODEGRAPH_MCP_TOOLS';
describe('CODEGRAPH_MCP_TOOLS allowlist', () => {
const original = process.env[ENV];
afterEach(() => {
if (original === undefined) delete process.env[ENV];
else process.env[ENV] = original;
});
const listed = () => new ToolHandler(null).getTools().map(t => t.name).sort();
it('exposes the full tool surface when unset', () => {
delete process.env[ENV];
const all = listed();
expect(all).toContain('codegraph_explore');
expect(all).not.toContain('codegraph_context');
expect(all).not.toContain('codegraph_trace');
expect(all.length).toBeGreaterThanOrEqual(8);
});
it('filters ListTools to the allowlisted short names', () => {
process.env[ENV] = 'explore,search,node';
expect(listed()).toEqual(['codegraph_explore', 'codegraph_node', 'codegraph_search']);
});
it('accepts fully-qualified codegraph_ names and ignores whitespace', () => {
process.env[ENV] = ' codegraph_explore , search ';
expect(listed()).toEqual(['codegraph_explore', 'codegraph_search']);
});
it('treats an empty/whitespace value as unset (full surface)', () => {
process.env[ENV] = ' ';
expect(listed().length).toBeGreaterThanOrEqual(8);
});
it('rejects a disabled tool on execute (defense in depth)', async () => {
process.env[ENV] = 'node';
const res = await new ToolHandler(null).execute('codegraph_explore', {});
expect(res.isError).toBe(true);
expect(res.content[0].text).toMatch(/disabled via CODEGRAPH_MCP_TOOLS/);
});
it('lets an allowlisted tool past the guard', async () => {
process.env[ENV] = 'search';
// No CodeGraph attached, so it fails *after* the allowlist guard — the
// "disabled" message must NOT appear, proving the guard passed it through.
const res = await new ToolHandler(null).execute('codegraph_search', { query: 'x' });
expect(res.content[0].text).not.toMatch(/disabled via CODEGRAPH_MCP_TOOLS/);
});
});