|
1 | 1 | import fs from "node:fs"; |
2 | 2 | import path from "node:path"; |
3 | | -import type { ValidClient, ClientConfig } from "./types.js"; |
4 | 3 | 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 | +} |
5 | 83 |
|
6 | 84 | export function getConfigPath(client: ValidClient): string { |
7 | 85 | const configPath = clientPaths[client]; |
@@ -31,6 +109,12 @@ export function readConfig(client: ValidClient): ClientConfig { |
31 | 109 |
|
32 | 110 | export function writeConfig(client: ValidClient, config: ClientConfig): void { |
33 | 111 | const configPath = getConfigPath(client); |
| 112 | + |
| 113 | + if (client === "codex") { |
| 114 | + writeCodexTomlConfig(configPath, config); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
34 | 118 | const configDir = path.dirname(configPath); |
35 | 119 |
|
36 | 120 | if (!fs.existsSync(configDir)) { |
|
0 commit comments