'use client' // Settings → MCP: Notion-style connections UI for OUTBOUND MCP servers. // Superbrain dials these as a client (internal/mcpclient) so its AI can use // their tools; this surface manages them. House disciplines visible here: // secrets are write-only (never prefilled; the mask round-trips unchanged), // every mutating PATCH carries the row version (409 reloads), and plan or // policy refusals surface the server's message — the UI never guesses. import { useCallback, useEffect, useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Badge } from '@/components/ui/badge' import { Switch } from '@/components/ui/switch' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Loader2, PlugZap, Plus, Trash2, FlaskConical } from 'lucide-react' import { toast } from 'sonner' const KEY_MASK = '••••••••' type McpConnection = { id: string; name: string; url: string; transport: string; authKind: string headerName: string; secret: string; enabled: boolean; version: number } type TestResult = { ok: boolean; count?: number; tools?: { name: string; description: string }[]; error?: string } type FormState = { id: string | null; version: number name: string; url: string; transport: string; authKind: string; headerName: string secret: string; hasStoredSecret: boolean } const emptyForm: FormState = { id: null, version: 0, name: '', url: '', transport: 'streamable-http', authKind: 'none', headerName: '', secret: '', hasStoredSecret: false, } async function api(path: string, init?: RequestInit): Promise { return fetch(path, { headers: { 'Content-Type': 'application/json' }, cache: 'no-store', ...init }) } export function McpSettings() { const [connections, setConnections] = useState([]) const [loading, setLoading] = useState(true) const [form, setForm] = useState(null) const [saving, setSaving] = useState(false) const [testing, setTesting] = useState(null) const [results, setResults] = useState>({}) const reload = useCallback(() => api('/api/mcp/connections') .then(r => r.json()) .then(data => setConnections(Array.isArray(data?.connections) ? data.connections : [])) .catch(() => toast.error('Could not load MCP connections')) .finally(() => setLoading(false)), []) useEffect(() => { reload() }, [reload]) const openCreate = () => setForm({ ...emptyForm }) const openEdit = (c: McpConnection) => setForm({ id: c.id, version: c.version, name: c.name, url: c.url, transport: c.transport, authKind: c.authKind, headerName: c.headerName, secret: '', hasStoredSecret: c.secret === KEY_MASK, }) const save = async () => { if (!form) return const body: Record = { name: form.name.trim(), url: form.url.trim(), transport: form.transport, authKind: form.authKind, headerName: form.headerName.trim(), } // Write-only secret: send only what the user typed; the mask (or an // empty field) tells the server to keep the stored value. if (form.secret) body.secret = form.secret setSaving(true) try { const res = form.id ? await api(`/api/mcp/connections/${form.id}`, { method: 'PATCH', body: JSON.stringify({ ...body, version: form.version }) }) : await api('/api/mcp/connections', { method: 'POST', body: JSON.stringify(body) }) const data = await res.json().catch(() => null) if (res.status === 409) { toast.error('Connection changed elsewhere — reloaded') await reload() setForm(null) return } if (!res.ok) { toast.error(data?.error ?? 'Could not save the connection'); return } toast.success(form.id ? 'Connection updated' : 'Connection added') setForm(null) await reload() } catch { toast.error('Could not save the connection') } finally { setSaving(false) } } const test = async (c: McpConnection) => { setTesting(c.id) setResults(prev => ({ ...prev, [c.id]: undefined as unknown as TestResult })) try { const res = await api(`/api/mcp/connections/${c.id}/test`, { method: 'POST', body: '{}' }) const data = await res.json().catch(() => null) setResults(prev => ({ ...prev, [c.id]: data as TestResult })) if (res.ok && data?.ok) toast.success(`${data.count} tool${data.count === 1 ? '' : 's'} available`) } catch { setResults(prev => ({ ...prev, [c.id]: { ok: false, error: 'Network error' } })) } finally { setTesting(null) } } const toggle = async (c: McpConnection) => { const res = await api(`/api/mcp/connections/${c.id}`, { method: 'PATCH', body: JSON.stringify({ enabled: !c.enabled, version: c.version }), }) if (res.status === 409) { toast.error('Connection changed elsewhere — reloaded'); await reload(); return } if (!res.ok) { toast.error((await res.json().catch(() => null))?.error ?? 'Could not update'); return } await reload() } const remove = async (c: McpConnection) => { const res = await api(`/api/mcp/connections/${c.id}`, { method: 'DELETE' }) if (res.ok) { toast.success(`Removed ${c.name}`); await reload() } else toast.error((await res.json().catch(() => null))?.error ?? 'Could not remove') } if (loading) { return (
Loading MCP connections…
) } return (

MCP connections

Connect Superbrain’s AI to external MCP servers (Streamable HTTP or SSE). Credentials are encrypted at rest and never shown again after saving.

{connections.length === 0 ? (

No MCP connections yet.

) : (
    {connections.map(c => (
  • toggle(c)} aria-label={`Enable ${c.name}`} /> {c.name} {c.transport} {c.authKind !== 'none' && {c.authKind}{c.authKind === 'header' && c.headerName ? `: ${c.headerName}` : ''}}

    {c.url}

    {results[c.id] && ( results[c.id].ok ? (

    {results[c.id].count} tools

      {results[c.id].tools?.map(t => (
    • {t.name}{t.description ? ` — ${t.description.slice(0, 90)}` : ''}
    • ))}
    ) : (

    {results[c.id].error}

    ) )}
  • ))}
)} { if (!open) setForm(null) }}> {form?.id ? 'Edit MCP connection' : 'Add MCP connection'} {form && (
setForm({ ...form, name: e.target.value })} placeholder="e.g. House gateway, Maps tools…" maxLength={100} />
setForm({ ...form, url: e.target.value })} placeholder="https://mcp.example.tld/mcp" />

Public http(s) endpoints on ports 80/443 — outbound dials are network-guarded.

{form.authKind === 'header' && (
setForm({ ...form, headerName: e.target.value })} placeholder="X-API-Key" maxLength={100} />
)} {form.authKind !== 'none' && (
setForm({ ...form, secret: e.target.value })} placeholder={form.hasStoredSecret ? `${KEY_MASK} (leave blank to keep)` : 'Paste the credential'} maxLength={2000} /> {form.hasStoredSecret &&

Stored credentials are never shown; blank keeps the current one.

}
)}
)}
) }