From 76dde40a231c83621a9d6610ae13fb7e9a1fb472 Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Sun, 15 Nov 2026 20:43:52 +0100 Subject: [PATCH 1/2] ui: sign in to MCP servers with the browser An MCP server that answers 401 and publishes an OAuth authorization server can now be signed in to, in the browser, with no server of our own: the MCP SDK's auth() runs discovery, dynamic client registration, PKCE, the code exchange and refresh, and an OAuthClientProvider here keeps the client, the tokens, the verifier and the discovery state per server in localStorage, apart from the settings so tokens never travel with a settings export. A connection in the background never opens a page or registers a client: it stops with "sign in to use this server", and a server whose 401 offers no sign-in says to add a token instead. A sign-in the user starts opens the authorization page in a popup (or this tab when it is blocked); the page comes back to the app's root, which exchanges the code, checks the state, and tells every tab on a BroadcastChannel, since the cross-origin isolated UI cuts the popup off from its opener. The server then connects again. Sign-out revokes the token where the authorization server allows it. An Authorization header the user set wins: such a server is never offered a sign-in. Through llama-server's CORS proxy the transport now keeps the real server URL and the fetch sends every request through the proxy, so the resource a token is asked for and the discovery URLs are the server's own. Mirrors Hugging Face chat-ui's MCP OAuth where a static app can, and builds on the earlier attempt on the fork by LPFchan. Assisted-by: Claude (Opus 5.5 Medium) --- ui/apps/chat/src/lib/extensions/mcp/index.ts | 2 + .../src/lib/extensions/mcp/oauth/fetch.ts | 29 ++ .../lib/extensions/mcp/oauth/flow.svelte.ts | 405 ++++++++++++++++++ .../src/lib/extensions/mcp/oauth/provider.ts | 243 +++++++++++ .../extensions/mcp/oauth/storage.svelte.ts | 143 +++++++ .../extensions/mcp/services/mcp.service.ts | 160 +++++-- .../chat/src/lib/extensions/mcp/sign-in.ts | 46 ++ .../tests/unit/fixtures/oauth-mcp-server.ts | 230 ++++++++++ ui/apps/chat/tests/unit/mcp-oauth.test.ts | 382 +++++++++++++++++ 9 files changed, 1600 insertions(+), 40 deletions(-) create mode 100644 ui/apps/chat/src/lib/extensions/mcp/oauth/fetch.ts create mode 100644 ui/apps/chat/src/lib/extensions/mcp/oauth/flow.svelte.ts create mode 100644 ui/apps/chat/src/lib/extensions/mcp/oauth/provider.ts create mode 100644 ui/apps/chat/src/lib/extensions/mcp/oauth/storage.svelte.ts create mode 100644 ui/apps/chat/src/lib/extensions/mcp/sign-in.ts create mode 100644 ui/apps/chat/tests/unit/fixtures/oauth-mcp-server.ts create mode 100644 ui/apps/chat/tests/unit/mcp-oauth.test.ts 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/tests/unit/fixtures/oauth-mcp-server.ts b/ui/apps/chat/tests/unit/fixtures/oauth-mcp-server.ts new file mode 100644 index 000000000000..99ac2e3b0fda --- /dev/null +++ b/ui/apps/chat/tests/unit/fixtures/oauth-mcp-server.ts @@ -0,0 +1,230 @@ +// A small MCP server behind OAuth, in process: protected resource metadata, +// authorization server metadata, dynamic client registration, an authorize +// step that approves at once, a token endpoint that checks PKCE and issues +// refreshable tokens, and token revocation. Enough for the browser client's +// whole sign-in to run against it. + +import { createMcpHandler, McpServer } from '@modelcontextprotocol/server'; +import { createHash, randomBytes } from 'node:crypto'; + +export const ISSUER = 'https://auth.example.com'; +export const MCP_URL = 'https://mcp.example.com/mcp'; + +interface Grant { + clientId: string; + codeChallenge: string; + redirectUri: string; + resource: string | null; +} + +export interface OAuthFixtureRequest { + authorization: string | null; + body: string; + method: string; + url: string; +} + +function json(body: unknown, status = 200, headers: Record = {}): Response { + return new Response(JSON.stringify(body), { + headers: { 'Content-Type': 'application/json', ...headers }, + status + }); +} + +function base64Url(buffer: Buffer): string { + return buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); +} + +export function createOAuthMcpServer() { + const clients = new Map(); + const codes = new Map(); + const accessTokens = new Set(); + const refreshTokens = new Map(); + const requests: OAuthFixtureRequest[] = []; + const revoked: string[] = []; + const mcp = createMcpHandler(() => { + const server = new McpServer({ name: 'oauth-test-server', version: '1.0.0' }); + + server.registerTool('whoami', { description: 'Who the token belongs to' }, async () => ({ + content: [{ text: 'signed-in user', type: 'text' }] + })); + + return server; + }); + const control = { noRegistration: false, rejectRefresh: false }; + + function issueTokens(clientId: string) { + const accessToken = base64Url(randomBytes(16)); + const refreshToken = base64Url(randomBytes(16)); + + accessTokens.add(accessToken); + refreshTokens.set(refreshToken, clientId); + + return { + access_token: accessToken, + expires_in: 3600, + refresh_token: refreshToken, + scope: 'mcp', + token_type: 'Bearer' + }; + } + + async function handle(request: Request): Promise { + const url = new URL(request.url); + const body = request.method === 'POST' ? await request.clone().text() : ''; + + requests.push({ + authorization: request.headers.get('authorization'), + body, + method: request.method, + url: request.url + }); + + if (url.href === MCP_URL) { + const token = request.headers.get('authorization')?.replace(/^Bearer /, ''); + + if (!token || !accessTokens.has(token)) { + return json({ error: 'invalid_token' }, 401, { + 'WWW-Authenticate': `Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource/mcp", scope="mcp"` + }); + } + + return mcp.fetch(request); + } + + if (url.href === 'https://mcp.example.com/.well-known/oauth-protected-resource/mcp') { + return json({ + authorization_servers: [ISSUER], + resource: MCP_URL, + scopes_supported: ['mcp'] + }); + } + + if (url.href === `${ISSUER}/.well-known/oauth-authorization-server`) { + return json({ + authorization_endpoint: `${ISSUER}/authorize`, + code_challenge_methods_supported: ['S256'], + grant_types_supported: ['authorization_code', 'refresh_token'], + issuer: ISSUER, + ...(control.noRegistration ? {} : { registration_endpoint: `${ISSUER}/register` }), + response_types_supported: ['code'], + revocation_endpoint: `${ISSUER}/revoke`, + scopes_supported: ['mcp'], + token_endpoint: `${ISSUER}/token`, + token_endpoint_auth_methods_supported: ['none', 'client_secret_post'] + }); + } + + if (url.href === `${ISSUER}/register` && request.method === 'POST') { + const metadata = JSON.parse(body) as { redirect_uris: string[] }; + const clientId = `client-${clients.size + 1}`; + + clients.set(clientId, { redirectUris: metadata.redirect_uris }); + + return json({ ...metadata, client_id: clientId, client_id_issued_at: 0 }, 201); + } + + if (url.href === `${ISSUER}/token` && request.method === 'POST') { + const form = new URLSearchParams(body); + + if (form.get('grant_type') === 'authorization_code') { + const grant = codes.get(form.get('code') ?? ''); + const verifier = form.get('code_verifier') ?? ''; + + codes.delete(form.get('code') ?? ''); + + if ( + !grant || + grant.clientId !== form.get('client_id') || + grant.redirectUri !== form.get('redirect_uri') || + base64Url(createHash('sha256').update(verifier).digest()) !== grant.codeChallenge + ) { + return json({ error: 'invalid_grant' }, 400); + } + + return json(issueTokens(grant.clientId)); + } + + if (form.get('grant_type') === 'refresh_token') { + const clientId = refreshTokens.get(form.get('refresh_token') ?? ''); + + if (!clientId || control.rejectRefresh) return json({ error: 'invalid_grant' }, 400); + + refreshTokens.delete(form.get('refresh_token') ?? ''); + + return json(issueTokens(clientId)); + } + + return json({ error: 'unsupported_grant_type' }, 400); + } + + if (url.href === `${ISSUER}/revoke` && request.method === 'POST') { + const token = new URLSearchParams(body).get('token') ?? ''; + + revoked.push(token); + refreshTokens.delete(token); + accessTokens.delete(token); + + return new Response(null, { status: 200 }); + } + + return new Response('not found', { status: 404 }); + } + + /** An app the user registered with the authorization server by hand. */ + function registerByHand(clientId: string, redirectUri: string): void { + clients.set(clientId, { redirectUris: [redirectUri] }); + } + + /** + * The user approving on the authorization page: the URL the browser is sent + * back to, with a code for this request, or with an error when denied. + */ + function authorize(authorizationUrl: URL, options: { deny?: boolean } = {}): URL { + const clientId = authorizationUrl.searchParams.get('client_id') ?? ''; + const redirectUri = authorizationUrl.searchParams.get('redirect_uri') ?? ''; + const callback = new URL(redirectUri); + const state = authorizationUrl.searchParams.get('state'); + + if (!clients.get(clientId)?.redirectUris.includes(redirectUri)) { + throw new Error(`redirect_uri ${redirectUri} was not registered for ${clientId}`); + } + + if (state) callback.searchParams.set('state', state); + + if (options.deny) { + callback.searchParams.set('error', 'access_denied'); + callback.searchParams.set('error_description', 'The user said no'); + + return callback; + } + + const code = base64Url(randomBytes(16)); + + codes.set(code, { + clientId, + codeChallenge: authorizationUrl.searchParams.get('code_challenge') ?? '', + redirectUri, + resource: authorizationUrl.searchParams.get('resource') + }); + callback.searchParams.set('code', code); + + return callback; + } + + /** Make every access token handed out so far stop working, as if they expired. */ + function expireAccessTokens(): void { + accessTokens.clear(); + } + + return { + authorize, + codes, + control, + expireAccessTokens, + handle, + registerByHand, + requests, + revoked + }; +} diff --git a/ui/apps/chat/tests/unit/mcp-oauth.test.ts b/ui/apps/chat/tests/unit/mcp-oauth.test.ts new file mode 100644 index 000000000000..a75eef426d20 --- /dev/null +++ b/ui/apps/chat/tests/unit/mcp-oauth.test.ts @@ -0,0 +1,382 @@ +// Signing in to an MCP server with the browser, end to end against an +// in-process server behind OAuth: a background connection stops at the sign-in +// without registering anything, the sign-in registers a client and sends the +// browser to the authorization page, the callback exchanges the code, and the +// server then connects with the token; expired tokens refresh on their own, +// a refused refresh asks for a sign-in again, and sign-out revokes. + +import { createOAuthMcpServer, ISSUER, MCP_URL } from './fixtures/oauth-mcp-server'; +import { CORS_PROXY, CORS_PROXY_ENDPOINT } from '#lib/constants/index.ts'; +import { MCPConnectionPhase, MCPTransportType } from '#lib/enums/index.ts'; +import { mcpOAuthFlow } from '#lib/extensions/mcp/oauth/flow.svelte.ts'; +import { + McpCredentialsRequiredError, + mcpOAuthRedirectUrl, + McpSignInRequiredError +} from '#lib/extensions/mcp/oauth/provider.ts'; +import { + MCP_OAUTH_LOCALSTORAGE_KEY, + mcpOAuthStorage +} from '#lib/extensions/mcp/oauth/storage.svelte.ts'; +import { MCPService } from '#lib/extensions/mcp/services/mcp.service.ts'; +import type { MCPConnectionLog, MCPServerConfig } from '#lib/types/index.ts'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +const APP = 'http://localhost:8080'; +const SERVER_ID = 'srv-oauth'; +const CONFIG: MCPServerConfig = { transport: MCPTransportType.STREAMABLE_HTTP, url: MCP_URL }; + +type Fixture = ReturnType; + +function stubBrowser(fixture: Fixture, options: { proxy?: boolean } = {}) { + const data = new Map(); + const location = new URL(`${APP}/#/mcp-servers`); + const history = { + replaceState: (_state: unknown, _title: string, url: string) => { + location.href = new URL(url, location.href).href; + }, + state: null + }; + + vi.stubGlobal('localStorage', { + getItem: (key: string) => data.get(key) ?? null, + removeItem: (key: string) => void data.delete(key), + setItem: (key: string, value: string) => void data.set(key, value) + }); + vi.stubGlobal('window', { + history, + location: { + ...locationProxy(location), + assign: vi.fn(), + replace: (url: string) => { + location.href = new URL(url, location.href).href; + } + } + }); + vi.stubGlobal('fetch', async (input: RequestInfo | URL, init?: RequestInit) => { + let request = new Request(input, init); + + // what llama-server's /cors-proxy does: unwrap the target and its headers + if (request.url.startsWith(`${APP}${CORS_PROXY_ENDPOINT}`)) { + if (!options.proxy) throw new Error('the proxy was used without being asked for'); + + const target = new URL(request.url).searchParams.get(CORS_PROXY.URL_PARAM)!; + const headers = new Headers(); + + for (const [key, value] of request.headers) { + if (key.startsWith(CORS_PROXY.HEADER_PREFIX)) { + headers.set(key.slice(CORS_PROXY.HEADER_PREFIX.length), value); + } + } + + request = new Request(target, { + body: request.method === 'POST' ? await request.text() : undefined, + headers, + method: request.method + }); + } else if (options.proxy) { + throw new Error(`${request.url} was fetched around the proxy`); + } + + return fixture.handle(request); + }); + + return { data, location }; +} + +// window.location keeps `href` live: the flow reads it and rewrites it through history +function locationProxy(location: URL) { + return { + get href() { + return location.href; + }, + get origin() { + return location.origin; + }, + get search() { + return location.search; + } + }; +} + +function connect(fixture: Fixture, config: MCPServerConfig = CONFIG) { + const logs: MCPConnectionLog[] = []; + const connection = MCPService.connect(SERVER_ID, config, undefined, undefined, (_phase, log) => { + logs.push(log); + }); + + return { connection, logs }; +} + +/** Run a sign-in: returns the authorization page it sent the popup to. */ +async function startSignIn(config: MCPServerConfig = CONFIG): Promise { + const popup = { close: vi.fn(), location: { href: '' } } as unknown as Window; + + await mcpOAuthFlow.signIn({ id: SERVER_ID, url: config.url, useProxy: config.useProxy }, popup); + + expect(popup.location.href).not.toBe(''); + + return new URL(popup.location.href); +} + +async function signIn(fixture: Fixture, config: MCPServerConfig = CONFIG) { + const authorizationUrl = await startSignIn(config); + + return mcpOAuthFlow.completeFromLocation(fixture.authorize(authorizationUrl)); +} + +describe('MCP OAuth sign-in', () => { + let fixture: Fixture; + + beforeEach(() => { + vi.restoreAllMocks(); + vi.unstubAllGlobals(); + fixture = createOAuthMcpServer(); + mcpOAuthFlow.clearSignInRequired(SERVER_ID); + }); + + it('stops a background connection at the sign-in, without registering a client', async () => { + stubBrowser(fixture); + + const { connection, logs } = connect(fixture); + + await expect(connection).rejects.toBeInstanceOf(McpSignInRequiredError); + expect(mcpOAuthFlow.needsSignIn(SERVER_ID)).toBe(true); + expect(fixture.requests.some((r) => r.url === `${ISSUER}/register`)).toBe(false); + expect(logs.map((log) => log.message)).toEqual( + expect.arrayContaining([ + `OAuth: Authorization server: ${ISSUER}`, + 'OAuth: The server needs you to sign in', + 'Sign in to use this server' + ]) + ); + expect(logs.at(-1)?.phase).toBe(MCPConnectionPhase.ERROR); + }); + + it('registers, sends the popup to the authorization page with PKCE, and signs in on the callback', async () => { + const { location } = stubBrowser(fixture); + const results: unknown[] = []; + const unsubscribe = mcpOAuthFlow.subscribe((result) => results.push(result)); + + mcpOAuthFlow.markSignInRequired(SERVER_ID); + + const authorizationUrl = await startSignIn(); + + expect(mcpOAuthFlow.isSigningIn(SERVER_ID)).toBe(true); + expect(authorizationUrl.origin + authorizationUrl.pathname).toBe(`${ISSUER}/authorize`); + expect(authorizationUrl.searchParams.get('client_id')).toBe('client-1'); + expect(authorizationUrl.searchParams.get('redirect_uri')).toBe(`${APP}/?mcp_oauth=1`); + expect(authorizationUrl.searchParams.get('code_challenge_method')).toBe('S256'); + expect(authorizationUrl.searchParams.get('resource')).toBe(MCP_URL); + expect(authorizationUrl.searchParams.get('state')).toMatch(/^[0-9a-f]{64}$/); + + // the popup comes back to the app, which finishes the sign-in there + const outcome = await mcpOAuthFlow.completeFromLocation(fixture.authorize(authorizationUrl)); + + expect(outcome).toEqual({ closeWindow: true, result: { ok: true, serverId: SERVER_ID } }); + expect(results).toEqual([{ ok: true, serverId: SERVER_ID }]); + expect(mcpOAuthFlow.isSigningIn(SERVER_ID)).toBe(false); + expect(mcpOAuthFlow.needsSignIn(SERVER_ID)).toBe(false); + expect(mcpOAuthFlow.isSignedIn({ id: SERVER_ID, url: MCP_URL })).toBe(true); + // the code and state leave the address bar at once + expect(location.search).toBe(''); + + const { connection } = connect(fixture); + const connected = await connection; + + expect(connected.tools.map((tool) => tool.name)).toEqual(['whoami']); + expect(fixture.requests.filter((r) => r.url === MCP_URL).at(-1)?.authorization).toMatch( + /^Bearer / + ); + unsubscribe(); + }); + + it('refreshes an expired token on its own', async () => { + stubBrowser(fixture); + await signIn(fixture); + fixture.expireAccessTokens(); + + const { connection, logs } = connect(fixture); + + await expect(connection).resolves.toMatchObject({ serverName: SERVER_ID }); + expect(logs.map((log) => log.message)).toContain('OAuth: Access token refreshed'); + expect(fixture.requests.filter((r) => r.url === `${ISSUER}/register`)).toHaveLength(1); + }); + + it('asks for a sign-in again once the refresh token is refused', async () => { + stubBrowser(fixture); + await signIn(fixture); + fixture.expireAccessTokens(); + fixture.control.rejectRefresh = true; + + const { connection } = connect(fixture); + + await expect(connection).rejects.toBeInstanceOf(McpSignInRequiredError); + expect(mcpOAuthFlow.needsSignIn(SERVER_ID)).toBe(true); + }); + + it('reports a refused sign-in without the description the address bar carried', async () => { + stubBrowser(fixture); + + const authorizationUrl = await startSignIn(); + const outcome = await mcpOAuthFlow.completeFromLocation( + fixture.authorize(authorizationUrl, { deny: true }) + ); + + expect(outcome?.result).toEqual({ + error: 'The sign-in was cancelled or refused', + ok: false, + serverId: SERVER_ID + }); + expect(mcpOAuthFlow.errorFor(SERVER_ID)).toBe('The sign-in was cancelled or refused'); + expect(mcpOAuthFlow.isSignedIn({ id: SERVER_ID, url: MCP_URL })).toBe(false); + }); + + it('ignores a callback whose state no sign-in of this browser is waiting for', async () => { + stubBrowser(fixture); + + const authorizationUrl = await startSignIn(); + const forged = fixture.authorize(authorizationUrl); + + forged.searchParams.set('state', 'not-the-state'); + + await expect(mcpOAuthFlow.completeFromLocation(forged)).resolves.toEqual({ + closeWindow: false, + result: null + }); + expect(fixture.requests.some((r) => r.url === `${ISSUER}/token`)).toBe(false); + }); + + it('leaves other page loads alone', async () => { + stubBrowser(fixture); + + await expect( + mcpOAuthFlow.completeFromLocation(new URL(`${APP}/?code=x&state=y`)) + ).resolves.toBeNull(); + }); + + it('goes back to where a sign-in in this tab started', async () => { + const { location } = stubBrowser(fixture); + + await mcpOAuthFlow.signIn({ id: SERVER_ID, url: MCP_URL }, null); + + const assign = vi.mocked(window.location.assign); + const authorizationUrl = new URL(assign.mock.calls[0][0] as string); + const outcome = await mcpOAuthFlow.completeFromLocation(fixture.authorize(authorizationUrl)); + + expect(outcome).toEqual({ closeWindow: false, result: { ok: true, serverId: SERVER_ID } }); + expect(location.href).toBe(`${APP}/#/mcp-servers`); + }); + + it('signs out by revoking the token and forgetting it, keeping the registration', async () => { + const { data } = stubBrowser(fixture); + + await signIn(fixture); + await mcpOAuthFlow.signOut({ id: SERVER_ID, url: MCP_URL }); + + expect(fixture.revoked).toHaveLength(1); + expect(fixture.requests.find((r) => r.url === `${ISSUER}/revoke`)?.body).toContain( + 'client_id=client-1' + ); + expect(mcpOAuthFlow.isSignedIn({ id: SERVER_ID, url: MCP_URL })).toBe(false); + + const record = JSON.parse(data.get(MCP_OAUTH_LOCALSTORAGE_KEY)!)[SERVER_ID]; + + expect(record.clientInformation.client_id).toBe('client-1'); + expect(record.tokens).toBeUndefined(); + }); + + it('keeps tokens of one server URL away from another', async () => { + stubBrowser(fixture); + await signIn(fixture); + + expect( + mcpOAuthStorage.get(SERVER_ID, 'https://other.example.com/mcp', mcpOAuthRedirectUrl()) + ).toBeUndefined(); + }); + + it('leaves a server with an Authorization header of its own to that header', async () => { + stubBrowser(fixture); + + const { connection } = connect(fixture, { + ...CONFIG, + headers: { Authorization: 'Bearer pasted-token' } + }); + + await expect(connection).rejects.not.toBeInstanceOf(McpSignInRequiredError); + expect(fixture.requests.some((r) => r.url.includes('/.well-known/'))).toBe(false); + expect(mcpOAuthFlow.needsSignIn(SERVER_ID)).toBe(false); + }); + + it('says to add a token when the server answers 401 with no sign-in on offer', async () => { + const plain = { + ...fixture, + handle: async (request: Request) => { + fixture.requests.push({ + authorization: null, + body: '', + method: request.method, + url: request.url + }); + + return new Response('{}', { status: request.url === MCP_URL ? 401 : 404 }); + } + }; + + stubBrowser(plain as Fixture); + + await expect(connect(fixture).connection).rejects.toBeInstanceOf(McpCredentialsRequiredError); + expect(fixture.requests.some((r) => r.url.endsWith('/register'))).toBe(false); + }); + + it('asks for the client ID of an app registered by hand where the server registers none', async () => { + stubBrowser(fixture); + fixture.control.noRegistration = true; + + const popup = { close: vi.fn(), location: { href: '' } } as unknown as Window; + + await mcpOAuthFlow.signIn({ id: SERVER_ID, url: MCP_URL }, popup); + + expect(mcpOAuthFlow.needsClientId(SERVER_ID)).toBe(true); + expect(mcpOAuthFlow.errorFor(SERVER_ID)).toMatch(/client ID of an app you registered/); + expect(popup.close).toHaveBeenCalled(); + + fixture.registerByHand('my-own-app', `${APP}/?mcp_oauth=1`); + mcpOAuthFlow.useClient( + { id: SERVER_ID, url: MCP_URL }, + { clientId: ' my-own-app ', clientSecret: 'shh' } + ); + + expect(mcpOAuthFlow.needsClientId(SERVER_ID)).toBe(false); + + const authorizationUrl = await startSignIn(); + + expect(authorizationUrl.searchParams.get('client_id')).toBe('my-own-app'); + + await mcpOAuthFlow.completeFromLocation(fixture.authorize(authorizationUrl)); + + expect(mcpOAuthFlow.isSignedIn({ id: SERVER_ID, url: MCP_URL })).toBe(true); + // the secret goes to the token endpoint, the one place it belongs + expect(fixture.requests.find((r) => r.url === `${ISSUER}/token`)?.body).toContain( + 'client_secret=shh' + ); + }); + + it('runs the whole sign-in through the CORS proxy, keeping the real resource', async () => { + stubBrowser(fixture, { proxy: true }); + + const config = { ...CONFIG, useProxy: true }; + const authorizationUrl = await startSignIn(config); + + expect(authorizationUrl.searchParams.get('resource')).toBe(MCP_URL); + + await mcpOAuthFlow.completeFromLocation(fixture.authorize(authorizationUrl)); + + await expect(connect(fixture, config).connection).resolves.toMatchObject({ + serverName: SERVER_ID + }); + expect(fixture.requests.map((r) => new URL(r.url).pathname)).toEqual( + expect.arrayContaining(['/register', '/token', '/mcp']) + ); + }); +}); From 75ac475ae5ae6f2b17da6e48928c78c8dd859e70 Mon Sep 17 00:00:00 2001 From: Aleksander Grygier Date: Sun, 15 Nov 2026 20:51:41 +0100 Subject: [PATCH 2/2] ui: show the MCP sign-in on the server card A server that needs a sign-in shows "Sign in with " on its card instead of the error its connection ended with. While the authorization page is open the card waits, with a way to give up; a server whose authorization server registers no apps asks for the client ID (and secret, if any) of an app the user registered there, with the redirect URL to register it under; a signed-in server says so and offers Sign out. Signing out, and deleting a server, revoke its tokens where the authorization server allows it, close its connection and take its tools out, so the card is back to the sign-in at once. The MCP server card story has a Sign-in state for each of these. Assisted-by: Claude (Opus 5.5 Medium) --- ui/apps/chat/README.md | 2 + .../McpServerCard/McpServerCard.svelte | 8 +- .../McpServerCard/McpServerCardSignIn.svelte | 146 ++++++++++++++++++ .../lib/extensions/mcp/stores/index.svelte.ts | 34 +++- .../stories/mcp/McpServerCard.stories.svelte | 38 ++++- ui/apps/chat/tests/stories/mcp/fixtures.ts | 82 ++++++++++ 6 files changed, 307 insertions(+), 3 deletions(-) create mode 100644 ui/apps/chat/src/lib/extensions/mcp/components/McpServerCard/McpServerCardSignIn.svelte 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/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 @@