Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(mcp): use a writable npm cache for command servers

David Neyra7a7f3b578a78
diff --git a/server/drivers/chat-mcp-tools.test.ts b/server/drivers/chat-mcp-tools.test.ts
index 16d9e83852..9c0ab987fe 100644
--- a/server/drivers/chat-mcp-tools.test.ts
+++ b/server/drivers/chat-mcp-tools.test.ts
@@ -6,7 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { augmentedPath } from "../env-path.ts";
import { MESSAGE_FILE_MAX_BYTES } from "../message-file.ts";
import { Ajv2020 } from "ajv/dist/2020.js";
-import { ChatMcpClient, ChatToolSessionError, SAVED_RESULTS_FOLDER, SAVED_RESULTS_KEPT, chatToolNotices, directServers, mcpStartupMs, mountChatTools, trimmedToolsNotice, type ChatToolSession } from "./chat-mcp-tools.ts";
+import { ChatMcpClient, ChatToolSessionError, SAVED_RESULTS_FOLDER, SAVED_RESULTS_KEPT, chatMcpEnvironment, chatToolNotices, directServers, mcpStartupMs, mountChatTools, trimmedToolsNotice, type ChatToolSession } from "./chat-mcp-tools.ts";
import { EXECUTOR_LIKE_TOOLS } from "../testing/executor-like-tools.ts";
import type { ToolScope } from "../../shared/tool-scope.ts";
import type { SendTurnInput } from "../contracts.ts";
@@ -41,6 +41,16 @@ const sessions: ChatToolSession[] = [];
const controllers: AbortController[] = [];
const schema = { type: "object", properties: { value: { type: "string" } }, required: ["value"], additionalProperties: false };
+describe("configured MCP environment", () => {
+ it("replaces an inherited npm cache with a writable default and preserves an explicit override", () => {
+ const inherited = { npm_config_cache: "/data/.npm/_cacache", PATH: "/bin" };
+ const env = chatMcpEnvironment({}, inherited);
+ expect(env.npm_config_cache).toContain("openmausbot-mcp-npm-");
+ expect(env.npm_config_cache).not.toBe(inherited.npm_config_cache);
+ expect(chatMcpEnvironment({ npm_config_cache: "/tmp/custom-npm-cache" }, inherited).npm_config_cache).toBe("/tmp/custom-npm-cache");
+ });
+});
+
function fixture(body = "", toolSchema: Record<string, unknown> = schema) {
const dir = mkdtempSync(join(tmpdir(), "omb-chat-mcp-"));
dirs.push(dir);
diff --git a/server/drivers/chat-mcp-tools.ts b/server/drivers/chat-mcp-tools.ts
index fe432e7051..776a01f9c5 100644
--- a/server/drivers/chat-mcp-tools.ts
+++ b/server/drivers/chat-mcp-tools.ts
@@ -10,6 +10,7 @@ import { cloudRole } from "../bot-box/detect.ts";
import { redactSecretsInText } from "../redact.ts";
import { MESSAGE_FILE_MAX_BYTES } from "../message-file.ts";
import { augmentedPath } from "../env-path.ts";
+import { mcpCommandEnvironment } from "../mcp-environment.ts";
import { cloudHomeConfigured } from "../cloud-home.ts";
import { killCliTree, spawnCli } from "../procs.ts";
import { AGENTS_TOOL_ALIASES } from "../../shared/agents-tool-names.ts";
@@ -127,7 +128,7 @@ function mcpRefusalText(method: string | undefined, error: unknown): string {
export function chatMcpEnvironment(serverEnv: Record<string, string>, source: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
const env: NodeJS.ProcessEnv = { ...source, PATH: augmentedPath() };
stripControlPlaneEnv(env);
- return { ...env, ...serverEnv };
+ return mcpCommandEnvironment(env, serverEnv);
}
export class ChatMcpClient {
diff --git a/server/mcp-environment.ts b/server/mcp-environment.ts
new file mode 100644
index 0000000000..69d7a7a877
--- /dev/null
+++ b/server/mcp-environment.ts
@@ -0,0 +1,15 @@
+import { randomBytes } from "node:crypto";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const mcpNpmCache = join(tmpdir(), `openmausbot-mcp-npm-${randomBytes(12).toString("hex")}`);
+
+/** Give npm-backed MCP commands a writable cache even when the server's inherited cache is not writable. */
+export function mcpCommandEnvironment(base: NodeJS.ProcessEnv, serverEnv: Record<string, string>): NodeJS.ProcessEnv {
+ const env: NodeJS.ProcessEnv = { ...base };
+ for (const key of Object.keys(env)) if (key.toLowerCase() === "npm_config_cache") delete env[key];
+
+ const explicitCache = Object.keys(serverEnv).some((key) => key.toLowerCase() === "npm_config_cache");
+ if (!explicitCache) env.npm_config_cache = mcpNpmCache;
+ return { ...env, ...serverEnv };
+}
diff --git a/server/mcp-probe.test.ts b/server/mcp-probe.test.ts
index 252fd6515c..66bd7ae314 100644
--- a/server/mcp-probe.test.ts
+++ b/server/mcp-probe.test.ts
@@ -7,7 +7,7 @@ import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, it, vi } from "vitest";
import { MAX_REMOTE_MCP_BYTES } from "./mcp-http.ts";
-import { probeMcpServer, SIGN_IN_CHECK_TOOL } from "./mcp-probe.ts";
+import { probeEnvironment, probeMcpServer, SIGN_IN_CHECK_TOOL } from "./mcp-probe.ts";
import { removeTempDir } from "./testing/cleanup.ts";
import { startFakeHttpMcp } from "./testing/fake-http-mcp-server.ts";
import { startFakeOAuth } from "./testing/fake-oauth-server.ts";
@@ -49,6 +49,16 @@ function watch<T>(pending: Promise<T>): { done: boolean; value?: T } {
afterEach(() => { vi.useRealTimers(); });
describe("custom MCP probe", () => {
+ it("uses a writable default npm cache and preserves a per-server override", () => {
+ const inherited = { npm_config_cache: "/data/.npm/_cacache", PATH: "/bin" };
+ const defaultEnv = probeEnvironment({ env: {} }, inherited);
+ expect(defaultEnv.npm_config_cache).toContain("openmausbot-mcp-npm-");
+ expect(defaultEnv.npm_config_cache).not.toBe(inherited.npm_config_cache);
+
+ const custom = probeEnvironment({ env: { npm_config_cache: "/tmp/custom-npm-cache" } }, inherited);
+ expect(custom.npm_config_cache).toBe("/tmp/custom-npm-cache");
+ });
+
it("performs an MCP handshake and returns the bounded public tool list", async () => {
await expect(probeMcpServer({
command: process.execPath,
diff --git a/server/mcp-probe.ts b/server/mcp-probe.ts
index a1ff39ea66..3be3a5900e 100644
--- a/server/mcp-probe.ts
+++ b/server/mcp-probe.ts
@@ -5,6 +5,7 @@ import {
stripWorkspaceCredentialEnv,
} from "./config.ts";
import { createLineSplitter } from "./mcp-bridge.ts";
+import { mcpCommandEnvironment } from "./mcp-environment.ts";
import { UI_EXTENSION, withUiExtension } from "./mcp-app-observer.ts";
import { MAX_REMOTE_MCP_BYTES, McpHttpError, McpRpcError, REMOTE_MCP_STARTUP_MS, RemoteMcpClient } from "./mcp-http.ts";
import { discoverMcpAuth } from "./mcp-oauth-discovery.ts";
@@ -58,12 +59,11 @@ const MAX_TOOLS = 100;
export const STDIO_PROBE_TIMEOUT_MS = 8_000;
export const REMOTE_PROBE_TIMEOUT_MS = REMOTE_MCP_STARTUP_MS;
-export function probeEnvironment(server: Pick<StoredStdioMcpServer, "env">): NodeJS.ProcessEnv {
- const env: NodeJS.ProcessEnv = { ...process.env, PATH: augmentedPath() };
+export function probeEnvironment(server: Pick<StoredStdioMcpServer, "env">, source: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
+ const env: NodeJS.ProcessEnv = { ...source, PATH: augmentedPath() };
stripWorkspaceCredentialEnv(env);
for (const key of PROVIDER_CREDENTIAL_ENV) delete env[key];
- Object.assign(env, server.env);
- return env;
+ return mcpCommandEnvironment(env, server.env);
}
function publicProbeError(kind: "spawn" | "timeout" | "protocol" | "closed" | "cancelled"): string {