'use client'; import { useEffect, useState } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { apiFetch } from '@/lib/api'; import { Button } from '@/components/ui/button'; import { Input, Label, Textarea } from '@/components/input'; import { StreamingLogs } from '@/components/streaming-logs'; import { InstallSnippets } from '@/components/install-snippets'; import { CodeBlock } from '@/components/code-block'; import { Loader2, RotateCcw, X } from 'lucide-react'; const EXAMPLE_PROMPTS = [ { label: 'Echo / demo (no external API)', text: 'Build a simple echo server with two tools: echo (string in, same string back) and now (returns current UTC timestamp). No external API needed.', }, { label: 'Notion search', text: 'Search and read pages from our Notion workspace via the Notion API. Tools: search_pages(query), get_page_content(page_id). Auth: NOTION_API_KEY.', }, { label: 'Postgres reader', text: 'Read-only Postgres reader for the users and orders tables at db.example.com. One tool per table: list_users(limit), list_orders(limit, customer_id?). Auth: DATABASE_URL.', }, { label: 'Wrap our REST API', text: 'Wrap our internal HTTP API at api.acme.com — endpoints /search?q= and /lookup?id=. Tools: search(query), lookup(id). Auth: ACME_API_TOKEN.', }, { label: 'Stripe charges (read-only)', text: 'Stripe charges and customers, read-only. Tools: list_recent_charges(limit), get_customer(customer_id). Auth: STRIPE_SECRET_KEY.', }, ]; type Step = 'prompt' | 'analyzing' | 'confirm' | 'building' | 'done'; interface PreviewTool { name: string; description: string; inputSchema: Record; } interface PreviewResponse { previewId: string; source: 'claude' | 'mock'; spec: { name: string; description?: string; tools: PreviewTool[]; requiredSecrets: string[]; scopes: string[]; }; } interface EditableTool { name: string; description: string; inputSchemaJson: string; schemaError: string | null; } interface EditableSpec { tools: EditableTool[]; requiredSecrets: string[]; } interface BuildResult { serverId: string; publicUrl: string | null; } function specToEditable(spec: PreviewResponse['spec']): EditableSpec { return { tools: spec.tools.map((t) => ({ name: t.name, description: t.description, inputSchemaJson: JSON.stringify(t.inputSchema, null, 2), schemaError: null, })), requiredSecrets: [...spec.requiredSecrets], }; } export default function NewServerPage() { const router = useRouter(); const [step, setStep] = useState('prompt'); const [prompt, setPrompt] = useState(''); const [name, setName] = useState(''); const [slug, setSlug] = useState(''); const [preview, setPreview] = useState(null); const [editable, setEditable] = useState(null); const [secretValues, setSecretValues] = useState>({}); const [error, setError] = useState(null); const [buildId, setBuildId] = useState(null); const [serverId, setServerId] = useState(null); const [result, setResult] = useState(null); const [forkedTemplateId, setForkedTemplateId] = useState(null); const [forkedTemplateTitle, setForkedTemplateTitle] = useState(null); const searchParams = useSearchParams(); const templateSlug = searchParams.get('template'); const trySlug = (n: string) => n.toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 32); // Fork-from-template flow: skip Step 1, jump straight to Step 2 with the template's spec useEffect(() => { if (!templateSlug || preview) return; let cancelled = false; setStep('analyzing'); (async () => { try { const res = await apiFetch<{ previewId: string; templateId: string; template: { slug: string; title: string; shortDescription: string; tools: PreviewTool[]; requiredSecrets: Array<{ key: string; description: string; howToGetUrl?: string; }>; }; }>(`/v1/templates/${templateSlug}/fork`, { method: 'POST', body: '{}' }); if (cancelled) return; setName(res.template.title); setSlug(trySlug(res.template.title)); setPrompt(`Fork of "${res.template.title}" template.`); setForkedTemplateId(res.templateId); setForkedTemplateTitle(res.template.title); setPreview({ previewId: res.previewId, source: 'mock', spec: { name: res.template.title, description: res.template.shortDescription, tools: res.template.tools, requiredSecrets: res.template.requiredSecrets.map((s) => s.key), scopes: [], }, }); setEditable(null); setStep('confirm'); } catch (e) { if (cancelled) return; const detail = (e as { detail?: { error?: string } }).detail; setError(detail?.error ?? (e as Error).message); setStep('prompt'); } })(); return () => { cancelled = true; }; }, [templateSlug, preview]); useEffect(() => { if (preview && !editable) { const e = specToEditable(preview.spec); setEditable(e); const initial: Record = {}; for (const key of e.requiredSecrets) initial[key] = ''; setSecretValues(initial); } }, [preview, editable]); async function analyze() { setError(null); if (prompt.trim().length < 10) { setError('Prompt must be at least 10 characters.'); return; } if (!name || !slug) { setError('Name and slug are required.'); return; } setStep('analyzing'); try { const res = await apiFetch('/v1/servers/preview', { method: 'POST', body: JSON.stringify({ prompt }), }); setPreview(res); setEditable(null); // will re-init via useEffect setStep('confirm'); } catch (e) { const detail = (e as { detail?: { error?: string; detail?: string } }).detail; setError(detail?.detail ?? detail?.error ?? (e as Error).message); setStep('prompt'); } } function updateTool(i: number, patch: Partial) { setEditable((prev) => { if (!prev) return prev; const tools = [...prev.tools]; const cur = tools[i]; if (!cur) return prev; tools[i] = { ...cur, ...patch }; return { ...prev, tools }; }); } function updateToolSchema(i: number, json: string) { let schemaError: string | null = null; try { const parsed = JSON.parse(json); if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) { schemaError = 'Schema must be a JSON object.'; } } catch (e) { schemaError = `JSON parse error: ${(e as Error).message}`; } updateTool(i, { inputSchemaJson: json, schemaError }); } function addSecret() { setEditable((prev) => prev ? { ...prev, requiredSecrets: [...prev.requiredSecrets, ''] } : prev, ); } function removeSecret(idx: number) { setEditable((prev) => { if (!prev) return prev; const removedKey = prev.requiredSecrets[idx]; const next = prev.requiredSecrets.filter((_, i) => i !== idx); const sv = { ...secretValues }; if (removedKey) delete sv[removedKey]; setSecretValues(sv); return { ...prev, requiredSecrets: next }; }); } function updateSecretKey(idx: number, newKey: string) { const cleaned = newKey.toUpperCase().replace(/[^A-Z0-9_]/g, ''); setEditable((prev) => { if (!prev) return prev; const prevKey = prev.requiredSecrets[idx]; const next = [...prev.requiredSecrets]; next[idx] = cleaned; const sv = { ...secretValues }; if (prevKey && prevKey in sv) { sv[cleaned] = sv[prevKey] ?? ''; if (prevKey !== cleaned) delete sv[prevKey]; } setSecretValues(sv); return { ...prev, requiredSecrets: next }; }); } function resetEdits() { if (preview) setEditable(specToEditable(preview.spec)); } async function build() { setError(null); if (!preview || !editable) return; // Validate every schema parses const badTool = editable.tools.find((t) => t.schemaError); if (badTool) { setError(`Fix schema for "${badTool.name}": ${badTool.schemaError}`); return; } // Validate secret keys: non-empty + UPPER_SNAKE_CASE for (const key of editable.requiredSecrets) { if (!key) { setError('Empty secret name. Remove the row or fill it in.'); return; } if (!/^[A-Z][A-Z0-9_]*$/.test(key)) { setError(`Secret name "${key}" must be UPPER_SNAKE_CASE.`); return; } } // Validate filled secret values for present keys const filledSecrets: Record = {}; for (const k of editable.requiredSecrets) { const v = secretValues[k] ?? ''; if (v.trim()) filledSecrets[k] = v; } const missing = editable.requiredSecrets.filter((k) => !filledSecrets[k]); if (missing.length > 0) { setError(`Fill values for: ${missing.join(', ')} — or remove from the list.`); return; } const specEdit = { tools: editable.tools.map((t) => ({ name: t.name, description: t.description, inputSchema: JSON.parse(t.inputSchemaJson), })), requiredSecrets: editable.requiredSecrets, }; try { const res = await apiFetch<{ server: { id: string }; build: { id: string } }>( '/v1/servers', { method: 'POST', body: JSON.stringify({ name, slug, prompt, secrets: filledSecrets, previewId: preview.previewId, // Don't send specEdit when forking — the template's spec + pre-rendered code // are already in the Redis cache. Edits would invalidate the impls. ...(forkedTemplateId ? { templateId: forkedTemplateId } : { specEdit }), }), }, ); setBuildId(res.build.id); setServerId(res.server.id); setStep('building'); } catch (e) { const detail = (e as { detail?: { error?: string; detail?: unknown } }).detail; setError(detail?.error ?? (e as Error).message); } } const stepNumber = step === 'prompt' ? 1 : step === 'analyzing' || step === 'confirm' ? 2 : 3; const hasSchemaErrors = editable?.tools.some((t) => t.schemaError); const editsDirty = preview && editable ? JSON.stringify(specToEditable(preview.spec)) !== JSON.stringify(editable) : false; return (

New MCP server

STEP {stepNumber} / 3
{step === 'prompt' && (