Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(codex): hand a person's own stdio connection the bot box's proxy
AdityaUmale1e8200d540f7
diff --git a/server/bot-box/box.ts b/server/bot-box/box.tsindex 57fc9f23a1..639fe7f10b 100644--- a/server/bot-box/box.ts+++ b/server/bot-box/box.ts@@ -19,6 +19,8 @@ import { boxUploadsDir, bwrapArgs, planBox, taskWorkspaceIds, type BoxBridge, ty/** The loopback port inside a box that reaches the egress gate. The harness gate sits at the server's own port. */export const BOX_EGRESS_PORT = 3128;+/** What the box sets on an isolated network: the egress proxy, and the local hosts that skip it. */+export const BOX_PROXY_ENV = ["HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy"] as const, BOX_NO_PROXY_ENV = ["NO_PROXY", "no_proxy"] as const;/** The loopback port inside a box that reaches the credential broker. */export const BOX_BROKER_PORT = 18080;/** The descriptor the box reads its seccomp filter from: the first after stdio. */@@ -100,9 +102,9 @@ export class BoxRunner {: boxEnvironment(request.env, this.options.denyEnv, new Set(request.allowEnv ?? []));if (plan.net === "isolated") {const proxy = `http://127.0.0.1:${this.options.egressPort ?? BOX_EGRESS_PORT}`;- for (const name of ["HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy"]) env[name] = proxy;+ for (const name of BOX_PROXY_ENV) env[name] = proxy;// Node's env proxy matches an IPv6 host only bracketed; curl and the engines take the plain forms.- for (const name of ["NO_PROXY", "no_proxy"]) env[name] = "127.0.0.1,localhost,::1,[::1]";+ for (const name of BOX_NO_PROXY_ENV) env[name] = "127.0.0.1,localhost,::1,[::1]";}const bridges = plan.net === "isolated" ? this.options.setup.bridges : [];const inner = bridgedCommand(this.options.bridge, bridges, request.command, request.args);diff --git a/server/drivers/codex-mcp-mount.ts b/server/drivers/codex-mcp-mount.tsindex b0dd114c26..9081d7c76d 100644--- a/server/drivers/codex-mcp-mount.ts+++ b/server/drivers/codex-mcp-mount.ts@@ -1,4 +1,5 @@import type { StdioMcpSpec } from "../contracts.ts";+import { BOX_NO_PROXY_ENV, BOX_PROXY_ENV, botBox } from "../bot-box/box.ts";import { tomlInlineTable } from "./codex-process.ts";/** Codex's default for one MCP server's tools (openai/codex#17843): "auto"@@ -15,6 +16,8 @@ export function mountMcpServer(/** Written into the mount's own `env` table: its process gets them, Codex's command shell does not. */literalEnv: Record<string, string> = {},written: Set<string> = new Set(),+ /** More names Codex hands the server from its own environment; a name it does not have is skipped. */+ forward: readonly string[] = [],): void {const prefix = `mcp_servers.${name}`;Object.assign(env, server.env);@@ -23,8 +26,14 @@ export function mountMcpServer("-c", `${prefix}.command=${JSON.stringify(server.command)}`,"-c", `${prefix}.args=${JSON.stringify(server.args)}`,// argv carries names only, so credentials never appear in process listings.- "-c", `${prefix}.env_vars=${JSON.stringify(Object.keys(server.env))}`,+ "-c", `${prefix}.env_vars=${JSON.stringify([...new Set([...Object.keys(server.env), ...forward])])}`,);if (Object.keys(literalEnv).length) appServerArgs.push("-c", `${prefix}.env=${tomlInlineTable(literalEnv)}`);if (approval !== "prompt") appServerArgs.push("-c", `${prefix}.default_tools_approval_mode="${approval}"`);}++/** In the bot box the network is reached only through the box's proxy (bot-box/box.ts), and Codex starts an MCP server with+ * just a few env names plus env_vars: a person's own stdio server would otherwise look hosts up itself and fail. */+export function boxProxyEnvNames(boxed: boolean = botBox.enabled()): string[] {+ return boxed ? [...BOX_PROXY_ENV, ...BOX_NO_PROXY_ENV] : [];+}diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.tsindex 474927939d..af2179763e 100644--- a/server/drivers/codex.test.ts+++ b/server/drivers/codex.test.ts@@ -24,6 +24,7 @@ import { unsupportedCallsNotice } from "./codex-unsupported-calls.ts";import { rebuiltSessionNotice } from "../resume-recovery.ts";import { startFakeHttpMcp, type FakeHttpMcp } from "../testing/fake-http-mcp-server.ts";import * as procs from "../procs.ts";+import { botBox } from "../bot-box/box.ts";import { autoVerdict } from "../auto-approve.ts";import { formatQuestionAnswers } from "../../shared/ask-question.ts";@@ -1230,6 +1231,22 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)expect(argv).toContain("mcp_servers.notes.command");});+ it("hands a person's own stdio server the bot box's proxy, its only way out of the box's network", async () => {+ await create();+ const servers = { custom: { todo: { command: "python3", args: ["todo.py"], env: { LIST: "x" } } } };+ const envVars = async (threadId: string) => {+ const dump = join(scratch, `${threadId}.json`);+ process.env.FAKE_CODEX_DUMP = dump;+ await instance.adapter.sendTurn({ threadId, text: "go", integrations: servers });+ await recorder.until((event) => event.type === "turn.completed" && event.threadId === threadId);+ const argv: string[] = JSON.parse(readFileSync(dump, "utf8")).argv;+ return JSON.parse(argv.find((arg) => arg.startsWith("mcp_servers.todo.env_vars="))!.split("=").slice(1).join("="));+ };+ expect(await envVars("t-unboxed-proxy")).toEqual(["LIST"]);+ vi.spyOn(botBox, "enabled").mockReturnValue(true);+ expect(await envVars("t-boxed-proxy")).toEqual(["LIST", "HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy", "NO_PROXY", "no_proxy"]);+ }, 20_000);+it("reaches a url server through OpenMausBot's connector on Codex's own login, header values off argv", async () => {await create();const dump = join(scratch, "remote-mcp.json");diff --git a/server/drivers/codex.ts b/server/drivers/codex.tsindex b607003f7f..9d66f2d4a6 100644--- a/server/drivers/codex.ts+++ b/server/drivers/codex.ts@@ -2,7 +2,7 @@ import { codexToolSurfaceArgs } from "./codex-tool-surface.ts";import { codexShellDisabled, GUEST_CONFINED_CODEX_ARGS } from "./codex-guest.ts";import { TEAM_TOOLS_FAILED, teamToolsFailed } from "./team-tools.ts";import { enabledMcpMounts, mcpStartupTracker, supportsMcpStartup } from "./codex-mcp-startup.ts";-import { type McpApprovalMode, mountMcpServer } from "./codex-mcp-mount.ts";+import { boxProxyEnvNames, type McpApprovalMode, mountMcpServer } from "./codex-mcp-mount.ts";import { reportUnsupportedCalls, rolloutMark, type RolloutMark } from "./codex-unsupported-calls.ts";// The `codex` CLI headless over its app-server JSON-RPC (NDJSON on stdio): completion is// `turn/completed`, approvals arrive as server→client requests, resumeCursor is the codex thread id.@@ -610,12 +610,12 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {}return { command: proxy.command, args: proxy.args ?? [], env: shared };};- const mountSelected = (name: string, mountName: string, server: McpServerSpec, approval: McpApprovalMode = "auto") => {+ const mountSelected = (name: string, mountName: string, server: McpServerSpec, approval: McpApprovalMode = "auto", forward: readonly string[] = []) => {const selected = scopedServer(name, mountName, server);if (selected) {selectedMcp.set(mountName, selected);selectedApprovals.set(mountName, approval);- mountMcpServer(appServerArgs, env, mountName, selected, approval, literalEnvs.get(mountName), mountedNames);+ mountMcpServer(appServerArgs, env, mountName, selected, approval, literalEnvs.get(mountName), mountedNames, forward);if (turn.toolScope !== undefined && approval === "prompt") appServerArgs.push("-c", `mcp_servers.${mountName}.default_tools_approval_mode="prompt"`);}};@@ -641,7 +641,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {for (const [name, server] of Object.entries(turn.integrations?.custom ?? {}).filter(([name, server]) => turn.toolScope !== undefined || !codexConfigRunsServer(codexConfig, name, server))) {const mountName = mountedMcpServerName(name, declaredInCodexConfig);if (mountName !== name) noteRenamedMcpServer(name, mountName);- mountSelected(name, mountName, server, "prompt");+ mountSelected(name, mountName, server, "prompt", turn.toolScope === undefined && !("url" in server) ? boxProxyEnvNames() : []);}if (turn.integrations?.phone) {if (turn.toolScope !== undefined) {