codex icon indicating copy to clipboard operation
codex copied to clipboard

Importing an MCP server erases unrelated comments and disabled settings from the project config

Open dovvnloading opened this issue 2 weeks ago • 1 comments

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

  1. Initialize an empty Git repository and create .codex/config.toml with these contents:

    # NOTE: retain these project-specific setup instructions.
    # model = "other-model"
    model = "gpt-5.4" # current selection
    
  2. Create .mcp.json at the repository root:

    {"mcpServers":{"demo":{"command":"echo","args":["demo"]}}}
    
  3. Start codex app-server --stdio in the repo. Send initialize with {"clientInfo":{"name":"repro","version":"1"},"capabilities":{"experimentalApi":true}}, then the initialized notification.

  4. Call externalAgentConfig/detect with {"includeHome":false,"cwds":["<absolute repo path>"]}. Pass its returned items array to externalAgentConfig/import as migrationItems.

  5. 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

dovvnloading avatar Nov 14 '26 01:11 dovvnloading

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:

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.

Nakagawa-master avatar Nov 15 '26 03:11 Nakagawa-master

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.

TheOutlandsDev avatar Nov 17 '26 19:11 TheOutlandsDev