diff --git a/ui/apps/chat/README.md b/ui/apps/chat/README.md index 05a04c500589..2bf24b34849e 100644 --- a/ui/apps/chat/README.md +++ b/ui/apps/chat/README.md @@ -174,6 +174,8 @@ MCP is the first extension. All of its code is in `src/lib/extensions/mcp/`: | `services/` (`MCPService`) | MCP protocol: transports, connect, list/execute tools, prompts, resources | | `components/` | Server cards and forms, resource browser, dialogs, the prompt picker and the chat renderers | +A server that answers 401 and publishes an OAuth authorization server shows **Sign in** on its card: the browser registers the app with it (dynamic client registration, or the client ID of an app the user registered there by hand), opens the authorization page in a popup, and keeps the tokens per server in localStorage (`oauth/`), apart from the settings, so a settings export never carries them. An Authorization header set on the server wins over a sign-in. With **Use llama-server proxy** on, the discovery, registration and token requests go through the proxy too. + MCP's shared data stays in the core: types, enums, constants, `utils/mcp.ts`, settings keys and the stored message formats. MCP servers come two ways. A remote server (Streamable HTTP, SSE or WebSocket) is one the browser connects to by URL, added on the MCP servers page. A local server that runs as a command (stdio) is one llama-server starts from `--mcp-servers-config`, since a page cannot start a process: llama-server lists its tools in `/tools` with the type `mcp` and the server's name in `mcp_server`, and runs them on `POST /tools`. The MCP extension takes those tools over from the core's server tools (`serverToolType` on its tool provider, see `llama-server.ts`), so both kinds show up the same way, one group per server, on the MCP servers page and in the tools menu. With the extension off, the core lists them as server tools again. diff --git a/ui/apps/chat/src/lib/extensions/mcp/components/McpServerCard/McpServerCard.svelte b/ui/apps/chat/src/lib/extensions/mcp/components/McpServerCard/McpServerCard.svelte index 4d899d977eb1..35420a055038 100644 --- a/ui/apps/chat/src/lib/extensions/mcp/components/McpServerCard/McpServerCard.svelte +++ b/ui/apps/chat/src/lib/extensions/mcp/components/McpServerCard/McpServerCard.svelte @@ -1,4 +1,5 @@ + +{#if signingIn} +
+

+ + + Finish signing in in the window that opened. +

+ + +
+{:else if needsClientId} +
+

+ {issuerHost ?? 'This server'} does not register apps by itself. Register an OAuth app there with + this redirect URL, then enter its client ID. +

+ +
+ {redirectUrl} + + +
+ +
+ + + +
+ +
+ + + +
+ + +
+{:else if needsSignIn} +
+

This server needs you to sign in.

+ + +
+{:else if signedIn && connected} +
+

+ + + {issuerHost ? `Signed in with ${issuerHost}` : 'Signed in'} +

