Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix: bound metadata in held MCP query replies
Afloat3183c8c20da9
diff --git a/server/mcp-gate.test.ts b/server/mcp-gate.test.tsindex db40dd60b6..52f6e1eb33 100644--- a/server/mcp-gate.test.ts+++ b/server/mcp-gate.test.ts@@ -441,6 +441,25 @@ createInterface({ input: process.stdin }).on("line", (line) => {expect(existsSync(join(scratch, "reply.json.calls"))).toBe(false);});+ it("bounds wide query metadata on the wire and keeps the held result queryable", async () => {+ const key = `items_${"x".repeat(10_000)}`;+ const rows = Array.from({ length: 20 }, (_, id) => ({ id, text: "y".repeat(100) }));+ start({ text: JSON.stringify({ [key]: rows }) }, { OMB_GATE_BUDGET: "512" }, SCOPED_UPSTREAM);+ const shaped = (await call("read")).result.content[0].text;+ expect(shaped.length).toBeLessThanOrEqual(512);+ expect(shaped).toContain('"r1"');++ const whole = (await query({ result: "r1", query: "@" }, 2)).result;+ expect(whole.content[0].text.length).toBeLessThanOrEqual(512);+ expect(whole.content[0].text).toContain("Narrow the query");+ const invalid = (await query({ result: "r1", query: "[?" }, 3)).result;+ expect(invalid.isError).toBe(true);+ expect(invalid.content[0].text.length).toBeLessThanOrEqual(512);+ const selected = (await query({ result: "r1", query: `${JSON.stringify(key)}[-1].id` }, 4)).result;+ expect(selected.content[0].text).toBe("19");+ expect(readFileSync(join(scratch, "reply.json.calls"), "utf8")).toBe("read\n");+ });+it("keeps query_result listed and callable under a tool selection that never names it", async () => {start({ text: full }, { OMB_GATE_TOOL_SCOPE: JSON.stringify({ allow: ["mcp:shop:read"] }) }, SCOPED_UPSTREAM);send({ jsonrpc: "2.0", id: 1, method: "tools/list", params: { cursor: "second" } });diff --git a/server/mcp-shape.ts b/server/mcp-shape.tsindex 9082062633..dc465a7ff9 100644--- a/server/mcp-shape.ts+++ b/server/mcp-shape.ts@@ -126,12 +126,13 @@ const text = (body: string, isError?: true): QueryToolResult => ({ content: [{ t/** Answer one query_result call locally; the upstream server never sees it. */export function answerQuery(store: ResultStore, args: unknown, budget: number): QueryToolResult {+ const fail = (body: string) => text(trimResultText({ text: body, budget }).text, true);if (!isRecord(args) || typeof args.result !== "string" || typeof args.query !== "string" || !args.query.trim()) {- return text(`${QUERY_TOOL_NAME} needs {"result": "<handle from the shaped result>", "query": "<JMESPath>"}.`, true);+ return fail(`${QUERY_TOOL_NAME} needs {"result": "<handle from the shaped result>", "query": "<JMESPath>"}.`);}const held = store.get(args.result);if (!held) {- return text(`Result "${args.result}" is no longer held; call the original tool again and query the new handle it gives you.`, true);+ return fail(`Result "${args.result}" is no longer held; call the original tool again and query the new handle it gives you.`);}const shape = describeShape(held.value);const example = exampleQuery(held.value);@@ -140,7 +141,7 @@ export function answerQuery(store: ResultStore, args: unknown, budget: number):selected = jmespath.search(held.value, args.query);} catch (error) {const reason = error instanceof Error ? error.message : String(error);- return text(`Invalid JMESPath query (${reason}). The held result "${args.result}" is ${shape}. Examples: ${example}; length(@).`, true);+ return fail(`Invalid JMESPath query (${reason}). The held result "${args.result}" is ${shape}. Examples: ${example}; length(@).`);}const body = JSON.stringify(selected) ?? "null";if (body.length <= budget) return text(body);diff --git a/server/mcp-shaping-budget.test.ts b/server/mcp-shaping-budget.test.tsnew file mode 100644index 0000000000..c4b784901c--- /dev/null+++ b/server/mcp-shaping-budget.test.ts@@ -0,0 +1,59 @@+import { describe, expect, it } from "vitest";++import { ResultStore, answerQuery } from "./mcp-shape.ts";++const wideResult = () => Object.fromEntries(Array.from({ length: 80 }, (_, index) => [+ `field_${index}_${"x".repeat(100)}`,+ Array.from({ length: 20 }, (_, id) => ({ id, text: "payload".repeat(20) })),+]));++describe("held-result metadata obeys the query budget", () => {+ it.each([512, 2_000, 8_000])("bounds a successful wide query to %i characters", budget => {+ const store = new ResultStore();+ const value = wideResult();+ const handle = store.put("wide_search", value);+ const answer = answerQuery(store, { result: handle, query: "@" }, budget);+ expect(answer.isError).toBeUndefined();+ expect(answer.content[0]!.text.length).toBeLessThanOrEqual(budget);+ expect(answer.content[0]!.text).toContain("TRUNCATED by OpenMausBot");+ expect(answer.content[0]!.text).toContain(`"${handle}"`);+ expect(answer.content[0]!.text).toContain("Narrow the query");+ expect(store.get(handle)!.value).toBe(value);+ expect(answerQuery(store, { result: handle, query: "length(keys(@))" }, budget).content[0]!.text).toBe("80");+ });++ it.each([512, 2_000, 8_000])("bounds the shape in a query error to %i characters", budget => {+ const store = new ResultStore();+ const handle = store.put("wide_search", wideResult());+ const answer = answerQuery(store, { result: handle, query: "[?" }, budget);+ expect(answer.isError).toBe(true);+ expect(answer.content[0]!.text.length).toBeLessThanOrEqual(budget);+ expect(answer.content[0]!.text).toContain("Invalid JMESPath query");+ expect(answerQuery(store, { result: handle, query: "length(keys(@))" }, budget).content[0]!.text).toBe("80");+ });++ it("bounds an unknown handle echoed into an error", () => {+ const answer = answerQuery(new ResultStore(), { result: "r".repeat(20_000), query: "@" }, 512);+ expect(answer.isError).toBe(true);+ expect(answer.content[0]!.text.length).toBeLessThanOrEqual(512);+ });++ it("keeps a small diagnostic and selected JSON unchanged", () => {+ const store = new ResultStore();+ const value = { items: [{ id: 1, title: "中文🙂" }] };+ const handle = store.put("search", value);+ expect(answerQuery(store, { result: handle, query: "@" }, 512)).toEqual({ content: [{ type: "text", text: JSON.stringify(value) }] });+ const invalid = answerQuery(store, { result: handle, query: "[?" }, 8_000);+ expect(invalid.isError).toBe(true);+ expect(invalid.content[0]!.text.startsWith("Invalid JMESPath query")).toBe(true);+ expect(invalid.content[0]!.text).toContain("items: 1 items{id, title}");+ expect(invalid.content[0]!.text).not.toContain("TRUNCATED");+ });++ it("keeps a missing handle error readable at the minimum budget", () => {+ expect(answerQuery(new ResultStore(), { result: "r77", query: "@" }, 512)).toEqual({+ content: [{ type: "text", text: 'Result "r77" is no longer held; call the original tool again and query the new handle it gives you.' }],+ isError: true,+ });+ });+});diff --git a/server/mcp-trim.ts b/server/mcp-trim.tsindex bf1f432034..34573149f1 100644--- a/server/mcp-trim.ts+++ b/server/mcp-trim.ts@@ -119,6 +119,7 @@ function heldBanner(const call = `${QUERY_TOOL_NAME} {"result":"${held.handle}","query":"<JMESPath>"}`;if (held.queried) {if (input.footer) return `⚠️ TRUNCATED by OpenMausBot: ${kept} of your query on "${held.handle}"; narrow it (see above).`;+ if (input.short) return `⚠️ TRUNCATED by OpenMausBot: ${kept} of your query on "${held.handle}"; the rest was NOT shown. Narrow the query.`;return `⚠️ TRUNCATED by OpenMausBot: your query on "${held.handle}" returned ${fmt(input.originalChars)} chars; ${kept}. The rest was NOT shown. Narrow the query: pick fewer fields with {a: a, b: b}, slice with [0:20], or filter with [?cond]. The result is ${held.shape}.`;}if (input.footer) return `⚠️ SHAPED by OpenMausBot: ${kept}; the rest is held as "${held.handle}" for ${QUERY_TOOL_NAME} (see the note above).`;