From 343e654da42f48024af9340ab5e60efe9559acd7 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sun, 17 May 2026 14:32:08 +0100 Subject: [PATCH] fix(plugin): inherit AGENTMEMORY_URL/SECRET via env passthrough (#375) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bundled plugin .mcp.json + the `agentmemory connect ` wiring both hardcoded the MCP server to localhost:3111 with no env-var seam. Users running agentmemory remotely (k8s cluster-internal DNS, reverse proxy with API key auth) hit a permanent /doctor warning because they had to add a second entry with the right AGENTMEMORY_URL + SECRET, and both entries fought for the "agentmemory" name. Both files now use ${AGENTMEMORY_URL} / ${AGENTMEMORY_SECRET} expansion. Host (Claude Code, Cursor, etc.) substitutes the shell value at MCP-server launch; when vars are unset the host passes empty string, which the standalone shim treats as missing and falls back to http://localhost:3111. One wired entry now covers both local and remote without duplicates. - plugin/.mcp.json: env block added with ${VAR} expansion - src/cli/connect/util.ts: AGENTMEMORY_MCP_BLOCK env updated; comment documents the empty-string fallback contract - test/cli-connect.test.ts: regression test asserting the env block shape after install() — protects against future regressions where the literal hardcoded URL leaks back in 1008/1008 tests pass. --- plugin/.mcp.json | 6 +++++- src/cli/connect/util.ts | 10 +++++++++- test/cli-connect.test.ts | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/plugin/.mcp.json b/plugin/.mcp.json index 12702a2a5a..f7c3760336 100644 --- a/plugin/.mcp.json +++ b/plugin/.mcp.json @@ -2,7 +2,11 @@ "mcpServers": { "agentmemory": { "command": "npx", - "args": ["-y", "@agentmemory/mcp"] + "args": ["-y", "@agentmemory/mcp"], + "env": { + "AGENTMEMORY_URL": "${AGENTMEMORY_URL}", + "AGENTMEMORY_SECRET": "${AGENTMEMORY_SECRET}" + } } } } diff --git a/src/cli/connect/util.ts b/src/cli/connect/util.ts index ce9110c655..6d5f61ac98 100644 --- a/src/cli/connect/util.ts +++ b/src/cli/connect/util.ts @@ -10,11 +10,19 @@ import { dirname, join } from "node:path"; import { homedir } from "node:os"; import * as p from "@clack/prompts"; +// Env values use ${VAR} expansion so the wired MCP entry inherits +// AGENTMEMORY_URL / AGENTMEMORY_SECRET from the user's shell. When the +// vars are unset, the host (Claude Code, Cursor, etc.) substitutes an +// empty string; the standalone shim treats empty as missing and falls +// back to http://localhost:3111. This lets a single wired entry serve +// both local and remote (Kubernetes / reverse-proxied) deployments +// without doctor-warning duplicates (#375). export const AGENTMEMORY_MCP_BLOCK = { command: "npx", args: ["-y", "@agentmemory/mcp"], env: { - AGENTMEMORY_URL: "http://localhost:3111", + AGENTMEMORY_URL: "${AGENTMEMORY_URL}", + AGENTMEMORY_SECRET: "${AGENTMEMORY_SECRET}", }, }; diff --git a/test/cli-connect.test.ts b/test/cli-connect.test.ts index 4a6c0227ee..99174dacbc 100644 --- a/test/cli-connect.test.ts +++ b/test/cli-connect.test.ts @@ -112,6 +112,27 @@ describe("agentmemory connect — claude-code adapter (mock filesystem)", () => expect(second.kind).toBe("already-wired"); }); + it("install() writes env passthrough block for AGENTMEMORY_URL + AGENTMEMORY_SECRET (#375)", async () => { + // Remote deployments (k8s, reverse proxy) set AGENTMEMORY_URL + + // AGENTMEMORY_SECRET in the shell. The wired MCP entry must honour + // those via ${VAR} expansion so a single entry covers both local + // and remote without the user needing to add a duplicate config + // that triggers a /doctor duplicate-server warning. + const claudeDir = join(tmpHome, ".claude"); + require("node:fs").mkdirSync(claudeDir, { recursive: true }); + writeFileSync(join(tmpHome, ".claude.json"), JSON.stringify({})); + + const a = await loadAdapter(); + const result = await a.install({ dryRun: false, force: false }); + expect(result.kind).toBe("installed"); + + const config = JSON.parse(readFileSync(join(tmpHome, ".claude.json"), "utf-8")); + const entry = config.mcpServers.agentmemory; + expect(entry.env).toBeDefined(); + expect(entry.env.AGENTMEMORY_URL).toBe("${AGENTMEMORY_URL}"); + expect(entry.env.AGENTMEMORY_SECRET).toBe("${AGENTMEMORY_SECRET}"); + }); + it("install() with --force re-writes even when already wired", async () => { require("node:fs").mkdirSync(join(tmpHome, ".claude"), { recursive: true }); writeFileSync(