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:
@@ -1,11 +1,203 @@
|
||||
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { COMPACT_INPUT_SCHEMA_BUDGET_BYTES } from "../../../src/config/mcpInputSchemaProfile.js";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
for (const profile of ["", "bogus"])
|
||||
cliTest(
|
||||
`setup and doctor refuse an invalid retained schema profile ${JSON.stringify(profile)}`,
|
||||
async ({ cli }) => {
|
||||
const home = await createTestTempDirectory("rea-pi-invalid-profile-cli-");
|
||||
const agent = join(home, ".pi", "agent");
|
||||
const config = join(agent, "mcp.json");
|
||||
const original = JSON.stringify({
|
||||
mcpServers: {
|
||||
rea: {
|
||||
type: "stdio",
|
||||
command: "rea",
|
||||
args: ["mcp"],
|
||||
env: { REA_MCP_INPUT_SCHEMA_PROFILE: profile },
|
||||
},
|
||||
},
|
||||
});
|
||||
await mkdir(agent, { recursive: true });
|
||||
await writeFile(config, original);
|
||||
const environment = {
|
||||
HOME: home,
|
||||
USERPROFILE: home,
|
||||
HOPPER_LAUNCHER_PATH: join(home, "unconfigured-hopper"),
|
||||
};
|
||||
const setup = await cli.run({
|
||||
arguments: [
|
||||
"setup",
|
||||
"--client",
|
||||
"pi",
|
||||
"--skill=false",
|
||||
"--yes",
|
||||
"--json",
|
||||
],
|
||||
cwd: home,
|
||||
environment,
|
||||
});
|
||||
expect(setup.exitCode).toBe(1);
|
||||
expect(setup.json).toMatchObject({
|
||||
status: "needs_human",
|
||||
plannedActions: [],
|
||||
appliedActions: [],
|
||||
remediation: expect.stringContaining("REA_MCP_INPUT_SCHEMA_PROFILE"),
|
||||
});
|
||||
const doctor = await cli.run({
|
||||
arguments: ["doctor", "--client", "pi", "--json"],
|
||||
cwd: home,
|
||||
environment,
|
||||
});
|
||||
expect(doctor.exitCode).toBe(1);
|
||||
expect(doctor.json).toMatchObject({
|
||||
healthy: false,
|
||||
identity: {
|
||||
registrations: expect.arrayContaining([
|
||||
expect.objectContaining({ client: "pi", state: "invalid" }),
|
||||
]),
|
||||
},
|
||||
});
|
||||
expect(await readFile(config, "utf8")).toBe(original);
|
||||
await expect(readFile(`${config}.rea.backup`)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"setup preserves a configured compact catalog through CLI migration and MCP startup",
|
||||
async ({ cli }) => {
|
||||
const home = await createTestTempDirectory("rea-pi-environment-cli-");
|
||||
const agent = join(home, ".pi", "agent");
|
||||
const config = join(agent, "mcp.json");
|
||||
const custom = {
|
||||
REA_MCP_INPUT_SCHEMA_PROFILE: "compact",
|
||||
REA_LOG_LEVEL: "silent",
|
||||
};
|
||||
const original = JSON.stringify({
|
||||
mcpServers: {
|
||||
rea: {
|
||||
type: "stdio",
|
||||
command: "npx",
|
||||
args: ["-y", "rea-agents@1.0.0", "mcp"],
|
||||
env: custom,
|
||||
},
|
||||
},
|
||||
});
|
||||
await mkdir(agent, { recursive: true });
|
||||
await writeFile(config, original);
|
||||
const environment = {
|
||||
HOME: home,
|
||||
USERPROFILE: home,
|
||||
HOPPER_LAUNCHER_PATH: join(home, "unconfigured-hopper"),
|
||||
};
|
||||
const arguments_ = [
|
||||
"setup",
|
||||
"--client",
|
||||
"pi",
|
||||
"--skill=false",
|
||||
"--yes",
|
||||
"--json",
|
||||
];
|
||||
const planned = await cli.run({
|
||||
arguments: [...arguments_, "--dry-run"],
|
||||
cwd: home,
|
||||
environment,
|
||||
});
|
||||
expect(planned.exitCode).toBe(0);
|
||||
expect(planned.json).toMatchObject({
|
||||
plannedActions: [
|
||||
expect.objectContaining({
|
||||
id: "configure_client:pi",
|
||||
detail: expect.stringContaining(
|
||||
"Existing server environment overrides are retained",
|
||||
),
|
||||
}),
|
||||
],
|
||||
appliedActions: [],
|
||||
});
|
||||
expect(await readFile(config, "utf8")).toBe(original);
|
||||
const applied = await cli.run({
|
||||
arguments: arguments_,
|
||||
cwd: home,
|
||||
environment,
|
||||
timeoutMs: 20_000,
|
||||
});
|
||||
expect(applied.exitCode).toBe(0);
|
||||
expect(applied.json).toMatchObject({
|
||||
status: "ready",
|
||||
clients: { pi: { status: "configured" } },
|
||||
});
|
||||
const configured = await readFile(config, "utf8");
|
||||
const registration = z
|
||||
.object({
|
||||
mcpServers: z.object({
|
||||
rea: z.object({
|
||||
command: z.string(),
|
||||
args: z.array(z.string()),
|
||||
env: z.record(z.string(), z.string()).optional(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
.parse(JSON.parse(configured)).mcpServers.rea;
|
||||
const transport = new StdioClientTransport({
|
||||
command: registration.command,
|
||||
args: registration.args,
|
||||
cwd: home,
|
||||
env: {
|
||||
PATH: process.env.PATH ?? "/usr/bin:/bin",
|
||||
...environment,
|
||||
...registration.env,
|
||||
},
|
||||
stderr: "pipe",
|
||||
});
|
||||
const client = new Client({ name: "setup-environment", version: "1" });
|
||||
try {
|
||||
await client.connect(transport);
|
||||
const { tools } = await client.listTools();
|
||||
expect(tools.length).toBeGreaterThan(0);
|
||||
expect(
|
||||
tools
|
||||
.filter(
|
||||
({ inputSchema }) =>
|
||||
Buffer.byteLength(JSON.stringify(inputSchema), "utf8") >
|
||||
COMPACT_INPUT_SCHEMA_BUDGET_BYTES,
|
||||
)
|
||||
.map(({ name }) => name),
|
||||
).toEqual([]);
|
||||
expect(await client.ping()).toEqual({});
|
||||
} finally {
|
||||
await client.close();
|
||||
await transport.close();
|
||||
}
|
||||
expect(registration.env).toMatchObject(custom);
|
||||
expect(await readFile(`${config}.rea.backup`, "utf8")).toBe(original);
|
||||
const repeated = await cli.run({
|
||||
arguments: arguments_,
|
||||
cwd: home,
|
||||
environment,
|
||||
timeoutMs: 20_000,
|
||||
});
|
||||
expect(repeated.exitCode).toBe(0);
|
||||
expect(repeated.json).toMatchObject({
|
||||
plannedActions: [],
|
||||
appliedActions: [],
|
||||
});
|
||||
expect(await readFile(config, "utf8")).toBe(configured);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"Pi public CLI plans, requires approval, applies, diagnoses, repeats, and uninstalls only owned content",
|
||||
async ({ cli }) => {
|
||||
|
||||
@@ -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