DEV Community

Cover image for Keep your MCP servers in sync between Claude Code and Codex with a short script
AnBoWork
AnBoWork

Posted on

Keep your MCP servers in sync between Claude Code and Codex with a short script

Claude Code and Codex keep their MCP servers in different files, so adding a server to one does not add it to the other. This post shows where each tool stores them and gives a short Node script (about 35 lines) that copies your Claude Code servers into Codex, using Codex's own mcp add command.

Everything below was run on Windows 11 with Claude Code 2.1.332 and Codex CLI 0.172.0, inside throwaway config directories, so the output is real and your own configuration was never touched. The two vendors' docs pages we cite carry no publication date, so check them if your versions are newer.

Claude Code's MCP servers copied into Codex by a small script

The problem, in two commands

Add a server in Claude Code:

$ claude mcp add --scope user context7 -- npx -y @upstash/context7-mcp
Added stdio MCP server context7 with command: npx -y @upstash/context7-mcp to user config
Enter fullscreen mode Exit fullscreen mode

Ask Codex what it knows:

$ codex mcp list
No MCP servers configured yet. Try `codex mcp add my-tool -- my-command`.
Enter fullscreen mode Exit fullscreen mode

Codex has never heard of it. You would have to run codex mcp add with the same command, and again every time you add, change or remove a server.

Where each tool keeps its MCP servers

Claude Code Codex
User-wide servers ~/.claude.json, top-level mcpServers ~/.codex/config.toml, one [mcp_servers.<name>] table per server
Per project Local scope: ~/.claude.json under that project's path. Project scope: .mcp.json in the project root .codex/config.toml, only in trusted projects
Add command `claude mcp add [--scope local\ user\
Remove command {% raw %}claude mcp remove <name> codex mcp remove <name>

Sources: Claude Code docs, "Connect Claude Code to tools via MCP"; Codex docs, "Model Context Protocol" (ChatGPT Learn). Two details that matter below: claude mcp add defaults to the local scope, not user, and if you set CLAUDE_CONFIG_DIR the file moves to $CLAUDE_CONFIG_DIR/.claude.json (we saw this when we ran with a throwaway directory).

The script

It reads the user-scope servers from Claude Code's JSON, asks Codex which servers it already has (codex mcp list --json), and adds the missing ones with codex mcp add. It never edits config.toml by hand and never deletes anything.

// sync-mcp.mjs
// Copy user-scope MCP servers from Claude Code to Codex, using Codex's own `mcp add`.
// Adds only; never edits config.toml by hand and never deletes anything.
import { readFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';

const claudeJson = process.env.CLAUDE_CONFIG_DIR
  ? join(process.env.CLAUDE_CONFIG_DIR, '.claude.json')
  : join(homedir(), '.claude.json');
const servers = JSON.parse(readFileSync(claudeJson, 'utf8')).mcpServers ?? {};

// On Windows `codex` is a .cmd shim, so it needs a shell; quote every argument there.
const win = process.platform === 'win32';
const run = (args) => {
  const a = win ? args.map((x) => `"${String(x).replace(/"/g, '\\"')}"`) : args;
  return spawnSync('codex', a, { encoding: 'utf8', shell: win });
};

const have = new Set(JSON.parse(run(['mcp', 'list', '--json']).stdout || '[]').map((s) => s.name));

for (const [name, s] of Object.entries(servers)) {
  if (have.has(name)) { console.log(`skip   ${name} (already in Codex)`); continue; }
  let args;
  if (s.type === 'http' && s.url) {
    if (s.headers && Object.keys(s.headers).length) { console.log(`skip   ${name} (HTTP headers need manual setup)`); continue; }
    args = ['mcp', 'add', name, '--url', s.url];
  } else if (s.command) {
    const env = Object.entries(s.env ?? {}).flatMap(([k, v]) => ['--env', `${k}=${v}`]);
    args = ['mcp', 'add', name, ...env, '--', s.command, ...(s.args ?? [])];
  } else { console.log(`skip   ${name} (unsupported transport: ${s.type})`); continue; }
  const r = run(args);
  console.log(r.status === 0 ? `added  ${name}` : `FAILED ${name}: ${(r.stderr || r.stdout).trim()}`);
}
Enter fullscreen mode Exit fullscreen mode

Running it

We added two more servers in Claude Code first: notes (a stdio server with an environment variable) and docs (plain HTTP), plus private, an HTTP server with a header. Then:

$ node sync-mcp.mjs
skip   context7 (already in Codex)
added  notes
added  docs
skip   private (HTTP headers need manual setup)
Enter fullscreen mode Exit fullscreen mode

(context7 was already there because we had run the script once before; the first run printed added context7.) Codex now has the three it could copy:

$ codex mcp list
Name      Command  Args                      Env               Cwd  Status   Auth
context7  npx      -y @upstash/context7-mcp  -                 -    enabled  Unsupported
notes     node     ./notes-server.js         DEMO_TOKEN=*****  -    enabled  Unsupported

Name  Url                      Bearer Token Env Var  Status   Auth
docs  https://example.com/mcp  -                     enabled  Unsupported
Enter fullscreen mode Exit fullscreen mode

And this is what landed in ~/.codex/config.toml:

[mcp_servers.context7]
command = "npx"
args = ["-y", "@upstash/context7-mcp"]

[mcp_servers.notes]
command = "node"
args = ["./notes-server.js"]

[mcp_servers.notes.env]
DEMO_TOKEN = "not-a-real-token"

[mcp_servers.docs]
url = "https://example.com/mcp"
Enter fullscreen mode Exit fullscreen mode

Run it a second time and every line says skip, so it is safe to put in a shell alias or a post-install hook.

What it does not do

What the script copies and what it skips

  • It only reads user scope. Servers you added with the default --scope local, and project servers in .mcp.json, are not copied. Re-add them with --scope user first if you want them shared.
  • It skips HTTP servers that need headers. Codex has --bearer-token-env-var and an OAuth login (codex mcp login), but this script does not translate Claude Code's headers into either. The private server above was skipped for that reason.
  • It copies environment values as plain text. Claude Code already keeps them in ~/.claude.json; after the script they are also in Codex's config.toml. Two files now hold the same secret, so keep both out of version control.
  • It does not compare definitions. If a server with the same name exists in Codex with a different command, the script skips it rather than overwriting. Remove it first (codex mcp remove <name>) if you want the Claude Code version.
  • It never deletes. Removing a server still takes claude mcp remove and codex mcp remove, one each.
  • Only Windows was tested. The shell handling for the .cmd shim is Windows-specific; the macOS and Linux branch calls codex directly and we did not run it there.
  • Skills are a separate story. They have the same split between the two tools, but we only verified Claude Code's ~/.claude/skills/, so this post covers MCP only.

Try it without risking your real config

Both tools honour a directory override, which is how we produced the output above. In a throwaway shell:

export CLAUDE_CONFIG_DIR="$(mktemp -d)" CODEX_HOME="$(mktemp -d)"
claude mcp add --scope user demo -- npx -y @upstash/context7-mcp
node sync-mcp.mjs
codex mcp list
Enter fullscreen mode Exit fullscreen mode

On PowerShell, set $env:CLAUDE_CONFIG_DIR and $env:CODEX_HOME to two new empty folders instead. Close the shell afterwards and your real configuration is exactly as you left it.

This article was created with the help of AI.

Top comments (0)