Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
feat(mcp): graft_ask / graft_check / graft_blast_radius tool implementations
Shrish Dwivedi5d6451c136e4
diff --git a/src/mcp/tools.ts b/src/mcp/tools.tsnew file mode 100644index 00000000..692ee8a5--- /dev/null+++ b/src/mcp/tools.ts@@ -0,0 +1,87 @@+/**+ * The three MCP tools, as pure functions over the existing engine.+ * `callTool` never throws — hosts get soft errors as isError content.+ */+import { join } from 'node:path';+import { Graft } from '../engine.js';+import { formatAsk } from '../ask/ask.js';+import { formatCheckReport } from '../context/check.js';+import { formatGraphCheckReport } from '../graph/check.js';+import { formatBlastRadius } from '../claude/format.js';+import { readGraph, GRAPH_FILE, GRAPH_DIR } from '../graph/write.js';+import { contextDirFor } from '../context/node-file.js';++export interface ToolDef {+ name: string;+ description: string;+ inputSchema: object;+}++export const TOOLS: ToolDef[] = [+ {+ name: 'graft_ask',+ description:+ 'Query the repo context graph in plain words. Returns ranked nodes with exact file:line spans and the relevant source inlined — usually the full answer, no file reads needed.',+ inputSchema: {+ type: 'object',+ properties: {+ query: { type: 'string', description: 'what you want to understand, in plain words' },+ limit: { type: 'number', description: 'max results (default 5)' },+ },+ required: ['query'],+ },+ },+ {+ name: 'graft_check',+ description: 'Report whether the committed graph is in sync with the code (drift check).',+ inputSchema: { type: 'object', properties: {} },+ },+ {+ name: 'graft_blast_radius',+ description: 'List what depends on a file — who breaks if it changes.',+ inputSchema: {+ type: 'object',+ properties: { file: { type: 'string', description: 'repo-relative path, e.g. src/cache.ts' } },+ required: ['file'],+ },+ },+];++export function callTool(+ root: string,+ name: string,+ args: Record<string, unknown>,+): { text: string; isError: boolean } {+ try {+ switch (name) {+ case 'graft_ask': {+ 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 r = engine.ask(root, query, { limit, source: true });+ return { text: formatAsk(r), isError: false };+ }+ case 'graft_check': {+ const engine = new Graft();+ const r = engine.check(root);+ const g = engine.checkGraph(root);+ const parts = [formatCheckReport(r)];+ if (!g.missing) parts.push(formatGraphCheckReport(g));+ return { text: parts.join('\n\n'), isError: false };+ }+ case 'graft_blast_radius': {+ const file = String(args.file ?? '');+ if (!file) return { text: 'graft_blast_radius requires a file', isError: true };+ const w = readGraph(join(contextDirFor(root), GRAPH_DIR, GRAPH_FILE));+ if (!w) return { text: 'no graph found — run `graft build` first', isError: true };+ const br = formatBlastRadius(w, file);+ return { text: br ?? `no known dependents of ${file}`, isError: false };+ }+ default:+ return { text: `unknown tool: ${name}`, isError: true };+ }+ } catch (err) {+ return { text: err instanceof Error ? err.message : String(err), isError: true };+ }+}diff --git a/test/mcp-tools.test.ts b/test/mcp-tools.test.tsnew file mode 100644index 00000000..9656a224--- /dev/null+++ b/test/mcp-tools.test.ts@@ -0,0 +1,56 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { execFileSync } from 'node:child_process';+import { TOOLS, callTool } from '../src/mcp/tools.js';++function builtRepo(): string {+ const d = mkdtempSync(join(tmpdir(), 'graft-mcptools-'));+ 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');+ execFileSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', 'build', d], { stdio: 'pipe' });+ return d;+}++test('TOOLS lists the three tools with schemas', () => {+ assert.deepEqual(TOOLS.map((t) => t.name), ['graft_ask', 'graft_check', 'graft_blast_radius']);+ for (const t of TOOLS) {+ assert.ok(t.description.length > 0);+ assert.equal((t.inputSchema as { type: string }).type, 'object');+ }+});++test('graft_ask returns ranked hits for a built repo', () => {+ const d = builtRepo();+ const r = callTool(d, 'graft_ask', { query: 'how do I add numbers' });+ assert.equal(r.isError, false);+ assert.match(r.text, /add/);+ assert.match(r.text, /src\/math\.ts/);+});++test('graft_check reports the wiring state', () => {+ const d = builtRepo();+ const r = callTool(d, 'graft_check', {});+ assert.equal(r.isError, false);+ assert.match(r.text, /graph check: OK/);+});++test('graft_blast_radius names dependents of a file', () => {+ const d = builtRepo();+ const r = callTool(d, 'graft_blast_radius', { file: 'src/math.ts' });+ assert.equal(r.isError, false);+ assert.ok(r.text.length > 0);+});++test('unbuilt repo and unknown tool are soft errors', () => {+ const bare = mkdtempSync(join(tmpdir(), 'graft-mcptools-bare-'));+ const r1 = callTool(bare, 'graft_blast_radius', { file: 'x.ts' });+ assert.equal(r1.isError, true);+ assert.match(r1.text, /graft build/);+ const r2 = callTool(bare, 'nope', {});+ assert.equal(r2.isError, true);+ assert.match(r2.text, /unknown tool/i);+});