Skip to content

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

A web connection can be one bot's own

Bruno1d53cbb992d6
diff --git a/server/cloud-connections-stayed.ts b/server/cloud-connections-stayed.ts
index 359a4dc0d5..6d5ddf5477 100644
--- a/server/cloud-connections-stayed.ts
+++ b/server/cloud-connections-stayed.ts
@@ -30,7 +30,7 @@ export function stayedConnectors(servers: Record<string, unknown> | undefined, p
const remote = isRemoteMcpServer(server);
const program = remote ? "" : launcherOf(server.command);
const why: StayedWhy = isMailConnector(server) ? { kind: "mail" } : !remote && !/[\\/]/u.test(server.command) && !programs.includes(program) ? { kind: "program", program } : { kind: "here" };
- return [{ name, computer: server.runsOn!.label, runsOn: server.runsOn!, why, ...(!remote && server.bot ? { bot: server.bot } : {}) }];
+ return [{ name, computer: server.runsOn!.label, runsOn: server.runsOn!, why, ...(server.bot ? { bot: server.bot } : {}) }];
});
}
diff --git a/server/host/mcp-servers-host.test.ts b/server/host/mcp-servers-host.test.ts
index 035f190743..4b57fdb888 100644
--- a/server/host/mcp-servers-host.test.ts
+++ b/server/host/mcp-servers-host.test.ts
@@ -5,6 +5,7 @@ import { afterEach, expect, it } from "vitest";
import type { AppConfig } from "../config.ts";
import { mcpServerConfigHash } from "../mcp-app-catalog.ts";
+import { McpOAuthManager } from "../mcp-oauth.ts";
import { VaultStore } from "../vault-store.ts";
import { createMcpKeys } from "../vault/mcp-keys.ts";
import { claimedNotice, createMcpServersHost, startCheckTimeout } from "./mcp-servers-host.ts";
@@ -198,3 +199,16 @@ it("forgets which Vault items a server could read when its bot's own server goes
expect(cfg.mcpServers).toEqual({});
expect(vault.mcpServerKeys("bot-server")).toEqual([]);
});
+
+it("forgets the sign-in of a bot's own web connection when it goes with the bot, so a new one by that name starts signed out", () => {
+ const data = mkdtempSync(join(tmpdir(), "omb-mcp-host-"));
+ dirs.push(data);
+ const url = "https://mcp.linear.app/mcp";
+ const oauth = new McpOAuthManager({ file: join(data, "mcp-oauth.json") });
+ oauth.adopt("linear_iris", url, { url, state: "signed-in", tokens: { access: "token" } });
+ const cfg = { mcpServers: { linear_iris: { type: "http", url, headers: {}, enabled: true, bot: "iris" } } } as unknown as AppConfig;
+ const host = createMcpServersHost({ dataDir: data, cfg: () => cfg, persist: (next) => { cfg.mcpServers = next; }, bots: () => [IRIS], startsServers: () => true, boxed: () => false, policy: (servers) => servers, tell: () => {}, forgetSignIn: (name) => oauth.forget(name) });
+ host.botDeleted("iris");
+ expect(cfg.mcpServers).toEqual({});
+ expect(oauth.authState("linear_iris", url)).not.toBe("signed-in");
+});
diff --git a/server/host/mcp-servers-host.ts b/server/host/mcp-servers-host.ts
index 61a3891483..8b82d5ad7e 100644
--- a/server/host/mcp-servers-host.ts
+++ b/server/host/mcp-servers-host.ts
@@ -35,6 +35,8 @@ export interface McpServersHostDeps {
start?: McpHealthDeps["start"];
/** A server's `vault:<id>` keys, read when it is launched or called. */
keys?: McpKeys;
+ /** Drops a server's sign-in: its tokens and any sign-in still open. */
+ forgetSignIn?(name: string): void;
}
const CLAIMED_FILE = (dataDir: string) => join(dataDir, "mcp-bot-servers.json");
@@ -151,7 +153,11 @@ export function createMcpServersHost(deps: McpServersHostDeps) {
const next = withoutBotMcpServers(before, botId);
if (!next) return;
deps.persist(next);
- for (const name of Object.keys(before ?? {})) if (!Object.hasOwn(next, name)) deps.keys?.forgetServer(name);
+ for (const name of Object.keys(before ?? {})) {
+ if (Object.hasOwn(next, name)) continue;
+ deps.keys?.forgetServer(name);
+ deps.forgetSignIn?.(name);
+ }
},
/** At start: the first time bots run boxed here, servers in one existing bot's folders become its own (said once);
* then every server is checked. A server added after that stays as it was added. */
diff --git a/server/index.ts b/server/index.ts
index b62fd6f6c0..f3da6014ad 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -1376,7 +1376,7 @@ const attributeUsage = createUsageAttribution({ section: (botId) => store.bot(bo
}
}
-const providerAuthSessions = new ProviderAuthSessions({ signedIn: (instanceId) => limitTakeover.signedIn(instanceId) }); const mcpServersHost = createMcpServersHost({ dataDir: DATA_DIR, cfg: () => cfg, persist: persistMcpServers, bots: () => store.bots, policy: (servers) => managedPolicy.filterMcp(servers), tell: (text, about) => notify(buildNoticeNotification(about, store.bot(about.id)?.threadId ?? "", text)), startsServers: (botId) => { const bot = store.bot(botId); return Boolean(bot && registry.get(bot.modelSelection.instanceId)?.adapter.capabilities.customMcp === true); }, boxed: () => botBox.enabled(), keys: mcpKeys });
+const providerAuthSessions = new ProviderAuthSessions({ signedIn: (instanceId) => limitTakeover.signedIn(instanceId) }); const mcpServersHost = createMcpServersHost({ dataDir: DATA_DIR, cfg: () => cfg, persist: persistMcpServers, bots: () => store.bots, policy: (servers) => managedPolicy.filterMcp(servers), tell: (text, about) => notify(buildNoticeNotification(about, store.bot(about.id)?.threadId ?? "", text)), startsServers: (botId) => { const bot = store.bot(botId); return Boolean(bot && registry.get(bot.modelSelection.instanceId)?.adapter.capabilities.customMcp === true); }, boxed: () => botBox.enabled(), keys: mcpKeys, forgetSignIn: (name) => mcpOAuth.forget(name) });
const mcpOAuth = new McpOAuthManager({
file: join(DATA_DIR, "mcp-oauth.json"),
isOwnerLive: (owner) => owner === "loopback" || sessions.isLive(owner) || isCardOwnerLive(store, owner),
diff --git a/server/mcp-bot-rules.test.ts b/server/mcp-bot-rules.test.ts
index 8d8234c891..cee061e1a7 100644
--- a/server/mcp-bot-rules.test.ts
+++ b/server/mcp-bot-rules.test.ts
@@ -28,6 +28,10 @@ describe("mcpOwner and botMayChange", () => {
expect(owner(stdio({ bot: "bot2" }))).toBe("theirs");
expect(botMayChange("theirs")).toEqual({ ok: false, error: "That server belongs to another bot." });
});
+ it("a web connection that is another bot's own is theirs, not shared", () => {
+ expect(owner(remote({ bot: "bot2" }))).toBe("theirs");
+ expect(owner(remote({ bot: "bot1" }))).toBe("own");
+ });
it("a Team Cloud personal server wins over everything else", () => {
expect(owner(remote(), ["x"])).toBe("person");
expect(owner(stdio({ bot: "bot2" }), ["x"])).toBe("person");
diff --git a/server/mcp-bot-rules.ts b/server/mcp-bot-rules.ts
index dc10165e8f..7bd8f4b50b 100644
--- a/server/mcp-bot-rules.ts
+++ b/server/mcp-bot-rules.ts
@@ -13,9 +13,9 @@ const SECRET_FLAG_ARG = /^--(?:token|password|passwd|api-key|apikey|secret|acces
export function mcpOwner(name: string, server: StoredMcpServer, botId: string, personal: readonly string[]): McpOwner {
if (personal.includes(name)) return "person";
- if (!isRemoteMcpServer(server) && server.bot && server.bot !== botId) return "theirs";
+ if (server.bot && server.bot !== botId) return "theirs";
if (server.runsOn) return "elsewhere";
- if (!isRemoteMcpServer(server) && server.bot) return "own";
+ if (server.bot) return "own";
return "shared";
}
diff --git a/server/mcp-bot-servers.test.ts b/server/mcp-bot-servers.test.ts
index 1e6ed3bf6f..57fb968061 100644
--- a/server/mcp-bot-servers.test.ts
+++ b/server/mcp-bot-servers.test.ts
@@ -125,6 +125,11 @@ describe("existing installs", () => {
expect(withoutBotMcpServers(raw, "iris")).toEqual({ notes: raw.notes });
expect(withoutBotMcpServers(raw, "paul")).toBeNull();
});
+
+ it("drops a deleted bot's own web connection too", () => {
+ const raw = { linear_iris: { type: "http", url: "https://mcp.linear.app/mcp", headers: {}, enabled: true, bot: "iris" }, shared: { type: "http", url: "https://b.example/mcp", headers: {}, enabled: true } };
+ expect(withoutBotMcpServers(raw, "iris")).toEqual({ shared: raw.shared });
+ });
});
describe("a bot adding a server where bots are boxed", () => {
diff --git a/server/mcp-bot-servers.ts b/server/mcp-bot-servers.ts
index abdacdc683..3fb6f3a488 100644
--- a/server/mcp-bot-servers.ts
+++ b/server/mcp-bot-servers.ts
@@ -207,7 +207,7 @@ export function claimBotMcpServers(raw: Record<string, unknown> | undefined, dat
export function withoutBotMcpServers(raw: Record<string, unknown> | undefined, botId: string): Record<string, unknown> | null {
const kept = Object.entries(raw ?? {}).filter(([name, value]) => {
const parsed = parseStoredMcpServer(name, value);
- return !(parsed.ok && !isRemoteMcpServer(parsed.server) && parsed.server.bot === botId);
+ return !(parsed.ok && parsed.server.bot === botId);
});
return kept.length === Object.keys(raw ?? {}).length ? null : Object.fromEntries(kept);
}
diff --git a/server/mcp-registry.test.ts b/server/mcp-registry.test.ts
index 8ff3fdf9c2..187ef4da2a 100644
--- a/server/mcp-registry.test.ts
+++ b/server/mcp-registry.test.ts
@@ -4,6 +4,7 @@ import {
addDisabledMcpServer,
listMcpServers,
MAX_MCP_SERVERS,
+ mcpServerLimitError,
mcpSharedServerRoom,
mcpServerNameError,
parseMcpServerMutation,
@@ -399,3 +400,44 @@ describe("mcpSharedServerRoom", () => {
expect(mcpSharedServerRoom(full)).toBe(0);
});
});
+
+describe("a web connection that is one bot's own", () => {
+ const BOT = "8c7d0a1e-1111-4222-8333-444455556666";
+ const linear = { type: "http", url: "https://mcp.linear.app/mcp", headers: { Authorization: "Bearer k" }, bot: BOT };
+
+ it("keeps the bot it is for, lists it, and keeps it through an edit that does not mention it", () => {
+ const stored = parseStoredMcpServer("linear_ada", linear);
+ expect(stored).toEqual({ ok: true, server: { type: "http", url: "https://mcp.linear.app/mcp", headers: { Authorization: "Bearer k" }, enabled: true, bot: BOT } });
+ if (!stored.ok) throw new Error("unreachable");
+ expect(listMcpServers({ linear_ada: linear })).toEqual([expect.objectContaining({ name: "linear_ada", bot: BOT })]);
+ expect(parseMcpServerMutation("linear_ada", { type: "http", url: "https://mcp.linear.app/mcp", headers: { Authorization: true } }, stored.server))
+ .toMatchObject({ ok: true, server: { bot: BOT } });
+ expect(parseStoredMcpServer("linear_ada", { ...linear, bot: "../x" }).ok).toBe(false);
+ });
+
+ it("starts only for that bot", async () => {
+ const { customMcpServers } = await import("./config.ts");
+ const cfg = { mcpServers: { linear_ada: linear, shared: { type: "http", url: "https://b.example/mcp" } } } as never;
+ expect(Object.keys(customMcpServers(cfg, undefined, BOT))).toEqual(["linear_ada", "shared"]);
+ expect(Object.keys(customMcpServers(cfg, undefined, "someone-else"))).toEqual(["shared"]);
+ });
+
+ it("counts as that bot's own, not against every bot's 20", () => {
+ const own = (i: number) => ({ type: "http", url: `https://mcp.linear.app/mcp?${i}`, headers: {}, enabled: true, bot: `bot${i}` });
+ const servers: Record<string, unknown> = {};
+ for (let i = 0; i < 30; i++) servers[`l${i}`] = own(i);
+ expect(mcpSharedServerRoom(servers)).toBe(MAX_MCP_SERVERS - 1);
+ });
+
+ it("lets one bot reach 20 with its own web connections, refuses a 21st, and leaves other bots' own out of its count", () => {
+ const mine = (i: number) => ({ type: "http", url: `https://m.example/${i}`, headers: {}, enabled: true, bot: BOT });
+ const servers: Record<string, unknown> = { theirs: { ...mine(99), bot: "other-bot" } };
+ for (let i = 0; i < 19; i++) servers[`m${i}`] = mine(i);
+ expect(mcpServerLimitError(servers, [mine(19)])).toBeNull();
+ expect(mcpServerLimitError(servers, [mine(19), mine(20)])).toBe(`A bot can use at most ${MAX_MCP_SERVERS} MCP servers: the shared ones plus its own.`);
+ });
+
+ it("an sse connection can be one bot's own too", () => {
+ expect(parseStoredMcpServer("s", { type: "sse", url: "https://s.example/sse", bot: BOT })).toMatchObject({ ok: true, server: { type: "sse", bot: BOT } });
+ });
+});
diff --git a/server/mcp-registry.ts b/server/mcp-registry.ts
index b2596df8c2..8bd965979e 100644
--- a/server/mcp-registry.ts
+++ b/server/mcp-registry.ts
@@ -40,6 +40,8 @@ export interface StoredRemoteMcpServer extends RemoteMcpSpec {
oauth?: McpOAuthClientConfig;
resultBudget?: number;
runsOn?: McpRunsOn;
+ /** The id of the bot this connection is for, such as one holding that bot's own key: only that bot reaches it. Absent: every bot's. */
+ bot?: string;
/** "Show its apps in chat": stored only as `false`; on by default. */
apps?: false;
}
@@ -67,6 +69,7 @@ export interface RemoteMcpServerListing {
apps: boolean;
oauth?: { clientId: string; scopes: string[]; clientSecretConfigured: boolean };
runsOn?: McpRunsOn;
+ bot?: string;
}
export type McpServerListing = StdioMcpServerListing | RemoteMcpServerListing;
@@ -234,6 +237,7 @@ const remoteEntrySchema = z.object({
resultBudget: resultBudgetSchema.optional(),
runsOn: runsOnSchema.optional(),
apps: z.boolean().optional(),
+ bot: z.string().regex(BOT_ID).optional(),
}).strict();
const remoteMutationSchema = remoteEntrySchema.extend({
@@ -329,7 +333,7 @@ function parseStdio(raw: unknown, mutation: boolean, existing?: StoredMcpServer)
}
function botFor(value: string | undefined, mutation: boolean, existing?: StoredMcpServer): { bot?: string } {
- const bot = value ?? (mutation && existing && !isRemoteMcpServer(existing) ? existing.bot : undefined);
+ const bot = value ?? (mutation ? existing?.bot : undefined);
return bot ? { bot } : {};
}
@@ -361,6 +365,7 @@ function parseRemote(raw: unknown, mutation: boolean, existing?: StoredMcpServer
...(parsed.data.resultBudget !== undefined ? { resultBudget: parsed.data.resultBudget } : {}),
...runsOnFor(parsed.data.runsOn, mutation, existing),
...appsFor(parsed.data.apps, mutation, existing),
+ ...botFor(parsed.data.bot, mutation, existing),
},
};
}
@@ -465,6 +470,7 @@ export function listMcpServers(raw: Record<string, unknown> | undefined): McpSer
oauth: { clientId: server.oauth.clientId, scopes: server.oauth.scopes ?? [], clientSecretConfigured: Boolean(server.oauth.clientSecret) },
} : {}),
...(server.runsOn ? { runsOn: server.runsOn } : {}),
+ ...(server.bot ? { bot: server.bot } : {}),
}];
}
return [{
diff --git a/server/routes/internal-mcp.test.ts b/server/routes/internal-mcp.test.ts
index c3c0eea68a..6d62ab1641 100644
--- a/server/routes/internal-mcp.test.ts
+++ b/server/routes/internal-mcp.test.ts
@@ -988,6 +988,13 @@ describe("internal mcp_server route, below Full access", () => {
expect(personal.signIns).toEqual([{ name: "linear", url: linear.url }]);
});
+ it("sign_in to another bot's own web connection is refused and posts no card", async () => {
+ const { post, signIns, lines } = fixture({ servers: { linear: { ...linear, bot: "scout" } } });
+ expect(await post({ action: "sign_in", name: "linear" })).toEqual({ status: 403, body: { error: "That server belongs to another bot." } });
+ expect(signIns).toEqual([]);
+ expect(lines).toEqual([]);
+ });
+
it("sign_in on a server that is off posts no card and tells the bot to call enable", async () => {
const off = { linear: { ...linear, enabled: false } };
const full = fixture({ servers: off });
diff --git a/server/routes/internal-mcp.ts b/server/routes/internal-mcp.ts
index a48a28525d..715d04d769 100644
--- a/server/routes/internal-mcp.ts
+++ b/server/routes/internal-mcp.ts
@@ -176,11 +176,9 @@ async function act(deps: Ctx, body: unknown, signal: AbortSignal): Promise<Outco
const server = parsed.server;
const owner = mcpOwner(name, server, deps.sender.id, deps.personal());
const may = botMayChange(owner, server);
+ if (owner === "theirs" && !may.ok) return { status: 403, error: may.error };
if (action === "sign_in") return signIn(deps, name, server, owner);
- if (action === "test") {
- if (owner === "theirs" && !may.ok) return { status: 403, error: may.error };
- return test(deps, name, server, signal);
- }
+ if (action === "test") return test(deps, name, server, signal);
// The person's own answer to the card for a server this bot added turns it on, wherever it runs.
const theirAnswer = deps.approved === true && action === "enable" && owner === "elsewhere";