From a062f26559ff289bea3b20f1e9bda66734d88dd4 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Mon, 28 Jul 2025 21:51:47 +0530 Subject: [PATCH] add authenticate button in gui - added a startAuthentication protocol --- core/context/mcp/MCPConnection.ts | 1 - core/context/mcp/MCPOauth.ts | 14 ++---- core/core.ts | 17 ++++++- core/protocol/core.ts | 2 + core/protocol/passThrough.ts | 1 + .../Lump/sections/mcp/MCPSection.tsx | 46 +++++++++++++++++-- 6 files changed, 65 insertions(+), 16 deletions(-) diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index 71412d476..7b1d0b615 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -117,7 +117,6 @@ class MCPConnection { if (this.options.transport.type === "sse") { // currently support oauth for sse transports only - console.log("debug1 extras", this.extras); if (!this.options.transport.requestOptions) { this.options.transport.requestOptions = { headers: {}, diff --git a/core/context/mcp/MCPOauth.ts b/core/context/mcp/MCPOauth.ts index 6e1544533..043117251 100644 --- a/core/context/mcp/MCPOauth.ts +++ b/core/context/mcp/MCPOauth.ts @@ -8,7 +8,7 @@ import { OAuthTokens, OAuthTokensSchema, } from "@modelcontextprotocol/sdk/shared/auth.js"; -import { IDE } from "../.."; +import { IDE, MCPServerStatus, SSEOptions } from "../.."; import http from "http"; import url from "url"; @@ -173,13 +173,12 @@ export async function getOauthToken(mcpServerUrl: string, ide: IDE) { return tokens?.access_token; } -// TODO: this needs to be called from an authenticate button in gui -export async function performAuth(mcpServerUrl: string, ide: IDE) { +export async function performAuth(mcpServer: MCPServerStatus, ide: IDE) { + const mcpServerUrl = (mcpServer.transport as SSEOptions).url; const authProvider = new MCPConnectionOauthProvider(mcpServerUrl, ide); - const result = await auth(authProvider, { + return await auth(authProvider, { serverUrl: mcpServerUrl, }); - console.log("debug1 first result", result); } export async function handleMCPOauthCode( @@ -201,11 +200,8 @@ export async function handleMCPOauthCode( authorizationCode, }); const authProvider = new MCPConnectionOauthProvider(serverUrl, ide); - console.log("debug1 starting auth"); - const result = await auth(authProvider, { + return await auth(authProvider, { serverUrl, authorizationCode, }); - - console.log("debug1 after auth result", result); } diff --git a/core/core.ts b/core/core.ts index 74702f80c..c72503af0 100644 --- a/core/core.ts +++ b/core/core.ts @@ -61,7 +61,7 @@ import { } from "./config/onboarding"; import { createNewWorkspaceBlockFile } from "./config/workspace/workspaceBlocks"; import { MCPManagerSingleton } from "./context/mcp/MCPManagerSingleton"; -import { handleMCPOauthCode } from "./context/mcp/MCPOauth"; +import { handleMCPOauthCode, performAuth } from "./context/mcp/MCPOauth"; import { setMdmLicenseKey } from "./control-plane/mdm/mdm"; import { ApplyAbortManager } from "./edit/applyAbortManager"; import { streamDiffLines } from "./edit/streamDiffLines"; @@ -434,8 +434,21 @@ export class Core { description: prompt.description, }; }); + on("mcp/startAuthentication", async (msg) => { + const status = await performAuth(msg.data, this.ide); + if (status === "AUTHORIZED") { + await MCPManagerSingleton.getInstance().refreshConnection(msg.data.id); + } + }); on("mcp/sendOauthCode", async (msg) => { - await handleMCPOauthCode(msg.data.code, this.ide, this.messenger); + const status = await handleMCPOauthCode( + msg.data.code, + this.ide, + this.messenger, + ); + if (status === "AUTHORIZED") { + // await MCPManagerSingleton.getInstance().refreshConnection(msg.data.id); + } }); // Context providers diff --git a/core/protocol/core.ts b/core/protocol/core.ts index 5f1aa7ad9..517dd34fd 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -26,6 +26,7 @@ import { FileSymbolMap, IdeSettings, LLMFullCompletionOptions, + MCPServerStatus, MessageOption, ModelDescription, PromptLog, @@ -142,6 +143,7 @@ export type ToCoreFromIdeOrWebviewProtocol = { description: string | undefined; }, ]; + "mcp/startAuthentication": [MCPServerStatus, void]; "mcp/sendOauthCode": [ { code: string; diff --git a/core/protocol/passThrough.ts b/core/protocol/passThrough.ts index 482ec37f6..96858f29d 100644 --- a/core/protocol/passThrough.ts +++ b/core/protocol/passThrough.ts @@ -28,6 +28,7 @@ export const WEBVIEW_TO_CORE_PASS_THROUGH: (keyof ToCoreFromWebviewProtocol)[] = "config/updateSelectedModel", "mcp/reloadServer", "mcp/getPrompt", + "mcp/startAuthentication", "context/getContextItems", "context/getSymbolsForFiles", "context/loadSubmenuItems", diff --git a/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx b/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx index 76fb338e9..73b890fd2 100644 --- a/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/mcp/MCPSection.tsx @@ -1,13 +1,16 @@ import { ConfigYaml, parseConfigYaml } from "@continuedev/config-yaml"; import { + ArrowLeftCircleIcon, ArrowPathIcon, CircleStackIcon, CommandLineIcon, + GlobeAltIcon, InformationCircleIcon, + UserCircleIcon, WrenchScrewdriverIcon, } from "@heroicons/react/24/outline"; import { MCPServerStatus } from "core"; -import { useContext, useMemo } from "react"; +import { useContext, useMemo, useState } from "react"; import { useAuth } from "../../../../../context/Auth"; import { IdeMessengerContext } from "../../../../../context/IdeMessenger"; import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks"; @@ -17,6 +20,10 @@ import { ToolTip } from "../../../../gui/Tooltip"; import EditBlockButton from "../../EditBlockButton"; import { ExploreBlocksButton } from "../ExploreBlocksButton"; +function has401Error(errors: string[]) { + return !!errors.find((error) => error.includes("401")); +} + interface MCPServerStatusProps { server: MCPServerStatus; serverFromYaml?: NonNullable[number]; @@ -30,7 +37,17 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { const resourcesTooltipId = `${server.id}-resources`; const errorsTooltipId = `${server.id}-errors`; - async function onRefresh() { + const [mcpAuthStatus, setMcpAuthStatus] = useState< + "unauthenticated" | "authenticated" | "authenticating" + >("unauthenticated"); + + const onAuthenticate = async () => { + setMcpAuthStatus("authenticating"); // TODO: move this updateConfig later with "authenticated" and "authenticating" status + await ideMessenger.request("mcp/startAuthentication", server); + setMcpAuthStatus("authenticated"); + }; + + const onRefresh = async () => { // optimistic config update dispatch( updateConfig({ @@ -48,7 +65,13 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { ideMessenger.post("mcp/reloadServer", { id: server.id, }); - } + }; + + // useEffect(() => { + // if (hasConnectionResetError(server.errors)) { + // onRefresh(); + // } + // }, [server.errors]); return (
- {/* Refresh button */}
+ {has401Error(server.errors) && ( + <> +
+ {mcpAuthStatus === "authenticating" ? ( + + ) : mcpAuthStatus === "authenticated" ? ( + + ) : ( + + )} +
+ + )}