Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ui/apps/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<script lang="ts">
import { mcpOAuthFlow } from '../../oauth/flow.svelte';
import { mcpStore } from '../../stores/index.svelte';
import McpConnectionLogs from '../McpConnectionLogs.svelte';
import McpServerInfo from '../McpServerInfo.svelte';
import McpServerCardActions from './McpServerCardActions.svelte';
import McpServerCardDeleteDialog from './McpServerCardDeleteDialog.svelte';
import McpServerCardEditForm from './McpServerCardEditForm.svelte';
import McpServerCardHeader from './McpServerCardHeader.svelte';
import McpServerCardSignIn from './McpServerCardSignIn.svelte';
import McpServerCardToolsList from './McpServerCardToolsList.svelte';
import * as Card from '@ggml-org/llama-ui/ui/card';
import { Skeleton } from '@ggml-org/llama-ui/ui/skeleton';
Expand Down Expand Up @@ -38,6 +40,8 @@
let errorMessage = $derived(
healthState.status === HealthCheckStatus.ERROR ? healthState.message : undefined
);
// a connection that stopped at a sign-in shows the sign-in, not the error it ended with
let needsSignIn = $derived(mcpOAuthFlow.needsSignIn(server.id));
let tools = $derived(healthState.status === HealthCheckStatus.SUCCESS ? healthState.tools : []);

let connectionLogs = $derived(
Expand Down Expand Up @@ -130,10 +134,12 @@
{transportType}
/>

{#if isError && errorMessage}
{#if isError && errorMessage && !needsSignIn}
<p class="text-xs text-destructive">{errorMessage}</p>
{/if}

<McpServerCardSignIn connected={isConnected} {server} />

{#if isConnected && serverInfo?.description}
<p class="line-clamp-2 text-xs text-muted-foreground">
{serverInfo.description}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<script lang="ts">
import { mcpOAuthFlow } from '../../oauth/flow.svelte';
import { mcpOAuthRedirectUrl } from '../../oauth/provider';
import { mcpStore } from '../../stores/index.svelte';
import { Button } from '@ggml-org/llama-ui/ui/button';
import { Input } from '@ggml-org/llama-ui/ui/input';
import { Label } from '@ggml-org/llama-ui/ui/label';
import { KeyRound, Loader2, LogIn, LogOut, ShieldCheck } from '@lucide/svelte';
import { ActionIconCopyToClipboard } from '#lib/components/app/index.ts';
import type { MCPServerSettingsEntry } from '#lib/types/index.ts';

interface Props {
server: MCPServerSettingsEntry;
/** The server is connected right now. */
connected: boolean;
}

let { connected, server }: Props = $props();

const POPUP_NAME = 'llama-mcp-sign-in';
const POPUP_FEATURES = 'popup,width=520,height=720';

let target = $derived({ id: server.id, url: server.url, useProxy: server.useProxy });
let signedIn = $derived(mcpOAuthFlow.isSignedIn(target));
let signingIn = $derived(mcpOAuthFlow.isSigningIn(server.id));
let needsSignIn = $derived(mcpOAuthFlow.needsSignIn(server.id));
let needsClientId = $derived(mcpOAuthFlow.needsClientId(server.id));
let error = $derived(mcpOAuthFlow.errorFor(server.id));
let issuerHost = $derived(mcpOAuthFlow.issuerHost(target));
let redirectUrl = $derived(mcpOAuthRedirectUrl());

let clientId = $state('');
let clientSecret = $state('');
let signingOut = $state(false);

function signIn() {
// the popup opens in the click itself: a browser blocks one opened after an await
const popup = window.open('', POPUP_NAME, POPUP_FEATURES);

void mcpOAuthFlow.signIn(target, popup);
}

function useClientAndSignIn(event: SubmitEvent) {
event.preventDefault();

if (!clientId.trim()) return;

mcpOAuthFlow.useClient(target, { clientId, clientSecret });
clientSecret = '';
signIn();
}

async function signOut() {
signingOut = true;

try {
await mcpOAuthFlow.signOut(target);
// its tools go with the tokens; the check that follows shows the sign-in again
await mcpStore.disconnectServer(server.id);
mcpStore.clearHealthCheck(server.id);
await mcpStore.runHealthCheck(server, true);
} finally {
signingOut = false;
}
}
</script>

{#if signingIn}
<div class="flex items-center justify-between gap-3 rounded-md bg-muted/50 px-3 py-2">
<p class="flex items-center gap-2 text-xs text-muted-foreground">
<Loader2 class="size-icon-md shrink-0 animate-spin" />

Finish signing in in the window that opened.
</p>

<Button onclick={() => mcpOAuthFlow.cancelSignIn(server.id)} size="sm" variant="ghost">
Cancel
</Button>
</div>
{:else if needsClientId}
<form class="grid gap-2 rounded-md bg-muted/50 p-3" onsubmit={useClientAndSignIn}>
<p class="text-xs text-muted-foreground">
{issuerHost ?? 'This server'} does not register apps by itself. Register an OAuth app there with
this redirect URL, then enter its client ID.
</p>

<div class="flex items-center gap-1 rounded bg-background px-2 py-1">
<code class="min-w-0 flex-1 truncate text-xs">{redirectUrl}</code>

<ActionIconCopyToClipboard ariaLabel="Copy the redirect URL" text={redirectUrl} />
</div>

<div class="grid gap-1">
<Label class="text-xs" for="mcp-client-id-{server.id}">Client ID</Label>

<Input bind:value={clientId} autocomplete="off" id="mcp-client-id-{server.id}" required />
</div>

<div class="grid gap-1">
<Label class="text-xs" for="mcp-client-secret-{server.id}"
>Client secret (if it has one)</Label
>

<Input
bind:value={clientSecret}
autocomplete="off"
id="mcp-client-secret-{server.id}"
type="password"
/>
</div>

<Button class="justify-self-start" disabled={!clientId.trim()} size="sm" type="submit">
<KeyRound class="size-icon-md" />

Save and sign in
</Button>
</form>
{:else if needsSignIn}
<div class="flex flex-wrap items-center justify-between gap-3 rounded-md bg-muted/50 px-3 py-2">
<p class="text-xs text-muted-foreground">This server needs you to sign in.</p>

<Button onclick={signIn} size="sm">
<LogIn class="size-icon-md" />

{issuerHost ? `Sign in with ${issuerHost}` : 'Sign in'}
</Button>
</div>
{:else if signedIn && connected}
<div class="flex items-center justify-between gap-3">
<p class="flex items-center gap-1.5 text-xs text-muted-foreground">
<ShieldCheck class="size-icon-md shrink-0" />

{issuerHost ? `Signed in with ${issuerHost}` : 'Signed in'}
</p>

<Button disabled={signingOut} onclick={signOut} size="sm" variant="ghost">
<LogOut class="size-icon-md" />

Sign out
</Button>
</div>
{/if}

{#if error && !signingIn}
<p class="text-xs text-destructive">{error}</p>
{/if}
2 changes: 2 additions & 0 deletions ui/apps/chat/src/lib/extensions/mcp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ 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';

const mcp: Extension = {
activate(ctx) {
contributeComposer(ctx);
contributeNavigation(ctx);
contributeSignIn(ctx);
contributePages(ctx);
contributeRenderers(ctx);
contributeStartup(ctx);
Expand Down
29 changes: 29 additions & 0 deletions ui/apps/chat/src/lib/extensions/mcp/oauth/fetch.ts
Original file line number Diff line number Diff line change
@@ -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 });
};
}
Loading
Loading