Skip to content

fix(mcp-migrator): handle malformed JSON in legacy .kilocode/mcp.json - #7012

Merged
marius-kilocode merged 1 commit into
Kilo-Org:mainfrom
marius-kiloclaw:fix/pr-6881-mcp-migrator-malformed-json
Mar 13, 2026
Merged

marius-kilocode merged 1 commit into
Kilo-Org:mainfrom
marius-kiloclaw:fix/pr-6881-mcp-migrator-malformed-json

Conversation

@marius-kiloclaw

Copy link
Copy Markdown
Contributor

What's wrong

readMcpSettings() calls JSON.parse() without a try/catch. In migrate(), the function iterates over [".kilocode", ".kilo"] directories to load MCP config with a "later wins" precedence strategy. If .kilocode/mcp.json exists but contains malformed JSON, the uncaught SyntaxError propagates out of the loop, and .kilo/mcp.json is never read.

The outer loadMcpConfig() catches the error and returns {} — silently losing all valid MCP server configuration from .kilo/mcp.json.

Scenario: User has a stale/corrupt .kilocode/mcp.json leftover from a previous install, and a perfectly valid .kilo/mcp.json. Their MCP servers silently fail to load.

The fix

Wrap the file read + JSON parse in readMcpSettings() with a try/catch. On parse failure, log a warning with the filepath and error, then return null so the migration loop continues to the next directory.

This is the minimal, correct fix — the function already returns null for non-existent files, so returning null for unparseable files is consistent.

Tests

Two regression tests added:

  1. Unit test (readMcpSettings): Verifies that a malformed JSON file returns null instead of throwing
  2. Integration test (migrate): Writes malformed JSON to .kilocode/mcp.json and valid config to .kilo/mcp.json, then verifies migrate() successfully returns the valid .kilo config

Both tests fail without the fix and pass with it.

Found during automated review of #6881. cc @marius-kilocode

readMcpSettings() called JSON.parse() without try/catch. In migrate(),
if .kilocode/mcp.json existed but contained malformed JSON, the uncaught
exception propagated out of the dual-read loop, preventing .kilo/mcp.json
from ever being read. The outer loadMcpConfig() catch returned {} instead
of the valid config — silently losing MCP server configuration.

Wrap the file read + parse in try/catch, log a warning, and return null
so the loop continues to the next config directory.

Regression tests added for both readMcpSettings (unit) and migrate
(integration) to prove the bug and verify the fix.

Refs: Kilo-Org#6881
@kilo-code-bot

kilo-code-bot Bot commented Mar 12, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/opencode/src/kilocode/mcp-migrator.ts
  • packages/opencode/test/kilocode/mcp-migrator.test.ts

Reviewed by gpt-5.4-20260305 · 79,842 tokens

@marius-kilocode
marius-kilocode merged commit 7473015 into Kilo-Org:main Mar 13, 2026
15 checks passed
jliounis pushed a commit to jliounis/kilocode that referenced this pull request May 18, 2026
…Kilo-Org#7012)

readMcpSettings() called JSON.parse() without try/catch. In migrate(),
if .kilocode/mcp.json existed but contained malformed JSON, the uncaught
exception propagated out of the dual-read loop, preventing .kilo/mcp.json
from ever being read. The outer loadMcpConfig() catch returned {} instead
of the valid config — silently losing MCP server configuration.

Wrap the file read + parse in try/catch, log a warning, and return null
so the loop continues to the next config directory.

Regression tests added for both readMcpSettings (unit) and migrate
(integration) to prove the bug and verify the fix.

Refs: Kilo-Org#6881
t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
…Kilo-Org#7012)

readMcpSettings() called JSON.parse() without try/catch. In migrate(),
if .kilocode/mcp.json existed but contained malformed JSON, the uncaught
exception propagated out of the dual-read loop, preventing .kilo/mcp.json
from ever being read. The outer loadMcpConfig() catch returned {} instead
of the valid config — silently losing MCP server configuration.

Wrap the file read + parse in try/catch, log a warning, and return null
so the loop continues to the next config directory.

Regression tests added for both readMcpSettings (unit) and migrate
(integration) to prove the bug and verify the fix.

Refs: Kilo-Org#6881

Co-authored-by: marius-kiloclaw <[email protected]>
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.

3 participants