Skip to content

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

Lift the per-bot MCP switch rule into mcpSwitch

aivsomkar6a0553789e4d
diff --git a/src/components/bot-settings/AccessSection.tsx b/src/components/bot-settings/AccessSection.tsx
index 3edef77d77..fd30ca64d4 100644
--- a/src/components/bot-settings/AccessSection.tsx
+++ b/src/components/bot-settings/AccessSection.tsx
@@ -7,7 +7,7 @@ import { useBotEditor } from "./BotEditorContext";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import type { LocaleKey } from "@/locales";
-import { mcpServersForBot, mcpServersOfBot, useMcpServers } from "@/lib/mcp-servers";
+import { mcpServersForBot, mcpServersOfBot, mcpSwitch, useMcpServers } from "@/lib/mcp-servers";
import { placeFacts, placeViewFor, usePlaceSeat } from "@/lib/place-view";
import { shortPath } from "@/lib/short-path";
import { isOwnThreadFolder } from "../../../shared/thread-folder";
@@ -133,9 +133,7 @@ export function McpServersCard({ bot, patch }: { bot: Bot; patch: (patch: { mcpS
const toggle = (name: string) => {
if (!servers || bot.busy || error) return;
- const current = usesAll ? servers.filter((server) => server.enabled).map((server) => server.name) : [...(bot.mcpServers ?? [])];
- const next = current.includes(name) ? current.filter((item) => item !== name) : [...current, name];
- patch({ mcpServers: next });
+ patch({ mcpServers: mcpSwitch(servers, bot.mcpServers, name) });
};
const openPlugins = () => {
dispatch({ type: "toggleSettings", open: false });
diff --git a/src/lib/mcp-servers.test.ts b/src/lib/mcp-servers.test.ts
index 7f375a55af..8ae4a7e775 100644
--- a/src/lib/mcp-servers.test.ts
+++ b/src/lib/mcp-servers.test.ts
@@ -8,7 +8,7 @@ vi.mock("react", async (importOriginal) => ({
useSyncExternalStore: (_subscribe: unknown, snapshot: () => unknown) => snapshot(),
}));
-import { mcpServersForBot, mcpServersOfBot } from "./mcp-servers";
+import { mcpServersForBot, mcpServersOfBot, mcpSwitch } from "./mcp-servers";
describe("mcpServersForBot", () => {
const all = [
@@ -92,3 +92,21 @@ describe("shared MCP inventory", () => {
expect(useMcpServers()).toMatchObject({ servers: [changed], error: false });
});
});
+
+describe("mcpSwitch", () => {
+ const all = [
+ { name: "notes", enabled: true },
+ { name: "linear", enabled: true },
+ { name: "off", enabled: false },
+ ];
+
+ it("starts from every enabled server when the bot has no list", () => {
+ expect(mcpSwitch(all, null, "notes")).toEqual(["linear"]);
+ expect(mcpSwitch(all, undefined, "linear")).toEqual(["notes"]);
+ });
+
+ it("adds a name the explicit list leaves out and removes one it holds", () => {
+ expect(mcpSwitch(all, ["notes"], "linear")).toEqual(["notes", "linear"]);
+ expect(mcpSwitch(all, ["notes", "linear"], "notes")).toEqual(["linear"]);
+ });
+});
diff --git a/src/lib/mcp-servers.ts b/src/lib/mcp-servers.ts
index 6f14171e0d..f8b801e133 100644
--- a/src/lib/mcp-servers.ts
+++ b/src/lib/mcp-servers.ts
@@ -68,3 +68,9 @@ export function mcpServersForBot(all: McpServerSummary[], own: string[] | null |
if (own == null) return enabled;
return enabled.filter((server) => own.includes(server.name));
}
+
+/** The explicit list after flipping `name`. An absent list means every enabled server, so the first flip starts from those. */
+export function mcpSwitch(servers: McpServerSummary[], own: string[] | null | undefined, name: string): string[] {
+ const current = own == null ? servers.filter((server) => server.enabled).map((server) => server.name) : own;
+ return current.includes(name) ? current.filter((item) => item !== name) : [...current, name];
+}