fix: idempotent config writes - don't overwrite existing entries or add URL duplicates

Closed
SB
8mo ago
+512-14

Problem

When add-mcp is called with -y (e.g. from automation tools like skillpm), it auto-detects agents and writes to all of them. On repeated runs this causes:

  1. Existing entries overwritten - custom args, auth config, or other user modifications are lost. For example, a workiq entry with custom args gets its args array replaced, losing extra arguments the user had added.

  2. URL duplicates - if a URL is already configured under a different name (e.g. powerbi-remote with custom copilotTokenAuthentication), add-mcp adds a new bare entry for the same URL without checking.

  3. Unnecessary reformatting - the entire config section is replaced via jsonc.modify, reformatting all sibling entries even when only one new entry is being added.

Fix

src/installer.ts

  • Before writing, read existing config and check if the server name already exists -> skip
  • Check if any existing entry has the same URL -> skip
  • InstallResult gains an optional skipped field so callers can distinguish a fresh install from a no-op

src/formats/json.ts

  • Changed from "merge everything + replace entire section" to "only add entries not present by name"
  • Uses per-entry jsonc.modify at the [configKey, serverName] path instead of replacing the whole configKey section

src/formats/toml.ts

  • Same skip-existing-entries logic for TOML configs (Codex)

Tests

4 new tests in tests/installer.test.ts:

  • Skips when server name already exists
  • Skips when URL already exists under a different name
  • Does not overwrite existing entry args
  • Preserves existing entries when adding new ones

All 24 installer tests pass. All 79 total tests pass.