Skip to content

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

MCP npm cache (#6273): keep the inherited cache when it can be written

milind-soniecdb86325b9d
diff --git a/server/drivers/chat-mcp-tools.test.ts b/server/drivers/chat-mcp-tools.test.ts
index 3f22443e04..972046bba1 100644
--- a/server/drivers/chat-mcp-tools.test.ts
+++ b/server/drivers/chat-mcp-tools.test.ts
@@ -42,12 +42,15 @@ 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");
+ it("keeps an npm cache that works and preserves an explicit override", () => {
+ const cache = mkdtempSync(join(tmpdir(), "omb-npm-cache-"));
+ try {
+ const inherited = { npm_config_cache: cache, PATH: "/bin" };
+ expect(chatMcpEnvironment({}, inherited).npm_config_cache).toBe(cache);
+ expect(chatMcpEnvironment({ npm_config_cache: "/tmp/custom-npm-cache" }, inherited).npm_config_cache).toBe("/tmp/custom-npm-cache");
+ } finally {
+ rmSync(cache, { recursive: true, force: true });
+ }
});
});
diff --git a/server/mcp-environment.test.ts b/server/mcp-environment.test.ts
new file mode 100644
index 0000000000..886d07c1f3
--- /dev/null
+++ b/server/mcp-environment.test.ts
@@ -0,0 +1,30 @@
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { describe, expect, it } from "vitest";
+
+import { mcpCommandEnvironment, writableCache } from "./mcp-environment.ts";
+
+describe("the npm cache an MCP command gets", () => {
+ it("swaps a cache the server cannot write for its own, whatever the variable's case", () => {
+ const env = mcpCommandEnvironment({ NPM_CONFIG_CACHE: "/data/.npm", PATH: "/bin" }, {}, () => false);
+ expect(env.NPM_CONFIG_CACHE).toBeUndefined();
+ expect(env.npm_config_cache).toContain("openmausbot-mcp-npm-");
+ expect(env.PATH).toBe("/bin");
+ expect(mcpCommandEnvironment({ HOME: "/data", LOCALAPPDATA: "C:\\data" }, {}, () => false).npm_config_cache).toContain("openmausbot-mcp-npm-");
+ });
+
+ it("keeps a cache that works, and a server's own setting always wins", () => {
+ expect(mcpCommandEnvironment({ npm_config_cache: "/home/me/.npm" }, {}, () => true).npm_config_cache).toBe("/home/me/.npm");
+ expect(mcpCommandEnvironment({ npm_config_cache: "/data/.npm" }, { npm_config_cache: "/srv/cache" }, () => false).npm_config_cache).toBe("/srv/cache");
+ });
+
+ it("counts a cache that does not exist yet as writable when its nearest folder is", () => {
+ const home = mkdtempSync(join(tmpdir(), "omb-npm-home-"));
+ try {
+ expect(writableCache(join(home, ".npm"))).toBe(true);
+ } finally {
+ rmSync(home, { recursive: true, force: true });
+ }
+ });
+});
diff --git a/server/mcp-environment.ts b/server/mcp-environment.ts
index 69d7a7a877..ee5551bc64 100644
--- a/server/mcp-environment.ts
+++ b/server/mcp-environment.ts
@@ -1,15 +1,37 @@
import { randomBytes } from "node:crypto";
+import { accessSync, constants } from "node:fs";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { dirname, 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];
+/** npm's cache for this environment: `npm_config_cache` when set, else npm's own default under the home folder. */
+function inheritedNpmCache(env: NodeJS.ProcessEnv): string | undefined {
+ const key = Object.keys(env).find((name) => name.toLowerCase() === "npm_config_cache");
+ if (key) return env[key] || undefined;
+ if (process.platform === "win32") return env.LOCALAPPDATA ? join(env.LOCALAPPDATA, "npm-cache") : undefined;
+ return env.HOME ? join(env.HOME, ".npm") : undefined;
+}
+/** The nearest folder that exists on the way to `path` can be written. */
+export function writableCache(path: string): boolean {
+ for (let at = join(path, "_cacache", "tmp"); ; at = dirname(at)) {
+ try {
+ accessSync(at, constants.W_OK);
+ return true;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT" || dirname(at) === at) return false;
+ }
+ }
+}
+
+/** Give npm-backed MCP commands a writable cache when the one the server inherited cannot be written. A cache that
+ * works is kept, so packages it already holds still start offline and are not fetched again on every launch. */
+export function mcpCommandEnvironment(base: NodeJS.ProcessEnv, serverEnv: Record<string, string>, canWrite = writableCache): NodeJS.ProcessEnv {
const explicitCache = Object.keys(serverEnv).some((key) => key.toLowerCase() === "npm_config_cache");
- if (!explicitCache) env.npm_config_cache = mcpNpmCache;
- return { ...env, ...serverEnv };
+ const inherited = inheritedNpmCache({ ...base, ...serverEnv });
+ if (explicitCache || !inherited || canWrite(inherited)) return { ...base, ...serverEnv };
+ const env: NodeJS.ProcessEnv = { ...base };
+ for (const key of Object.keys(env)) if (key.toLowerCase() === "npm_config_cache") delete env[key];
+ return { ...env, npm_config_cache: mcpNpmCache, ...serverEnv };
}
diff --git a/server/mcp-probe.test.ts b/server/mcp-probe.test.ts
index 66bd7ae314..d9bbbd6975 100644
--- a/server/mcp-probe.test.ts
+++ b/server/mcp-probe.test.ts
@@ -1,4 +1,4 @@
-import { mkdtempSync, writeFileSync } from "node:fs";
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
import { tmpdir } from "node:os";
@@ -49,14 +49,15 @@ 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("keeps an npm cache that works and preserves a per-server override", () => {
+ const cache = mkdtempSync(join(tmpdir(), "omb-npm-cache-"));
+ try {
+ const inherited = { npm_config_cache: cache, PATH: "/bin" };
+ expect(probeEnvironment({ env: {} }, inherited).npm_config_cache).toBe(cache);
+ expect(probeEnvironment({ env: { npm_config_cache: "/tmp/custom-npm-cache" } }, inherited).npm_config_cache).toBe("/tmp/custom-npm-cache");
+ } finally {
+ rmSync(cache, { recursive: true, force: true });
+ }
});
it("performs an MCP handshake and returns the bounded public tool list", async () => {