From 88f5edc2365da92673c184ad0d2a9b218ba22f91 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 12 Jul 2026 09:09:26 +0800 Subject: [PATCH] fix(setup): preserve invalid MCP configuration --- src/application/Setup.ts | 15 +++++++++------ tests/setupConfig.test.ts | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/application/Setup.ts b/src/application/Setup.ts index 47e9d1587..271908a7a 100644 --- a/src/application/Setup.ts +++ b/src/application/Setup.ts @@ -197,7 +197,12 @@ export const configureJsonClient = async ( } catch (cause: unknown) { if (!isMissing(cause)) return { status: "failed", reason: "readback" }; } - const servers = objectValue(document.mcpServers); + let servers: Record; + try { + servers = parseOptionalObject(document.mcpServers); + } catch { + return { status: "failed", reason: "readback" }; + } const desired = { command: "npx", args: ["-y", PRODUCT_IDENTITY.packageName, "mcp"], @@ -232,7 +237,7 @@ export const configureJsonClient = async ( } try { const readback = parseObject(await readFile(client.configPath, "utf8")); - const value = objectValue(readback.mcpServers)[ + const value = parseOptionalObject(readback.mcpServers)[ PRODUCT_IDENTITY.mcpServerKey ]; if (JSON.stringify(value) !== JSON.stringify(desired)) { @@ -303,10 +308,8 @@ const exists = async (path: string): Promise => { } }; const objectSchema = z.record(z.string(), z.unknown()); -const objectValue = (value: unknown): Record => { - const parsed = objectSchema.safeParse(value); - return parsed.success ? parsed.data : {}; -}; +const parseOptionalObject = (value: unknown): Record => + value === undefined ? {} : objectSchema.parse(value); const parseObject = (text: string): Record => objectSchema.parse(JSON.parse(text)); const isMissing = (cause: unknown): boolean => diff --git a/tests/setupConfig.test.ts b/tests/setupConfig.test.ts index 4836ee2ef..048bd1449 100644 --- a/tests/setupConfig.test.ts +++ b/tests/setupConfig.test.ts @@ -75,4 +75,21 @@ describe("JSON client configuration transaction", () => { ).rejects.toThrow(); }, ); + + it.each(["null", "[]", '"value"'])( + "preserves a non-object mcpServers value %s", + async (servers) => { + directory = await mkdtemp(join(tmpdir(), "rea-setup-")); + const configPath = join(directory, "mcp.json"); + const original = `{"mcpServers":${servers}}`; + await writeFile(configPath, original); + expect(await configureJsonClient({ name: "cursor", configPath })).toEqual( + { status: "failed", reason: "readback" }, + ); + expect(await readFile(configPath, "utf8")).toBe(original); + await expect( + readFile(`${configPath}.rea.backup`, "utf8"), + ).rejects.toThrow(); + }, + ); });