Skip to content

Commit 9dd9825

Browse files
committed
fix(fws): mcp_settings merge array guard, spec-mock production parity, unknown-safe code read
The mcp_settings merge callback now requires a plain object (!Array.isArray), so an existing mcpServers: [] is replaced by the empty stub instead of being preserved and later rejected by McpSettingsSchema; the safeWriteJson spec mock mirrors the production merge contract (only ENOENT and SyntaxError are recoverable, any other read failure rejects before the merge callback runs, with an EACCES regression); the error.code read in the mock uses an unknown-safe type guard instead of an object cast. (CodeRabbit findings on trial #1413).
1 parent 332253a commit 9dd9825

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎src/services/mcp/McpHub.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,16 @@ export class McpHub {
517517
prettyPrint: true,
518518
merge: (existing) => {
519519
const parsed = existing as { mcpServers?: unknown } | null
520-
if (parsed && parsed.mcpServers && typeof parsed.mcpServers === "object") {
520+
// Arrays satisfy `typeof === "object"` but are not a valid
521+
// mcpServers map; preserve only a plain object, otherwise the
522+
// file would be rewritten with a value McpSettingsSchema
523+
// rejects on the next load.
524+
if (
525+
parsed &&
526+
parsed.mcpServers &&
527+
!Array.isArray(parsed.mcpServers) &&
528+
typeof parsed.mcpServers === "object"
529+
) {
521530
return existing
522531
}
523532
return { mcpServers: {} }

‎src/services/mcp/__tests__/McpHub.spec.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,19 @@ vi.mock("../../../utils/safeWriteJson", () => ({
5252
try {
5353
const fs = await import("fs/promises")
5454
existing = JSON.parse(await fs.readFile(filePath, "utf8"))
55-
} catch {
55+
} catch (error) {
56+
// Mirror the production safeWriteJson merge contract: only ENOENT
57+
// and SyntaxError are recoverable; an EACCES or I/O failure must
58+
// reject before the merge callback runs.
59+
// unknown-safe narrowing: no cast on the caught value (the "in"
60+
// check narrows to object & Record<"code", unknown>).
61+
const code =
62+
error && typeof error === "object" && "code" in error && typeof error.code === "string"
63+
? error.code
64+
: undefined
65+
if (!(error instanceof SyntaxError) && code !== "ENOENT") {
66+
throw error
67+
}
5668
existing = null
5769
}
5870
value = options.merge(existing, data)
@@ -312,6 +324,41 @@ describe("McpHub", () => {
312324
const [, writtenData] = vi.mocked(fs.writeFile).mock.calls[0]
313325
expect(JSON.parse(writtenData as string)).toEqual({ mcpServers: {} })
314326
})
327+
328+
it("writes the default stub when the existing mcpServers value is an array", async () => {
329+
const settingsPath = path.join("/mock/settings/path", "mcp_settings.json")
330+
331+
// Arrays satisfy `typeof === "object"`; an mcpServers map must be a
332+
// plain object, so an array is invalid and replaced by the stub
333+
// instead of being preserved and rejected by McpSettingsSchema later.
334+
vi.mocked(fs.access).mockRejectedValueOnce(
335+
Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" }),
336+
)
337+
vi.mocked(fs.readFile).mockResolvedValueOnce(JSON.stringify({ mcpServers: [] }))
338+
339+
await mcpHub.getMcpSettingsFilePath()
340+
341+
expect(fs.writeFile).toHaveBeenCalledTimes(1)
342+
const [, writtenData] = vi.mocked(fs.writeFile).mock.calls[0]
343+
expect(JSON.parse(writtenData as string)).toEqual({ mcpServers: {} })
344+
})
345+
346+
it("rejects creation when the locked read fails with an I/O error (EACCES)", async () => {
347+
const settingsPath = path.join("/mock/settings/path", "mcp_settings.json")
348+
349+
vi.mocked(fs.access).mockRejectedValueOnce(
350+
Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" }),
351+
)
352+
// The locked read fails with a real I/O error (not ENOENT): the
353+
// safeWriteJson mock mirrors the production contract — reject before
354+
// the merge callback runs instead of treating the file as absent.
355+
vi.mocked(fs.readFile).mockRejectedValueOnce(
356+
Object.assign(new Error("EACCES: permission denied"), { code: "EACCES" }),
357+
)
358+
359+
await expect(mcpHub.getMcpSettingsFilePath()).rejects.toThrow("EACCES: permission denied")
360+
expect(fs.writeFile).not.toHaveBeenCalled()
361+
})
315362
})
316363

317364
describe("Discriminated union type handling", () => {

0 commit comments

Comments
 (0)