Files
hapi/cli/src/codex/utils/buildHapiMcpBridge.ts
T
weishu 9a28e6e0a1 refactor: unify MCP bridge setup and system prompt delivery
Extract duplicate MCP bridge initialization code from codexLocalLauncher and
codexRemoteLauncher into a new buildHapiMcpBridge utility. This ensures both
modes use identical server setup logic and provides a single source of truth
for MCP configuration.

Update codexStartConfig to deliver system prompt via developer_instructions
instead of appending to the user message. This ensures consistent behavior
between local and remote modes and properly separates system guidance from
user intent.
2026-01-05 18:38:58 +08:00

62 lines
1.6 KiB
TypeScript

/**
* Unified MCP bridge setup for Codex local and remote modes.
*
* This module provides a single source of truth for starting the hapi MCP
* bridge server and generating the MCP server configuration that Codex needs.
*/
import { startHappyServer } from '@/claude/utils/startHappyServer';
import { getHappyCliCommand } from '@/utils/spawnHappyCLI';
import type { ApiSessionClient } from '@/api/apiSession';
/**
* MCP server entry configuration.
*/
export interface McpServerEntry {
command: string;
args: string[];
}
/**
* Map of MCP server names to their configurations.
*/
export type McpServersConfig = Record<string, McpServerEntry>;
/**
* Result of starting the hapi MCP bridge.
*/
export interface HapiMcpBridge {
/** The running server instance */
server: {
url: string;
stop: () => void;
};
/** MCP server config to pass to Codex (works for both CLI and SDK) */
mcpServers: McpServersConfig;
}
/**
* Start the hapi MCP bridge server and return the configuration
* needed to connect Codex to it.
*
* This is the single source of truth for MCP bridge setup,
* used by both local and remote launchers.
*/
export async function buildHapiMcpBridge(client: ApiSessionClient): Promise<HapiMcpBridge> {
const happyServer = await startHappyServer(client);
const bridgeCommand = getHappyCliCommand(['mcp', '--url', happyServer.url]);
return {
server: {
url: happyServer.url,
stop: happyServer.stop
},
mcpServers: {
hapi: {
command: bridgeCommand.command,
args: bridgeCommand.args
}
}
};
}