+ + +
+{/if} + +{#if error && !signingIn} +

{error}

+{/if} diff --git a/ui/apps/chat/src/lib/extensions/mcp/index.ts b/ui/apps/chat/src/lib/extensions/mcp/index.ts index f820c1243128..2e29720d6a18 100644 --- a/ui/apps/chat/src/lib/extensions/mcp/index.ts +++ b/ui/apps/chat/src/lib/extensions/mcp/index.ts @@ -13,6 +13,7 @@ import { contributeComposer } from './composer'; import { contributeNavigation } from './navigation'; import { contributePages } from './pages'; import { contributeRenderers } from './renderers'; +import { contributeSignIn } from './sign-in'; import { contributeStartup } from './startup.svelte'; import { contributeTools } from './tools'; @@ -20,6 +21,7 @@ const mcp: Extension = { activate(ctx) { contributeComposer(ctx); contributeNavigation(ctx); + contributeSignIn(ctx); contributePages(ctx); contributeRenderers(ctx); contributeStartup(ctx); diff --git a/ui/apps/chat/src/lib/extensions/mcp/oauth/fetch.ts b/ui/apps/chat/src/lib/extensions/mcp/oauth/fetch.ts new file mode 100644 index 000000000000..9b3bb9d0dddd --- /dev/null +++ b/ui/apps/chat/src/lib/extensions/mcp/oauth/fetch.ts @@ -0,0 +1,29 @@ +/** + * The fetch the OAuth steps a sign-in runs itself use: the callback's token + * exchange, sign-out's revocation. Through the CORS proxy, like the server's + * own traffic, when the server is reached through it. + */ + +import { MCPService } from '../services/mcp.service'; +import type { FetchLike } from '@modelcontextprotocol/client'; +import { CORS_PROXY } from '#lib/constants/index.ts'; +import { getAuthHeaders } from '#lib/utils/index.ts'; + +export function mcpOAuthFetch(useProxy: boolean): FetchLike { + if (!useProxy) return (url, init) => fetch(url, init); + + return (url, init) => { + const proxied = MCPService.proxiedRequestUrl(url); + + if (!proxied) return fetch(url, init); + + // the proxy forwards only the headers it is told to, under its prefix; llama-server's own key stays as it is + const headers = new Headers(getAuthHeaders()); + + for (const [key, value] of new Headers(init?.headers).entries()) { + headers.set(`${CORS_PROXY.HEADER_PREFIX}${key}`, value); + } + + return fetch(proxied, { ...init, headers }); + }; +} diff --git a/ui/apps/chat/src/lib/extensions/mcp/oauth/flow.svelte.ts b/ui/apps/chat/src/lib/extensions/mcp/oauth/flow.svelte.ts new file mode 100644 index 000000000000..4a3347fe0e5a --- /dev/null +++ b/ui/apps/chat/src/lib/extensions/mcp/oauth/flow.svelte.ts @@ -0,0 +1,405 @@ +/** + * Signing in to an MCP server with the browser: the sign-in the user starts, + * the callback that finishes it, and sign-out. + * + * A sign-in opens the authorization page in a popup, or in this tab when the + * popup is blocked. The authorization server sends the browser back to the + * app's root with the code; that page load (the popup, or this tab) exchanges + * it for tokens. The UI is served cross-origin isolated (COOP same-origin), so + * the popup cannot reach the tab that opened it: the result goes out on a + * BroadcastChannel to every tab of the app. + */ + +import { mcpOAuthFetch } from './fetch'; +import { MCP_OAUTH_CALLBACK_PARAM, McpOAuthProvider, mcpOAuthRedirectUrl } from './provider'; +import { McpOAuthFlowMode, mcpOAuthStorage } from './storage.svelte'; +import { auth, type FetchLike, RegistrationRejectedError } from '@modelcontextprotocol/client'; +import { SvelteMap, SvelteSet } from 'svelte/reactivity'; + +const CHANNEL_NAME = 'llama-mcp-oauth'; +/** The callback's own query parameters, cleared from the address once read. */ +const CALLBACK_PARAMS = [ + MCP_OAUTH_CALLBACK_PARAM, + 'code', + 'state', + 'iss', + 'error', + 'error_description', + 'error_uri' +]; + +/** The server a sign-in is for. */ +export interface McpOAuthServer { + id: string; + url: string; + useProxy?: boolean; +} + +/** How a sign-in ended, as every tab hears it. */ +export interface McpOAuthResult { + error?: string; + ok: boolean; + serverId: string; +} + +/** What a page load that came back from an authorization page did. */ +export interface McpOAuthCallbackOutcome { + /** The window was a sign-in popup: it is done and can close. */ + closeWindow: boolean; + result: McpOAuthResult | null; +} + +/** A plain sentence for an OAuth error code the authorization page sent back. */ +function describeAuthorizationError(code: string): string { + if (code === 'access_denied') return 'The sign-in was cancelled or refused'; + + return `The authorization server refused the sign-in (${code.replace(/[^\w.-]/g, '').slice(0, 64)})`; +} + +function errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +class McpOAuthFlow { + private channel: BroadcastChannel | null = null; + private readonly errors = new SvelteMap(); + private readonly listeners = new Set<(result: McpOAuthResult) => void>(); + private readonly manualClientRequired = new SvelteSet(); + private readonly signingIn = new SvelteSet(); + private readonly signInRequired = new SvelteSet(); + + /** Stop waiting for a sign-in the user gave up on, e.g. by closing the popup. */ + cancelSignIn(serverId: string): void { + this.signingIn.delete(serverId); + } + + clearSignInRequired(serverId: string): void { + this.signInRequired.delete(serverId); + } + + /** + * Finish a sign-in when this page load is the authorization server sending + * the browser back. Reads and clears the callback's parameters; returns + * null for any other page load. + */ + async completeFromLocation( + location: URL = new URL(window.location.href) + ): Promise { + if (!location.searchParams.has(MCP_OAUTH_CALLBACK_PARAM)) return null; + + const params = new URLSearchParams(location.search); + const cleaned = new URL(location.href); + + for (const name of CALLBACK_PARAMS) cleaned.searchParams.delete(name); + + // a reload must not run the exchange again + window.history.replaceState(window.history.state, '', cleaned.href); + + const state = params.get('state'); + const found = state ? mcpOAuthStorage.findByState(state) : undefined; + const pending = found?.record.pending; + + if (!found || !pending) { + console.warn( + '[MCP OAuth] A sign-in came back that this browser did not start, or already finished' + ); + + return { closeWindow: false, result: null }; + } + + const { record, serverId } = found; + const closeWindow = pending.mode === McpOAuthFlowMode.POPUP; + const failure = params.get('error'); + + let result: McpOAuthResult; + + if (failure) { + mcpOAuthStorage.update(serverId, record.serverUrl, record.redirectUrl, (current) => ({ + ...current, + pending: undefined + })); + // only the error code: its description comes from the address bar, anyone could have written it + result = { error: describeAuthorizationError(failure), ok: false, serverId }; + } else { + result = await this.exchangeCode(serverId, record.serverUrl, params, pending.useProxy, { + mode: pending.mode, + returnUrl: pending.returnUrl + }); + } + + this.publish(result); + + // a sign-in that took over this tab goes back to where it started: a navigation, not a + // history entry rewrite, so the hash router follows it to the route + if (!closeWindow) window.location.replace(pending.returnUrl); + + return { closeWindow, result }; + } + + /** The error the last sign-in of a server ended with, if it failed. */ + errorFor(serverId: string): string | null { + return this.errors.get(serverId) ?? null; + } + + /** Whether the browser holds tokens for the server at this URL. */ + isSignedIn(server: McpOAuthServer): boolean { + return mcpOAuthStorage.get(server.id, server.url, mcpOAuthRedirectUrl())?.tokens !== undefined; + } + + /** Whether a sign-in of the server is under way, waiting for the authorization page. */ + isSigningIn(serverId: string): boolean { + return this.signingIn.has(serverId); + } + + /** The host of the server's authorization server, once discovery found it. */ + issuerHost(server: McpOAuthServer): string | null { + const discovered = mcpOAuthStorage.get(server.id, server.url, mcpOAuthRedirectUrl()) + ?.discoveryState?.authorizationServerUrl; + + try { + return discovered ? new URL(discovered).host : null; + } catch { + return null; + } + } + + markSignInRequired(serverId: string): void { + this.signInRequired.add(serverId); + } + + /** + * Whether the server's authorization server registers no apps on its own: + * signing in needs the client ID of an app the user registered there. + */ + needsClientId(serverId: string): boolean { + return this.manualClientRequired.has(serverId); + } + + /** Whether the server's last connection stopped at a sign-in. */ + needsSignIn(serverId: string): boolean { + return this.signInRequired.has(serverId); + } + + /** + * Start a sign-in. Call it from the click that asks for it, with the popup + * opened in that same click (browsers block a popup opened later); without + * one, this tab goes to the authorization page itself. + */ + async signIn(server: McpOAuthServer, popup: Window | null): Promise { + const useProxy = server.useProxy ?? false; + const mode = popup ? McpOAuthFlowMode.POPUP : McpOAuthFlowMode.REDIRECT; + const provider = new McpOAuthProvider({ + interactive: { + mode, + navigate: (authorizationUrl) => { + if (popup) popup.location.href = authorizationUrl.href; + else window.location.assign(authorizationUrl.href); + }, + returnUrl: window.location.href + }, + serverId: server.id, + serverUrl: server.url, + useProxy + }); + + this.errors.delete(server.id); + this.signingIn.add(server.id); + + try { + const outcome = await auth(provider, { + fetchFn: mcpOAuthFetch(useProxy), + serverUrl: server.url + }); + + // tokens it could refresh: no page to visit + if (outcome === 'AUTHORIZED') { + popup?.close(); + this.publish({ ok: true, serverId: server.id }); + } + } catch (error) { + popup?.close(); + + // no registration endpoint, or one that turned the app down: the user's own app it is + if ( + error instanceof RegistrationRejectedError || + /dynamic client registration/i.test(errorMessage(error)) + ) { + this.manualClientRequired.add(server.id); + this.publish({ + error: + 'This server does not register apps by itself: enter the client ID of an app you registered with it', + ok: false, + serverId: server.id + }); + + return; + } + + this.publish({ error: errorMessage(error), ok: false, serverId: server.id }); + } + } + + /** Forget the server's tokens, revoking them where the authorization server allows it. */ + async signOut(server: McpOAuthServer): Promise { + const redirectUrl = mcpOAuthRedirectUrl(); + const record = mcpOAuthStorage.get(server.id, server.url, redirectUrl); + + if (!record?.tokens) return; + + // RFC 7009, which OpenID provider metadata does not list: read it from the raw metadata + const metadata = record.discoveryState?.authorizationServerMetadata as + | { revocation_endpoint?: unknown } + | undefined; + const endpoint = + typeof metadata?.revocation_endpoint === 'string' ? metadata.revocation_endpoint : undefined; + + if (endpoint && record.clientInformation) { + await this.revoke( + endpoint, + record.tokens.refresh_token ?? record.tokens.access_token, + record.tokens.refresh_token ? 'refresh_token' : 'access_token', + record.clientInformation, + mcpOAuthFetch(server.useProxy ?? false) + ); + } + + // the client registration stays: the next sign-in reuses it + mcpOAuthStorage.update(server.id, server.url, redirectUrl, (current) => ({ + ...current, + pending: undefined, + tokens: undefined + })); + } + + /** + * Hear how sign-ins end, in this tab and in the others (a popup finishes + * its sign-in in a tab of its own). Returns the function that stops. + */ + subscribe(listener: (result: McpOAuthResult) => void): () => void { + this.listeners.add(listener); + this.openChannel(); + + return () => { + this.listeners.delete(listener); + + if (this.listeners.size === 0) { + this.channel?.close(); + this.channel = null; + } + }; + } + + /** Sign in from now on as an app the user registered with the authorization server. */ + useClient(server: McpOAuthServer, client: { clientId: string; clientSecret?: string }): void { + const clientSecret = client.clientSecret?.trim(); + + mcpOAuthStorage.update(server.id, server.url, mcpOAuthRedirectUrl(), (record) => ({ + ...record, + clientInformation: { + client_id: client.clientId.trim(), + ...(clientSecret ? { client_secret: clientSecret } : {}) + } + })); + this.manualClientRequired.delete(server.id); + this.errors.delete(server.id); + } + + private async exchangeCode( + serverId: string, + serverUrl: string, + params: URLSearchParams, + useProxy: boolean, + interactive: { mode: McpOAuthFlowMode; returnUrl: string } + ): Promise { + const code = params.get('code'); + + if (!code) return { error: 'The authorization server sent no code back', ok: false, serverId }; + + const provider = new McpOAuthProvider({ + interactive: { ...interactive, navigate: () => {} }, + serverId, + serverUrl, + useProxy + }); + + try { + await auth(provider, { + authorizationCode: code, + fetchFn: mcpOAuthFetch(useProxy), + iss: params.get('iss') ?? undefined, + serverUrl + }); + + return { ok: true, serverId }; + } catch (error) { + return { error: errorMessage(error), ok: false, serverId }; + } + } + + private openChannel(): void { + if (this.channel || typeof BroadcastChannel === 'undefined') return; + + this.channel = new BroadcastChannel(CHANNEL_NAME); + this.channel.onmessage = (event: MessageEvent) => { + // another tab wrote the tokens: read them again + mcpOAuthStorage.invalidate(); + this.settle(event.data); + }; + } + + private publish(result: McpOAuthResult): void { + this.settle(result); + + // a channel of its own: the one listening here does not hear its own messages + if (typeof BroadcastChannel === 'undefined') return; + + const channel = new BroadcastChannel(CHANNEL_NAME); + + channel.postMessage(result); + channel.close(); + } + + private async revoke( + endpoint: string, + token: string, + tokenTypeHint: string, + client: { client_id: string; client_secret?: string }, + fetchFn: FetchLike + ): Promise { + const fields: Record = { + client_id: client.client_id, + token, + token_type_hint: tokenTypeHint + }; + + if (client.client_secret) fields.client_secret = client.client_secret; + + const body = new URLSearchParams(fields).toString(); + + try { + await fetchFn(endpoint, { + body, + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + method: 'POST' + }); + } catch (error) { + // the tokens are forgotten here either way + console.warn('[MCP OAuth] Could not revoke the token:', error); + } + } + + private settle(result: McpOAuthResult): void { + this.signingIn.delete(result.serverId); + + if (result.ok) { + this.errors.delete(result.serverId); + this.signInRequired.delete(result.serverId); + } else if (result.error) { + this.errors.set(result.serverId, result.error); + } + + for (const listener of this.listeners) listener(result); + } +} + +export const mcpOAuthFlow = new McpOAuthFlow(); diff --git a/ui/apps/chat/src/lib/extensions/mcp/oauth/provider.ts b/ui/apps/chat/src/lib/extensions/mcp/oauth/provider.ts new file mode 100644 index 000000000000..d68d78e68cfa --- /dev/null +++ b/ui/apps/chat/src/lib/extensions/mcp/oauth/provider.ts @@ -0,0 +1,243 @@ +/** + * The OAuth client the MCP SDK drives for one server. The SDK runs the + * protocol (discovery, registration, PKCE, token exchange and refresh); this + * provider keeps what it needs across the trip to the authorization page and + * decides what happens when a sign-in is needed. + * + * A connection in the background never opens anything: when the server wants + * a sign-in and there are no tokens to refresh, the provider records that and + * the connection fails with {@link McpSignInRequiredError}. Only a sign-in the + * user started navigates to the authorization page. + */ + +import { type McpOAuthPendingFlow, mcpOAuthStorage } from './storage.svelte'; +import type { + OAuthClientMetadata, + OAuthClientProvider, + OAuthDiscoveryState, + StoredOAuthClientInformation, + StoredOAuthTokens +} from '@modelcontextprotocol/client'; +import { DEFAULT_CLIENT_VERSION } from '#lib/constants/index.ts'; +import { withBase } from '#lib/utils/base-path.ts'; + +/** Query parameter that marks a page load as an MCP sign-in coming back. */ +export const MCP_OAUTH_CALLBACK_PARAM = 'mcp_oauth'; + +/** The name the app registers under, shown on the authorization page. */ +export const MCP_OAUTH_CLIENT_NAME = 'Llama Chat'; + +/** Thrown where a server wants credentials and offers no sign-in: a token has to be set by hand. */ +export class McpCredentialsRequiredError extends Error { + constructor(readonly serverId: string) { + super( + "The server needs credentials and offers no sign-in: add a token under Authorization in the server's settings" + ); + this.name = 'McpCredentialsRequiredError'; + } +} + +/** Thrown where a server needs the user to sign in before it can be used. */ +export class McpSignInRequiredError extends Error { + constructor(readonly serverId: string) { + super('Sign in to use this server'); + this.name = 'McpSignInRequiredError'; + } +} + +/** The page the authorization server sends the browser back to: the app's root, with a marker. */ +export function mcpOAuthRedirectUrl(): string { + const url = new URL(withBase('/'), window.location.origin); + + url.searchParams.set(MCP_OAUTH_CALLBACK_PARAM, '1'); + + return url.href; +} + +/** A random `state`, so a callback can only finish the sign-in that asked for it. */ +export function createOAuthState(): string { + const bytes = new Uint8Array(32); + + crypto.getRandomValues(bytes); + + return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join(''); +} + +export interface McpOAuthProviderOptions { + /** + * Set for a sign-in the user started: where to send the browser for the + * authorization page. Left out, the provider never leaves the page. + */ + interactive?: { + mode: McpOAuthPendingFlow['mode']; + navigate: (authorizationUrl: URL) => void; + returnUrl: string; + }; + /** Steps worth showing in the connection log. */ + onStep?: (message: string) => void; + serverId: string; + serverUrl: string; + useProxy: boolean; +} + +export class McpOAuthProvider implements OAuthClientProvider { + /** Set once the server answered 401 without publishing an authorization server. */ + credentialsRequired = false; + /** Set once the server asked for a sign-in this provider could not complete on its own. */ + signInRequired = false; + + private readonly redirect: string; + + get clientMetadata(): OAuthClientMetadata { + return { + client_name: MCP_OAUTH_CLIENT_NAME, + grant_types: ['authorization_code', 'refresh_token'], + redirect_uris: [this.redirect], + response_types: ['code'], + software_version: DEFAULT_CLIENT_VERSION, + // a public client: the browser holds no secret worth the name + token_endpoint_auth_method: 'none' + }; + } + + get redirectUrl(): string { + return this.redirect; + } + + clientInformation(): StoredOAuthClientInformation | undefined { + const stored = this.record()?.clientInformation; + + // registering a client is a side effect on the authorization server: only for a sign-in the user started + if (!stored && !this.options.interactive) { + // discovery ran just before this: without an authorization server there is nothing to sign in to + if (!this.record()?.discoveryState?.authorizationServerMetadata) { + this.credentialsRequired = true; + + throw new McpCredentialsRequiredError(this.options.serverId); + } + + this.requireSignIn(); + } + + return stored; + } + + codeVerifier(): string { + const verifier = this.record()?.pending?.codeVerifier; + + if (!verifier) throw new Error('This sign-in has no PKCE verifier; start it again'); + + return verifier; + } + + constructor(private readonly options: McpOAuthProviderOptions) { + this.redirect = mcpOAuthRedirectUrl(); + } + + discoveryState(): OAuthDiscoveryState | undefined { + return this.record()?.discoveryState; + } + + invalidateCredentials(scope: 'all' | 'client' | 'tokens' | 'verifier' | 'discovery'): void { + if (scope === 'all') { + mcpOAuthStorage.remove(this.options.serverId); + + return; + } + + this.update((record) => { + const next = { ...record }; + + if (scope === 'client') delete next.clientInformation; + + if (scope === 'tokens') delete next.tokens; + + if (scope === 'discovery') delete next.discoveryState; + + if (scope === 'verifier' && next.pending) { + next.pending = { ...next.pending, codeVerifier: undefined }; + } + + return next; + }); + } + + redirectToAuthorization(authorizationUrl: URL): void { + const { interactive } = this.options; + + if (!interactive) { + this.requireSignIn(); + + return; + } + + this.options.onStep?.(`Opening the sign-in page of ${authorizationUrl.origin}`); + interactive.navigate(authorizationUrl); + } + + saveClientInformation(clientInformation: StoredOAuthClientInformation): void { + this.options.onStep?.( + `Registered with the authorization server as ${clientInformation.client_id}` + ); + this.update((record) => ({ ...record, clientInformation })); + } + + saveCodeVerifier(codeVerifier: string): void { + this.update((record) => + record.pending ? { ...record, pending: { ...record.pending, codeVerifier } } : record + ); + } + + saveDiscoveryState(discoveryState: OAuthDiscoveryState): void { + this.options.onStep?.(`Authorization server: ${discoveryState.authorizationServerUrl}`); + this.update((record) => ({ ...record, discoveryState })); + } + + saveTokens(tokens: StoredOAuthTokens): void { + const refreshed = this.record()?.tokens !== undefined; + + this.options.onStep?.(refreshed ? 'Access token refreshed' : 'Signed in'); + this.update((record) => ({ ...record, pending: undefined, tokens })); + } + + /** A fresh `state` for the sign-in about to start, kept with it until the callback. */ + state(): string { + const { interactive } = this.options; + const state = createOAuthState(); + + // a connection in the background never leaves for the authorization page: nothing to come back to + if (!interactive) return state; + + const pending: McpOAuthPendingFlow = { + mode: interactive.mode, + returnUrl: interactive.returnUrl, + startedAt: Date.now(), + state, + useProxy: this.options.useProxy + }; + + this.update((record) => ({ ...record, pending })); + + return state; + } + + tokens(): StoredOAuthTokens | undefined { + return this.record()?.tokens; + } + + private record() { + return mcpOAuthStorage.get(this.options.serverId, this.options.serverUrl, this.redirect); + } + + private requireSignIn(): never { + if (!this.signInRequired) this.options.onStep?.('The server needs you to sign in'); + + this.signInRequired = true; + + throw new McpSignInRequiredError(this.options.serverId); + } + + private update(change: Parameters[3]): void { + mcpOAuthStorage.update(this.options.serverId, this.options.serverUrl, this.redirect, change); + } +} diff --git a/ui/apps/chat/src/lib/extensions/mcp/oauth/storage.svelte.ts b/ui/apps/chat/src/lib/extensions/mcp/oauth/storage.svelte.ts new file mode 100644 index 000000000000..3aa52092614e --- /dev/null +++ b/ui/apps/chat/src/lib/extensions/mcp/oauth/storage.svelte.ts @@ -0,0 +1,143 @@ +/** + * Where the browser keeps each MCP server's OAuth state: the client it + * registered, its tokens, the discovery it made and, while a sign-in is under + * way, the PKCE verifier and the `state` that ties the callback to it. + * + * One localStorage entry, apart from the settings: tokens never travel with a + * settings export. A record belongs to one server URL and one redirect URL; + * when either changes, the record no longer applies. + */ + +import type { + OAuthDiscoveryState, + StoredOAuthClientInformation, + StoredOAuthTokens +} from '@modelcontextprotocol/client'; +import { STORAGE_APP_NAME } from '#lib/constants/index.ts'; + +export const MCP_OAUTH_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.mcpOAuth`; + +/** How a sign-in that left for the authorization page comes back. */ +export enum McpOAuthFlowMode { + /** In a popup, which closes itself once the tokens are in. */ + POPUP = 'popup', + /** In this tab, which goes back to where it was. */ + REDIRECT = 'redirect' +} + +/** A sign-in waiting for its callback. */ +export interface McpOAuthPendingFlow { + codeVerifier?: string; + mode: McpOAuthFlowMode; + /** Where a redirect sign-in returns to. */ + returnUrl: string; + startedAt: number; + state: string; + useProxy: boolean; +} + +export interface McpOAuthRecord { + clientInformation?: StoredOAuthClientInformation; + discoveryState?: OAuthDiscoveryState; + pending?: McpOAuthPendingFlow; + redirectUrl: string; + serverUrl: string; + tokens?: StoredOAuthTokens; +} + +type McpOAuthRecords = Record; + +// bumped on every write here and in other tabs, so what reads a record stays current +let version = $state(0); + +function readAll(): McpOAuthRecords { + if (typeof localStorage === 'undefined') return {}; + + try { + const raw = localStorage.getItem(MCP_OAUTH_LOCALSTORAGE_KEY); + const parsed: unknown = raw ? JSON.parse(raw) : null; + + return parsed && typeof parsed === 'object' && !Array.isArray(parsed) + ? (parsed as McpOAuthRecords) + : {}; + } catch (error) { + console.warn('[MCP OAuth] Failed to read the stored OAuth state:', error); + + return {}; + } +} + +function writeAll(records: McpOAuthRecords): void { + if (typeof localStorage === 'undefined') return; + + try { + localStorage.setItem(MCP_OAUTH_LOCALSTORAGE_KEY, JSON.stringify(records)); + } catch (error) { + console.warn('[MCP OAuth] Failed to store the OAuth state:', error); + } + + version++; +} + +export const mcpOAuthStorage = { + /** All records, by server id. Reactive. */ + all(): McpOAuthRecords { + void version; + + return readAll(); + }, + + /** The server whose sign-in is waiting for this `state`. */ + findByState(state: string): { record: McpOAuthRecord; serverId: string } | undefined { + for (const [serverId, record] of Object.entries(readAll())) { + if (record.pending?.state === state) return { record, serverId }; + } + + return undefined; + }, + + /** The record of a server, or undefined when there is none for this URL and redirect URL. */ + get(serverId: string, serverUrl: string, redirectUrl: string): McpOAuthRecord | undefined { + void version; + + const record = readAll()[serverId]; + + if (!record || record.serverUrl !== serverUrl || record.redirectUrl !== redirectUrl) { + return undefined; + } + + return record; + }, + + /** Re-read the stored state, for a change another tab made. */ + invalidate(): void { + version++; + }, + + remove(serverId: string): void { + const records = readAll(); + + if (!(serverId in records)) return; + + delete records[serverId]; + writeAll(records); + }, + + /** Change a server's record; one for another URL or redirect URL starts over. */ + update( + serverId: string, + serverUrl: string, + redirectUrl: string, + change: (record: McpOAuthRecord) => McpOAuthRecord + ): void { + const records = readAll(); + const current = records[serverId]; + const base = + current && current.serverUrl === serverUrl && current.redirectUrl === redirectUrl + ? current + : { redirectUrl, serverUrl }; + + records[serverId] = change(base); + writeAll(records); + } +}; diff --git a/ui/apps/chat/src/lib/extensions/mcp/services/mcp.service.ts b/ui/apps/chat/src/lib/extensions/mcp/services/mcp.service.ts index 48b330ee6e36..56bd4e3432be 100644 --- a/ui/apps/chat/src/lib/extensions/mcp/services/mcp.service.ts +++ b/ui/apps/chat/src/lib/extensions/mcp/services/mcp.service.ts @@ -6,6 +6,12 @@ * formatting. No reactive state; consumed by mcpStore and its managers. */ +import { mcpOAuthFlow } from '../oauth/flow.svelte'; +import { + McpCredentialsRequiredError, + McpOAuthProvider, + McpSignInRequiredError +} from '../oauth/provider'; import { WebSocketClientTransport } from './mcp-websocket-transport'; import { Client, @@ -221,8 +227,18 @@ export class MCPService { console.log(`[MCPService][${serverName}] Creating transport...`); } - const createdTransport = this.createTransport(serverName, serverConfig, (log) => - onPhase?.(log.phase, log) + // a server reached without an Authorization header of the user's own may ask for a sign-in + const authProvider = this.createOAuthProvider(serverName, serverConfig, (message) => + onPhase?.( + MCPConnectionPhase.INITIALIZING, + this.createLog(MCPConnectionPhase.INITIALIZING, `OAuth: ${message}`) + ) + ); + const createdTransport = this.createTransport( + serverName, + serverConfig, + (log) => onPhase?.(log.phase, log), + authProvider ); const transportType = createdTransport.type; @@ -354,8 +370,11 @@ export class MCPService { // the SDK closed the probed transport stopPhaseLogging(); - ({ stopPhaseLogging, transport } = this.createTransport(serverName, serverConfig, (log) => - onPhase?.(log.phase, log) + ({ stopPhaseLogging, transport } = this.createTransport( + serverName, + serverConfig, + (log) => onPhase?.(log.phase, log), + authProvider )); await Promise.race([ @@ -371,6 +390,22 @@ export class MCPService { client.onerror = runtimeErrorHandler; } catch (error) { client.onerror = runtimeErrorHandler; + + if (authProvider?.signInRequired) { + stopPhaseLogging(); + mcpOAuthFlow.markSignInRequired(serverName); + onPhase?.( + MCPConnectionPhase.ERROR, + this.createLog(MCPConnectionPhase.ERROR, 'Sign in to use this server', MCPLogLevel.WARN) + ); + + throw new McpSignInRequiredError(serverName); + } + + // a 401 with no sign-in on offer: say what to do rather than how the SDK failed + const failure = authProvider?.credentialsRequired + ? new McpCredentialsRequiredError(serverName) + : error; const url = (serverConfig.useProxy ?? false) ? buildProxiedUrl(serverConfig.url) @@ -381,7 +416,7 @@ export class MCPService { this.createLog( MCPConnectionPhase.ERROR, `Connection failed during handshake: ${ - error instanceof Error ? error.message : String(error) + failure instanceof Error ? failure.message : String(failure) }`, MCPLogLevel.ERROR, { @@ -399,15 +434,17 @@ export class MCPService { transportType, useProxy: serverConfig.useProxy ?? false }, - error: this.summarizeError(error), - hints: this.getConnectionHints(url, serverConfig, error) + error: this.summarizeError(failure), + hints: this.getConnectionHints(url, serverConfig, failure) } ) ); - throw error; + throw failure; } + mcpOAuthFlow.clearSignInRequired(serverName); + const serverVersion = client.getServerVersion(); const serverCapabilities = client.getServerCapabilities(); const instructions = client.getInstructions(); @@ -498,6 +535,35 @@ export class MCPService { }; } + /** + * The OAuth client of a server, for a server that may ask the user to sign + * in: an HTTP server whose Authorization header the user did not set. A + * header of the user's own wins over a sign-in, so a token pasted in the + * server's settings keeps working as before. + */ + static createOAuthProvider( + serverName: string, + config: MCPServerConfig, + onStep?: (message: string) => void + ): McpOAuthProvider | undefined { + if (config.transport === MCPTransportType.WEBSOCKET || typeof window === 'undefined') { + return undefined; + } + + const hasOwnAuthorization = Object.keys(config.headers ?? {}).some( + (key) => key.trim().toLowerCase() === HEADERS.AUTHORIZATION.toLowerCase() + ); + + if (hasOwnAuthorization) return undefined; + + return new McpOAuthProvider({ + onStep, + serverId: serverName, + serverUrl: config.url, + useProxy: config.useProxy ?? false + }); + } + /** * Create transport based on server configuration. * Supports WebSocket, StreamableHTTP (modern), and SSE (legacy) transports. @@ -515,7 +581,8 @@ export class MCPService { static createTransport( serverName: string, config: MCPServerConfig, - onLog?: (log: MCPConnectionLog) => void + onLog?: (log: MCPConnectionLog) => void, + authProvider?: McpOAuthProvider ): { transport: Transport; type: MCPTransportType; @@ -564,7 +631,8 @@ export class MCPService { } if (config.transport === MCPTransportType.SSE) { - const url = useProxy ? buildProxiedUrl(config.url) : new URL(config.url); + // the real URL even through the proxy: the diagnostic fetch sends it there + const url = new URL(config.url); const { disable: stopPhaseLogging, fetch: diagnosticFetch } = this.createDiagnosticFetch( serverName, config, @@ -581,6 +649,7 @@ export class MCPService { return { stopPhaseLogging, transport: new SSEClientTransport(url, { + authProvider, eventSourceInit: { fetch: diagnosticFetch }, fetch: diagnosticFetch, requestInit @@ -589,7 +658,8 @@ export class MCPService { }; } - const url = useProxy ? buildProxiedUrl(config.url) : new URL(config.url); + // the real URL even through the proxy: the diagnostic fetch sends it there + const url = new URL(config.url); const { disable: stopPhaseLogging, fetch: diagnosticFetch } = this.createDiagnosticFetch( serverName, config, @@ -600,7 +670,7 @@ export class MCPService { ); if (useProxy && import.meta.env.DEV && import.meta.env.VITE_DEBUG) { - console.log(`[MCPService] Using CORS proxy for ${config.url} -> ${url.href}`); + console.log(`[MCPService] Using CORS proxy for ${config.url}`); } try { @@ -611,6 +681,7 @@ export class MCPService { return { stopPhaseLogging, transport: new StreamableHTTPClientTransport(url, { + authProvider, fetch: diagnosticFetch, requestInit }), @@ -623,6 +694,7 @@ export class MCPService { return { stopPhaseLogging, transport: new SSEClientTransport(url, { + authProvider, eventSourceInit: { fetch: diagnosticFetch }, fetch: diagnosticFetch, requestInit @@ -858,6 +930,35 @@ export class MCPService { } } + /** + * The CORS proxy URL a request through the proxy goes to, or null when it is + * already addressed to the proxy (or is not an absolute http(s) request). + */ + static proxiedRequestUrl(input: RequestInfo | URL): string | URL | null { + let requestUrl: URL; + + try { + if (typeof input === 'string') requestUrl = new URL(input, window.location.origin); + else if (input instanceof URL) requestUrl = input; + else return null; + } catch { + return null; + } + + if (requestUrl.protocol !== 'http:' && requestUrl.protocol !== 'https:') return null; + + if ( + requestUrl.origin === window.location.origin && + requestUrl.pathname.includes(CORS_PROXY_ENDPOINT) + ) { + return null; + } + + const proxied = buildProxiedUrl(requestUrl.href); + + return typeof input === 'string' ? proxied.href : proxied; + } + /** * Read the contents of a resource. * @param connection - The MCP connection to use @@ -996,36 +1097,15 @@ export class MCPService { enabled = false; }, fetch: async (input, init) => { + // Through the proxy every request goes to llama-server's /cors-proxy: the MCP + // endpoint and, for a server that needs a sign-in, its OAuth discovery, + // registration and token endpoints. The transport and the OAuth flow keep the + // real URLs, so the resource they ask a token for and the discovery URLs they + // derive are the server's own. if (useProxy && typeof window !== 'undefined') { - let requestUrlStr = ''; - - if (typeof input === 'string') { - requestUrlStr = input; - } else if (input instanceof URL) { - requestUrlStr = input.href; - } + const proxied = this.proxiedRequestUrl(input); - if (requestUrlStr) { - const parsedRequestUrl = new URL(requestUrlStr, window.location.origin); - - if ( - parsedRequestUrl.origin === window.location.origin && - !parsedRequestUrl.pathname.includes(CORS_PROXY_ENDPOINT) - ) { - const originalConfigUrl = new URL(config.url); - const realTargetUrl = new URL( - parsedRequestUrl.pathname + parsedRequestUrl.search, - originalConfigUrl.origin - ); - const proxiedUrl = buildProxiedUrl(realTargetUrl.href); - - if (typeof input === 'string') { - input = proxiedUrl.href; - } else if (input instanceof URL) { - input = proxiedUrl; - } - } - } + if (proxied) input = proxied; } const startedAt = performance.now(); diff --git a/ui/apps/chat/src/lib/extensions/mcp/sign-in.ts b/ui/apps/chat/src/lib/extensions/mcp/sign-in.ts new file mode 100644 index 000000000000..ede4be3320ad --- /dev/null +++ b/ui/apps/chat/src/lib/extensions/mcp/sign-in.ts @@ -0,0 +1,46 @@ +/** + * Signing in to MCP servers with the browser, from the moment the extension + * is on: a page load that is an authorization server sending the browser back + * finishes its sign-in, and a server whose sign-in finished (in this tab or a + * popup) connects again with its new tokens. + */ + +import type { ExtensionContext } from '../types'; +import { mcpOAuthFlow } from './oauth/flow.svelte'; +import { mcpStore } from './stores/index.svelte'; +import { toast } from 'svelte-sonner'; + +/** How long a finished popup waits to close itself before saying it can be closed. */ +const POPUP_CLOSE_GRACE_MS = 400; + +export function contributeSignIn(ctx: ExtensionContext): void { + const unsubscribe = mcpOAuthFlow.subscribe((result) => { + if (!result.ok) return; + + const server = mcpStore.getServerById(result.serverId); + + if (server?.url.trim()) void mcpStore.runHealthCheck(server, true); + }); + + ctx.onDeactivate(unsubscribe); + + void mcpOAuthFlow.completeFromLocation().then((outcome) => { + if (!outcome?.result) return; + + const { result } = outcome; + + if (outcome.closeWindow) { + window.close(); + + // a browser may keep a window its script did not open in this document: say so + setTimeout(() => { + if (result.ok) toast.success('Signed in. You can close this window.'); + else toast.error(`${result.error ?? 'The sign-in failed'}. You can close this window.`); + }, POPUP_CLOSE_GRACE_MS); + + return; + } + + if (!result.ok) toast.error(result.error ?? 'The sign-in failed'); + }); +} diff --git a/ui/apps/chat/src/lib/extensions/mcp/stores/index.svelte.ts b/ui/apps/chat/src/lib/extensions/mcp/stores/index.svelte.ts index 086c55741eb5..a2af33200a96 100644 --- a/ui/apps/chat/src/lib/extensions/mcp/stores/index.svelte.ts +++ b/ui/apps/chat/src/lib/extensions/mcp/stores/index.svelte.ts @@ -9,6 +9,8 @@ * MCPService for the protocol layer. */ +import { mcpOAuthFlow } from '../oauth/flow.svelte'; +import { mcpOAuthStorage } from '../oauth/storage.svelte'; import { MCPService } from '../services/mcp.service'; // direct imports between stores, not via the barrel, to avoid circular deps import { MCPHealthCheckManager, type McpHealthHost } from './health.svelte'; @@ -322,6 +324,28 @@ class MCPStore implements McpHealthHost { return extras; } + /** + * Close a server's live connection and take its tools out, e.g. once its + * tokens are revoked: its next health check connects afresh. + */ + async disconnectServer(serverId: string): Promise { + const connection = this.connections.get(serverId); + + if (!connection) return; + + this.connections.delete(serverId); + + for (const [toolName, ownerServer] of this.toolsIndex.entries()) { + if (ownerServer === serverId) this.toolsIndex.delete(toolName); + } + + this.updateState({ toolCount: this.toolsIndex.size }); + + await MCPService.disconnect(connection).catch((error) => + console.warn(`[MCPStore] Error disconnecting ${serverId}:`, error) + ); + } + /** * Initialize every settings-enabled server. Policy filtering happens at tool * collection time, so switching conversation policies never re-initializes. @@ -471,7 +495,6 @@ class MCPStore implements McpHealthHost { console.error(`[MCPStore][${serverName}] Failed to fetch resources:`, error); } } - /** * Resolve which configured MCP server owns a given tool name. Looks at * active connections first (fast path), then falls back to per-server @@ -496,6 +519,7 @@ class MCPStore implements McpHealthHost { return undefined; } + getActiveFlowCount(): number { return this.activeFlowCount; } @@ -964,6 +988,14 @@ class MCPStore implements McpHealthHost { removeServer(id: string): void { const servers = this.getServers(); + const removed = servers.find((s) => s.id === id); + + // its tokens go too: revoked where the authorization server allows it, then forgotten + if (removed) { + void mcpOAuthFlow + .signOut({ id, url: removed.url, useProxy: removed.useProxy }) + .finally(() => mcpOAuthStorage.remove(id)); + } settingsStore.updateConfig( SETTINGS_KEYS.MCP_SERVERS, diff --git a/ui/apps/chat/tests/stories/mcp/McpServerCard.stories.svelte b/ui/apps/chat/tests/stories/mcp/McpServerCard.stories.svelte index 7e70657a5f1a..95fdf2e35372 100644 --- a/ui/apps/chat/tests/stories/mcp/McpServerCard.stories.svelte +++ b/ui/apps/chat/tests/stories/mcp/McpServerCard.stories.svelte @@ -1,9 +1,10 @@