Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Hermes: remember "Always allow" for MCP tools on its untrusted servers
Bugfixer 2b479b025fd73
diff --git a/server/drivers/acp/hermes-mcp-trust.test.ts b/server/drivers/acp/hermes-mcp-trust.test.tsnew file mode 100644index 0000000000..c7b889f0cf--- /dev/null+++ b/server/drivers/acp/hermes-mcp-trust.test.ts@@ -0,0 +1,99 @@+import { mkdtempSync, readFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";+import { fileURLToPath } from "node:url";+import { describe, expect, it } from "vitest";++import { ensureDirs } from "../../config.ts";+import { providerToolAllowKey, providerToolConfig } from "../../provider-tool-allow.ts";+import { removeTempDir } from "../../testing/cleanup.ts";+import { recordEvents } from "../../testing/events.ts";+import { HermesAgentDriver } from "./hermes.ts";+import { hermesPermissionRequest } from "./hermes-mcp-trust.ts";++const FAKE_CLI = join(dirname(fileURLToPath(import.meta.url)), "../../testing/fake-acp-cli.ts");++// As hermes-agent's acp_adapter/permissions.py sends `_ask_for_call`'s question for a write on an untrusted server.+const message = "MCP tool 'create_issue' on UNTRUSTED server 'linear' wants to run. This tool is write-capable (no readOnlyHint=true annotation) and may modify external state.\n\nArguments:\n title: Fix login";+const trustAsk = (server = "linear", tool = "create_issue", scope = "once, for this session, or always") => {+ const description = `Server '${server}' is configured 'trust: untrusted'. Approve to run '${tool}' ${scope}, or deny to block it.`;+ return { toolCallId: "perm-check-1", title: `${description}: ${message}`, kind: "execute", status: "pending", rawInput: { command: message, description } };+};+const OPTIONS = [+ { optionId: "allow_once", kind: "allow_once", name: "Allow once" },+ { optionId: "allow_session", kind: "allow_always", name: "Allow for session" },+ { optionId: "allow_always", kind: "allow_always", name: "Allow always" },+ { optionId: "deny", kind: "reject_once", name: "Deny" },+];+const ONCE_ONLY = [OPTIONS[0], OPTIONS[3]];++describe("Hermes MCP trust asks", () => {+ it("names a rememberable ask as its MCP tool, keeping the message and options", () => {+ const params = { sessionId: "s", toolCall: trustAsk(), options: OPTIONS };+ const named = hermesPermissionRequest(params);+ expect(named.toolCall).toMatchObject({ kind: "other", title: "mcp__linear__create_issue", rawInput: { command: message } });+ expect(named.options).toBe(OPTIONS);+ });++ it("leaves one-time asks, shell asks and names that would not read back as one tool as sent", () => {+ const sent = [+ { toolCall: trustAsk("linear", "create_issue", "once"), options: ONCE_ONLY }, // past the spend limit+ { toolCall: trustAsk(), options: ONCE_ONLY },+ { toolCall: { ...trustAsk(), kind: "edit" }, options: OPTIONS },+ { toolCall: { kind: "execute", title: "rm -rf build", rawInput: { command: "rm -rf build", description: "recursive delete" } }, options: OPTIONS },+ { toolCall: trustAsk("my__server"), options: OPTIONS },+ { toolCall: trustAsk("linear", "create issue"), options: OPTIONS },+ { toolCall: trustAsk("linear'"), options: OPTIONS },+ ];+ for (const params of sent) expect(hermesPermissionRequest(params)).toBe(params);+ });++ it("keys a grant for a server only Hermes's own config has, never for a device or for another engine", () => {+ const own = providerToolConfig("linear", {}, "hermesAgent");+ const key = providerToolAllowKey("hermes", "mcp__linear__create_issue", own);+ expect(key).toMatch(/^provider-tool:hermes:linear:create_issue:[0-9a-f]{16}$/);+ expect(providerToolAllowKey("hermes", "mcp__linear__create_issue", providerToolConfig("linear", {}, "hermesAgent"))).toBe(key);+ expect(providerToolAllowKey("hermes", "mcp__linear__delete_issue", own)).not.toBe(key);+ expect(providerToolAllowKey("hermes", "mcp__computer__click", providerToolConfig("computer", {}, "hermesAgent"))).toBeNull();+ expect(providerToolConfig("linear", {}, "kimiAgent")).toBeUndefined();+ expect(providerToolConfig("linear", {})).toBeUndefined();+ const configured = { command: "linear-mcp" };+ expect(providerToolConfig("linear", { linear: configured }, "hermesAgent")).toBe(configured);+ });++ it("asks as the MCP tool through the Hermes driver, and Always answers for the session only", async () => {+ ensureDirs();+ const scratch = mkdtempSync(join(tmpdir(), "omb-hermes-trust-"));+ const answer = join(scratch, "answer.txt");+ const instance = await HermesAgentDriver.create({+ instanceId: "hermes-trust",+ displayName: "Hermes trust fixture",+ enabled: true,+ environment: {+ HOME: scratch,+ USERPROFILE: scratch,+ HERMES_HOME: join(scratch, ".hermes"),+ OPENMAUSBOT_PROBE_LOCAL_INJECT: "0",+ FAKE_ACP_MODE: "permission",+ FAKE_ACP_PERMISSION_TOOL_CALL: JSON.stringify(trustAsk()),+ FAKE_ACP_PERMISSION_OPTIONS: JSON.stringify(OPTIONS),+ FAKE_ACP_PERMISSION_ANSWER: answer,+ },+ config: { cli: FAKE_CLI, fullAuto: false },+ });+ const recorder = recordEvents(instance.adapter);+ try {+ const { turnId } = await instance.adapter.sendTurn({ threadId: "hermes-trust-thread", text: "File the issue.", cwd: scratch, approvalMode: "ask" });+ const opened = await recorder.until((event) => event.type === "request.opened" && event.turnId === turnId);+ expect(opened).toMatchObject({ requestType: "permission", tool: "mcp__linear__create_issue", summary: message.slice(0, 200) });+ expect((opened as { command?: unknown }).command).toBeUndefined(); // no shell command to save with its arguments+ await instance.adapter.respondToRequest("hermes-trust-thread", opened.requestId!, { behavior: "allow", always: true });+ await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);+ expect(readFileSync(answer, "utf8")).toBe("allow_session");+ } finally {+ recorder.stop();+ await instance.dispose();+ await removeTempDir(scratch);+ }+ });+});diff --git a/server/drivers/acp/hermes-mcp-trust.ts b/server/drivers/acp/hermes-mcp-trust.tsnew file mode 100644index 0000000000..f51a4b00c4--- /dev/null+++ b/server/drivers/acp/hermes-mcp-trust.ts@@ -0,0 +1,19 @@+// hermes-agent asks before a write-capable tool runs on an MCP server its own config.yaml marks `trust: untrusted`+// (tools/mcp_tool_handlers.py `_ask_for_call`). The ask reaches ACP as a shell call (acp_adapter/permissions.py): kind+// `execute`, title "<description>: <message>", rawInput {command: message, description}. As a shell call, "Always allow"+// saved the message, arguments and all, so the next call of the same tool asked again.++/** The description of an ask that may be remembered: it offers "once, for this session, or always". An ask past a spend+ * limit says "once" only, and a spend-limit ask on a trusted server reads otherwise: both stay one-time asks. */+const REMEMBERABLE_TRUST_ASK = /^Server '([\w-]+)' is configured 'trust: untrusted'\. Approve to run '([\w.-]+)' once, for this session, or always, or deny to block it\.$/;++/** The trust ask named as the MCP tool it is about (kind `other`, title mcp__<server>__<tool>), so a saved grant is the+ * tool's, never the message's; every other request, and a name that would not read back as one tool, is left as sent. */+export function hermesPermissionRequest(params: Record<string, any>): Record<string, any> {+ const toolCall = params.toolCall ?? {};+ const options: Array<{ kind?: unknown }> = Array.isArray(params.options) ? params.options : [];+ const description = toolCall.rawInput?.description;+ const asked = toolCall.kind === "execute" && typeof description === "string" ? REMEMBERABLE_TRUST_ASK.exec(description) : null;+ if (!asked || asked[1].includes("__") || !options.some((option) => option.kind === "allow_always")) return params;+ return { ...params, toolCall: { ...toolCall, kind: "other", title: `mcp__${asked[1]}__${asked[2]}` } };+}diff --git a/server/drivers/acp/hermes.ts b/server/drivers/acp/hermes.tsindex 4c0ba234c6..80c08bab29 100644--- a/server/drivers/acp/hermes.ts+++ b/server/drivers/acp/hermes.ts@@ -11,6 +11,7 @@ import { findCliCandidates, harnessHome, resolveCliSpawn, splitCliString, whichWimport { killCliTree, resolveCli, spawnCli } from "../../procs.ts";import { decodeInjectId, hostApiKey, INJECT_SEP, localHost, mergeLocalInject } from "../local-inject.ts";import { createAcpDriver, type AcpSupport } from "./core.ts";+import { hermesPermissionRequest } from "./hermes-mcp-trust.ts";import { selectAdvertisedMode } from "./session-modes.ts";const EMPTY: ModelCatalog = { default: "", options: [] };@@ -440,6 +441,7 @@ const support: AcpSupport = {// Hermes keeps the first MCP server it registers under a name for the process's life, so a new bearer needs a fresh process.restartOnMcpChange: true,sessionScopedApproval: true,+ permissionRequest: hermesPermissionRequest,resendsReasoningReply: true,steer: { command: "steer", answer: hermesSteerAnswer },transformEnv: (env) => {diff --git a/server/index.ts b/server/index.tsindex 5f6b6ab84b..8ebd64d40e 100644--- a/server/index.ts+++ b/server/index.ts@@ -6452,7 +6452,7 @@ bus.subscribe((event: RuntimeEvent) => {const providerTool = permission && asker && event.requestId && !event.requiresExplicitApproval && !guestDriven && !linkHeld && event.approvalScope !== "local-computer"? providerMcpTool(event.tool): null;- const configuredMcp = providerTool && asker ? providerToolConfig(providerTool.server, engineMcpServers(asker)) : undefined;+ const configuredMcp = providerTool && asker ? providerToolConfig(providerTool.server, engineMcpServers(asker), event.provider) : undefined;const toolAllowKey = configuredMcp === undefined? null: providerToolAllowKey(providerInstanceId, event.tool, configuredMcp);diff --git a/server/provider-tool-allow.ts b/server/provider-tool-allow.tsindex 1f0f594d3d..92d650bbdc 100644--- a/server/provider-tool-allow.ts+++ b/server/provider-tool-allow.ts@@ -9,11 +9,15 @@ const BUILTIN_SERVERS: ReadonlySet<string> = new Set(["composio", "own_apps", "aconst CONTROLS_A_DEVICE: ReadonlySet<string> = new Set(["computer", "phone", "openmausbot_phone"]);const CONTROL_TOOLS: ReadonlySet<string> = new Set(["agents:shared_computer"]);-/** What a grant on `server` is tied to: the server as configured, a built-in bridge by its name, or nothing for a server- * neither configured nor built in. */-export function providerToolConfig(server: string, configured: Record<string, unknown>): unknown {+/** Engines that ask about servers from their own config, which the app cannot read (Hermes: its config.yaml). */+const OWN_MCP_CONFIG: ReadonlySet<string> = new Set(["hermesAgent"]);++/** What a grant on `server` is tied to: the server as configured, a built-in bridge by its name, a server from the+ * engine's own config by the engine and its name, or nothing for a server none of these. */+export function providerToolConfig(server: string, configured: Record<string, unknown>, provider?: string): unknown {if (Object.hasOwn(configured, server)) return configured[server];- return BUILTIN_SERVERS.has(server) ? { builtin: server } : undefined;+ if (BUILTIN_SERVERS.has(server)) return { builtin: server };+ return provider && OWN_MCP_CONFIG.has(provider) ? { engineOwn: provider, server } : undefined;}export interface ProviderMcpTool {