| 1 | import { useEffect, useState } from 'react'; |
| 2 | import { X, Plus, Trash2, Server, Check, ExternalLink } from 'lucide-react'; |
| 3 | import { api } from '../api.js'; |
| 4 | |
| 5 | export default function McpModal({ agentId, onClose }) { |
| 6 | const [registry, setRegistry] = useState([]); |
| 7 | const [active, setActive] = useState({}); // mcpServers from agent's mcp.json |
| 8 | const [picking, setPicking] = useState(null); // registry entry being configured |
| 9 | const [values, setValues] = useState({}); |
| 10 | |
| 11 | async function reload() { |
| 12 | const [r, mcp] = await Promise.all([ |
| 13 | api('/api/mcp/registry'), |
| 14 | api(`/api/mcp/${agentId}`), |
| 15 | ]); |
| 16 | setRegistry(r); |
| 17 | setActive(mcp.mcpServers || {}); |
| 18 | } |
| 19 | useEffect(() => { reload().catch(() => {}); }, [agentId]); |
| 20 | |
| 21 | async function add() { |
| 22 | if (!picking) return; |
| 23 | try { |
| 24 | await api(`/api/mcp/${agentId}/servers`, { |
| 25 | method: 'POST', |
| 26 | body: JSON.stringify({ name: picking.name, values }), |
| 27 | }); |
| 28 | setPicking(null); setValues({}); |
| 29 | reload(); |
| 30 | } catch (e) { alert(e.message); } |
| 31 | } |
| 32 | |
| 33 | async function remove(name) { |
| 34 | if (!confirm(`Quitar MCP server "${name}"?`)) return; |
| 35 | await api(`/api/mcp/${agentId}/servers/${name}`, { method: 'DELETE' }); |
| 36 | reload(); |
| 37 | } |
| 38 | |
| 39 | function startPicking(entry) { |
| 40 | setPicking(entry); |
| 41 | setValues({}); |
| 42 | } |
| 43 | |
| 44 | return ( |
| 45 | <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4" onClick={onClose}> |
| 46 | <div className="bg-surface-1 border border-border-subtle rounded-xl w-full max-w-3xl max-h-[88vh] flex flex-col" onClick={e => e.stopPropagation()}> |
| 47 | <header className="px-5 py-3 border-b border-border-subtle flex items-center justify-between"> |
| 48 | <div className="flex items-center gap-2"> |
| 49 | <Server size={14} className="text-violet-300" /> |
| 50 | <h2 className="text-sm font-semibold text-slate-100"> |
| 51 | Tools (MCP servers) · <span className="font-mono">{agentId}</span> |
| 52 | </h2> |
| 53 | </div> |
| 54 | <button onClick={onClose} className="text-slate-500 hover:text-slate-200"> |
| 55 | <X size={16} /> |
| 56 | </button> |
| 57 | </header> |
| 58 | |
| 59 | <div className="flex-1 overflow-y-auto p-4 space-y-4"> |
| 60 | {/* Active servers */} |
| 61 | <div> |
| 62 | <h3 className="text-[11px] uppercase tracking-wider text-slate-500 mb-2">Equipados</h3> |
| 63 | {Object.keys(active).length === 0 ? ( |
| 64 | <div className="text-xs text-slate-600">Ninguno todavía. Añade abajo.</div> |
| 65 | ) : ( |
| 66 | <div className="space-y-1.5"> |
| 67 | {Object.entries(active).map(([name, cfg]) => ( |
| 68 | <div key={name} className="flex items-center justify-between bg-surface-2/50 rounded-lg px-3 py-2 text-sm"> |
| 69 | <div className="flex items-center gap-2"> |
| 70 | <Check size={11} className="text-emerald-400" /> |
| 71 | <span className="font-mono text-slate-200">{name}</span> |
| 72 | <span className="text-[11px] text-slate-500 truncate">{cfg.command} {(cfg.args || []).slice(0, 2).join(' ')}</span> |
| 73 | </div> |
| 74 | <button onClick={() => remove(name)} className="text-rose-300 hover:text-rose-200"> |
| 75 | <Trash2 size={12} /> |
| 76 | </button> |
| 77 | </div> |
| 78 | ))} |
| 79 | </div> |
| 80 | )} |
| 81 | </div> |
| 82 | |
| 83 | {/* Pick form */} |
| 84 | {picking && ( |
| 85 | <div className="card border-violet/40 space-y-3"> |
| 86 | <div className="flex items-center justify-between"> |
| 87 | <div> |
| 88 | <div className="text-sm font-semibold text-slate-100">Configurar {picking.label}</div> |
| 89 | <div className="text-[11px] text-slate-500">{picking.description}</div> |
| 90 | </div> |
| 91 | <button onClick={() => setPicking(null)} className="text-slate-500 hover:text-slate-200"> |
| 92 | <X size={14} /> |
| 93 | </button> |
| 94 | </div> |
| 95 | {(picking.fields || []).map(f => ( |
| 96 | <div key={f.key}> |
| 97 | <label className="text-[11px] uppercase tracking-wider text-slate-500"> |
| 98 | {f.label} {f.required && <span className="text-rose-400">*</span>} |
| 99 | </label> |
| 100 | <input |
| 101 | type={f.secret ? 'password' : 'text'} |
| 102 | placeholder={f.placeholder} |
| 103 | value={values[f.key] || ''} |
| 104 | onChange={e => setValues(v => ({ ...v, [f.key]: e.target.value }))} |
| 105 | className="input mt-1 font-mono text-sm" |
| 106 | /> |
| 107 | {f.help && <p className="text-[10px] text-slate-600 mt-0.5">{f.help}</p>} |
| 108 | </div> |
| 109 | ))} |
| 110 | {picking.docs && ( |
| 111 | <a href={picking.docs} target="_blank" rel="noreferrer" className="text-[11px] text-violet-300 hover:underline inline-flex items-center gap-1"> |
| 112 | <ExternalLink size={10} /> Docs |
| 113 | </a> |
| 114 | )} |
| 115 | <div className="flex justify-end gap-2"> |
| 116 | <button className="btn-ghost text-xs" onClick={() => setPicking(null)}>Cancelar</button> |
| 117 | <button className="btn-primary text-xs" onClick={add}> |
| 118 | <Plus size={11} /> Añadir |
| 119 | </button> |
| 120 | </div> |
| 121 | </div> |
| 122 | )} |
| 123 | |
| 124 | {/* Registry grid */} |
| 125 | <div> |
| 126 | <h3 className="text-[11px] uppercase tracking-wider text-slate-500 mb-2">Disponibles</h3> |
| 127 | <div className="grid grid-cols-1 sm:grid-cols-2 gap-2"> |
| 128 | {registry.map(e => { |
| 129 | const installed = !!active[e.name]; |
| 130 | return ( |
| 131 | <button key={e.name} |
| 132 | onClick={() => !installed && startPicking(e)} |
| 133 | disabled={installed} |
| 134 | className={`text-left px-3 py-2.5 rounded-lg border transition ${ |
| 135 | installed |
| 136 | ? 'border-emerald-500/30 bg-emerald-500/5 cursor-default' |
| 137 | : 'border-border-subtle hover:bg-surface-2' |
| 138 | }`}> |
| 139 | <div className="flex items-center gap-2 mb-0.5"> |
| 140 | <span className="text-sm font-medium text-slate-100">{e.label}</span> |
| 141 | <span className="pill bg-slate-800 text-slate-500 border border-slate-700 text-[10px]">{e.category}</span> |
| 142 | {installed && <Check size={11} className="text-emerald-400 ml-auto" />} |
| 143 | </div> |
| 144 | <div className="text-[11px] text-slate-500 leading-snug">{e.description}</div> |
| 145 | </button> |
| 146 | ); |
| 147 | })} |
| 148 | </div> |
| 149 | </div> |
| 150 | </div> |
| 151 | </div> |
| 152 | </div> |
| 153 | ); |
| 154 | } |