feat: add MCP config and system prompt support for Codex local mode

Implements MCP server configuration and developer instructions support in
Codex local mode, bringing it to parity with remote mode.

Key additions:
- buildMcpServerConfigArgs() and buildDeveloperInstructionsArg() utilities
  to construct -c config arguments for passing MCP servers and instructions
  to the Codex CLI at runtime
- TITLE_INSTRUCTION for Codex to call hapi__change_title to update chat
  session titles dynamically
- Codex local mode now starts hapi MCP bridge server and passes both MCP
  configuration and developer instructions to Claude, enabling full feature
  parity with remote mode

Files changed:
- New: codexMcpConfig.ts (utilities), systemPrompt.ts (prompt definition),
  codexMcpConfig.test.ts (comprehensive tests)
- Updated: codexLocal.ts (accepts mcpServers, builds config args),
  codexLocalLauncher.ts (starts hapi server, passes config),
  codexStartConfig.ts (imports TITLE_INSTRUCTION)
This commit is contained in:
weishu
2026-01-05 18:38:58 +08:00
parent 45822afa49
commit c50621e85a
8 changed files with 214 additions and 7 deletions
@@ -0,0 +1,93 @@
import { describe, it, expect } from 'vitest';
import { buildMcpServerConfigArgs, buildDeveloperInstructionsArg } from './codexMcpConfig';
describe('codexMcpConfig', () => {
describe('buildMcpServerConfigArgs', () => {
it('builds config args for a single MCP server', () => {
const mcpServers = {
hapi: {
command: 'hapi',
args: ['mcp', '--url', 'http://localhost:3000']
}
};
const args = buildMcpServerConfigArgs(mcpServers);
expect(args).toEqual([
'-c', 'mcp_servers.hapi.command="hapi"',
'-c', 'mcp_servers.hapi.args=["mcp","--url","http://localhost:3000"]'
]);
});
it('builds config args for multiple MCP servers', () => {
const mcpServers = {
hapi: { command: 'hapi', args: ['mcp'] },
other: { command: 'node', args: ['server.js'] }
};
const args = buildMcpServerConfigArgs(mcpServers);
expect(args).toContain('-c');
expect(args).toContain('mcp_servers.hapi.command="hapi"');
expect(args).toContain('mcp_servers.other.command="node"');
});
it('handles empty args array', () => {
const mcpServers = {
simple: { command: 'simple-server', args: [] }
};
const args = buildMcpServerConfigArgs(mcpServers);
expect(args).toContain('mcp_servers.simple.args=[]');
});
it('escapes special characters in command', () => {
const mcpServers = {
test: { command: 'path/to/server', args: [] }
};
const args = buildMcpServerConfigArgs(mcpServers);
expect(args).toContain('mcp_servers.test.command="path/to/server"');
});
});
describe('buildDeveloperInstructionsArg', () => {
it('builds developer instructions arg', () => {
const instructions = 'Call functions.hapi__change_title to set title.';
const args = buildDeveloperInstructionsArg(instructions);
expect(args).toEqual([
'-c',
'developer_instructions="Call functions.hapi__change_title to set title."'
]);
});
it('escapes double quotes', () => {
const instructions = 'Use "quotes" in text.';
const args = buildDeveloperInstructionsArg(instructions);
expect(args[1]).toContain('\\"quotes\\"');
});
it('escapes newlines', () => {
const instructions = 'Line 1\nLine 2';
const args = buildDeveloperInstructionsArg(instructions);
expect(args[1]).toContain('\\n');
expect(args[1]).not.toContain('\n');
});
it('escapes backslashes', () => {
const instructions = 'Path: C:\\Users\\test';
const args = buildDeveloperInstructionsArg(instructions);
expect(args[1]).toContain('\\\\');
});
});
});