Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(codex): let a person's stdio MCP server env through the bot box
AdityaUmalea16108662d34
diff --git a/server/drivers/codex-mcp-mount.ts b/server/drivers/codex-mcp-mount.tsindex 9081d7c76d..4e09311359 100644--- a/server/drivers/codex-mcp-mount.ts+++ b/server/drivers/codex-mcp-mount.ts@@ -1,4 +1,4 @@-import type { StdioMcpSpec } from "../contracts.ts";+import type { McpServerSpec, StdioMcpSpec } from "../contracts.ts";import { BOX_NO_PROXY_ENV, BOX_PROXY_ENV, botBox } from "../bot-box/box.ts";import { tomlInlineTable } from "./codex-process.ts";@@ -37,3 +37,14 @@ export function mountMcpServer(export function boxProxyEnvNames(boxed: boolean = botBox.enabled()): string[] {return boxed ? [...BOX_PROXY_ENV, ...BOX_NO_PROXY_ENV] : [];}++/** A person's own stdio server mounted as configured gets its env only through Codex's own environment (env_vars),+ * which the bot box would otherwise cut down to its allow-list: the app-server spawn lets those names through. */+export function customServerEnvBox(+ custom: Record<string, McpServerSpec> | undefined,+ mounted: ReadonlyMap<string, StdioMcpSpec>,+): { box?: { allowEnv: string[] } } {+ const configured = new Set<McpServerSpec>(Object.values(custom ?? {}));+ const names = [...new Set([...mounted.values()].filter((server) => configured.has(server)).flatMap((server) => Object.keys(server.env)))];+ return names.length ? { box: { allowEnv: names } } : {};+}diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.tsindex 57253ad82f..3ede638874 100644--- a/server/drivers/codex.test.ts+++ b/server/drivers/codex.test.ts@@ -27,6 +27,7 @@ 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 { boxEnvironment } from "../bot-box/env.ts";import { autoVerdict } from "../auto-approve.ts";import { formatQuestionAnswers } from "../../shared/ask-question.ts";@@ -1314,6 +1315,36 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)expect(await envVars("t-boxed-proxy")).toEqual(["LIST", "HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy", "NO_PROXY", "no_proxy"]);}, 20_000);+ it("lets a person's own stdio server's env through the bot box to Codex, which hands it on by name", async () => {+ await create();+ const realSpawnCli = procs.spawnCli;+ const spawns: Parameters<typeof procs.spawnCli>[] = [];+ const spawnSpy = vi.spyOn(procs, "spawnCli").mockImplementation((...args: Parameters<typeof procs.spawnCli>) => {+ spawns.push(args);+ return realSpawnCli(...args);+ });+ const appServer = () => spawns.find(([, args]) => args.some((arg) => arg.startsWith("mcp_servers.")))?.[2];+ try {+ const woo = { command: "python3", args: ["woo.py"], env: { WOO_KEY: "ck_fixture", TRC_ONLY: "1" } };+ await instance.adapter.sendTurn({ threadId: "t-box-env", text: "go", integrations: { custom: { woo } } });+ await recorder.until((event) => event.type === "turn.completed" && event.threadId === "t-box-env");+ const options = appServer()!;+ const allow = new Set(typeof options.box === "object" ? options.box.allowEnv : []);+ expect([...allow].sort()).toEqual(["TRC_ONLY", "WOO_KEY"]);+ // The box keeps only its allow-list: without these names the server would start with neither.+ expect(boxEnvironment(options.env!, new Set(), new Set())).not.toHaveProperty("WOO_KEY");+ expect(boxEnvironment(options.env!, new Set(), allow)).toMatchObject({ WOO_KEY: "ck_fixture", TRC_ONLY: "1" });++ spawns.length = 0;+ const plain = { command: "python3", args: ["todo.py"], env: {} };+ await instance.adapter.sendTurn({ threadId: "t-box-no-env", text: "go", integrations: { custom: { plain } } });+ await recorder.until((event) => event.type === "turn.completed" && event.threadId === "t-box-no-env");+ expect(appServer()).not.toHaveProperty("box");+ } finally {+ spawnSpy.mockRestore();+ }+ }, 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 2393ea4905..791c000a5d 100644--- a/server/drivers/codex.ts+++ b/server/drivers/codex.ts@@ -5,7 +5,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 { boxProxyEnvNames, type McpApprovalMode, mountMcpServer } from "./codex-mcp-mount.ts";+import { boxProxyEnvNames, customServerEnvBox, 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.@@ -659,7 +659,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {const child = spawnHoldingSlot(releaseSlot, () => spawnCli(...codexLaunch(config.cli, appServerArgs), {cwd: turn.cwd ?? homedir(),env,- stdio: ["pipe", "pipe", "pipe"],+ stdio: ["pipe", "pipe", "pipe"], ...customServerEnvBox(turn.integrations?.custom, selectedMcp),}, "bot"));let abandoned = false;