2 import { Settings, Plus } from '@lucide/svelte';
3 import { Switch } from '$lib/components/ui/switch';
4 import * as DropdownMenu from '$lib/components/ui/dropdown-menu';
5 import { McpLogo, DropdownMenuSearchable, McpServerIdentity } from '$lib/components/app';
6 import { conversationsStore } from '$lib/stores/conversations.svelte';
7 import { mcpStore } from '$lib/stores/mcp.svelte';
8 import { HealthCheckStatus } from '$lib/enums';
9 import type { MCPServerSettingsEntry } from '$lib/types';
10 import { goto } from '$app/navigation';
11 import { ROUTES } from '$lib/constants/routes';
14 onMcpSettingsClick?: () => void;
17 let { onMcpSettingsClick }: Props = $props();
19 let mcpSearchQuery = $state('');
20 let allMcpServers = $derived(mcpStore.getServersSorted());
21 let mcpServers = $derived(allMcpServers.filter((s) => s.enabled));
22 let hasMcpServers = $derived(mcpServers.length > 0);
23 // let hasAnyMcpServers = $derived(allMcpServers.length > 0);
24 let filteredMcpServers = $derived.by(() => {
25 const query = mcpSearchQuery.toLowerCase().trim();
26 if (!query) return mcpServers;
27 return mcpServers.filter((s) => {
28 const name = getServerLabel(s).toLowerCase();
29 const url = s.url.toLowerCase();
30 return name.includes(query) || url.includes(query);
34 function getServerLabel(server: MCPServerSettingsEntry): string {
35 return mcpStore.getServerLabel(server);
38 function isServerEnabledForChat(serverId: string): boolean {
39 return conversationsStore.isMcpServerEnabledForChat(serverId);
42 async function toggleServerForChat(serverId: string) {
43 await conversationsStore.toggleMcpServerForChat(serverId);
46 function handleMcpSubMenuOpen(open: boolean) {
49 mcpStore.runHealthChecksForServers(allMcpServers);
53 function handleMcpSettingsClick() {
54 onMcpSettingsClick?.();
56 goto(`${hasMcpServers ? '' : '?add'}${ROUTES.MCP_SERVERS}`);
61 <DropdownMenu.Sub onOpenChange={handleMcpSubMenuOpen}>
62 <DropdownMenu.SubTrigger class="flex cursor-pointer items-center gap-2">
63 <McpLogo class="h-4 w-4" />
65 <span>MCP Servers</span>
66 </DropdownMenu.SubTrigger>
68 <DropdownMenu.SubContent class="w-72 pt-0">
70 <DropdownMenuSearchable
71 placeholder="Search servers..."
72 bind:searchValue={mcpSearchQuery}
73 emptyMessage="No servers found"
74 isEmpty={filteredMcpServers.length === 0}
76 <div class="max-h-64 overflow-y-auto">
77 {#each filteredMcpServers as server (server.id)}
78 {@const healthState = mcpStore.getHealthCheckState(server.id)}
79 {@const hasError = healthState.status === HealthCheckStatus.ERROR}
80 {@const isEnabledForChat = isServerEnabledForChat(server.id)}
81 {@const displayName = getServerLabel(server)}
82 {@const faviconUrl = mcpStore.getServerFavicon(server.id)}
86 class="flex w-full items-center justify-between gap-2 rounded-sm px-2 py-2 text-left transition-colors hover:bg-accent disabled:cursor-not-allowed disabled:opacity-50"
87 onclick={() => !hasError && toggleServerForChat(server.id)}
90 <div class="flex min-w-0 flex-1 items-center gap-2">
91 <div class="min-w-0 flex-1">
96 iconRounded="rounded-sm"
104 class="shrink-0 rounded bg-destructive/15 px-1.5 py-0.5 text-xs text-destructive"
112 checked={isEnabledForChat}
114 onclick={(e) => e.stopPropagation()}
115 onCheckedChange={() => toggleServerForChat(server.id)}
123 class="flex cursor-pointer items-center gap-2"
124 onclick={handleMcpSettingsClick}
126 <Settings class="h-4 w-4" />
128 <span>Manage MCP Servers</span>
131 </DropdownMenuSearchable>
133 <div class="px-2 py-3 text-center text-sm text-muted-foreground">
134 No MCP servers configured
137 <DropdownMenu.Separator />
140 class="flex cursor-pointer items-center gap-2"
141 onclick={handleMcpSettingsClick}
143 <Plus class="h-4 w-4" />
145 <span>Add MCP Servers</span>
148 </DropdownMenu.SubContent>