import { useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Loader2 } from 'lucide-react' import { Switch } from '@/components/ui/switch' import { useMcpServers } from '@/hooks/useMcpServers' import { settingsApi } from '@/api/settings' import { useMutation, useQueryClient } from '@tanstack/react-query' interface AddMcpServerDialogProps { open: boolean onOpenChange: (open: boolean) => void configName?: string onUpdate?: (configName: string, content: Record) => Promise } interface EnvironmentVariable { key: string value: string } export function AddMcpServerDialog({ open, onOpenChange, onUpdate }: AddMcpServerDialogProps) { const [serverId, setServerId] = useState('') const [serverType, setServerType] = useState<'local' | 'remote'>('local') const [command, setCommand] = useState('') const [url, setUrl] = useState('') const [environment, setEnvironment] = useState([]) const [timeout, setTimeout] = useState('') const [enabled, setEnabled] = useState(true) const queryClient = useQueryClient() const { addServerAsync, isAddingServer } = useMcpServers() const addMcpServerMutation = useMutation({ mutationFn: async () => { const config = await settingsApi.getDefaultOpenCodeConfig() if (!config) throw new Error('No default config found') const currentMcp = (config.content?.mcp as Record) || {} const mcpConfig: Record = { type: serverType, enabled, } if (serverType === 'local') { const commandArray = command.split(' ').filter(arg => arg.trim()) if (commandArray.length === 0) { throw new Error('Command is required for local MCP servers') } mcpConfig.command = commandArray const envVars: Record = {} environment.forEach(env => { if (env.key.trim() && env.value.trim()) { envVars[env.key.trim()] = env.value.trim() } }) if (Object.keys(envVars).length > 0) { mcpConfig.environment = envVars } } else { if (!url.trim()) { throw new Error('URL is required for remote MCP servers') } mcpConfig.url = url.trim() } if (timeout && parseInt(timeout)) { mcpConfig.timeout = parseInt(timeout) } const updatedConfig = { ...config.content, mcp: { ...currentMcp, [serverId]: mcpConfig, }, } await settingsApi.updateOpenCodeConfig(config.name, { content: updatedConfig }) if (enabled) { await addServerAsync({ name: serverId, config: { type: serverType, enabled, command: serverType === 'local' ? command.split(' ').filter(arg => arg.trim()) : undefined, url: serverType === 'remote' ? url.trim() : undefined, environment: serverType === 'local' && Object.keys(environment).length > 0 ? environment.reduce((acc, env) => { if (env.key.trim() && env.value.trim()) { acc[env.key.trim()] = env.value.trim() } return acc }, {} as Record) : undefined, timeout: timeout && parseInt(timeout) ? parseInt(timeout) : undefined, } }) } }, onSuccess: async () => { if (onUpdate) { const config = await settingsApi.getDefaultOpenCodeConfig() if (config) { await onUpdate(config.name, config.content) } } else { queryClient.invalidateQueries({ queryKey: ['opencode-config'] }) } queryClient.invalidateQueries({ queryKey: ['mcp-status'] }) handleClose() }, }) const handleAddEnvironmentVar = () => { setEnvironment([...environment, { key: '', value: '' }]) } const handleRemoveEnvironmentVar = (index: number) => { setEnvironment(environment.filter((_, i) => i !== index)) } const handleUpdateEnvironmentVar = (index: number, field: 'key' | 'value', value: string) => { const updated = [...environment] updated[index][field] = value setEnvironment(updated) } const handleAdd = () => { if (serverId) { addMcpServerMutation.mutate() } } const handleClose = () => { setServerId('') setServerType('local') setCommand('') setUrl('') setEnvironment([]) setTimeout('') setEnabled(true) onOpenChange(false) } const isPending = addMcpServerMutation.isPending || isAddingServer return ( Add MCP Server Configure a new Model Context Protocol server for your OpenCode configuration
setServerId(e.target.value)} placeholder="e.g., filesystem, git, my-server" className="bg-background border-border" />

Unique identifier for this MCP server (lowercase, no spaces)

{serverType === 'local' ? (
setCommand(e.target.value)} placeholder="npx @modelcontextprotocol/server-filesystem /tmp" className="bg-background border-border font-mono" />

Command and arguments to run the MCP server

) : (
setUrl(e.target.value)} placeholder="http://localhost:3000/mcp" className="bg-background border-border font-mono" />

URL of the remote MCP server

)} {serverType === 'local' && (
{environment.map((env, index) => (
handleUpdateEnvironmentVar(index, 'key', e.target.value)} placeholder="API_KEY" className="bg-background border-border font-mono" /> handleUpdateEnvironmentVar(index, 'value', e.target.value)} placeholder="your-api-key-here" className="bg-background border-border font-mono" /> {environment.length > 1 && ( )}
))}

Environment variables to set when running the MCP server

)}
setTimeout(e.target.value)} placeholder="5000" className="bg-background border-border" />

Timeout in milliseconds for fetching tools (default: 5000)

) }