Skip to content

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

fix: cut MCP results over the 25 MB attach limit instead of saving them

AdityaUmale819d41877102
diff --git a/server/drivers/chat-mcp-tools.test.ts b/server/drivers/chat-mcp-tools.test.ts
index f301c7f2ae..dff3a03048 100644
--- a/server/drivers/chat-mcp-tools.test.ts
+++ b/server/drivers/chat-mcp-tools.test.ts
@@ -4,6 +4,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { augmentedPath } from "../env-path.ts";
+import { MESSAGE_FILE_MAX_BYTES } from "../message-file.ts";
import { ChatToolSessionError, SAVED_RESULTS_FOLDER, SAVED_RESULTS_KEPT, chatToolNotices, directServers, mountChatTools, trimmedToolsNotice, type ChatToolSession } from "./chat-mcp-tools.ts";
import type { ToolScope } from "../../shared/tool-scope.ts";
import { catalogServer, CUA_DRIVER_WINDOWS_TOOLS, syntheticToolNames } from "../testing/cua-driver-tools.ts";
@@ -439,6 +440,16 @@ describe("Chat MCP session", () => {
expect(Buffer.byteLength(result.text)).toBeLessThan(52_000);
});
+ // Only computer-use servers may send a frame this big (32 MiB).
+ it("is cut as before when it is over the attach limit", async () => {
+ const f = fixture(`if (message.method === "tools/call") { reply(message,{content:[{type:"text",text:"a".repeat(${MESSAGE_FILE_MAX_BYTES + 1})}]}); continue; }`);
+ const folder = workFolder();
+ const result = await (await f.mount(true, false, undefined, folder)).execute("audit_write", { value: "test" }, f.controller.signal);
+ expect(existsSync(join(folder, SAVED_RESULTS_FOLDER)) ? readdirSync(join(folder, SAVED_RESULTS_FOLDER)) : []).toEqual([]);
+ expect(result.text).toContain("truncated");
+ expect(Buffer.byteLength(result.text)).toBeLessThan(52_000);
+ });
+
it("stays inside the folder whatever the server is called", async () => {
const f = bigFixture();
const folder = workFolder();
diff --git a/server/drivers/chat-mcp-tools.ts b/server/drivers/chat-mcp-tools.ts
index 739c8769bd..b81762ec9a 100644
--- a/server/drivers/chat-mcp-tools.ts
+++ b/server/drivers/chat-mcp-tools.ts
@@ -7,6 +7,7 @@ import type { ValidateFunction } from "ajv";
import { stripControlPlaneEnv } from "../config.ts";
import type { SendTurnInput } from "../contracts.ts";
import { redactSecretsInText } from "../redact.ts";
+import { MESSAGE_FILE_MAX_BYTES } from "../message-file.ts";
import { augmentedPath } from "../env-path.ts";
import { killCliTree, spawnCli } from "../procs.ts";
import { AGENTS_TOOL_ALIASES } from "../../shared/agents-tool-names.ts";
@@ -299,7 +300,7 @@ async function pruneSavedResults(dir: string, keep: string): Promise<void> {
}
/** A result over the limit is kept whole (secrets masked by `scrub`) in `folder`/mcp-results and the model reads its start and the file's path.
- * Without a folder, or when the file cannot be written, it is cut as before. */
+ * Without a folder, when the file cannot be written, or when it is over the attach limit, it is cut as before. */
async function savedOrBounded(value: string, tool: string, folder: string | undefined, scrub: (text: string) => string): Promise<string> {
const bytes = Buffer.from(value);
if (bytes.length <= OUTPUT_BYTES || !folder) return boundedText(value, bytes);
@@ -311,6 +312,8 @@ async function savedOrBounded(value: string, tool: string, folder: string | unde
const name = `result-${tool}-${Date.now()}-${randomBytes(3).toString("hex")}.txt`, file = join(dir, name);
// Key-shaped values are masked before the result reaches disk, as for every other kept tool result.
const kept = scrub(value);
+ // A file over the attach limit could not be attached, so it is cut as before.
+ if (Buffer.byteLength(kept) > MESSAGE_FILE_MAX_BYTES) return boundedText(value, bytes);
await writeFile(file, kept, { flag: "wx", mode: 0o600 });
await pruneSavedResults(dir, name).catch(() => {});
const kb = Math.ceil(bytes.length / 1024), masked = kept === value ? "" : " (secret-looking values in it are masked)";