Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

15.2 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { ChevronDownStandardIcon } from "@/lib/chevron-icons";
import { MenuTickIcon } from "@/lib/tick-icon";
import { McpServerIcon, Cancel01Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { useCallback, useEffect, useRef, useState } from "react";
import { toast } from "sonner";

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import {
  loadSystemOneSettings,
  subscribeSystemOneSettings,
  useShortcut,
} from "@/features/settings";

import { subscribeToMcpServerMutationSettlements } from "./api/mcp-server-mutation-tracker";
import {
  type McpServerConfig,
  createMcpServer,
  listMcpServers,
  updateMcpServer,
} from "./api/mcp-servers-api";
import { BlenderMcpSetup } from "./blender-mcp-setup";
import { ChatMcpServersDialog } from "./chat-mcp-servers-dialog";
import {
  UNSLOTH_STUDIO_TOOLS_MCP_URL,
  normalizeMcpUrl,
} from "./mcp-server-url";
import { useChatActive } from "./runtime-provider";
import { useChatRuntimeStore } from "./stores/chat-runtime-store";
import { useMcpServersDialogStore } from "./stores/mcp-servers-dialog-store";

type McpPreset = {
  id: string;
  displayName: string;
  url: string;
  label?: string;
  hint?: string;
  disablesWebSearch?: boolean;
};

// Unsloth's own tools, then keyless remote presets (rate-limited free tiers). Hugging Face
// runs anonymously; add a token via "Manage MCP servers".
const MCP_PRESETS: readonly McpPreset[] = [
  {
    id: "unsloth-studio",
    displayName: "Unsloth",
    url: UNSLOTH_STUDIO_TOOLS_MCP_URL,
  },
  {
    id: "unsloth-docs",
    displayName: "Unsloth Docs",
    url: "https://unsloth.ai/docs/~gitbook/mcp",
  },
  {
    id: "context7",
    displayName: "Context7",
    url: "https://mcp.context7.com/mcp",
    label: "Context7 (Realtime Docs)",
  },
  {
    id: "huggingface",
    displayName: "Hugging Face",
    url: "https://huggingface.co/mcp",
  },
] as const;

export function McpComposerButton({
  side = "bottom",
}: {
  side?: "top" | "bottom";
} = {}) {
  const modelLoaded = useChatRuntimeStore(
    (s) => !!s.params.checkpoint && !s.modelLoading,
  );
  const supportsTools = useChatRuntimeStore((s) => s.supportsTools);
  const mcpEnabledForChat = useChatRuntimeStore((s) => s.mcpEnabledForChat);
  const setMcpEnabledForChat = useChatRuntimeStore(
    (s) => s.setMcpEnabledForChat,
  );
  const setToolsEnabled = useChatRuntimeStore((s) => s.setToolsEnabled);

  const [servers, setServers] = useState<McpServerConfig[]>([]);
  const dialogOpen = useMcpServersDialogStore((s) => s.open);
  const setDialogOpen = useMcpServersDialogStore((s) => s.setOpen);
  const [menuOpen, setMenuOpen] = useState(false);
  const [blenderOpen, setBlenderOpen] = useState(false);
  const [blenderBusy, setBlenderBusy] = useState(false);
  const [serversLoaded, setServersLoaded] = useState(false);
  const [pendingUrls, setPendingUrls] = useState<ReadonlySet<string>>(
    () => new Set(),
  );
  const pendingUrlsRef = useRef(new Set<string>());
  const [hintKey, setHintKey] = useState<string | null>(null);
  const [decisionsUrl, setDecisionsUrl] = useState<string | null>(null);
  const decisionsRefreshGenerationRef = useRef(0);
  const listRefreshGenerationRef = useRef(0);
  const hasLoadedServerSnapshotRef = useRef(false);

  const usable = !modelLoaded || supportsTools;

  const refresh = useCallback(
    async (waitForPendingMutations = true, minimumMutationEpoch = 0) => {
      const generation = listRefreshGenerationRef.current + 1;
      listRefreshGenerationRef.current = generation;
      const decisionsGeneration = decisionsRefreshGenerationRef.current + 1;
      decisionsRefreshGenerationRef.current = decisionsGeneration;
      setServersLoaded(false);
      loadSystemOneSettings().then(
        (settings) => {
          if (decisionsRefreshGenerationRef.current !== decisionsGeneration)
            return;
          setDecisionsUrl(settings.enabled ? settings.mcpUrl : null);
        },
        () => {
          if (decisionsRefreshGenerationRef.current !== decisionsGeneration)
            return;
          setDecisionsUrl(null);
        },
      );
      try {
        const rows = await listMcpServers({
          waitForPendingMutations,
          minimumMutationEpoch,
        });
        if (listRefreshGenerationRef.current !== generation) return;
        setServers(rows);
        hasLoadedServerSnapshotRef.current = true;
        setServersLoaded(true);
      } catch {
        if (
          listRefreshGenerationRef.current === generation &&
          hasLoadedServerSnapshotRef.current
        ) {
          setServersLoaded(true);
        }
      }
    },
    [],
  );

  useEffect(
    () =>
      subscribeSystemOneSettings((settings) => {
        decisionsRefreshGenerationRef.current += 1;
        setDecisionsUrl(settings.enabled ? settings.mcpUrl : null);
      }),
    [],
  );

  const applyServer = useCallback((server: McpServerConfig) => {
    setServers((current) => {
      const index = current.findIndex(
        (candidate) => candidate.id === server.id,
      );
      if (index === -1) return [...current, server];
      return current.map((candidate) =>
        candidate.id === server.id ? server : candidate,
      );
    });
  }, []);

  useEffect(() => {
    const unsubscribe = subscribeToMcpServerMutationSettlements((epoch) => {
      void refresh(false, epoch);
    });
    return () => {
      unsubscribe();
      decisionsRefreshGenerationRef.current += 1;
      listRefreshGenerationRef.current += 1;
    };
  }, [refresh]);

  // Load the server list on mount, and again when the dialog closes: it can be opened from the
  // chord as well as from this menu.
  useEffect(() => {
    if (dialogOpen) return;
    let cancelled = false;
    queueMicrotask(() => {
      if (!cancelled) void refresh();
    });
    return () => {
      cancelled = true;
    };
  }, [refresh, dialogOpen]);

  const presets: readonly McpPreset[] = decisionsUrl
    ? [
        ...MCP_PRESETS,
        {
          id: "unsloth-decisions",
          displayName: "Unsloth Decisions",
          url: decisionsUrl,
        },
      ]
    : MCP_PRESETS;
  const enabledUrls = new Set(
    servers.filter((s) => s.is_enabled).map((s) => normalizeMcpUrl(s.url)),
  );
  const customServers = servers.filter(
    (s) =>
      !s.builtin_id &&
      normalizeMcpUrl(s.url) !== "studio:decisions" &&
      normalizeMcpUrl(s.url) !== "studio:tools" &&
      !presets.some((p) => normalizeMcpUrl(p.url) === normalizeMcpUrl(s.url)),
  );
  const blenderEnabled = servers.some(
    (server) => server.builtin_id === "blender" && server.is_enabled,
  );
  const enabledCount = servers.filter(
    (s) =>
      s.is_enabled &&
      (decisionsUrl !== null || normalizeMcpUrl(s.url) !== "studio:decisions"),
  ).length;
  const active = usable && mcpEnabledForChat && enabledCount > 0;

  async function toggleServer(args: {
    url: string;
    displayName: string;
    checked: boolean;
    existing?: McpServerConfig;
    disablesWebSearch?: boolean;
  }) {
    const norm = normalizeMcpUrl(args.url);
    if (pendingUrlsRef.current.has(norm)) return;
    pendingUrlsRef.current.add(norm);
    setPendingUrls(new Set(pendingUrlsRef.current));
    try {
      if (args.checked) {
        if (args.existing) {
          if (!args.existing.is_enabled) {
            applyServer(
              await updateMcpServer(args.existing.id, { isEnabled: true }),
            );
          }
        } else {
          applyServer(
            await createMcpServer({
              displayName: args.displayName,
              url: args.url,
              isEnabled: true,
            }),
          );
        }
        setMcpEnabledForChat(true);
        if (args.disablesWebSearch) setToolsEnabled(false);
      } else if (args.existing) {
        applyServer(
          await updateMcpServer(args.existing.id, { isEnabled: false }),
        );
      }
    } catch (err) {
      toast.error("Failed to update MCP server", {
        description: err instanceof Error ? err.message : String(err),
      });
    } finally {
      pendingUrlsRef.current.delete(norm);
      setPendingUrls(new Set(pendingUrlsRef.current));
    }
  }

  const renderRow = (opts: {
    key: string;
    label: string;
    url: string;
    displayName: string;
    enabled: boolean;
    existing?: McpServerConfig;
    hint?: string;
    disablesWebSearch?: boolean;
  }) => (
    <DropdownMenuItem
      key={opts.key}
      disabled={!serversLoaded || pendingUrls.has(normalizeMcpUrl(opts.url))}
      onSelect={(e) => {
        e.preventDefault();
        void toggleServer({
          url: opts.url,
          displayName: opts.displayName,
          checked: !opts.enabled,
          existing: opts.existing,
          disablesWebSearch: opts.disablesWebSearch,
        });
      }}
      onPointerEnter={opts.hint ? () => setHintKey(opts.key) : undefined}
      onPointerLeave={
        opts.hint
          ? () => setHintKey((k) => (k === opts.key ? null : k))
          : undefined
      }
      className={
        opts.enabled ? "relative text-primary font-medium" : "relative"
      }
    >
      <span className="truncate">{opts.label}</span>
      {opts.enabled ? (
        <HugeiconsIcon icon={MenuTickIcon} strokeWidth={2} className="ml-auto" />
      ) : null}
      {opts.hint ? (
        <Tooltip open={hintKey === opts.key}>
          <TooltipTrigger asChild={true}>
            <span
              aria-hidden={true}
              className="pointer-events-none absolute inset-y-0 right-0 w-0"
            />
          </TooltipTrigger>
          <TooltipContent side="right">{opts.hint}</TooltipContent>
        </Tooltip>
      ) : null}
    </DropdownMenuItem>
  );

  return (
    <>
      <DropdownMenu
        open={menuOpen}
        onOpenChange={(open) => {
          setMenuOpen(open);
          if (open) void refresh();
        }}
      >
        <DropdownMenuTrigger asChild={true}>
          <button
            type="button"
            className={`composer-pill-btn ${usable ? "" : "opacity-40"}`}
            data-pill-label="MCP"
            data-active={active ? "true" : "false"}
            aria-label={
              usable
                ? "MCP servers"
                : "MCP servers, unavailable for the loaded model"
            }
          >
            <span
              role="button"
              aria-label="Turn off MCP"
              tabIndex={-1}
              onPointerDown={(e) => {
                if (e.currentTarget.closest('[data-pill-compact="true"]'))
                  return;
                e.stopPropagation();
              }}
              onClick={(e) => {
                if (e.currentTarget.closest('[data-pill-compact="true"]'))
                  return;
                e.stopPropagation();
                setMcpEnabledForChat(false);
              }}
              className="composer-pill-glyph cursor-pointer"
            >
              <HugeiconsIcon
                icon={McpServerIcon}
                className="size-[calc(15px*var(--ui-space-scale,1))]"
                strokeWidth={2}
              />
              <HugeiconsIcon icon={Cancel01Icon} className="composer-pill-x" />
            </span>
            <span>MCP</span>
            <HugeiconsIcon icon={ChevronDownStandardIcon} strokeWidth={1.5} className="composer-pill-caret size-[calc(15px*var(--ui-space-scale,1))]" />
          </button>
        </DropdownMenuTrigger>
        <DropdownMenuContent
          side={side}
          align="start"
          sideOffset={0}
          avoidCollisions={true}
          className="unsloth-plus-menu mcp-menu w-[calc(232px*var(--ui-space-scale,1))]"
        >
          <DropdownMenuLabel>MCP Servers</DropdownMenuLabel>
          {usable ? null : (
            <DropdownMenuLabel className="text-muted-foreground text-xs font-normal">
              The loaded model cannot use MCP tools
            </DropdownMenuLabel>
          )}
          {presets.map((preset) => {
            const norm = normalizeMcpUrl(preset.url);
            return renderRow({
              key: preset.id,
              label: preset.label ?? preset.displayName,
              url: preset.url,
              displayName: preset.displayName,
              enabled: enabledUrls.has(norm),
              existing: servers.find((s) => normalizeMcpUrl(s.url) === norm),
              hint: preset.hint,
              disablesWebSearch: preset.disablesWebSearch,
            });
          })}
          <DropdownMenuItem
            onSelect={() => {
              setMenuOpen(false);
              setBlenderOpen(true);
            }}
            className={blenderEnabled ? "relative text-primary font-medium" : "relative"}
          >
            <span className="truncate">Blender</span>
            {blenderEnabled ? (
              <HugeiconsIcon icon={MenuTickIcon} strokeWidth={2} className="ml-auto" />
            ) : null}
          </DropdownMenuItem>
          {customServers.length > 0 ? <DropdownMenuSeparator /> : null}
          {customServers.map((server) =>
            renderRow({
              key: server.id,
              label: server.display_name,
              url: server.url,
              displayName: server.display_name,
              enabled: server.is_enabled,
              existing: server,
            }),
          )}
          <DropdownMenuSeparator />
          <DropdownMenuItem
            onSelect={() => {
              setMenuOpen(false);
              setDialogOpen(true);
            }}
          >
            Manage MCP servers
          </DropdownMenuItem>
        </DropdownMenuContent>
      </DropdownMenu>
      <Dialog
        open={blenderOpen}
        onOpenChange={(open) => {
          if (!open && blenderBusy) return;
          setBlenderOpen(open);
        }}
      >
        <DialogContent
          className="max-w-lg max-h-[85dvh] overflow-y-auto"
          showCloseButton={!blenderBusy}
        >
          <DialogHeader>
            <DialogTitle>Blender MCP</DialogTitle>
            <DialogDescription>
              Let the model control Blender through MCP.
            </DialogDescription>
          </DialogHeader>
          {blenderOpen && (
            <BlenderMcpSetup
              servers={servers}
              disabled={false}
              onBusyChange={setBlenderBusy}
            />
          )}
        </DialogContent>
      </Dialog>
    </>
  );
}

/** Mounted independently so its shortcut works while MCP is disabled. */
export function McpServersDialogMount() {
  const open = useMcpServersDialogStore((s) => s.open);
  const setOpen = useMcpServersDialogStore((s) => s.setOpen);
  const chatActive = useChatActive();
  useShortcut("openMcpServers", () => setOpen(true), { enabled: chatActive });
  useEffect(() => {
    if (!chatActive && open) setOpen(false);
  }, [chatActive, open, setOpen]);
  useEffect(() => {
    return () => useMcpServersDialogStore.getState().setOpen(false);
  }, []);
  return (
    <ChatMcpServersDialog open={chatActive && open} onOpenChange={setOpen} />
  );
}