Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(mcp): reserve local tool names across every catalog page
Afloat76dc471a5770
diff --git a/server/mcp-gate.test.ts b/server/mcp-gate.test.tsindex e45b58f850..5184a57284 100644--- a/server/mcp-gate.test.ts+++ b/server/mcp-gate.test.ts@@ -50,7 +50,9 @@ createInterface({ input: process.stdin }).on("line", (line) => {if (script.malformedFrame) return process.stdout.write("not-json\\n");if (script.malformedCatalog) return send(msg.id, { tools: [{ inputSchema: { type: "object" } }] });if (msg.params?.collision) { delayedList = msg; return; }- return send(msg.id, catalog(msg.params));+ const listed = catalog(msg.params);+ if (script.collidingQuery) listed.tools.push({ name: "query_result", description: "Upstream query with different arguments", inputSchema: { type: "object", properties: { sql: { type: "string" } } } });+ return send(msg.id, listed);}if (msg.method === "tools/call") {appendFileSync(process.env.SCRIPT + ".calls", msg.params.name + "\\n");@@ -446,6 +448,34 @@ createInterface({ input: process.stdin }).on("line", (line) => {expect(last.tools[1].annotations.readOnlyHint).toBe(true);});+ it("replaces a colliding upstream query_result across all catalog pages", async () => {+ start({ text: full, collidingQuery: true }, {}, SCOPED_UPSTREAM);+ send({ jsonrpc: "2.0", id: 1, method: "tools/list" });+ const first = JSON.parse(await nextLine()).result;+ expect(first.nextCursor).toBe("second");+ expect(first.tools.map((tool: { name: string }) => tool.name)).toEqual(["read", "write"]);+ send({ jsonrpc: "2.0", id: 2, method: "tools/list", params: { cursor: "second" } });+ const last = JSON.parse(await nextLine()).result;+ expect(last.tools.map((tool: { name: string }) => tool.name)).toEqual(["write", "query_result"]);+ expect(last.tools[1].inputSchema.required).toEqual(["result", "query"]);+ expect(last.tools[1].description).not.toContain("Upstream query");+ const local = await query({ result: "missing", query: "@" }, 3);+ expect(local.result.isError).toBe(true);+ expect(local.result.content[0].text).toContain("no longer held");+ expect(existsSync(join(scratch, "reply.json.calls"))).toBe(false);+ });++ it("keeps the upstream query_result catalog and execution when shaping is disabled", async () => {+ start({ text: "upstream query executed", collidingQuery: true }, { OMB_GATE_BUDGET: "0" }, SCOPED_UPSTREAM);+ send({ jsonrpc: "2.0", id: 1, method: "tools/list" });+ const listed = JSON.parse(await nextLine()).result;+ expect(listed.tools.map((tool: { name: string }) => tool.name)).toEqual(["read", "write", "query_result"]);+ expect(listed.tools[2].description).toContain("Upstream query");+ const reply = await query({ sql: "SELECT 1" }, 2);+ expect(reply.result.content[0].text).toBe("upstream query executed");+ expect(readFileSync(join(scratch, "reply.json.calls"), "utf8")).toBe("query_result\n");+ });+it("never lists it when the budget is zero", async () => {start({ text: full }, { OMB_GATE_BUDGET: "0" }, SCOPED_UPSTREAM);send({ jsonrpc: "2.0", id: 1, method: "tools/list", params: { cursor: "second" } });diff --git a/server/mcp-gate.ts b/server/mcp-gate.tsindex 67aec2ac80..abde8aedaa 100644--- a/server/mcp-gate.ts+++ b/server/mcp-gate.ts@@ -402,8 +402,10 @@ createInterface({ input: child.stdout }).on("line", (line) => {if (SCHEMA_PROFILE && request?.kind === "list" && message.error === undefined && isRecord(result) && Array.isArray(result.tools)) {result.tools = fitToolList(result.tools, SCHEMA_PROFILE, (note) => process.stderr.write(`mcp-gate(${NAME}): ${note}\n`));}- if (BUDGET > 0 && request?.kind === "list" && message.error === undefined && isRecord(result) && Array.isArray(result.tools) && result.nextCursor === undefined) {- result.tools = [...result.tools.filter((tool: unknown) => !isRecord(tool) || tool.name !== QUERY_TOOL_NAME), QUERY_TOOL];+ if (BUDGET > 0 && request?.kind === "list" && message.error === undefined && isRecord(result) && Array.isArray(result.tools)) {+ const tools = result.tools.filter((tool: unknown) => !isRecord(tool) || tool.name !== QUERY_TOOL_NAME);+ if (result.nextCursor === undefined) tools.push(QUERY_TOOL);+ result.tools = tools;}if (TOOL_LIMIT !== undefined && request?.kind === "list" && message.error === undefined && isRecord(result)) {message.result = capToolListResult(result, TOOL_LIMIT, (note) => process.stderr.write(`mcp-gate(${NAME}): ${note}\n`));diff --git a/server/mcp-tool-schema.test.ts b/server/mcp-tool-schema.test.tsindex 91b6cf9022..aeba30b2b8 100644--- a/server/mcp-tool-schema.test.ts+++ b/server/mcp-tool-schema.test.ts@@ -219,3 +219,50 @@ describe("a tool list under a provider's schema profile", () => {expect(lines).toHaveLength(1);});});+++describe("owned tools across paginated catalogs", () => {+ const own = { name: "read_file", description: "Read through the authorized host relay", inputSchema: { type: "object", properties: { path: { type: "string" } } } };+ const shadow = { name: "read_file", description: "An upstream tool with incompatible arguments", inputSchema: { type: "object", properties: { id: { type: "number" } } } };+ const other = { name: "search", inputSchema: { type: "object" }, _meta: { source: "child" } };+ const page = (id: number, tools: unknown[], nextCursor?: string) => JSON.stringify({ jsonrpc: "2.0", id, result: { tools, ...(nextCursor === undefined ? {} : { nextCursor }), _meta: { page: id } } });+ const observe = (normalizer: ReturnType<typeof createToolListNormalizer>, id: number) => normalizer.observeRequest(JSON.stringify({ jsonrpc: "2.0", id, method: "tools/list" }));++ for (const profile of [undefined, MOONSHOT]) {+ it(`removes an owned name from every child page before advertising its relay schema (${profile?.provider ?? "no profile"})`, () => {+ const normalizer = createToolListNormalizer(profile, undefined, [own]);+ const all: unknown[] = [];+ for (const [id, tools, cursor] of [[1, [shadow, other], "next"], [2, [shadow], "last"], [3, [shadow], undefined]] as const) {+ observe(normalizer, id);+ const result = JSON.parse(normalizer.rewriteResponse(page(id, [...tools], cursor))).result;+ expect(result._meta).toEqual({ page: id });+ expect(result.nextCursor).toBe(cursor);+ expect(result.tools).toEqual(id === 1 ? [other] : id === 2 ? [] : [own]);+ all.push(...result.tools);+ }+ expect(all).toEqual([other, own]);+ });+ }++ it("keeps byte-identical intermediate pages that contain no owned names", () => {+ const normalizer = createToolListNormalizer(undefined, undefined, [own]);+ observe(normalizer, 1);+ const input = page(1, [other], "next");+ expect(normalizer.rewriteResponse(input)).toBe(input);+ });++ it("preserves the upstream tool when no local replacement is registered", () => {+ const normalizer = createToolListNormalizer();+ observe(normalizer, 1);+ const input = page(1, [shadow, other], "next");+ expect(normalizer.rewriteResponse(input)).toBe(input);+ });++ it("does not rewrite unsolicited or tools/call frames with a matching name", () => {+ const normalizer = createToolListNormalizer(undefined, undefined, [own]);+ const input = page(1, [shadow], "next");+ expect(normalizer.rewriteResponse(input)).toBe(input);+ normalizer.observeRequest(JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call" }));+ expect(normalizer.rewriteResponse(input)).toBe(input);+ });+});diff --git a/server/mcp-tool-schema.ts b/server/mcp-tool-schema.tsindex f080d1f412..ad168bac6e 100644--- a/server/mcp-tool-schema.ts+++ b/server/mcp-tool-schema.ts@@ -175,8 +175,8 @@ export function createToolListNormalizer(profile?: ToolSchemaProfile, log?: (linif (!isObject(frame.result) || !Array.isArray(frame.result.tools)) return line;const own = frame.result.nextCursor ? [] : ownTools;- const ownNames = new Set(own.map((tool) => tool.name));- const listed: unknown[] = own.length+ const ownNames = new Set(ownTools.map((tool) => tool.name));+ const listed: unknown[] = ownNames.size? [...frame.result.tools.filter((tool: unknown) => !isObject(tool) || !ownNames.has(tool.name)), ...own]: frame.result.tools;const tools = profile ? fitToolList(listed, profile, log) : listed.map((tool: unknown) => {@@ -184,7 +184,7 @@ export function createToolListNormalizer(profile?: ToolSchemaProfile, log?: (linconst inputSchema = providerSafeInputSchema(tool.inputSchema);return inputSchema === tool.inputSchema ? tool : { ...tool, inputSchema };});- const changed = own.length > 0 || tools.length !== listed.length || tools.some((tool, index) => tool !== listed[index]);+ const changed = own.length > 0 || listed.length !== frame.result.tools.length || tools.length !== listed.length || tools.some((tool, index) => tool !== listed[index]);return changed ? JSON.stringify({ ...frame, result: { ...frame.result, tools } }) : line;},};