Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(codex): drop the host's MCP restart marker from custom server env

AdityaUmale57cd5931ff67
diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.ts
index 4eb3f52ebd..fabc32dd16 100644
--- a/server/drivers/codex.test.ts
+++ b/server/drivers/codex.test.ts
@@ -1712,6 +1712,27 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)
expect(thread.params.config["shell_environment_policy.exclude"]).toContain("ELECTRON_RUN_AS_NODE");
});
+ it("runs a turn for a restarted custom server, leaving the host's restart count out of its environment", async () => {
+ await create();
+ const dump = join(scratch, "custom-restarted.json");
+ process.env.FAKE_CODEX_DUMP = dump;
+ await instance.adapter.sendTurn({
+ threadId: "t-custom-mcp-restarted",
+ text: "go",
+ integrations: {
+ custom: {
+ health: { command: "health-mcp", args: [], env: { OMB_MCP_RESTART: "1", HEALTH_REGION: "eu" } },
+ },
+ },
+ });
+ await recorder.until((event) => event.type === "turn.completed");
+
+ const seen = JSON.parse(readFileSync(dump, "utf8"));
+ const envVars = JSON.parse(seen.argv.find((arg: string) => arg.startsWith("mcp_servers.health.env_vars=")).split("=").slice(1).join("="));
+ expect(envVars).toEqual(["HEALTH_REGION"]);
+ expect(seen.env.OMB_MCP_RESTART).toBeUndefined();
+ });
+
it.each(["ask", "auto"] as const)("pre-allows peer-agent comms without exposing its token in %s mode", async (approvalMode) => {
await create();
const dump = join(scratch, "agents.json");
diff --git a/server/drivers/codex.ts b/server/drivers/codex.ts
index 06c06f8054..7be3babfbc 100644
--- a/server/drivers/codex.ts
+++ b/server/drivers/codex.ts
@@ -22,7 +22,7 @@ import { ChatGptPlanAuthController } from "./chatgpt-plan-auth.ts";
import { captureCliTree, describeSpawnFailure, killCliTree, spawnCli, waitForExit } from "../procs.ts";
import { cliVersionProbe } from "../cli-version-probe.ts";
import { codexLaunch, codexLaunchesStarting, codexLaunchSlot, codexRpcTimeoutMs, keepStopping, RELAUNCH_EXIT_WAIT_MS, spawnHoldingSlot, tryCodexLaunchSlot } from "./codex-process.ts";
-import { isReservedMcpEnvEntry } from "../mcp-registry.ts";
+import { perTurnCustomMcp } from "../mcp-registry.ts";
import type {
DriverCreateInput,
@@ -533,13 +533,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {
const stopSignal = new AbortController();
const { threadId } = turn;
const approvalMode: ApprovalMode = turn.approvalMode ?? (config.fullAuto ? "full" : "ask");
- for (const [name, server] of Object.entries(turn.integrations?.custom ?? {})) {
- if ("url" in server) continue;
- const reserved = Object.entries(server.env).find(([key, value]) => isReservedMcpEnvEntry(key, value));
- if (reserved) {
- throw new Error(`Custom MCP server “${name}” cannot set reserved environment variable “${reserved[0]}”`);
- }
- }
+ turn = perTurnCustomMcp(turn);
let autoAcceptPermissions = approvalMode === "full";
const chatgptTurn = ownLogin &&
decodeCodexSelection(turn.model || models.default).modelProvider === OFFICIAL_CODEX_PROVIDER;
diff --git a/server/mcp-registry.ts b/server/mcp-registry.ts
index 05332a2ba1..1ebe178b56 100644
--- a/server/mcp-registry.ts
+++ b/server/mcp-registry.ts
@@ -2,6 +2,7 @@ import { z } from "zod";
import { RESERVED_MCP_NAMES, slugMcpName } from "../shared/mcp-name.ts";
import type { McpServerSpec, RemoteMcpSpec, StdioMcpSpec } from "./contracts.ts";
+import { MCP_RESTART_ENV } from "./mcp-app-catalog.ts";
export { importedMcpServerName } from "../shared/mcp-name.ts";
@@ -103,6 +104,22 @@ export function isReservedMcpEnvEntry(name: string, value: string): boolean {
return isHarnessOwnedMcpEnvName(name);
}
+/** Custom command servers as Codex mounts them, with new MCP children every turn: the host's restart count (MCP_RESTART_ENV)
+ * only tells an engine that keeps servers across turns to start a new one, so it is dropped here instead of failing every
+ * turn as an OMB_ name; any other reserved variable is refused. */
+export function perTurnCustomMcp<T extends { integrations?: { custom?: Record<string, McpServerSpec> } }>(turn: T): T {
+ if (!turn.integrations?.custom) return turn;
+ const custom = { ...turn.integrations.custom };
+ for (const [name, server] of Object.entries(custom)) {
+ if ("url" in server) continue;
+ const { [MCP_RESTART_ENV]: _restarts, ...env } = server.env;
+ const reserved = Object.entries(env).find(([key, value]) => isReservedMcpEnvEntry(key, value));
+ if (reserved) throw new Error(`Custom MCP server “${name}” cannot set reserved environment variable “${reserved[0]}”`);
+ custom[name] = { ...server, env };
+ }
+ return { ...turn, integrations: { ...turn.integrations, custom } };
+}
+
function environmentEntryError(name: string, value: string): string | null {
if (!ENV_NAME.test(name)) return `Environment variable “${name}” is not valid.`;
if (isReservedMcpEnvEntry(name, value)) {