Skip to content

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

fix(mcp): avoid personal lookups for known team edits

Sebastian B6fdb435e4fad
diff --git a/server/index.test.ts b/server/index.test.ts
index 7b0399c871..3fdc15389d 100644
--- a/server/index.test.ts
+++ b/server/index.test.ts
@@ -8505,7 +8505,7 @@ describe("harness HTTP API", () => {
try {
const created = await api("POST", "/api/mcp/servers", { name: "docs", url: fake.url, headers: { Authorization: secret } });
expect(created.status).toBe(201);
- expect(created.body.servers).toEqual([{ name: "docs", type: "http", url: fake.url, headerKeys: ["Authorization"], enabled: false, apps: true }]);
+ expect(created.body.servers).toEqual([{ name: "docs", type: "http", url: fake.url, headerKeys: ["Authorization"], enabled: false, apps: true, scope: "team" }]);
expect(JSON.stringify(created.body)).not.toContain(secret);
const tested = await api("POST", "/api/mcp/servers/docs/test");
@@ -8552,7 +8552,7 @@ describe("harness HTTP API", () => {
const first = await api("POST", "/api/mcp/servers/hf/test");
expect(first.body).toEqual({ ok: false, auth: "required", error: "This server needs you to sign in." });
expect((await api("GET", "/api/mcp/servers")).body.servers).toEqual([
- { name: "hf", type: "http", url: fake.url, headerKeys: [], enabled: false, apps: true, auth: "needs-sign-in" },
+ { name: "hf", type: "http", url: fake.url, headerKeys: [], enabled: false, apps: true, auth: "needs-sign-in", scope: "team" },
]);
const started = await api("POST", "/api/mcp/servers/hf/sign-in");
@@ -8617,7 +8617,7 @@ describe("harness HTTP API", () => {
const created = await api("POST", "/api/mcp/servers", { name: "corp", url, oauth: { clientId: "corp-app", clientSecret: secret, scopes: ["mcp", "offline_access"] } });
expect(created.status).toBe(201);
expect(created.body.servers).toEqual([{
- name: "corp", type: "http", url, headerKeys: [], enabled: false, apps: true,
+ name: "corp", type: "http", url, headerKeys: [], enabled: false, apps: true, scope: "team",
oauth: { clientId: "corp-app", scopes: ["mcp", "offline_access"], clientSecretConfigured: true, redirectUri: expect.stringMatching(/^http:\/\/127\.0\.0\.1:\d+\/mcp-oauth\/callback$/) },
}]);
diff --git a/server/routes/mcp-servers.test.ts b/server/routes/mcp-servers.test.ts
index 987a94c079..3eb5c5bffd 100644
--- a/server/routes/mcp-servers.test.ts
+++ b/server/routes/mcp-servers.test.ts
@@ -7,6 +7,7 @@ import { DATA_DIR, type AppConfig } from "../config.ts";
import { json, readBody } from "../harness/http.ts";
import type { TeamConnectionsHost } from "../host/team-connections-host.ts";
import { McpOAuthManager } from "../mcp-oauth.ts";
+import { listMcpServers } from "../mcp-registry.ts";
import { createMcpServerRoutes } from "./mcp-servers.ts";
import { dispatchRoutes } from "./table.ts";
@@ -28,7 +29,7 @@ async function serve() {
cloudHome: null,
fallbackPublicUrl: null,
mcpConfigBusy: { get: () => busy, set: (value) => { busy = value; } },
- mcpServerResponse: () => ({ servers: [] }),
+ mcpServerResponse: () => ({ servers: listMcpServers(cfg.mcpServers) }),
mcpPolicyRefusal: () => undefined,
persistMcpServers: (next) => { cfg.mcpServers = next; },
savedCustomDomain: () => null,
@@ -46,9 +47,17 @@ async function serve() {
const post = (path: string, body: unknown) => fetch(`${base}${path}`, {
method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body),
});
- return { cfg, post };
+ return { cfg, post, get: () => fetch(`${base}/api/mcp/servers`) };
}
+it.each([["private", "person"], ["public", "team"]])("lists the scope of %s in reads and mutation responses", async (name, scope) => {
+ const { post, get } = await serve();
+ const response = await post("/api/mcp/servers", { name, url: "https://mail.example.com/mcp", headers: { Authorization: "" } });
+ expect(response.status).toBe(201);
+ expect(await response.json()).toMatchObject({ servers: [{ name, scope }] });
+ expect(await (await get()).json()).toMatchObject({ servers: [{ name, scope }] });
+});
+
it.each(["create", "import"])("rejects personal header values on %s without saving any server", async (action) => {
const { cfg, post } = await serve();
const entry = { url: "https://mail.example.com/mcp", headers: { Authorization: "Bearer private-secret" } };
diff --git a/server/routes/mcp-servers.ts b/server/routes/mcp-servers.ts
index 07973ad6df..e46ac8f417 100644
--- a/server/routes/mcp-servers.ts
+++ b/server/routes/mcp-servers.ts
@@ -35,15 +35,17 @@ export interface McpServerRouteDeps {
const runsElsewhere = (runsOn: McpRunsOn) => `This server runs on ${runsOn.label}, so it isn't started here.`;
export function createMcpServerRoutes(deps: McpServerRouteDeps): RouteHandler {
- const withHasApps = <T,>(body: T): T => {
+ const withServerDetails = <T,>(body: T): T => {
const hasApps = deps.appCatalog?.hasApps;
const servers = (body as { servers?: unknown }).servers;
- if (!hasApps || !Array.isArray(servers)) return body;
+ if (!Array.isArray(servers)) return body;
+ const personal = deps.teamConnections.personMcp();
return {
...body,
servers: servers.map((server: { name?: unknown }) => {
- const known = typeof server?.name === "string" ? hasApps(server.name) : undefined;
- return known === undefined ? server : { ...server, hasApps: known };
+ const known = typeof server?.name === "string" ? hasApps?.(server.name) : undefined;
+ return { ...server, scope: typeof server.name === "string" && personal.includes(server.name) ? "person" : "team",
+ ...(known === undefined ? {} : { hasApps: known }) };
}),
};
};
@@ -59,7 +61,7 @@ export function createMcpServerRoutes(deps: McpServerRouteDeps): RouteHandler {
let mcpProbesInFlight = 0;
return async ({ req, res, path, method, auth, json, readBody }) => {
if (method === "GET" && path === "/api/mcp/servers") {
- return json(res, 200, withHasApps(mcpServerResponse()));
+ return json(res, 200, withServerDetails(mcpServerResponse()));
}
const mcpTest = /^\/api\/mcp\/servers\/([a-z][a-z0-9_-]{0,31})\/test$/.exec(path);
@@ -115,7 +117,7 @@ export function createMcpServerRoutes(deps: McpServerRouteDeps): RouteHandler {
const url = parsed.server.url;
if (action === "sign-out" && method === "POST" && !flowId) {
await mcpOAuth.signOut(name!, url);
- return json(res, 200, withHasApps(mcpServerResponse()));
+ return json(res, 200, withServerDetails(mcpServerResponse()));
}
const owner = auth.kind === "session" ? auth.session.id : "loopback";
res.setHeader("Cache-Control", "no-store");
@@ -181,7 +183,7 @@ export function createMcpServerRoutes(deps: McpServerRouteDeps): RouteHandler {
if (refusal) return json(res, 403, { error: refusal, code: "managed_policy" });
persistMcpServers({ ...current, [name]: parsed.server });
appCatalog?.serverChanged(name);
- return json(res, 201, withHasApps(mcpServerResponse()));
+ return json(res, 201, withServerDetails(mcpServerResponse()));
} finally {
mcpConfigBusy.set(false);
}
@@ -217,7 +219,7 @@ export function createMcpServerRoutes(deps: McpServerRouteDeps): RouteHandler {
const refused = names.filter(name => mcpPolicyRefusal(name, parsed.servers[name] as object));
if (refused.length) return json(res, 403, { error: `${mcpPolicyRefusal(refused[0]!, parsed.servers[refused[0]!] as object)} Not approved: ${refused.join(", ")}.`, code: "managed_policy" });
persistMcpServers({ ...current, ...parsed.servers });
- return json(res, 201, { ...withHasApps(mcpServerResponse()), added: names });
+ return json(res, 201, { ...withServerDetails(mcpServerResponse()), added: names });
} finally {
mcpConfigBusy.set(false);
}
@@ -241,7 +243,7 @@ export function createMcpServerRoutes(deps: McpServerRouteDeps): RouteHandler {
mcpOAuth.forget(name);
forgetPeople?.(name);
appCatalog?.forgetServer(name);
- return json(res, 200, withHasApps(mcpServerResponse()));
+ return json(res, 200, withServerDetails(mcpServerResponse()));
}
const existing = parseStoredMcpServer(name, current[name]);
@@ -255,7 +257,7 @@ export function createMcpServerRoutes(deps: McpServerRouteDeps): RouteHandler {
const { apps: _apps, ...rest } = existing.server;
persistMcpServers({ ...current, [name]: key === "enabled" ? { ...existing.server, enabled: body.enabled } : { ...rest, ...(body.apps ? {} : { apps: false }) } });
if (body[key] === true) appCatalog?.serverChanged(name);
- return json(res, 200, withHasApps(mcpServerResponse()));
+ return json(res, 200, withServerDetails(mcpServerResponse()));
}
const parsed = parseMcpServerMutation(name, body, existing.server);
@@ -275,7 +277,7 @@ export function createMcpServerRoutes(deps: McpServerRouteDeps): RouteHandler {
? JSON.stringify([server.oauth.clientId, server.oauth.scopes ?? []]) : null;
if (before !== after || app(existing.server) !== app(parsed.server)
|| (after && mcpOAuth.authState(name, after) === "needs-sign-in")) mcpOAuth.forget(name);
- return json(res, 200, withHasApps(mcpServerResponse()));
+ return json(res, 200, withServerDetails(mcpServerResponse()));
} finally {
mcpConfigBusy.set(false);
}
diff --git a/src/components/McpServersPanel.add.test.ts b/src/components/McpServersPanel.add.test.ts
index ca6ab5d6c0..f8983bf917 100644
--- a/src/components/McpServersPanel.add.test.ts
+++ b/src/components/McpServersPanel.add.test.ts
@@ -416,9 +416,43 @@ describe("Adding an MCP server on a Team Cloud", () => {
expect(calls("PUT", "/api/team/my-connections/mcp/mail/key")).toEqual([]);
});
- it.each(["/api/team/my-connections", "/api/team/my-connections/mcp/mail/key"])(
- "does not share a replacement key or test the connection when %s fails", async (failedPath) => {
- fixture.servers = [{ name: "mail", type: "http", url: "https://mail.example.com/mcp", headerKeys: ["Authorization"], enabled: true }];
+ it.each([
+ ["edit", "403 Forbidden"], ["fix", "403 Forbidden"],
+ ["edit", "Network request failed"], ["fix", "Network request failed"],
+ ])("saves a known team key on %s without a personal lookup that would fail with %s", async (action, failure) => {
+ fixture.servers = [{ name: "mail", type: "http", url: "https://mail.example.com/mcp", headerKeys: ["Authorization"], enabled: true, scope: "team" }];
+ fixture.sharedHeaders.mail = { Authorization: "Bearer team-key-12345" };
+ await act(async () => host.querySelector<HTMLButtonElement>("[aria-label='Refresh MCP servers']")!.click());
+ await settle();
+ const previous = fixture.api.getMockImplementation()!;
+ fixture.api.mockImplementation(async (path: string, init?: RequestInit) => {
+ if (path === "/api/team/my-connections") throw new Error(failure);
+ return previous(path, init);
+ });
+ if (action === "fix") {
+ fixture.test = { ok: false, reason: "keys", status: 401 };
+ await click("Test");
+ await click("Fix the keys");
+ } else {
+ await act(async () => host.querySelector<HTMLButtonElement>("[aria-label='Edit mail']")!.click());
+ }
+ await type("input[aria-label='Authorization']", "Bearer replacement-team-key");
+ fixture.test = { ok: true, tools: [{ name: "read_inbox" }] };
+ await click("Save");
+
+ expect(fixture.sharedHeaders.mail).toEqual({ Authorization: "Bearer replacement-team-key" });
+ expect(calls("GET", "/api/team/my-connections")).toEqual([]);
+ expect(calls("PUT", "/api/team/my-connections/mcp/mail/key")).toEqual([]);
+ expect(host.querySelector("[data-mcp-editor]")).toBeNull();
+ expect(host.textContent).toContain("Connected: 1 tool");
+ });
+
+ it.each([
+ [undefined, "/api/team/my-connections"], ["person", "/api/team/my-connections"],
+ [undefined, "/api/team/my-connections/mcp/mail/key"], ["person", "/api/team/my-connections/mcp/mail/key"],
+ ])(
+ "does not share a replacement key or test the connection with scope %s when %s fails", async (scope, failedPath) => {
+ fixture.servers = [{ name: "mail", type: "http", url: "https://mail.example.com/mcp", headerKeys: ["Authorization"], enabled: true, scope }];
fixture.sharedHeaders.mail = { Authorization: "" };
fixture.personalMcp = ["mail"];
fixture.personalKeys.mail = "Bearer kept-key-12345";
diff --git a/src/components/McpServersPanel.tsx b/src/components/McpServersPanel.tsx
index 46d45b6b7e..35a5c84e6e 100644
--- a/src/components/McpServersPanel.tsx
+++ b/src/components/McpServersPanel.tsx
@@ -219,7 +219,7 @@ export function McpServersPanel({ embedded = false, whopCard = false, hideWhop =
try {
let body = item.body;
let ownKey: string | undefined;
- if (store.config?.team && typeof body.url === "string" && !body.oauth) {
+ if (store.config?.team && existing.scope !== "team" && typeof body.url === "string" && !body.oauth) {
const connections = await api("/api/team/my-connections");
if (connections.mcp.some((connection: { name: string }) => connection.name === item.name)) {
const headers = (body.headers ?? {}) as Record<string, string | true>;
diff --git a/src/components/mcp-draft.ts b/src/components/mcp-draft.ts
index 23b05160c3..dcb188192a 100644
--- a/src/components/mcp-draft.ts
+++ b/src/components/mcp-draft.ts
@@ -25,7 +25,7 @@ export interface McpOAuthClientListing {
clientSecretConfigured: boolean;
redirectUri: string;
}
-export type McpServerListing = (StdioMcpListing | RemoteMcpListing) & { managedBy?: string; runsOn?: { environmentId: string; label: string }; apps?: boolean };
+export type McpServerListing = (StdioMcpListing | RemoteMcpListing) & { scope?: "team" | "person"; managedBy?: string; runsOn?: { environmentId: string; label: string }; apps?: boolean };
export function isRemoteMcpListing(server: McpServerListing): server is RemoteMcpListing {
return "url" in server;