import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useTranslation } from "react-i18next"; import { useState, useEffect } from "react"; import { Loader2, Server, Save, Copy } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { apiRequest } from "@/lib/queryClient"; export function McpSettingsCard() { const { t } = useTranslation(); const { toast } = useToast(); const queryClient = useQueryClient(); const [formData, setFormData] = useState({ mcp_enabled: false, mcp_port: "5001" // Default to main app port unless specific override logic is added }); // Feature 1: Get User for API Key const { data: user } = useQuery({ queryKey: ['/api/user'] }); // Feature 2: API Key Mutations const generateApiKeyMutation = useMutation({ mutationFn: async () => { const res = await apiRequest("POST", "/api/user/apikey", {}); return res.json(); }, onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["/api/user"] }); toast({ title: t('settings.mcp.generated') }); }, }); const revokeApiKeyMutation = useMutation({ mutationFn: async () => { await apiRequest("DELETE", "/api/user/apikey"); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["/api/user"] }); toast({ title: t('settings.mcp.revoked') }); }, }); const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text); toast({ title: t('settings.mcp.copied', "Copied!") }); } const { data: settings, isLoading } = useQuery({ queryKey: ['/api/admin/settings'], }); useEffect(() => { if (settings) { setFormData({ mcp_enabled: settings.mcp_enabled === true, mcp_port: settings.mcp_port || "5001" }); } }, [settings]); const mutation = useMutation({ mutationFn: async (data: any) => { const res = await fetch("/api/admin/settings", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }); if (!res.ok) throw new Error("Failed to save settings"); return res.json(); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['/api/admin/settings'] }); toast({ title: t('settings.saved', 'Settings saved') }); }, onError: () => { toast({ title: t('settings.error', 'Failed to save settings'), variant: "destructive" }); } }); const handleSave = () => { mutation.mutate({ mcp_enabled: formData.mcp_enabled, mcp_port: formData.mcp_port }); }; if (isLoading) return
; return (
{t('settings.mcp.title')}
{t('settings.mcp.description')} {t('settings.mcp.descriptionDetail', "The MCP server runs on the same port as the application (/api/mcp).")}
setFormData(prev => ({ ...prev, mcp_enabled: checked }))} />
{/* Status Indicator */}

{t('settings.mcp.status')}

{t('settings.mcp.running')}
{/* Endpoint URL */}

{t('settings.mcp.url')}

{/* API Key Management */}

{t('settings.mcp.apiKey')}

{user?.apiKey ? (
) : (

{t('settings.mcp.noKey')}

)}
{t('settings.mcp.instructions')}
setFormData(prev => ({ ...prev, mcp_port: e.target.value }))} placeholder="5001" disabled className="bg-muted" />

{t('settings.mcp.portDesc')}

); }