Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(mcp): honor the global --dir override for the graph location

Shrish Dwivedi6a28a0a9f558
diff --git a/src/cli.ts b/src/cli.ts
index 478cb809..5363deb9 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -233,7 +233,8 @@ program
.action(async (dir: string) => {
const { resolve } = await import("node:path");
const { startMcpServer } = await import("./mcp/server.js");
- startMcpServer(resolve(dir));
+ const globalOpts = program.opts<{ dir?: string }>();
+ startMcpServer(resolve(dir), globalOpts.dir);
});
function traverseAction(kind: import("./graph/traverse-cli.js").TraverseKind) {
diff --git a/src/mcp/server.ts b/src/mcp/server.ts
index 439b0fed..f2edf059 100644
--- a/src/mcp/server.ts
+++ b/src/mcp/server.ts
@@ -17,7 +17,7 @@ function replyError(id: unknown, code: number, message: string): void {
send({ jsonrpc: '2.0', id, error: { code, message } });
}
-export function startMcpServer(root: string): void {
+export function startMcpServer(root: string, dirOverride?: string): void {
const rl = createInterface({ input: process.stdin, crlfDelay: Infinity });
rl.on('line', (line) => {
const text = line.trim();
@@ -54,7 +54,7 @@ export function startMcpServer(root: string): void {
if (isNotification) return;
const name = String(params?.name ?? '');
const args = (params?.arguments ?? {}) as Record<string, unknown>;
- const r = callTool(root, name, args);
+ const r = callTool(root, name, args, dirOverride);
reply(id, { content: [{ type: 'text', text: r.text }], isError: r.isError });
return;
}
diff --git a/src/mcp/tools.ts b/src/mcp/tools.ts
index 4bfa0d1a..1d71d350 100644
--- a/src/mcp/tools.ts
+++ b/src/mcp/tools.ts
@@ -105,6 +105,7 @@ export function callTool(
root: string,
name: string,
args: Record<string, unknown>,
+ dirOverride?: string,
): { text: string; isError: boolean } {
try {
switch (name) {
@@ -112,12 +113,12 @@ export function callTool(
const query = String(args.query ?? '');
if (!query) return { text: 'graft_ask requires a query', isError: true };
const limit = typeof args.limit === 'number' ? args.limit : 5;
- const engine = new Graft();
+ const engine = new Graft({ contextDir: dirOverride });
const r = engine.ask(root, query, { limit, source: true });
return { text: formatAsk(r), isError: false };
}
case 'graft_check': {
- const engine = new Graft();
+ const engine = new Graft({ contextDir: dirOverride });
const r = engine.check(root);
const g = engine.checkGraph(root);
const parts = [formatCheckReport(r)];
@@ -127,7 +128,7 @@ export function callTool(
case 'graft_blast_radius': {
const query = String(args.symbol ?? args.file ?? '');
if (!query) return { text: 'graft_blast_radius requires a file or symbol', isError: true };
- const w = loadGraphCached(contextDirFor(root));
+ const w = loadGraphCached(contextDirFor(root, dirOverride));
if (!w) return { text: NO_GRAPH, isError: true };
const matches = resolveSymbol(w, query);
if (matches.length === 0) return { text: unknownSymbolText(query), isError: true };
@@ -151,7 +152,7 @@ export function callTool(
case 'graft_callees': {
const symbol = String(args.symbol ?? '');
if (!symbol) return { text: `${name} requires a symbol`, isError: true };
- const w = loadGraphCached(contextDirFor(root));
+ const w = loadGraphCached(contextDirFor(root, dirOverride));
if (!w) return { text: NO_GRAPH, isError: true };
const inOpt = typeof args.in === 'string' && args.in ? { in: args.in } : {};
const matches = resolveSymbol(w, symbol, inOpt);
diff --git a/test/mcp-tools.test.ts b/test/mcp-tools.test.ts
index de19e43e..18585a7c 100644
--- a/test/mcp-tools.test.ts
+++ b/test/mcp-tools.test.ts
@@ -32,6 +32,18 @@ function chainRepo(): string {
return d;
}
+/** Graph lives in a NON-default dir (`<repo>/customgraph`, not `<repo>/graft`)
+ * — exercises the `--dir` override threaded through to `contextDirFor`. */
+function customDirRepo(): { repo: string; graphDir: string } {
+ const d = mkdtempSync(join(tmpdir(), 'graft-mcptools-customdir-'));
+ mkdirSync(join(d, 'src'), { recursive: true });
+ writeFileSync(join(d, 'src', 'math.ts'),
+ 'export function add(a: number, b: number): number {\n return a + b;\n}\nexport function sub(a: number, b: number): number {\n return add(a, -b);\n}\n');
+ const graphDir = join(d, 'customgraph');
+ execFileSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', 'build', d, '--dir', graphDir], { stdio: 'pipe' });
+ return { repo: d, graphDir };
+}
+
/** b.ts imports a.ts AND calls a function (`helper`) defined in a.ts. The
* `imports` edge targets a.ts's FILE id; the `calls` edge targets `helper`'s
* SYMBOL id — two different node ids, both "in" a.ts from a human's view. */
@@ -174,3 +186,22 @@ test('graft_blast_radius: unknown symbol is a soft isError with the check-spelli
assert.match(r.text, /no symbol "noSuchSymbolAnywhere" in the graph/);
assert.match(r.text, /check spelling/);
});
+
+test('callTool honors a dirOverride for a graph built in a non-default dir', () => {
+ const { repo, graphDir } = customDirRepo();
+
+ // With the override pointing at the actual graph location, tools find it.
+ const check = callTool(repo, 'graft_check', {}, graphDir);
+ assert.equal(check.isError, false);
+ assert.match(check.text, /graph check: OK/);
+
+ const callers = callTool(repo, 'graft_callers', { symbol: 'add' }, graphDir);
+ assert.equal(callers.isError, false);
+ assert.match(callers.text, /calls ← sub \(src\/math\.ts:/);
+
+ // Without the override, tools fall back to the default `<repo>/graft`,
+ // which doesn't exist here — must report no graph, not silently succeed.
+ const noOverride = callTool(repo, 'graft_callers', { symbol: 'add' });
+ assert.equal(noOverride.isError, true);
+ assert.match(noOverride.text, /graft build/);
+});