Importing an MCP server erases unrelated comments and disabled settings from the project config
Importing one Claude Code MCP server through the app server rewrites the entire existing .codex/config.toml. The import reports success, but setup notes and commented-out settings elsewhere in the file disappear.
Environment Codex CLI 0.169.0, Linux x86_64. Reproduced in a disposable local Git repository with a fresh Codex home. No model request or live MCP server is needed.
Steps to reproduce
-
Initialize an empty Git repository and create
.codex/config.tomlwith these contents:# NOTE: retain these project-specific setup instructions. # model = "other-model" model = "gpt-5.4" # current selection -
Create
.mcp.jsonat the repository root:{"mcpServers":{"demo":{"command":"echo","args":["demo"]}}} -
Start
codex app-server --stdioin the repo. Sendinitializewith{"clientInfo":{"name":"repro","version":"1"},"capabilities":{"experimentalApi":true}}, then theinitializednotification. -
Call
externalAgentConfig/detectwith{"includeHome":false,"cwds":["<absolute repo path>"]}. Pass its returneditemsarray toexternalAgentConfig/importasmigrationItems. -
Wait for
externalAgentConfig/import/completed, then read.codex/config.toml.
Observed The completion notification lists demo under successes with no failures. The entire file now reads:
model = "gpt-5.4"
[mcp_servers.demo]
command = "echo"
args = ["demo"]
Expected Add mcp_servers.demo while preserving the existing config text, including comments and disabled settings. In an unversioned config those notes can be difficult to recover.
Suggested fix Merge the new entries into a formatting-preserving TOML document and check the untouched portion byte-for-byte in a regression test. The import currently deserializes the entire file into toml::Value and serializes it again. Related issue #45432 covers codex mcp add/remove; this reproduction uses the external-agent migration handler. Import code
One extra source-specific repair boundary beyond the MCP-only reproduction: two distinct migration item types currently perform the same destructive rewrite of an existing config, so fixing only McpServerConfig would leave the same note/disabled-setting loss behind.
On current main:
external-agent-migration/src/service.rs:573-619handles the generalConfigmigration item. It parses the existing TOML intotoml::Value, merges missing values and callswrite_toml_file.service.rs:622-657separately handlesMcpServerConfigwith the sametoml::Valueround-trip. Both can target either repository.codex/config.tomlor user-homeconfig.toml.external-agent-migration/src/config_values.rs:77-81implements their sharedwrite_toml_fileastoml::to_string_pretty+fs::write, which intrinsically discards comments/disabled lines and rewrites otherwise untouched formatting.- Codex already has a local precedent for a formatting-preserving edit:
config/src/plugin_edit.rs:5-11,71-86usestoml_edit::DocumentMutandwrite_atomically.
Suggested implementation scope: adapt both migration paths to insert only missing keys/tables into a DocumentMut rather than round-tripping the whole file through toml::Value. Continue treating existing active keys as authoritative; do not reinterpret commented-out # model = ... as a live key. Reuse the existing atomic-write/path-handling discipline where appropriate, and keep the import result's success count semantically aligned with actually inserted MCP entries.
Acceptance matrix: (1) the reporter's McpServerConfig project import preserves the exact original comments, inline model comment and disabled lines; (2) a general Config item importing a new unrelated setting preserves those same lines; (3) repeat both against home-scoped config; (4) an import of only already-present keys is byte-for-byte no-op; (5) malformed TOML returns an error without truncating the original file; (6) existing active keys win over incoming values. These tests distinguish a genuine shared fix from merely correcting the reported MCP path.
The report already correctly identifies the toml::Value round-trip for the MCP path. This adds the separate Config ingress and an in-repo, already-used preservation/write primitive. I have not executed the app-server reproduction or proposed a finished patch.
I reproduced the comment loss through both migration item types on the official Linux CLI 0.169.0. This was one network-disabled app-server run with disposable HOME, CODEX_HOME, and repository files; it made no model request or real MCP call.
externalAgentConfig/detect returned a home CONFIG item and a repository MCP_SERVER_CONFIG item. Import reported one success and zero failures for each. Both target TOML files started with a setup comment, a commented-out setting, and an inline comment on the active model. The home import added a synthetic environment key and the repository import added a synthetic MCP server, but all three forms of original comment disappeared from both files.
The disposable repository was untrusted, so this verifies the file rewrite, not loading of its project-local config. I prepared two Rust test cases, but their source build was interrupted before they ran; no fix was tested. I can share the synthetic protocol fixture if useful.