-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodex-mcp.ts
More file actions
414 lines (372 loc) · 12.7 KB
/
Copy pathcodex-mcp.ts
File metadata and controls
414 lines (372 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
import { dirname } from 'node:path';
import { executeCommand } from './native/types.js';
import type { NativeCommandResult } from './native/types.js';
import type { ValidatedPlugin } from './sync.js';
import { collectMcpServers } from './vscode-mcp.js';
import type { McpMergeResult } from './vscode-mcp.js';
type ExecuteFn = (
binary: string,
args: string[],
) => NativeCommandResult | Promise<NativeCommandResult>;
/**
* Build CLI args for `codex mcp add <name>` from a .mcp.json server config.
* Returns null if the config format is unsupported.
*/
export function buildCodexMcpAddArgs(
name: string,
config: Record<string, unknown>,
): string[] | null {
// URL-based (streamable HTTP)
if (typeof config.url === 'string') {
return ['mcp', 'add', name, '--url', config.url];
}
// stdio-based (command + args)
if (typeof config.command === 'string') {
const args: string[] = ['mcp', 'add', name];
// Add --env flags if present
if (config.env && typeof config.env === 'object') {
for (const [key, value] of Object.entries(
config.env as Record<string, string>,
)) {
args.push('--env', `${key}=${value}`);
}
}
// Add -- separator and command
args.push('--', config.command);
// Add command args if present
if (Array.isArray(config.args)) {
args.push(...(config.args as string[]));
}
return args;
}
return null;
}
/**
* Sync MCP servers from plugins into the Codex CLI via `codex mcp add/remove`.
*
* Uses the same ownership model as syncVscodeMcpConfig:
* - Only tracked servers are updated/removed
* - Pre-existing user-managed servers are skipped
*/
export async function syncCodexMcpServers(
validatedPlugins: ValidatedPlugin[],
options?: {
dryRun?: boolean;
trackedServers?: string[];
serverOverrides?: Map<string, unknown>;
_mockExecute?: ExecuteFn;
},
): Promise<McpMergeResult> {
const dryRun = options?.dryRun ?? false;
const previouslyTracked = new Set(options?.trackedServers ?? []);
const hasTracking = options?.trackedServers !== undefined;
const exec: ExecuteFn =
options?._mockExecute ?? ((binary, args) => executeCommand(binary, args));
// Collect servers from all plugins
const { servers: pluginServers, warnings } = options?.serverOverrides
? { servers: options.serverOverrides, warnings: [] as string[] }
: collectMcpServers(validatedPlugins);
const result: McpMergeResult = {
added: 0,
skipped: 0,
overwritten: 0,
removed: 0,
warnings: [...warnings],
addedServers: [],
skippedServers: [],
overwrittenServers: [],
removedServers: [],
trackedServers: [],
};
// Skip calling codex CLI entirely when there are no MCP servers to sync and nothing to remove
if (pluginServers.size === 0 && previouslyTracked.size === 0) {
return result;
}
// List existing Codex MCP servers
const listResult = await exec('codex', ['mcp', 'list', '--json']);
if (!listResult.success) {
result.warnings.push(
`Codex CLI not available or 'codex mcp list' failed: ${listResult.error ?? 'unknown error'}`,
);
return result;
}
let existingNames: Set<string>;
try {
const parsed = JSON.parse(listResult.output) as Array<{ name: string }>;
existingNames = new Set(parsed.map((s) => s.name));
} catch {
result.warnings.push('Failed to parse codex mcp list output');
return result;
}
// Process plugin servers: add new, skip existing user-managed
for (const [name, config] of pluginServers) {
if (existingNames.has(name)) {
if (hasTracking && previouslyTracked.has(name)) {
// We own this server - keep tracking
result.trackedServers.push(name);
} else {
// User-managed server - skip (do not track)
result.skipped++;
result.skippedServers.push(name);
}
} else {
// New server - add it
const addArgs = buildCodexMcpAddArgs(
name,
config as Record<string, unknown>,
);
if (!addArgs) {
result.warnings.push(
`Unsupported MCP server config for '${name}', skipping`,
);
continue;
}
if (!dryRun) {
const addResult = await exec('codex', addArgs);
if (!addResult.success) {
result.warnings.push(
`Failed to add MCP server '${name}': ${addResult.error ?? 'unknown error'}`,
);
continue;
}
}
result.added++;
result.addedServers.push(name);
result.trackedServers.push(name);
}
}
// Remove orphaned tracked servers (previously tracked but no longer in plugins)
if (hasTracking) {
const currentServerNames = new Set(pluginServers.keys());
for (const trackedName of previouslyTracked) {
if (
!currentServerNames.has(trackedName) &&
existingNames.has(trackedName)
) {
if (!dryRun) {
const removeResult = await exec('codex', [
'mcp',
'remove',
trackedName,
]);
if (!removeResult.success) {
result.warnings.push(
`Failed to remove MCP server '${trackedName}': ${removeResult.error ?? 'unknown error'}`,
);
continue;
}
}
result.removed++;
result.removedServers.push(trackedName);
}
}
}
return result;
}
// ---------------------------------------------------------------------------
// Project-scoped .codex/config.toml helpers
// ---------------------------------------------------------------------------
/**
* Convert a TOML value to its string representation.
*/
function toTomlValue(value: unknown): string {
if (typeof value === 'string') return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
if (Array.isArray(value)) return `[${value.map(toTomlValue).join(', ')}]`;
return `"${String(value)}"`;
}
/**
* Generate TOML for a single MCP server entry.
*/
export function serverToToml(name: string, config: Record<string, unknown>): string {
const lines: string[] = [`[mcp_servers.${name}]`];
const envEntries: [string, unknown][] = [];
for (const [key, value] of Object.entries(config)) {
if (key === 'type') continue; // "type": "http" is implicit from url presence in codex
if (key === 'env' && typeof value === 'object' && value !== null) {
envEntries.push(...Object.entries(value as Record<string, unknown>));
continue;
}
lines.push(`${key} = ${toTomlValue(value)}`);
}
if (envEntries.length > 0) {
lines.push('');
lines.push(`[mcp_servers.${name}.env]`);
for (const [envKey, envValue] of envEntries) {
lines.push(`${envKey} = ${toTomlValue(envValue)}`);
}
}
return lines.join('\n');
}
/**
* Parse existing .codex/config.toml to extract mcp_servers names.
* Returns the set of server names and the non-mcp lines (to preserve).
*/
export function parseCodexConfigToml(content: string): {
serverNames: Set<string>;
nonMcpContent: string;
serverSections: Map<string, string>;
} {
const serverNames = new Set<string>();
const serverSections = new Map<string, string>();
const nonMcpLines: string[] = [];
const lines = content.split('\n');
let currentServer: string | null = null;
let currentLines: string[] = [];
for (const line of lines) {
// Match [mcp_servers.<name>] or [mcp_servers.<name>.env]
const sectionMatch = line.match(/^\[mcp_servers\.([^\].]+?)(?:\.[^\]]+)?\]$/);
if (sectionMatch) {
// Save previous server if any
if (currentServer) {
serverSections.set(currentServer, (serverSections.get(currentServer) ?? '') +
(serverSections.has(currentServer) ? '\n' : '') + currentLines.join('\n'));
}
currentServer = sectionMatch[1] ?? null;
if (currentServer) serverNames.add(currentServer);
currentLines = [line];
continue;
}
// Check if we're entering a non-mcp section
const otherSectionMatch = line.match(/^\[(?!mcp_servers\.)/);
if (otherSectionMatch) {
// Save previous server if any
if (currentServer) {
serverSections.set(currentServer, (serverSections.get(currentServer) ?? '') +
(serverSections.has(currentServer) ? '\n' : '') + currentLines.join('\n'));
currentServer = null;
currentLines = [];
}
nonMcpLines.push(line);
continue;
}
if (currentServer) {
currentLines.push(line);
} else {
nonMcpLines.push(line);
}
}
// Save last server if any
if (currentServer) {
serverSections.set(currentServer, (serverSections.get(currentServer) ?? '') +
(serverSections.has(currentServer) ? '\n' : '') + currentLines.join('\n'));
}
return {
serverNames,
nonMcpContent: nonMcpLines.join('\n').replace(/\n{3,}/g, '\n\n').trim(),
serverSections,
};
}
/**
* Sync MCP server configs from plugins into project-scoped .codex/config.toml.
*
* Codex reads .codex/config.toml at the project root for project-scoped config
* (in trusted projects). MCP servers are stored as [mcp_servers.<name>] sections.
*
* Uses the same ownership/tracking model as other MCP sync implementations.
*/
export function syncCodexProjectMcpConfig(
validatedPlugins: ValidatedPlugin[],
options?: {
dryRun?: boolean;
configPath?: string;
force?: boolean;
trackedServers?: string[];
/** Pre-computed servers (e.g. after proxy transform) — bypasses collectMcpServers */
serverOverrides?: Map<string, unknown>;
},
): McpMergeResult {
const dryRun = options?.dryRun ?? false;
const force = options?.force ?? false;
const configPath = options?.configPath;
if (!configPath) {
throw new Error('configPath is required for syncCodexProjectMcpConfig');
}
const previouslyTracked = new Set(options?.trackedServers ?? []);
const hasTracking = options?.trackedServers !== undefined;
const { servers: pluginServers, warnings } = options?.serverOverrides
? { servers: options.serverOverrides, warnings: [] as string[] }
: collectMcpServers(validatedPlugins);
const result: McpMergeResult = {
added: 0,
skipped: 0,
overwritten: 0,
removed: 0,
warnings: [...warnings],
addedServers: [],
skippedServers: [],
overwrittenServers: [],
removedServers: [],
trackedServers: [],
};
// Read existing config
let existingContent = '';
if (existsSync(configPath)) {
try {
existingContent = readFileSync(configPath, 'utf-8');
} catch {
result.warnings.push(`Could not read existing ${configPath}, starting fresh`);
}
}
const { serverNames: existingNames, nonMcpContent, serverSections } =
parseCodexConfigToml(existingContent);
// Track which servers to keep in the final output
const finalServers = new Map<string, string>(serverSections);
// Process plugin servers
for (const [name, config] of pluginServers) {
if (existingNames.has(name)) {
if (hasTracking && previouslyTracked.has(name)) {
// We own it — overwrite with new config
finalServers.set(name, serverToToml(name, config as Record<string, unknown>));
result.overwritten++;
result.overwrittenServers.push(name);
result.trackedServers.push(name);
} else if (force) {
finalServers.set(name, serverToToml(name, config as Record<string, unknown>));
result.overwritten++;
result.overwrittenServers.push(name);
result.trackedServers.push(name);
} else {
// User-managed — skip
result.skipped++;
result.skippedServers.push(name);
}
} else {
finalServers.set(name, serverToToml(name, config as Record<string, unknown>));
result.added++;
result.addedServers.push(name);
result.trackedServers.push(name);
}
}
// Remove orphaned tracked servers
if (hasTracking) {
const currentServerNames = new Set(pluginServers.keys());
for (const trackedName of previouslyTracked) {
if (!currentServerNames.has(trackedName) && finalServers.has(trackedName)) {
finalServers.delete(trackedName);
result.removed++;
result.removedServers.push(trackedName);
}
}
}
// Write back if changes occurred
const hasChanges = result.added > 0 || result.overwritten > 0 || result.removed > 0;
if (hasChanges && !dryRun) {
const parts: string[] = [];
if (nonMcpContent) {
parts.push(nonMcpContent);
}
for (const toml of finalServers.values()) {
parts.push(toml);
}
const output = `${parts.join('\n\n')}\n`;
const dir = dirname(configPath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(configPath, output, 'utf-8');
result.configPath = configPath;
}
return result;
}