Skip to content

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

feat(hosts): MCP server registration writers (JSON keyed merge + TOML append)

Shrish Dwivedi664c517487ea
diff --git a/src/hosts/mcp-config.ts b/src/hosts/mcp-config.ts
new file mode 100644
index 00000000..1eadc6d0
--- /dev/null
+++ b/src/hosts/mcp-config.ts
@@ -0,0 +1,84 @@
+/**
+ * Register the graft MCP server in each host's config.
+ * JSON hosts get a keyed merge (other servers preserved; unparseable files
+ * are never rewritten). The TOML host gets an append-if-absent section.
+ */
+import { readFileSync, writeFileSync, mkdirSync, existsSync, statSync } from 'node:fs';
+import { dirname, join } from 'node:path';
+import { homedir } from 'node:os';
+
+export interface McpWrite {
+ id: string;
+ path: string;
+ action: 'created' | 'updated' | 'unchanged' | 'skipped-unparseable';
+}
+
+const SERVER_ENTRY = { command: 'npx', args: ['-y', '@nanonets/graft', 'mcp'] };
+const OPENCODE_ENTRY = { type: 'local', command: ['npx', '-y', '@nanonets/graft', 'mcp'], enabled: true };
+
+function dirExists(p: string): boolean {
+ try { return statSync(p).isDirectory(); } catch { return false; }
+}
+
+function mergeJsonKey(id: string, path: string, topKey: string, entry: object): McpWrite {
+ let root: Record<string, any> = {};
+ const existed = existsSync(path);
+ if (existed) {
+ try {
+ root = JSON.parse(readFileSync(path, 'utf8'));
+ } catch {
+ return { id, path, action: 'skipped-unparseable' };
+ }
+ }
+ const bucket = (root[topKey] ??= {});
+ if (JSON.stringify(bucket.graft) === JSON.stringify(entry)) return { id, path, action: 'unchanged' };
+ const action = bucket.graft === undefined && !existed ? 'created' : existed ? 'updated' : 'created';
+ bucket.graft = entry;
+ mkdirSync(dirname(path), { recursive: true });
+ writeFileSync(path, `${JSON.stringify(root, null, 2)}\n`);
+ return { id, path, action };
+}
+
+function upsertCodexToml(id: string, path: string): McpWrite {
+ const existed = existsSync(path);
+ const text = existed ? readFileSync(path, 'utf8') : '';
+ if (/^\[mcp_servers\.graft\]$/m.test(text)) return { id, path, action: 'unchanged' };
+ const section = `[mcp_servers.graft]\ncommand = "npx"\nargs = ["-y", "@nanonets/graft", "mcp"]\n`;
+ const sep = text.length === 0 ? '' : text.endsWith('\n') ? '\n' : '\n\n';
+ mkdirSync(dirname(path), { recursive: true });
+ writeFileSync(path, `${text}${sep}${section}`);
+ return { id, path, action: existed ? 'updated' : 'created' };
+}
+
+export function registerMcpConfigs(
+ repo: string,
+ ids: string[],
+ opts: { home?: string } = {},
+): McpWrite[] {
+ const home = opts.home ?? homedir();
+ const out: McpWrite[] = [];
+ for (const id of ids) {
+ switch (id) {
+ case 'cursor':
+ out.push(mergeJsonKey(id, join(repo, '.cursor', 'mcp.json'), 'mcpServers', SERVER_ENTRY));
+ break;
+ case 'gemini':
+ out.push(mergeJsonKey(id, join(repo, '.gemini', 'settings.json'), 'mcpServers', SERVER_ENTRY));
+ break;
+ case 'kiro':
+ out.push(mergeJsonKey(id, join(repo, '.kiro', 'settings', 'mcp.json'), 'mcpServers', SERVER_ENTRY));
+ break;
+ case 'agents':
+ if (dirExists(join(home, '.codex'))) {
+ out.push(upsertCodexToml('codex', join(home, '.codex', 'config.toml')));
+ }
+ if (dirExists(join(home, '.config', 'opencode'))) {
+ out.push(mergeJsonKey('opencode', join(repo, 'opencode.json'), 'mcp', OPENCODE_ENTRY));
+ }
+ break;
+ default:
+ break; // copilot / windsurf / claude: no MCP target in this phase
+ }
+ }
+ return out;
+}
diff --git a/test/hosts-mcp-config.test.ts b/test/hosts-mcp-config.test.ts
new file mode 100644
index 00000000..692d16f1
--- /dev/null
+++ b/test/hosts-mcp-config.test.ts
@@ -0,0 +1,66 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, existsSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { registerMcpConfigs } from '../src/hosts/mcp-config.js';
+
+function fresh(): string { return mkdtempSync(join(tmpdir(), 'graft-mcpcfg-')); }
+
+test('cursor/gemini/kiro get repo-local JSON entries', () => {
+ const repo = fresh(); const home = fresh();
+ const w = registerMcpConfigs(repo, ['cursor', 'gemini', 'kiro'], { home });
+ assert.deepEqual(w.map((x) => x.action), ['created', 'created', 'created']);
+ const cursor = JSON.parse(readFileSync(join(repo, '.cursor', 'mcp.json'), 'utf8'));
+ assert.deepEqual(cursor.mcpServers.graft, { command: 'npx', args: ['-y', '@nanonets/graft', 'mcp'] });
+ assert.ok(existsSync(join(repo, '.gemini', 'settings.json')));
+ assert.ok(existsSync(join(repo, '.kiro', 'settings', 'mcp.json')));
+});
+
+test('existing config keys are preserved; re-run is unchanged', () => {
+ const repo = fresh(); const home = fresh();
+ mkdirSync(join(repo, '.cursor'), { recursive: true });
+ writeFileSync(join(repo, '.cursor', 'mcp.json'), JSON.stringify({ mcpServers: { other: { command: 'x' } } }));
+ registerMcpConfigs(repo, ['cursor'], { home });
+ const cfg = JSON.parse(readFileSync(join(repo, '.cursor', 'mcp.json'), 'utf8'));
+ assert.ok(cfg.mcpServers.other, 'foreign server preserved');
+ assert.ok(cfg.mcpServers.graft);
+ const again = registerMcpConfigs(repo, ['cursor'], { home });
+ assert.deepEqual(again.map((x) => x.action), ['unchanged']);
+});
+
+test('unparseable JSON is never clobbered', () => {
+ const repo = fresh(); const home = fresh();
+ mkdirSync(join(repo, '.cursor'), { recursive: true });
+ writeFileSync(join(repo, '.cursor', 'mcp.json'), '{ not json');
+ const w = registerMcpConfigs(repo, ['cursor'], { home });
+ assert.deepEqual(w.map((x) => x.action), ['skipped-unparseable']);
+ assert.equal(readFileSync(join(repo, '.cursor', 'mcp.json'), 'utf8'), '{ not json');
+});
+
+test('agents id: codex TOML + opencode JSON, gated on home dirs', () => {
+ const repo = fresh(); const home = fresh();
+ assert.deepEqual(registerMcpConfigs(repo, ['agents'], { home }), [], 'nothing without home dirs');
+ mkdirSync(join(home, '.codex'), { recursive: true });
+ mkdirSync(join(home, '.config', 'opencode'), { recursive: true });
+ const w = registerMcpConfigs(repo, ['agents'], { home });
+ assert.equal(w.length, 2);
+ const toml = readFileSync(join(home, '.codex', 'config.toml'), 'utf8');
+ assert.match(toml, /^\[mcp_servers\.graft\]$/m);
+ assert.match(toml, /"@nanonets\/graft"/);
+ const oc = JSON.parse(readFileSync(join(repo, 'opencode.json'), 'utf8'));
+ assert.equal(oc.mcp.graft.type, 'local');
+ const again = registerMcpConfigs(repo, ['agents'], { home });
+ assert.deepEqual(again.map((x) => x.action).sort(), ['unchanged', 'unchanged']);
+});
+
+test('codex TOML append preserves existing content', () => {
+ const repo = fresh(); const home = fresh();
+ mkdirSync(join(home, '.codex'), { recursive: true });
+ writeFileSync(join(home, '.codex', 'config.toml'), 'model = "o3"\n\n[mcp_servers.other]\ncommand = "x"\n');
+ registerMcpConfigs(repo, ['agents'], { home });
+ const toml = readFileSync(join(home, '.codex', 'config.toml'), 'utf8');
+ assert.match(toml, /model = "o3"/);
+ assert.match(toml, /\[mcp_servers\.other\]/);
+ assert.match(toml, /\[mcp_servers\.graft\]/);
+});