Skip to content

fix(cli): handle malformed MCP config files in remove detection#2574

Merged
fahreddinozcan merged 1 commit intomasterfrom
ctx7-1615-handle-mcp-config-detection-errors-in-remove-command
May 6, 2026
Merged

fix(cli): handle malformed MCP config files in remove detection#2574
fahreddinozcan merged 1 commit intomasterfrom
ctx7-1615-handle-mcp-config-detection-errors-in-remove-command

Conversation

@fahreddinozcan
Copy link
Copy Markdown
Contributor

@fahreddinozcan fahreddinozcan commented May 6, 2026

Summary

ctx7 remove crashed with an unhandled SyntaxError whenever any of the agents' well-known JSON config paths contained unparseable content (e.g. a hand-edited ~/.claude.json with a stray comma). readJsonConfig only caught file-read errors, not JSON.parse errors, and the detection path didn't wrap it.

This patch wraps the readJsonConfig call in hasMcpConfig with a try/catch that logs a warning naming the offending file and parse error, then skips that agent for detection. Detection continues for the remaining agents and the user gets an actionable line instead of a stack trace.

readJsonConfig itself stays strict so the write paths (setup, uninstallMcp) keep surfacing failures through their existing per-agent results-table error handling — silently swallowing parse errors there could risk overwriting a real-but-malformed user config.

Before

$ npx ctx7 remove
SyntaxError: Expected double-quoted property name in JSON at position 703 (line 21 column 25)
    at JSON.parse ()
    at readJsonConfig (...)
    at async hasMcpConfig (...)
    ...

After

$ npx ctx7 remove
⚠ Skipped /Users/.../.claude.json: could not parse (Expected double-quoted property name in JSON at position 703 (line 21 column 25))
⚠ No Context7 setup detected. Pass --claude, --cursor, --opencode, --codex, or --gemini.

Reported in #2561 where the user's pre-existing malformed ~/.claude.json (unrelated to ctx7) caused remove to crash with no indication of which file was at fault.

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.
@linear
Copy link
Copy Markdown

linear Bot commented May 6, 2026

@fahreddinozcan fahreddinozcan merged commit 6c71e4d into master May 6, 2026
3 of 4 checks passed
@fahreddinozcan fahreddinozcan deleted the ctx7-1615-handle-mcp-config-detection-errors-in-remove-command branch May 6, 2026 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants