Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: keep a bot-set MCP output limit, leave the limit unset when a server asks for whole payloads, and restart the warm process when the limit changes
AdityaUmale7fa3d6024a25
diff --git a/server/drivers/claude.test.ts b/server/drivers/claude.test.tsindex 9a6e4c22f2..cf7effb222 100644--- a/server/drivers/claude.test.ts+++ b/server/drivers/claude.test.ts@@ -1695,6 +1695,19 @@ describe("ClaudeDriver turns (fake CLI)", () => {expect(seen.settings.claudeMdExcludes).toEqual([join(homedir(), ".claude", "CLAUDE.md")]);});+ it("keeps an MCP output limit the bot's settings set instead of the automatic one", async () => {+ const dump = join(scratch, "bot-settings-mcp-limit.json");+ process.env.FAKE_CLAUDE_DUMP = dump;+ await create();+ const { thread, file } = botFolders("mcp-limit", { env: { MAX_MCP_OUTPUT_TOKENS: "40000" } });+ await instance.adapter.sendTurn({ threadId: "t-bot-settings-mcp-limit", text: "hi", cwd: thread, botSettingsFile: file,+ integrations: { custom: { docs: { type: "http", url: "https://docs.example/mcp", headers: {} } } } });+ await recorder.until((e) => e.type === "turn.completed");+ const seen = JSON.parse(readFileSync(dump, "utf8"));+ expect(seen.env.MAX_MCP_OUTPUT_TOKENS).toBeUndefined();+ expect(seen.settings.env.MAX_MCP_OUTPUT_TOKENS).toBe("40000");+ });+it("is left to Claude Code when the turn runs in the folder that holds it", async () => {const dump = join(scratch, "bot-settings-own-folder.json");process.env.FAKE_CLAUDE_DUMP = dump;@@ -1825,8 +1838,8 @@ describe("ClaudeDriver turns (fake CLI)", () => {process.env.FAKE_CLAUDE_DUMP = dump;const docs = { type: "http" as const, url: "https://docs.example/mcp", headers: {} };const turn = async (threadId: string, integrations: SendTurnInput["integrations"]) => {- await instance.adapter.sendTurn({ threadId, text: "hi", integrations });- await recorder.until((e) => e.type === "turn.completed" && e.threadId === threadId);+ const { turnId } = await instance.adapter.sendTurn({ threadId, text: "hi", integrations });+ await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);return JSON.parse(readFileSync(dump, "utf8")).env.MAX_MCP_OUTPUT_TOKENS;};@@ -1836,6 +1849,9 @@ describe("ClaudeDriver turns (fake CLI)", () => {expect(await turn("t-gated-url", { custom: { docs }, customResultBudgets: { docs: 4000 } })).toBeUndefined();expect(await turn("t-whole-url", { custom: { docs }, customResultBudgets: { docs: 0 } })).toBeUndefined();expect(await turn("t-no-custom", {})).toBeUndefined();+ // The limit is part of the warm process's contract: changing a server's limit on the same thread starts a fresh process.+ expect(await turn("t-ungated-url", { custom: { docs }, customResultBudgets: { docs: 0 } })).toBeUndefined();+ expect(await turn("t-ungated-url", { custom: { docs } })).toBe(String(UNGATED_MCP_OUTPUT_TOKENS));});// Claude Code advertises no MCP Apps UI extension and keeps tool results to itself, so only OMB'sdiff --git a/server/drivers/claude.ts b/server/drivers/claude.tsindex c7b91c6440..6b478e6d2b 100644--- a/server/drivers/claude.ts+++ b/server/drivers/claude.ts@@ -1150,7 +1150,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeConfig> = {allowed.push("mcp__ogb", ...gateQueryTools(mcpServers));// A guest pre-allows only harness tools; anything else (the browser can open file:) asks.if (turn.guestConfined) allowed.splice(0, allowed.length, ...allowed.filter((name) => name === "mcp__ogb" || name === "mcp__agents"));- const env = withEndpointFolder(environment(config.endpointModels ? undefined : turnModel), config, () => retryState.delete(threadId)); env.PATH = turnEnvironment.PATH; limitUngatedOutput(env, mcpServers, botOwned, turn.integrations?.customResultBudgets); // PATH: launch the copy this turn checked, not one a newer PATH finds+ const env = withEndpointFolder(environment(config.endpointModels ? undefined : turnModel), config, () => retryState.delete(threadId)); env.PATH = turnEnvironment.PATH; // launch the copy this turn checked, not one a newer PATH finds// The MCP config carries credentials and argv is readable via `ps`: a 0600 file, removed at settle.let mcpConfigPath: string | null = null;if (Object.keys(mcpServers).length) {@@ -1183,8 +1183,8 @@ export const ClaudeDriver: ProviderDriver<ClaudeConfig> = {if (turn.guestConfined) own.permissions = GUEST_CLAUDE_PERMISSIONS;// Approvals and browser credentials expire at the turn boundary; native background workers must not outlive it.env.CLAUDE_CODE_DISABLE_BACKGROUND_TASKS = "1";- // Never for a guest: --restricted leaves project settings out, and a hook is a command.- const botSettings = turn.guestConfined ? {} : botSettingsFor(turn.botSettingsFile, cwd, env);+ // Never for a guest: --restricted leaves project settings out, and a hook is a command. The MCP output limit comes after, so one the bot set there is kept.+ const botSettings = turn.guestConfined ? {} : botSettingsFor(turn.botSettingsFile, cwd, env); limitUngatedOutput(env, mcpServers, botOwned, turn.integrations?.customResultBudgets, (botSettings.env as Record<string, unknown> | undefined)?.MAX_MCP_OUTPUT_TOKENS);const settings = withBotSettings(botSettings, own);const authSettingsPath = mcpConfigPath && Object.keys(settings).length? join(dirname(mcpConfigPath), "auth-settings.json") : null;@@ -1201,7 +1201,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeConfig> = {base: env.ANTHROPIC_BASE_URL ?? null,configDir: env.CLAUDE_CONFIG_DIR ?? null,hooks: Boolean(hooks),- toolSearch,+ toolSearch, mcpOutputTokens: env.MAX_MCP_OUTPUT_TOKENS ?? null,botSettings: createHash("sha256").update(JSON.stringify(botSettings)).digest("hex"),// Rotating an account's key/helper must not reuse the old process.auth: createHash("sha256").update(JSON.stringify({diff --git a/server/mcp-gate-config.test.ts b/server/mcp-gate-config.test.tsindex 8e21d2b120..92eb3c727c 100644--- a/server/mcp-gate-config.test.ts+++ b/server/mcp-gate-config.test.ts@@ -186,4 +186,16 @@ describe("untrimmed bot servers", () => {expect(limit({ OMB_MCP_RESULT_BUDGET: "0" }, { docs: remote })).toBeUndefined();expect(limit({ MAX_MCP_OUTPUT_TOKENS: "40000" }, { docs: remote })).toBe("40000");});++ it("keeps a limit the bot's settings file sets", () => {+ const env: NodeJS.ProcessEnv = {};+ limitUngatedOutput(env, { docs: remote }, ["docs"], undefined, "40000");+ expect(env.MAX_MCP_OUTPUT_TOKENS).toBeUndefined();+ });++ it("leaves the process-wide limit unset when another untrimmed server asks for whole payloads", () => {+ expect(limit({}, { whole: remote, docs: remote }, { whole: 0 })).toBeUndefined();+ expect(limit({}, { docs: remote, whole: remote }, { whole: 0 })).toBeUndefined();+ expect(limit({}, { whole: gated, docs: remote }, { whole: 0 })).toBe(String(UNGATED_MCP_OUTPUT_TOKENS));+ });});diff --git a/server/mcp-gate-config.ts b/server/mcp-gate-config.tsindex 55c84744fc..307a7dc62c 100644--- a/server/mcp-gate-config.ts+++ b/server/mcp-gate-config.ts@@ -62,20 +62,24 @@ export function customServerGate(export const UNGATED_MCP_OUTPUT_TOKENS = 10_000;/** A bot server the gate leaves untrimmed (a URL server keeps its native connection) has the CLI save big results to a file- * instead of filling the thread; a limit of 0 asks for whole payloads, and an explicit MAX_MCP_OUTPUT_TOKENS wins. */+ * instead of filling the thread. The limit is process-wide, so it stays unset when any untrimmed server asks for whole+ * payloads (a limit of 0), and an explicit MAX_MCP_OUTPUT_TOKENS (the process's or the bot settings file's) wins. */export function limitUngatedOutput(env: NodeJS.ProcessEnv,mounted: Readonly<Record<string, unknown>>,owned: Iterable<string>,perServer: Readonly<Record<string, number>> | undefined,+ configured?: unknown,): void {- if (env.MAX_MCP_OUTPUT_TOKENS) return;+ if (env.MAX_MCP_OUTPUT_TOKENS || configured !== undefined) return;+ let untrimmed = false;for (const name of owned) {const server = Object.hasOwn(mounted, name) ? mounted[name] as { env?: Record<string, string> } | undefined : undefined;- if (!server || server.env?.OMB_GATE_BUDGET || serverResultBudget(name, perServer, env) <= 0) continue;- env.MAX_MCP_OUTPUT_TOKENS = String(UNGATED_MCP_OUTPUT_TOKENS);- return;+ if (!server || server.env?.OMB_GATE_BUDGET) continue;+ if (serverResultBudget(name, perServer, env) <= 0) return;+ untrimmed = true;}+ if (untrimmed) env.MAX_MCP_OUTPUT_TOKENS = String(UNGATED_MCP_OUTPUT_TOKENS);}export function spillDir(threadId: string): string {