Skip to content

Commit 6c71e4d

Browse files
fix(cli): handle malformed MCP config files in remove detection (#2574)
readJsonConfig only caught file-read errors, not JSON.parse errors. During `ctx7 remove`, the detector iterates every agent's well-known config path; an unparseable JSON file at any of them (e.g. a hand-edited ~/.claude.json) crashed the command with an unhandled SyntaxError before it could do anything. Wrap the readJsonConfig call in hasMcpConfig with a try/catch that logs a warning naming the path and parse error and skips that agent. Keep readJsonConfig itself strict so write paths in setup and uninstallMcp continue to surface failures via their existing handling.
1 parent 4056850 commit 6c71e4d

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ctx7": patch
3+
---
4+
5+
Handle malformed MCP config files gracefully during `ctx7 remove` agent detection. Previously, an unparseable JSON config at any agent's well-known path (e.g. a hand-edited `~/.claude.json`) would crash the command with an unhandled `SyntaxError` before it could do anything. The detector now skips the offending file and logs a warning naming the path and parse error so the user can fix it, while detection continues for the remaining agents.

packages/cli/src/commands/remove.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,15 @@ async function hasMcpConfig(agentName: SetupAgent, scope: Scope): Promise<boolea
198198
return readTomlServerExists(mcpPath, "context7");
199199
}
200200

201-
const existing = await readJsonConfig(mcpPath);
201+
let existing: Record<string, unknown>;
202+
try {
203+
existing = await readJsonConfig(mcpPath);
204+
} catch (err) {
205+
log.warn(
206+
`Skipped ${mcpPath}: could not parse (${err instanceof Error ? err.message : String(err)})`
207+
);
208+
return false;
209+
}
202210
const section = existing[agent.mcp.configKey];
203211
return (
204212
!!section && typeof section === "object" && !Array.isArray(section) && "context7" in section

0 commit comments

Comments
 (0)