The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): drop malformed mcp_servers entries instead of crashing
Nacho Chiappero13afe9e9e144
diff --git a/apps/desktop/src/lib/mcp-servers.test.ts b/apps/desktop/src/lib/mcp-servers.test.tsnew file mode 100644index 00000000000..f25848f6fc7--- /dev/null+++ b/apps/desktop/src/lib/mcp-servers.test.ts@@ -0,0 +1,28 @@+import { describe, expect, it } from 'vitest'++import { getServers } from './mcp-servers'++describe('getServers', () => {+ it('returns empty when the map is absent or not a map', () => {+ expect(getServers(null)).toEqual({})+ expect(getServers({})).toEqual({})+ expect(getServers({ mcp_servers: null })).toEqual({})+ expect(getServers({ mcp_servers: ['files'] })).toEqual({})+ expect(getServers({ mcp_servers: 'files' })).toEqual({})+ })++ it('passes object entries through untouched', () => {+ const servers = { docs: { url: 'https://example.com/mcp' }, files: { args: ['-y', 'pkg'], command: 'npx' } }++ expect(getServers({ mcp_servers: servers })).toEqual(servers)+ })++ // A key left without a value in config.yaml parses as `null`, and every+ // reader (the `enabled` gate, the transport label, the probe) reaches+ // straight into the entry — so one of these used to take the pane down.+ it('drops non-object entries and keeps their siblings', () => {+ const servers = { broken: null, list: ['a'], scalar: 3, working: { command: 'npx' } }++ expect(getServers({ mcp_servers: servers })).toEqual({ working: { command: 'npx' } })+ })+})diff --git a/apps/desktop/src/lib/mcp-servers.ts b/apps/desktop/src/lib/mcp-servers.tsindex 38bf9046bc6..58db1569ee8 100644--- a/apps/desktop/src/lib/mcp-servers.ts+++ b/apps/desktop/src/lib/mcp-servers.ts@@ -21,9 +21,28 @@ export function normalizeEntry(entry: Record<string, unknown>): Record<string, ureturn entry}-/** The `mcp_servers` map out of a config record, or `{}` when absent/malformed. */+/** A value a reader can reach into: an object, not `null`, an array or a scalar. */+const isEntry = (value: unknown): value is Record<string, unknown> =>+ !!value && typeof value === 'object' && !Array.isArray(value)++/** The `mcp_servers` map out of a config record, or `{}` when absent/malformed.+ *+ * Entries that aren't objects are dropped rather than handed on. Every reader+ * takes properties straight off the value — `enabled` for the runtime gate,+ * `command`/`url` for the transport, `tools` for the filter — so one bad entry+ * throws on the first property read and, with nothing between it and the pane,+ * takes the whole Capabilities workspace down with it. A `name:` left without+ * a value in `config.yaml` parses as `null` and does exactly that. The backend+ * already refuses to *write* a non-object entry (`_replace_mcp_servers`), so+ * this only has to survive a config edited by hand or by another tool. */export function getServers(config: { mcp_servers?: unknown } | null): McpServers {const raw = config?.mcp_servers- return raw && typeof raw === 'object' && !Array.isArray(raw) ? (raw as McpServers) : {}+ if (!isEntry(raw)) {+ return {}+ }++ return Object.fromEntries(+ Object.entries(raw).filter((entry): entry is [string, Record<string, unknown>] => isEntry(entry[1]))+ )}