From 07ca8737021a81d2785046ae0cd98afaba5dd0a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CBeeRad=E2=80=9D?= Date: Tue, 21 Apr 2026 08:49:31 +1000 Subject: [PATCH] fix: auto-write Codex MCP config - Merge RA-H MCP settings into Codex config.toml when --yes is passed - Preserve unrelated TOML config while replacing stale RA-H blocks - Update docs and package version for the Codex auto setup path Generated with Claude Code --- README.md | 4 +- apps/mcp-server-standalone/README.md | 4 +- apps/mcp-server-standalone/cli.js | 42 +++++++++++++++++--- apps/mcp-server-standalone/package-lock.json | 4 +- apps/mcp-server-standalone/package.json | 2 +- docs/8_mcp.md | 4 +- 6 files changed, 45 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0a8bfc8..15722cb 100644 --- a/README.md +++ b/README.md @@ -85,12 +85,12 @@ Other clients: ```bash npx -y ra-h-mcp-server@latest setup --client cursor --yes -npx -y ra-h-mcp-server@latest setup --client codex +npx -y ra-h-mcp-server@latest setup --client codex --yes ``` Notes: - `--yes` lets the installer write supported client config automatically. -- Codex uses TOML config, so the installer prints the block to add. +- Codex uses TOML config, so the installer writes `CODEX_HOME/config.toml` or `~/.codex/config.toml`. - The MCP-only path does not clone this repo and does not start the browser UI. - The installer defaults to the latest published MCP package. For release/debug reproducibility, pin an exact version intentionally. diff --git a/apps/mcp-server-standalone/README.md b/apps/mcp-server-standalone/README.md index 595965b..7f5dee4 100644 --- a/apps/mcp-server-standalone/README.md +++ b/apps/mcp-server-standalone/README.md @@ -14,13 +14,13 @@ Other useful commands: ```bash npx -y ra-h-mcp-server@latest setup --client cursor --yes -npx -y ra-h-mcp-server@latest setup --client codex +npx -y ra-h-mcp-server@latest setup --client codex --yes npx -y ra-h-mcp-server@latest init-db npx -y ra-h-mcp-server@latest doctor npx -y ra-h-mcp-server@latest print-config --client claude-code ``` -`--yes` lets the installer write supported JSON client config automatically. Codex uses TOML config, so the installer prints the block to add. +`--yes` lets the installer write supported client config automatically. Codex uses TOML config, so the installer writes `CODEX_HOME/config.toml` or `~/.codex/config.toml`. Important contract: - `@latest` is the default user-facing install path diff --git a/apps/mcp-server-standalone/cli.js b/apps/mcp-server-standalone/cli.js index 0c22420..93f12b8 100644 --- a/apps/mcp-server-standalone/cli.js +++ b/apps/mcp-server-standalone/cli.js @@ -261,11 +261,11 @@ function clientConfig(client, args, dbPath) { if (client === 'codex') { return { - type: 'toml', - path: path.join(os.homedir(), '.codex', 'config.toml'), + type: 'toml-merge', + path: path.join(process.env.CODEX_HOME || path.join(os.homedir(), '.codex'), 'config.toml'), snippet: `[mcp_servers.ra-h]\ncommand = "npx"\nargs = ["-y", "${packageSpec(args)}"]\n\n[mcp_servers.ra-h.env]\nRAH_DB_PATH = "${dbPath.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"\n`, - writable: false, - note: 'Codex config is TOML; this installer prints the block instead of mutating existing config.', + writable: true, + note: 'Codex config is TOML; pass --yes to merge this block into the Codex config file.', }; } @@ -306,6 +306,32 @@ function writeJsonMerge(config) { fs.writeFileSync(config.path, `${JSON.stringify(next, null, 2)}\n`); } +function removeTomlTables(raw, tableNames) { + const tables = new Set(tableNames); + const lines = raw.split(/\r?\n/); + const kept = []; + let skipping = false; + + for (const line of lines) { + const match = line.match(/^\s*\[([^\]]+)\]\s*$/); + if (match) { + skipping = tables.has(match[1]); + } + if (!skipping) kept.push(line); + } + + return kept.join('\n').trimEnd(); +} + +function writeTomlMerge(config) { + const current = fs.existsSync(config.path) ? fs.readFileSync(config.path, 'utf8') : ''; + const withoutRah = removeTomlTables(current, ['mcp_servers.ra-h', 'mcp_servers.ra-h.env']); + const next = `${withoutRah ? `${withoutRah}\n\n` : ''}${config.snippet.trimEnd()}\n`; + + fs.mkdirSync(path.dirname(config.path), { recursive: true }); + fs.writeFileSync(config.path, next); +} + function formatSnippet(snippet) { return typeof snippet === 'string' ? snippet : JSON.stringify(snippet, null, 2); } @@ -404,9 +430,13 @@ function commandSetup(args) { if (config.path) log(`MCP config target: ${config.path}`); if (config.note) log(config.note); - const canWrite = config.writable && config.type === 'json-merge' && config.path; + const canWrite = config.writable && ['json-merge', 'toml-merge'].includes(config.type) && config.path; if (canWrite && args.yes && !args.printOnly) { - writeJsonMerge(config); + if (config.type === 'toml-merge') { + writeTomlMerge(config); + } else { + writeJsonMerge(config); + } log(`Updated ${config.path}`); } else if (canWrite) { log('Pass --yes to write this config automatically.'); diff --git a/apps/mcp-server-standalone/package-lock.json b/apps/mcp-server-standalone/package-lock.json index fb7aaa7..7e99ecc 100644 --- a/apps/mcp-server-standalone/package-lock.json +++ b/apps/mcp-server-standalone/package-lock.json @@ -1,12 +1,12 @@ { "name": "ra-h-mcp-server", - "version": "3.2.0", + "version": "3.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ra-h-mcp-server", - "version": "3.2.0", + "version": "3.2.1", "license": "MIT", "dependencies": { "@modelcontextprotocol/sdk": "^1.0.0", diff --git a/apps/mcp-server-standalone/package.json b/apps/mcp-server-standalone/package.json index 707870c..df54a92 100644 --- a/apps/mcp-server-standalone/package.json +++ b/apps/mcp-server-standalone/package.json @@ -1,6 +1,6 @@ { "name": "ra-h-mcp-server", - "version": "3.2.0", + "version": "3.2.1", "description": "Connect Claude Code/Desktop to your RA-H knowledge base. Direct SQLite access to an existing RA-H database.", "main": "index.js", "bin": { diff --git a/docs/8_mcp.md b/docs/8_mcp.md index e4350a6..1185e5d 100644 --- a/docs/8_mcp.md +++ b/docs/8_mcp.md @@ -14,10 +14,10 @@ Other clients: ```bash npx -y ra-h-mcp-server@latest setup --client cursor --yes -npx -y ra-h-mcp-server@latest setup --client codex +npx -y ra-h-mcp-server@latest setup --client codex --yes ``` -`--yes` lets the installer write supported JSON client config automatically. Codex uses TOML config, so the installer prints the block to add. +`--yes` lets the installer write supported client config automatically. Codex uses TOML config, so the installer writes `CODEX_HOME/config.toml` or `~/.codex/config.toml`. Verify the install: