The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(mcp): handle empty JSON in editor gracefully
Steven Enamakel9fd3ca6a86d3
diff --git a/app/src/components/channels/mcp/McpJsonEditor.tsx b/app/src/components/channels/mcp/McpJsonEditor.tsxnew file mode 100644index 0000000000..ebef174ad3--- /dev/null+++ b/app/src/components/channels/mcp/McpJsonEditor.tsx@@ -0,0 +1,226 @@+/**+ * `mcp.json` — the user's MCP servers as one editable document.+ *+ * The second tab of the MCP page, beside the server rows. Both write the+ * *same* store through the same core, so this is not an import/export format+ * that drifts from the real configuration: it is the configuration, in the+ * spelling a user already knows from `claude_desktop_config.json`. Pasting a+ * block of servers copied off a server's install page is one action here,+ * which is the whole reason a text surface earns its place — and the only way+ * a server is added, since the registry is browse-only.+ *+ * Three things this deliberately does *not* do:+ *+ * - **Guess at the core's rules.** Only JSON-ness and the document's shape are+ * checked locally (see `mcpJson.ts`); a refusal from the core is shown+ * verbatim, because the core is the authority on what it will dial.+ * - **Show a credential.** The read carries `envKeys` and `authConfigured`,+ * never a value, so an entry with no `env` is not a server with no+ * credential — it is a credential this surface cannot show. Saving such an+ * entry leaves the stored value alone rather than clearing it, which is+ * stated on screen because the opposite guess is the destructive one.+ * - **Reformat as you type.** The text is the user's until they press Revert.+ */+import debug from 'debug';+import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react';++import { useT } from '../../../lib/i18n/I18nContext';+import { mcpClientsApi } from '../../../services/api/mcpClientsApi';+import Button from '../../ui/Button';+import TextArea from '../../ui/TextArea';+import { Alert, AlertDescription, AlertTitle } from '../../ui';+import { formatMcpConfig, mcpConfigChanged, parseMcpConfig } from './mcpJson';+import type { McpConfigDoc, McpConfigWriteResult } from './types';++const log = debug('mcp-clients:json');++interface McpJsonEditorProps {+ /** Called after a successful save, so the rows re-read what was written. */+ onSaved?: (result: McpConfigWriteResult) => void;+}++type Load = 'loading' | 'ready' | 'error';++const McpJsonEditor = ({ onSaved }: McpJsonEditorProps) => {+ const { t } = useT();+ const textareaId = useId();+ const [load, setLoad] = useState<Load>('loading');+ const [loaded, setLoaded] = useState<McpConfigDoc | null>(null);+ const [text, setText] = useState('');+ const [saving, setSaving] = useState(false);+ // The core's own refusal, kept until the next save rather than toasted: it+ // names an entry in a document still on screen, and a toast that names+ // `notion` is gone by the time the user finds `notion`.+ const [refusal, setRefusal] = useState<string | null>(null);+ const [lastWrite, setLastWrite] = useState<McpConfigWriteResult | null>(null);+ // Only the newest read may write its answer.+ const generation = useRef(0);++ const refresh = useCallback(async () => {+ const mine = ++generation.current;+ setLoad('loading');+ try {+ const doc = await mcpClientsApi.configGet();+ if (generation.current !== mine) return;+ log('loaded %d servers', Object.keys(doc.mcpServers).length);+ setLoaded(doc);+ setText(formatMcpConfig(doc));+ setLoad('ready');+ } catch (err) {+ if (generation.current !== mine) return;+ // We do not know what the user has, and an empty editor would invite a+ // save that wipes it.+ log('load failed: %s', err instanceof Error ? err.message : err);+ setLoad('error');+ }+ }, []);++ useEffect(() => {+ void refresh();+ }, [refresh]);++ const parsed = useMemo(() => parseMcpConfig(text), [text]);+ const changed = useMemo(() => mcpConfigChanged(text, loaded), [text, loaded]);++ const parseMessage = useMemo(() => {+ if (parsed.ok) return null;+ return t(`mcp.json.parseError.${parsed.code}`)+ .replace('{name}', parsed.name ?? '')+ .replace('{detail}', parsed.detail ?? '');+ }, [parsed, t]);++ const handleSave = useCallback(async () => {+ if (!parsed.ok || saving) return;+ setSaving(true);+ setRefusal(null);+ try {+ const result = await mcpClientsApi.configSet(parsed.doc);+ const next: McpConfigDoc = { mcpServers: result.mcpServers };+ setLoaded(next);+ setText(formatMcpConfig(next));+ setLastWrite(result);+ onSaved?.(result);+ } catch (err) {+ setRefusal(err instanceof Error ? err.message : t('mcp.json.saveFailed'));+ } finally {+ setSaving(false);+ }+ }, [parsed, saving, onSaved, t]);++ const handleRevert = useCallback(() => {+ if (loaded) setText(formatMcpConfig(loaded));+ setRefusal(null);+ }, [loaded]);++ if (load === 'loading') {+ return (+ <div className="py-10 text-center text-sm text-content-faint" data-testid="mcp-json-loading">+ {t('mcp.json.loading')}+ </div>+ );+ }++ if (load === 'error') {+ return (+ <Alert variant="destructive" data-testid="mcp-json-error">+ <AlertTitle>{t('mcp.json.loadFailedTitle')}</AlertTitle>+ <AlertDescription>+ {t('mcp.json.loadFailedBody')}{' '}+ <Button variant="tertiary" size="xs" onClick={() => void refresh()}>+ {t('common.retry')}+ </Button>+ </AlertDescription>+ </Alert>+ );+ }++ return (+ <div className="space-y-3" data-testid="mcp-json-editor">+ <p className="text-xs text-content-muted">{t('mcp.json.intro')}</p>++ <Alert density="compact" role={undefined}>+ <AlertDescription>{t('mcp.json.credentialsNote')}</AlertDescription>+ </Alert>++ <TextArea+ id={textareaId}+ aria-label={t('mcp.json.editorAria')}+ data-testid="mcp-json-textarea"+ value={text}+ onChange={e => setText(e.target.value)}+ spellCheck={false}+ invalid={!parsed.ok}+ rows={18}+ className="font-mono text-xs leading-relaxed"+ />++ {parseMessage && (+ <p className="text-xs text-coral-700 dark:text-coral-300" data-testid="mcp-json-parse-error">+ {parseMessage}+ </p>+ )}++ {refusal && (+ <Alert variant="destructive" density="compact" data-testid="mcp-json-refusal">+ <AlertTitle>{t('mcp.json.refusedTitle')}</AlertTitle>+ <AlertDescription>{refusal}</AlertDescription>+ </Alert>+ )}++ {lastWrite && !changed && !refusal && (+ <p className="text-xs text-content-muted" data-testid="mcp-json-saved">+ {t('mcp.json.saved')+ .replace('{added}', String(lastWrite.added.length))+ .replace('{updated}', String(lastWrite.updated.length))+ .replace('{removed}', String(lastWrite.removed.length))}+ </p>+ )}++ <div className="flex items-center gap-2">+ <Button+ variant="primary"+ size="sm"+ disabled={!parsed.ok || !changed || saving}+ onClick={() => void handleSave()}+ data-testid="mcp-json-save">+ {saving ? t('mcp.json.saving') : t('mcp.json.save')}+ </Button>+ <Button+ variant="secondary"+ size="sm"+ disabled={!changed || saving}+ onClick={handleRevert}+ data-testid="mcp-json-revert">+ {t('mcp.json.revert')}+ </Button>+ </div>++ <details className="text-xs text-content-muted">+ <summary className="cursor-pointer select-none">{t('mcp.json.exampleTitle')}</summary>+ <pre className="mt-2 overflow-x-auto rounded-lg border border-line bg-surface-muted px-3 py-2 font-mono text-[11px] leading-relaxed text-content">+ {EXAMPLE}+ </pre>+ </details>+ </div>+ );+};++const EXAMPLE = `{+ "mcpServers": {+ "filesystem": {+ "command": "npx",+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "~/Documents"]+ },+ "github": {+ "command": "npx",+ "args": ["-y", "@modelcontextprotocol/server-github"],+ "env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_…" }+ },+ "hosted": {+ "url": "https://mcp.example.com/mcp",+ "headers": { "Authorization": "Bearer …" }+ }+ }+}`;++export default McpJsonEditor;