Skip to content

Commit 4212ffd

Browse files
letmatdevchishiyacjinho-yeom
authored
feat: add Codex support for Magic MCP install (#3)
* feat: add codex client for mcp install * fix: harden codex config install * chore: remove pnpm lockfile * chore: revert version bump and sync lockfile --------- Co-authored-by: chishiyac <matheussdev3@gmail.com> Co-authored-by: Jinho Yeom <pwlsghq@naver.com>
1 parent 9454f51 commit 4212ffd

6 files changed

Lines changed: 96 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ npx @magicuidesign/cli@latest install <client>
1919
- [x] claude
2020
- [x] cline
2121
- [x] roo-cline
22+
- [x] codex
2223

2324
## Manual Installation
2425

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const platform = process.platform as keyof typeof platformPaths;
2222
const { baseDir, vscodePath } = platformPaths[platform];
2323

2424
export const clientPaths: Record<string, string> = {
25+
codex: path.join(
26+
process.env.CODEX_HOME || path.join(homeDir, ".codex"),
27+
"config.toml",
28+
),
2529
claude: path.join(baseDir, "Claude", "claude_desktop_config.json"),
2630
cline: path.join(
2731
baseDir,

‎src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function install(client: ValidClient): Promise<void> {
2828
`You may need to restart ${capitalizedClient} to see the Magic MCP server.`,
2929
),
3030
);
31-
await promptForRestart(client);
31+
if (client !== "codex") await promptForRestart(client);
3232
} catch (error) {
3333
spinner.fail(`Failed to install configuration for ${capitalizedClient}`);
3434
console.error(

‎src/types.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ export type ValidClient =
33
| "cline"
44
| "roo-cline"
55
| "windsurf"
6-
| "cursor";
6+
| "cursor"
7+
| "codex";
78

89
export const VALID_CLIENTS: ValidClient[] = [
910
"claude",
1011
"cline",
1112
"roo-cline",
1213
"windsurf",
1314
"cursor",
15+
"codex",
1416
];
1517

1618
export interface ServerConfig {

‎src/utils.ts‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,85 @@
11
import fs from "node:fs";
22
import path from "node:path";
3-
import type { ValidClient, ClientConfig } from "./types.js";
43
import { clientPaths } from "./config.js";
4+
import type { ClientConfig, ServerConfig, ValidClient } from "./types.js";
5+
6+
function escapeTomlBasicString(s: string): string {
7+
return s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
8+
}
9+
10+
const CODEX_MCP_TABLE_HEADER =
11+
/^\[mcp_servers\.(?:"([^"]+)"|([a-zA-Z0-9_.-]+))\]\s*$/;
12+
13+
function mcpServerNameFromTableHeader(line: string): string | undefined {
14+
if (!line.startsWith("[mcp_servers.")) return undefined;
15+
const match = line.match(CODEX_MCP_TABLE_HEADER);
16+
if (!match) return undefined;
17+
return match[1] ?? match[2];
18+
}
19+
20+
const MCP_SERVER_BLOCK_END = /\r?\n/;
21+
22+
function removeCodexMcpServerBlocks(
23+
content: string,
24+
serverNamesToReplace: Set<string>,
25+
): string {
26+
const lines = content.split(MCP_SERVER_BLOCK_END);
27+
const kept: string[] = [];
28+
let droppingBlock = false;
29+
30+
for (const line of lines) {
31+
const header = line.trimStart();
32+
if (droppingBlock && !header.startsWith("[")) {
33+
continue;
34+
}
35+
if (droppingBlock) {
36+
droppingBlock = false;
37+
}
38+
39+
const serverName = mcpServerNameFromTableHeader(header);
40+
if (serverName && serverNamesToReplace.has(serverName)) {
41+
droppingBlock = true;
42+
continue;
43+
}
44+
45+
kept.push(line);
46+
}
47+
48+
return kept.join("\n");
49+
}
50+
51+
function formatCodexMcpServerTable(name: string, sc: ServerConfig): string {
52+
const header = `[mcp_servers."${escapeTomlBasicString(name)}"]`;
53+
const cmd = `command = "${escapeTomlBasicString(sc.command)}"`;
54+
const argStrs = sc.args.map((a) => `"${escapeTomlBasicString(a)}"`);
55+
return `${header}\n${cmd}\nargs = [${argStrs.join(", ")}]\n`;
56+
}
57+
58+
function writeCodexTomlConfig(configPath: string, config: ClientConfig): void {
59+
if (!config.mcpServers || typeof config.mcpServers !== "object") {
60+
throw new Error("Invalid mcpServers structure");
61+
}
62+
63+
const configDir = path.dirname(configPath);
64+
if (!fs.existsSync(configDir)) {
65+
fs.mkdirSync(configDir, { recursive: true });
66+
}
67+
68+
const serverNames = new Set(Object.keys(config.mcpServers));
69+
let content = "";
70+
if (fs.existsSync(configPath)) {
71+
content = fs.readFileSync(configPath, "utf8");
72+
}
73+
74+
content = removeCodexMcpServerBlocks(content, serverNames);
75+
const additions = Object.entries(config.mcpServers)
76+
.map(([name, sc]) => formatCodexMcpServerTable(name, sc))
77+
.join("\n");
78+
79+
const trimmed = content.trimEnd();
80+
const sep = trimmed.length > 0 ? "\n\n" : "";
81+
fs.writeFileSync(configPath, `${trimmed}${sep}${additions}`.trimEnd() + "\n");
82+
}
583

684
export function getConfigPath(client: ValidClient): string {
785
const configPath = clientPaths[client];
@@ -31,6 +109,12 @@ export function readConfig(client: ValidClient): ClientConfig {
31109

32110
export function writeConfig(client: ValidClient, config: ClientConfig): void {
33111
const configPath = getConfigPath(client);
112+
113+
if (client === "codex") {
114+
writeCodexTomlConfig(configPath, config);
115+
return;
116+
}
117+
34118
const configDir = path.dirname(configPath);
35119

36120
if (!fs.existsSync(configDir)) {

0 commit comments

Comments
 (0)