fix(setup): preserve configured MCP server environment (#1607)
* fix(setup): preserve configured MCP server environment * fix(setup): validate retained server environment before writes
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
clientRegistrationEntry,
|
||||
effectiveClientServer,
|
||||
parseClientConfiguration,
|
||||
serializeClientConfiguration,
|
||||
} from "../../../src/application/ClientConfigurationDocument.js";
|
||||
import {
|
||||
clientConfigurationAligned,
|
||||
configureClientConfiguration,
|
||||
inspectClientConfiguration,
|
||||
} from "../../../src/application/SetupClientConfiguration.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
|
||||
|
||||
it.each([
|
||||
["cursor", "json", "mcpServers", "env"],
|
||||
["codex", "toml", "mcp_servers", "env"],
|
||||
["opencode", "opencode", "mcp", "environment"],
|
||||
["hermes", "hermes", "mcp_servers", "env"],
|
||||
] as const)(
|
||||
"preserves %s server environment through command and provider updates",
|
||||
async (name, format, serversKey, environmentKey) => {
|
||||
const root = await createTestTempDirectory("rea-setup-environment-");
|
||||
const configPath = join(root, "config");
|
||||
const client = { name, format, configPath };
|
||||
const custom = {
|
||||
REA_MCP_INPUT_SCHEMA_PROFILE: "compact",
|
||||
REA_MCP_MAX_RESPONSE_BYTES: "33554432",
|
||||
REA_BROWSER_EXECUTABLE: join(root, "custom-browser"),
|
||||
GHIDRA_INSTALL_DIR: join(root, "old-ghidra"),
|
||||
};
|
||||
const original = serializeClientConfiguration(
|
||||
{
|
||||
[serversKey]: {
|
||||
rea: clientRegistrationEntry(format, ["rea-old", "mcp"], custom),
|
||||
other: clientRegistrationEntry(format, ["other"], { OTHER: "keep" }),
|
||||
},
|
||||
},
|
||||
format,
|
||||
);
|
||||
await writeFile(configPath, original);
|
||||
const providers = { GHIDRA_INSTALL_DIR: join(root, "new-ghidra") };
|
||||
const command = ["rea", "mcp"];
|
||||
expect(
|
||||
await inspectClientConfiguration(client, providers, command),
|
||||
).toMatchObject({ status: "update" });
|
||||
expect(
|
||||
await configureClientConfiguration(client, providers, command),
|
||||
).toMatchObject({ status: "configured" });
|
||||
const configured = await readFile(configPath, "utf8");
|
||||
const parsed = parseClientConfiguration(configured, format);
|
||||
expect(effectiveClientServer(parsed, "rea")).toMatchObject({
|
||||
[environmentKey]: { ...custom, ...providers },
|
||||
});
|
||||
expect(effectiveClientServer(parsed, "other")).toEqual(
|
||||
effectiveClientServer(
|
||||
parseClientConfiguration(original, format),
|
||||
"other",
|
||||
),
|
||||
);
|
||||
expect(await readFile(`${configPath}.rea.backup`, "utf8")).toBe(original);
|
||||
expect(await clientConfigurationAligned(client, providers, command)).toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
await inspectClientConfiguration(client, providers, command),
|
||||
).toEqual({ status: "already_current" });
|
||||
expect(
|
||||
await configureClientConfiguration(client, providers, command),
|
||||
).toEqual({ status: "unchanged" });
|
||||
expect(await readFile(configPath, "utf8")).toBe(configured);
|
||||
},
|
||||
);
|
||||
|
||||
it("retains legacy OpenCode environment when migrating into a native server table", async () => {
|
||||
const root = await createTestTempDirectory("rea-opencode-environment-");
|
||||
const configPath = join(root, "opencode.json");
|
||||
const client = { name: "opencode", format: "opencode", configPath } as const;
|
||||
const custom = { REA_MCP_INPUT_SCHEMA_PROFILE: "compact" };
|
||||
await writeFile(
|
||||
configPath,
|
||||
JSON.stringify({
|
||||
mcp: {
|
||||
rea: clientRegistrationEntry("opencode", ["old", "mcp"], custom),
|
||||
servers: {
|
||||
other: clientRegistrationEntry("opencode_v2", ["other"], {}),
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
await configureClientConfiguration(client, {}, ["rea", "mcp"]),
|
||||
).toMatchObject({ status: "configured" });
|
||||
const parsed = parseClientConfiguration(
|
||||
await readFile(configPath, "utf8"),
|
||||
"opencode",
|
||||
);
|
||||
expect(parsed.legacyServers).toEqual({});
|
||||
expect(parsed.servers.rea).toEqual(
|
||||
clientRegistrationEntry("opencode_v2", ["rea", "mcp"], custom),
|
||||
);
|
||||
expect(
|
||||
await configureClientConfiguration(client, {}, ["rea", "mcp"]),
|
||||
).toEqual({ status: "unchanged" });
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
"non-string member",
|
||||
{ REA_MCP_INPUT_SCHEMA_PROFILE: "compact", PORT: 8080 },
|
||||
"env.PORT",
|
||||
],
|
||||
[
|
||||
"boolean member",
|
||||
{ REA_MCP_INPUT_SCHEMA_PROFILE: "compact", FLAG: true },
|
||||
"env.FLAG",
|
||||
],
|
||||
[
|
||||
"null member",
|
||||
{ REA_MCP_INPUT_SCHEMA_PROFILE: "compact", VALUE: null },
|
||||
"env.VALUE",
|
||||
],
|
||||
["array", [], "env"],
|
||||
["string", "not-a-map", "env"],
|
||||
["null map", null, "env"],
|
||||
[
|
||||
"empty profile",
|
||||
{ REA_MCP_INPUT_SCHEMA_PROFILE: "" },
|
||||
"REA_MCP_INPUT_SCHEMA_PROFILE",
|
||||
],
|
||||
[
|
||||
"invalid profile",
|
||||
{ REA_MCP_INPUT_SCHEMA_PROFILE: "bogus" },
|
||||
"REA_MCP_INPUT_SCHEMA_PROFILE",
|
||||
],
|
||||
[
|
||||
"invalid response budget",
|
||||
{ REA_MCP_MAX_RESPONSE_BYTES: "1" },
|
||||
"REA_MCP_MAX_RESPONSE_BYTES",
|
||||
],
|
||||
] as const)(
|
||||
"refuses %s before changing the configuration",
|
||||
async (_name, env, setting) => {
|
||||
const home = await createTestTempDirectory("rea-invalid-environment-");
|
||||
const directory = join(home, ".cursor");
|
||||
const configPath = join(directory, "mcp.json");
|
||||
await mkdir(directory);
|
||||
const original = JSON.stringify({
|
||||
mcpServers: { rea: { command: "rea", args: ["mcp"], env } },
|
||||
});
|
||||
await writeFile(configPath, original);
|
||||
const client = { name: "cursor", format: "json", configPath } as const;
|
||||
expect(
|
||||
await inspectClientConfiguration(client, {}, ["rea", "mcp"]),
|
||||
).toMatchObject({
|
||||
status: "invalid",
|
||||
remediation: expect.stringContaining(setting),
|
||||
});
|
||||
expect(
|
||||
await configureClientConfiguration(client, {}, ["rea", "mcp"]),
|
||||
).toEqual({ status: "failed", reason: "readback" });
|
||||
expect(await clientConfigurationAligned(client, {}, ["rea", "mcp"])).toBe(
|
||||
false,
|
||||
);
|
||||
const statuses = await readClientRegistrationStatuses(home, "rea", {
|
||||
environment: {},
|
||||
});
|
||||
expect(statuses.find(({ client }) => client === "cursor")).toMatchObject({
|
||||
state: "invalid",
|
||||
remediation: expect.stringContaining(setting),
|
||||
});
|
||||
expect(await readFile(configPath, "utf8")).toBe(original);
|
||||
await expect(access(`${configPath}.rea.backup`)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -248,7 +248,7 @@ describe("Grok Build server table edits", () => {
|
||||
expect(configured).toContain("share = false");
|
||||
expect(configured).toContain("[mcp_servers.rea]");
|
||||
expect(configured).toContain("startup_timeout_sec = 30");
|
||||
expect(configured).not.toContain('OLD = "1"');
|
||||
expect(configured).toContain('OLD = "1"');
|
||||
expect(configured).not.toContain('command = "old"');
|
||||
expect(parseToml(configured)).toMatchObject({
|
||||
mcp_servers: {
|
||||
@@ -258,6 +258,7 @@ describe("Grok Build server table edits", () => {
|
||||
args: ["mcp"],
|
||||
startup_timeout_sec: 30,
|
||||
env: {
|
||||
OLD: "1",
|
||||
JAVA_HOME: "/opt/jdk-21",
|
||||
HOPPER_LAUNCHER_PATH: "/Hopper Path",
|
||||
GHIDRA_INSTALL_DIR: "/opt/ghidra",
|
||||
@@ -507,7 +508,7 @@ describe("Grok Build text that resembles a server table", () => {
|
||||
const configured = await readFile(configPath, "utf8");
|
||||
expect(configured).not.toContain("mcp_servers . rea");
|
||||
expect(configured).not.toContain("\\u0072ea");
|
||||
expect(configured).not.toContain('OLD = "1"');
|
||||
expect(configured).toContain('OLD = "1"');
|
||||
expect(
|
||||
configured
|
||||
.split("\n")
|
||||
@@ -515,7 +516,12 @@ describe("Grok Build text that resembles a server table", () => {
|
||||
).toHaveLength(1);
|
||||
expect(parseToml(configured)).toMatchObject({
|
||||
mcp_servers: {
|
||||
rea: { command: "rea", args: ["mcp"], startup_timeout_sec: 30 },
|
||||
rea: {
|
||||
command: "rea",
|
||||
args: ["mcp"],
|
||||
startup_timeout_sec: 30,
|
||||
env: { OLD: "1" },
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user