Skip to content

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

fix(claude): save big results from bot URL MCP servers to a file

AdityaUmalee3f049c32cfb
diff --git a/server/drivers/claude.test.ts b/server/drivers/claude.test.ts
index 5da7ec53d6..9a6e4c22f2 100644
--- a/server/drivers/claude.test.ts
+++ b/server/drivers/claude.test.ts
@@ -7,7 +7,8 @@ import { fileURLToPath } from "node:url";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { DATA_DIR, ensureDirs, instanceConfigs, NATIVE_DIR, type AppConfig } from "../config.ts";
-import { TurnAuthenticationRefusedError, type ProviderInstance } from "../contracts.ts";
+import { TurnAuthenticationRefusedError, type ProviderInstance, type SendTurnInput } from "../contracts.ts";
+import { UNGATED_MCP_OUTPUT_TOKENS } from "../mcp-gate-config.ts";
import { recordEvents, type EventRecorder } from "../testing/events.ts";
import { BACKGROUND_CALL_TIMEOUT_MS, BACKGROUND_SETTINGS, backgroundCallArgs } from "./claude-background.ts";
import {
@@ -1818,6 +1819,25 @@ describe("ClaudeDriver turns (fake CLI)", () => {
expect(JSON.stringify(seen.argv)).not.toContain("tok-docs");
});
+ it("has the CLI save a big result from a bot's URL server to a file instead of the thread", async () => {
+ await create();
+ const dump = join(scratch, "ungated-mcp.json");
+ 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);
+ return JSON.parse(readFileSync(dump, "utf8")).env.MAX_MCP_OUTPUT_TOKENS;
+ };
+
+ expect(await turn("t-ungated-url", { custom: { docs } })).toBe(String(UNGATED_MCP_OUTPUT_TOKENS));
+ // The gate already trims a command server and a URL server with a limit of its own; a limit of 0 asks for whole payloads.
+ expect(await turn("t-gated-cmd", { custom: { shop: { command: "npx", args: ["shop"], env: {} } } })).toBeUndefined();
+ 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();
+ });
+
// Claude Code advertises no MCP Apps UI extension and keeps tool results to itself, so only OMB's
// remote proxy can observe a URL app server.
it("mounts a url app server through the proxy and a url server without apps natively, unchanged", async () => {
diff --git a/server/drivers/claude.ts b/server/drivers/claude.ts
index e86e077ad9..c7b91c6440 100644
--- a/server/drivers/claude.ts
+++ b/server/drivers/claude.ts
@@ -35,7 +35,7 @@ import type {
SteerOutcome,
TextGenerationOptions,
} from "../contracts.ts";
-import { appMountFor, customServerGate, gateQueryTools, gateServer } from "../mcp-gate-config.ts";
+import { appMountFor, customServerGate, gateQueryTools, gateServer, limitUngatedOutput } from "../mcp-gate-config.ts";
import { canUseMcpServer } from "../../shared/tool-scope.ts";
import { assertToolScopeSupported } from "../../shared/tool-scope-support.ts";
import { newEventId, newId } from "../contracts.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; // 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; limitUngatedOutput(env, mcpServers, botOwned, turn.integrations?.customResultBudgets); // 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) {
diff --git a/server/mcp-gate-config.test.ts b/server/mcp-gate-config.test.ts
index 25ab1614d5..8e21d2b120 100644
--- a/server/mcp-gate-config.test.ts
+++ b/server/mcp-gate-config.test.ts
@@ -1,6 +1,9 @@
import { describe, expect, it } from "vitest";
-import { customServerGate, gateQueryTools, gateServer, hasOwnResultBudget, mcpStdioServer, resultBudget, serverResultBudget } from "./mcp-gate-config.ts";
+import {
+ customServerGate, gateQueryTools, gateServer, hasOwnResultBudget, limitUngatedOutput, mcpStdioServer, resultBudget, serverResultBudget,
+ UNGATED_MCP_OUTPUT_TOKENS,
+} from "./mcp-gate-config.ts";
import { DEFAULT_RESULT_BUDGET } from "./mcp-trim.ts";
const server = { command: "example-mcp", args: ["--stdio"], env: { TOKEN: "disposable-test-token" } };
@@ -167,3 +170,20 @@ describe("the gate's own query tool", () => {
expect(gateQueryTools({ files: privateTrimming, broken: { args: trimming.args, env: {} } })).toEqual(["mcp__files__query_result"]);
});
});
+
+describe("untrimmed bot servers", () => {
+ const gated = { command: "node", args: ["gate"], env: { OMB_GATE_BUDGET: "8000" } };
+ const limit = (env: NodeJS.ProcessEnv, mounted: Record<string, unknown>, perServer?: Record<string, number>) => {
+ limitUngatedOutput(env, mounted, Object.keys(mounted), perServer);
+ return env.MAX_MCP_OUTPUT_TOKENS;
+ };
+
+ it("has the CLI save big results to a file only when a bot server reaches it untrimmed", () => {
+ expect(limit({}, { docs: remote })).toBe(String(UNGATED_MCP_OUTPUT_TOKENS));
+ expect(limit({}, { docs: gated })).toBeUndefined();
+ expect(limit({}, {})).toBeUndefined();
+ expect(limit({}, { docs: remote }, { docs: 0 })).toBeUndefined();
+ expect(limit({ OMB_MCP_RESULT_BUDGET: "0" }, { docs: remote })).toBeUndefined();
+ expect(limit({ MAX_MCP_OUTPUT_TOKENS: "40000" }, { docs: remote })).toBe("40000");
+ });
+});
diff --git a/server/mcp-gate-config.ts b/server/mcp-gate-config.ts
index 05b8aed0b8..55c84744fc 100644
--- a/server/mcp-gate-config.ts
+++ b/server/mcp-gate-config.ts
@@ -57,6 +57,27 @@ export function customServerGate(
};
}
+/** Claude Code keeps an MCP result in the thread up to MAX_MCP_OUTPUT_TOKENS (25k by default) and saves a bigger one whole to
+ * a file the bot reads in parts. Above the browser's 32k-character budget (about 8k tokens), so harness results stay inline. */
+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. */
+export function limitUngatedOutput(
+ env: NodeJS.ProcessEnv,
+ mounted: Readonly<Record<string, unknown>>,
+ owned: Iterable<string>,
+ perServer: Readonly<Record<string, number>> | undefined,
+): void {
+ if (env.MAX_MCP_OUTPUT_TOKENS) return;
+ 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;
+ }
+}
+
export function spillDir(threadId: string): string {
return join(DATA_DIR, "tool-results", threadId.replace(/[^A-Za-z0-9_-]/g, "-").slice(0, 80) || "thread");
}