Skip to content

feat(mcp): add preserveUnownedMcpServers to keep host-app Codex CLI servers - #4028

Merged
dyoshikawa merged 3 commits into
mainfrom
resolve-issue-4025-codexcli-preserve-unowned-mcp
Nov 30, 2026
Merged

dyoshikawa merged 3 commits into
mainfrom
resolve-issue-4025-codexcli-preserve-unowned-mcp

Conversation

@dyoshikawa

Copy link
Copy Markdown
Owner

Closes #4025

Summary

Codex CLI MCP generation replaced the whole mcp_servers table of .codex/config.toml / ~/.codex/config.toml with the servers declared in .rulesync/mcp.jsonc. Servers the Codex desktop app writes into that table itself (e.g. node_repl) were deleted on every generate, and generate --check reported the file as out of date.

This adds an opt-in preserveUnownedMcpServers key to rulesync.jsonc (default false, so behavior is unchanged):

  • CodexcliMcp.fromRulesyncMcp keeps every existing [mcp_servers.<name>] table the sources do not declare, verbatim, and writes the declared servers over it. A declared server wins on a name clash (including a declared server dropped for having no non-empty configuration, which is not resurrected from the old file).
  • Other keys in config.toml are untouched as before; per-tool approval merging for declared servers is unchanged.
  • generate --check sees the same merged content, so preserved servers do not report drift.
  • Plumbing: Config.getPreserveUnownedMcpServers(), resolver default/pick, McpProcessor({ preserveUnownedMcpServers }) → fromRulesyncMcp({ preserveUnowned }) (new optional field on ToolMcpFromRulesyncMcpParams).
  • Docs: docs/guide/configuration.md and a Codex CLI note in docs/reference/file-formats.md (docs-content regenerated).

Tests

  • codexcli-mcp.test.ts: default replaces the table; preserve keeps unowned + declared wins; generating twice is byte-identical; empty sources keep unowned servers (twice, stable); a dropped declared server is not carried over.
  • mcp-processor.test.ts: the flag is forwarded as preserveUnowned (default false).
  • config-resolver.test.ts: read from rulesync.jsonc, default false.
  • generate.test.ts: the config value reaches McpProcessor.
  • E2E (e2e-mcp.spec.ts, global mode): generate → --check reports up to date → generate again → --delete twice; config.toml stays byte-identical and the host-app server survives.

Design decisions

  • Config-file only, no CLI flag. Mirrors preserveUnownedHooks / preserveUnownedSkills / preserveUnmanagedRuleContent, which are all config-file only because ownership is a property of the destination file, not of a single run. (The issue suggested a CLI flag too; I kept consistency with the existing siblings.)
  • Codex CLI only. Codex is the case reported, and its config.toml is a file the host app actively writes server entries into. Other targets that use applySharedConfigPatch with replace-owned-keys have heterogeneous server shapes/keys; extending the option to them is possible later via the same preserveUnowned param on ToolMcpFromRulesyncMcpParams, which every other adapter currently ignores (documented on the type and in the docs).
  • No ownership lock file. Like preserveUnownedSkills, Rulesync keeps no record of servers it generated earlier, so with the option on a server removed from mcp.jsonc is left behind and must be removed by hand. This is documented as the trade-off. A lock-based approach like preserveUnownedHooks would add a new file next to ~/.codex/config.toml; I kept the change minimal and can follow up if wanted.
  • Delete / retire. Codex config.toml is non-deletable (isDeletable() === false), so --delete and retireTargets never touch it with or without the option; the E2E test runs --delete twice to confirm preserved entries survive.
  • Ordering. Preserved servers are emitted first, declared servers after; the result is stable across runs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4

dyoshikawa and others added 3 commits November 30, 2026 22:43
…ervers

Codex CLI generation replaced the whole `mcp_servers` table of
`config.toml`, so servers the Codex desktop app writes there itself (such
as `node_repl`) were deleted on every `generate` and `--check` reported
drift. With the new config-file-only `preserveUnownedMcpServers` option,
every `[mcp_servers.<name>]` table the sources do not declare is kept
verbatim and the declared servers are written over it (declared wins on a
name clash). Default stays off.

Closes #4025

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…version

Build the preserveUnownedMcpServers exclusion set from every declared
server name as Codex normalizes it, so a declared server dropped for an
invalid transport does not resurrect an older same-name entry. Document
that servers no longer targeted at Codex CLI are left behind too, and
point to the option from the global-mode guide.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
@dyoshikawa
dyoshikawa merged commit f61e2e5 into main Nov 30, 2026
10 checks passed
@dyoshikawa
dyoshikawa deleted the resolve-issue-4025-codexcli-preserve-unowned-mcp branch November 30, 2026 14:08
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.

codexcli MCP generation deletes mcp_servers entries written by the host app; request an MCP "preserve unowned" option

1 participant