Skip to content

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

fix(hosts): skip MCP config write when the servers key is not an object

Shrish Dwivedi9fd257ab15b0
diff --git a/src/hosts/mcp-config.ts b/src/hosts/mcp-config.ts
index 1eadc6d0..7f67e795 100644
--- a/src/hosts/mcp-config.ts
+++ b/src/hosts/mcp-config.ts
@@ -31,8 +31,11 @@ function mergeJsonKey(id: string, path: string, topKey: string, entry: object):
}
}
const bucket = (root[topKey] ??= {});
+ if (typeof bucket !== 'object' || bucket === null || Array.isArray(bucket)) {
+ return { id, path, action: 'skipped-unparseable' };
+ }
if (JSON.stringify(bucket.graft) === JSON.stringify(entry)) return { id, path, action: 'unchanged' };
- const action = bucket.graft === undefined && !existed ? 'created' : existed ? 'updated' : 'created';
+ const action = existed ? 'updated' : 'created';
bucket.graft = entry;
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, `${JSON.stringify(root, null, 2)}\n`);
diff --git a/test/hosts-mcp-config.test.ts b/test/hosts-mcp-config.test.ts
index 692d16f1..af015dab 100644
--- a/test/hosts-mcp-config.test.ts
+++ b/test/hosts-mcp-config.test.ts
@@ -64,3 +64,13 @@ test('codex TOML append preserves existing content', () => {
assert.match(toml, /\[mcp_servers\.other\]/);
assert.match(toml, /\[mcp_servers\.graft\]/);
});
+
+test('JSON with non-object mcpServers value is skipped', () => {
+ const repo = fresh(); const home = fresh();
+ mkdirSync(join(repo, '.cursor'), { recursive: true });
+ const badJson = '{"mcpServers": "not-an-object"}';
+ writeFileSync(join(repo, '.cursor', 'mcp.json'), badJson);
+ const w = registerMcpConfigs(repo, ['cursor'], { home });
+ assert.deepEqual(w.map((x) => x.action), ['skipped-unparseable']);
+ assert.equal(readFileSync(join(repo, '.cursor', 'mcp.json'), 'utf8'), badJson);
+});