fix(setup): treat empty client config files as new documents (#1117)

* fix(setup): treat empty client config files as new documents

An empty MCP config file (for example Antigravity's mcp_config.json, which
the client can create with zero bytes) was reported as malformed, which
blocked rea setup from configuring every other detected agent. Parse a
whitespace- or BOM-only JSON/JSONC file as an empty document and write a
fresh document over it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(setup): use explicit unicode escape for BOM-only empty config case

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
lalo adrian morales
2026-10-09 06:30:46 +08:00
committed by GitHub
co-authored by Claude Opus 5.5 morluto
parent 567a82f8d4
commit 9fd65ce071
2 changed files with 47 additions and 1 deletions
+17 -1
View File
@@ -80,12 +80,22 @@ export const clientConfigurationServersKey = (
}
};
/**
* Whether raw configuration text holds no settings to preserve. Some clients
* create a zero-byte config file before any server is added.
*/
const isEmptyClientConfigurationText = (text: string): boolean =>
text.replace(/^\uFEFF/, "").trim() === "";
const parseDocument = (
text: string,
format: ClientConfigurationFormat,
): Record<string, unknown> => {
if (format === "toml" || format === "grok")
return objectSchema.parse(parseToml(text));
// An empty file, which some clients create before any server is added,
// holds no settings to preserve.
if (isEmptyClientConfigurationText(text)) return {};
const errors: ParseError[] = [];
// Accept a UTF-8 BOM without shifting diagnostics or editing the original text.
const jsonText = text.startsWith("\uFEFF") ? ` ${text.slice(1)}` : text;
@@ -1148,7 +1158,13 @@ export const serializeClientConfiguration = (
if (format === "toml") return stringifyToml(document);
if (format === "grok")
return serializeGrokConfiguration(document, originalText);
if (originalText !== undefined && editedPaths !== undefined)
// An empty original holds no comments or settings to preserve. Writing a
// fresh document also keeps a lone BOM from shifting past the closing brace.
if (
originalText !== undefined &&
!isEmptyClientConfigurationText(originalText) &&
editedPaths !== undefined
)
return editedPaths.reduce((text, path) => {
const value = valueAt(document, path);
if (value === undefined) {