Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(mcp): Settings sign-in opens its browser page
Gyanano7a8c396035fd
diff --git a/src/components/McpServersPanel.sign-in.test.ts b/src/components/McpServersPanel.sign-in.test.tsnew file mode 100644index 0000000000..28a9c14700--- /dev/null+++ b/src/components/McpServersPanel.sign-in.test.ts@@ -0,0 +1,66 @@+// @vitest-environment happy-dom+import { act, createElement } from "react";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, expect, it, vi } from "vitest";++import { setLocale } from "@/lib/i18n";++const api = vi.hoisted(() => vi.fn());+vi.mock("@/state/store", () => ({+ api,+ useStore: () => ({ state: { config: null, bots: [] }, dispatch: vi.fn() }),+}));++import { McpServersPanel } from "./McpServersPanel";++const FLOW = "00000000-0000-4000-8000-000000000000";+const AUTHORIZE = "https://auth.example.test/authorize?client_id=omb";+const lusha = { name: "lusha", type: "http", url: "https://mcp.example.test", headerKeys: [], enabled: true, auth: "needs-sign-in" };++let root: Root;+let host: HTMLElement;+let started!: (value: unknown) => void;+const settle = () => act(async () => { for (let turn = 0; turn < 5; turn += 1) await Promise.resolve(); });+const signIn = () => [...host.querySelectorAll("button")].find((candidate) => candidate.textContent?.trim() === "Sign in")!;++beforeEach(async () => {+ (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;+ setLocale("en");+ api.mockReset();+ api.mockImplementation((path: string, init?: RequestInit) => {+ if (path === "/api/mcp/servers") return Promise.resolve({ servers: [lusha] });+ if (path === "/api/mcp/servers/lusha/sign-in" && init?.method === "POST") return new Promise((resolve) => { started = resolve; });+ return new Promise(() => {});+ });+ host = document.createElement("div");+ document.body.append(host);+ root = createRoot(host);+ await act(async () => { root.render(createElement(McpServersPanel)); });+ await settle();+});+afterEach(() => {+ act(() => root.unmount());+ host.remove();+ vi.unstubAllGlobals();+});++it("reserves the browser tab during the click, then sends it to the sign-in page", async () => {+ const page = { opener: {}, closed: false, location: { replace: vi.fn() }, close: vi.fn() };+ const open = vi.fn(() => page);+ vi.stubGlobal("open", open);+ await act(async () => { signIn().click(); });+ expect(open).toHaveBeenCalledWith("", "_blank");+ await act(async () => { started({ auth: { phase: "waiting", flowId: FLOW, authorizationUrl: AUTHORIZE } }); });+ await settle();+ expect(page.location.replace).toHaveBeenCalledWith(AUTHORIZE);+ expect(host.querySelector("[role=alert]")).toBeNull();+});++it("says the browser blocked the sign-in page instead of waiting in silence", async () => {+ vi.stubGlobal("open", vi.fn(() => null));+ await act(async () => { signIn().click(); });+ await act(async () => { started({ auth: { phase: "waiting", flowId: FLOW, authorizationUrl: AUTHORIZE } }); });+ await settle();+ expect(host.querySelector("[role=alert]")?.textContent).toBe("Your browser blocked the connection page. Allow pop-ups, then try again.");+ expect([...host.querySelectorAll("button")].some((candidate) => candidate.textContent?.trim() === "Open sign-in page")).toBe(true);+});diff --git a/src/components/McpServersPanel.tsx b/src/components/McpServersPanel.tsxindex eb864a6826..33def1841c 100644--- a/src/components/McpServersPanel.tsx+++ b/src/components/McpServersPanel.tsx@@ -19,6 +19,7 @@ import { cn } from "@/lib/cn";import { activeLocale, t } from "@/lib/i18n";import type { LocaleKey } from "@/locales";import { updateMcpServers } from "@/lib/mcp-servers";+import { reserveConnectionPage } from "@/lib/connector-oauth";import { completeMcpSignIn, mcpSignInLink, runMcpSignIn, type McpSignInStatus } from "@/lib/mcp-sign-in";import { listenLiveFrames } from "@/lib/live-events";import { api, useStore } from "@/state/store";@@ -328,6 +329,7 @@ export function McpServersPanel({ embedded = false, hostedCards, hideHosted = fa};async function signIn(server: McpServerListing) {+ const launch = reserveConnectionPage();const controller = new AbortController();signInAbort.current = controller;setSigningIn(server.name);@@ -340,7 +342,10 @@ export function McpServersPanel({ embedded = false, hostedCards, hideHosted = faforgetProbe(server.name);const hosted = hostedOf(server);try {- const result = await runMcpSignIn(server.name, { api, open: openExternalLink, signal: controller.signal, onStarted: (status, complete) => {+ const open = async (url: string) => {+ if (!await launch.open(url) && mounted.current && !controller.signal.aborted) setCallbackError(t("connectors.card.popupBlocked"));+ };+ const result = await runMcpSignIn(server.name, { api, open, signal: controller.signal, onStarted: (status, complete) => {setSignInFlow(status);signInComplete.current = complete;} });@@ -361,6 +366,7 @@ export function McpServersPanel({ embedded = false, hostedCards, hideHosted = fa} catch (cause) {if (mounted.current && !controller.signal.aborted) setProbe((current) => ({ ...current, [server.name]: { ok: false, error: cause instanceof Error ? cause.message : String(cause) } }));} finally {+ launch.cancel();if (signInAbort.current === controller) {signInAbort.current = null;signInComplete.current = null;@@ -475,7 +481,7 @@ export function McpServersPanel({ embedded = false, hostedCards, hideHosted = fa<Loader2 size={13} className="animate-spin" /> {signInFlow?.phase !== "succeeded" ? t("mcp.auth.waiting") : hosted ? t("hostedMcp.loadingTools", { service: hosted.label }) : t("mcp.auth.loadingTools")}</div>{signInFlow?.authorizationUrl && mcpSignInLink(signInFlow.authorizationUrl) && (- <button type="button" onClick={() => void openExternalLink(signInFlow.authorizationUrl!).catch(() => setCallbackError(t("mcp.auth.openFailed")))} className="text-accent hover:underline">+ <button type="button" onClick={() => { setCallbackError(null); void openExternalLink(signInFlow.authorizationUrl!).catch(() => setCallbackError(t("mcp.auth.openFailed"))); }} className="text-accent hover:underline">{t("mcp.auth.openAgain")}</button>)}diff --git a/src/lib/connector-oauth.test.ts b/src/lib/connector-oauth.test.tsindex d54a2ae977..383d3e3d22 100644--- a/src/lib/connector-oauth.test.ts+++ b/src/lib/connector-oauth.test.ts@@ -59,4 +59,18 @@ describe("OAuth tab reservation", () => {expect(await launch.open("https://auth.example.test/flow")).toBe(true);expect(openExternal).toHaveBeenCalledWith("https://auth.example.test/flow");});++ it("opens straight to the page from a remote server's page in the desktop app, which has no openExternal", async () => {+ const open = vi.fn(() => null);+ vi.stubGlobal("window", { ogb: { platform: "darwin" }, open });+ const launch = reserveConnectionPage();+ expect(open).not.toHaveBeenCalled();+ expect(await launch.open("https://auth.example.test/flow")).toBe(true);+ expect(open).toHaveBeenCalledWith("https://auth.example.test/flow", "_blank", "noopener,noreferrer");+ });++ it("says the page did not open when the desktop app refuses it", async () => {+ vi.stubGlobal("window", { ogb: { openExternal: vi.fn(async () => { throw new Error("only available while using the local server"); }) }, open: vi.fn() });+ expect(await reserveConnectionPage().open("https://auth.example.test/flow")).toBe(false);+ });});diff --git a/src/lib/connector-oauth.ts b/src/lib/connector-oauth.tsindex 6e22c6c7af..0664732233 100644--- a/src/lib/connector-oauth.ts+++ b/src/lib/connector-oauth.ts@@ -1,3 +1,5 @@+import { openExternalLink } from "./app-links";+export interface PendingAuthorization {url: string;createdAt: number;@@ -12,11 +14,11 @@ export function reusableConnectionUrl(page: PendingAuthorization | null | undefi/** Reserve the tab during the click, before an authorization request consumes the browser's user activation. */export function reserveConnectionPage() {- const openExternal = window.ogb?.openExternal;+ const appOpensLinks = Boolean(window.ogb);let page: Window | null = null;let navigated = false;let cancelled = false;- if (!openExternal) {+ if (!appOpensLinks) {try {page = window.open("", "_blank");if (page) page.opener = null;@@ -28,8 +30,12 @@ export function reserveConnectionPage() {return {async open(url: string): Promise<boolean> {if (cancelled) return false;- if (openExternal) {- await openExternal(url);+ if (appOpensLinks) {+ try {+ await openExternalLink(url);+ } catch {+ return false;+ }return !cancelled;}if (!page || page.closed) return false;