From a83e1fda59048883978d74b6e24ce71f501076cd Mon Sep 17 00:00:00 2001 From: "Charles C. Figueiredo" Date: Sat, 23 May 2026 10:49:53 -0400 Subject: [PATCH] fix(mcp): honor AGENTMEMORY_TOOLS from .env in getVisibleTools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getVisibleTools() read process.env["AGENTMEMORY_TOOLS"] directly, bypassing getMergedEnv() — the { ...fileEnv, ...process.env } helper that every other config reader reaches through getEnvVar(). As a result, AGENTMEMORY_TOOLS=all set in ~/.agentmemory/.env was silently ignored: the MCP surface stayed at the 8-tool core set instead of the full surface, even though .env.example documents that variable as the way to opt in. Route the lookup through getEnvVar() so the documented .env knob takes effect, while process.env (shell export, --tools flag) keeps precedence over the file. Fixes #553 Signed-off-by: Charles C. Figueiredo --- src/mcp/tools-registry.ts | 4 +- test/tools-visibility-env.test.ts | 64 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/tools-visibility-env.test.ts diff --git a/src/mcp/tools-registry.ts b/src/mcp/tools-registry.ts index 3001cae75d..72dcbf3f4a 100644 --- a/src/mcp/tools-registry.ts +++ b/src/mcp/tools-registry.ts @@ -1,3 +1,5 @@ +import { getEnvVar } from "../config.js"; + export type McpToolDef = { name: string; description: string; @@ -942,7 +944,7 @@ export function getAllTools(): McpToolDef[] { } export function getVisibleTools(): McpToolDef[] { - const mode = process.env["AGENTMEMORY_TOOLS"] || "core"; + const mode = getEnvVar("AGENTMEMORY_TOOLS") || "core"; if (mode === "all") return getAllTools(); return getAllTools().filter((t) => ESSENTIAL_TOOLS.has(t.name)); } diff --git a/test/tools-visibility-env.test.ts b/test/tools-visibility-env.test.ts new file mode 100644 index 0000000000..0c6d87b3ce --- /dev/null +++ b/test/tools-visibility-env.test.ts @@ -0,0 +1,64 @@ +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; +import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +const ORIGINAL_HOME = process.env["HOME"]; +const ORIGINAL_USERPROFILE = process.env["USERPROFILE"]; +const ORIGINAL_TOOLS = process.env["AGENTMEMORY_TOOLS"]; + +let sandboxHome: string; + +async function freshTools() { + vi.resetModules(); + return await import("../src/mcp/tools-registry.js"); +} + +function writeEnv(contents: string) { + const dir = join(sandboxHome, ".agentmemory"); + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, ".env"), contents); +} + +describe("getVisibleTools env resolution", () => { + beforeEach(() => { + sandboxHome = mkdtempSync(join(tmpdir(), "agentmemory-tools-")); + process.env["HOME"] = sandboxHome; + process.env["USERPROFILE"] = sandboxHome; + delete process.env["AGENTMEMORY_TOOLS"]; + }); + + afterEach(() => { + if (ORIGINAL_HOME === undefined) delete process.env["HOME"]; + else process.env["HOME"] = ORIGINAL_HOME; + if (ORIGINAL_USERPROFILE === undefined) delete process.env["USERPROFILE"]; + else process.env["USERPROFILE"] = ORIGINAL_USERPROFILE; + if (ORIGINAL_TOOLS === undefined) delete process.env["AGENTMEMORY_TOOLS"]; + else process.env["AGENTMEMORY_TOOLS"] = ORIGINAL_TOOLS; + rmSync(sandboxHome, { recursive: true, force: true }); + }); + + it("exposes the full tool surface when AGENTMEMORY_TOOLS=all is set in the .env file", async () => { + writeEnv("AGENTMEMORY_TOOLS=all"); + const reg = await freshTools(); + const visible = reg.getVisibleTools(); + expect(visible.length).toBe(reg.getAllTools().length); + expect(visible.map((t) => t.name)).toContain("memory_graph_query"); + }); + + it("falls back to the core subset when AGENTMEMORY_TOOLS is unset", async () => { + writeEnv(""); + const reg = await freshTools(); + const names = reg.getVisibleTools().map((t) => t.name); + expect(names).toContain("memory_save"); + expect(names).not.toContain("memory_graph_query"); + expect(names.length).toBeLessThan(reg.getAllTools().length); + }); + + it("lets process.env override the .env file value", async () => { + writeEnv("AGENTMEMORY_TOOLS=core"); + process.env["AGENTMEMORY_TOOLS"] = "all"; + const reg = await freshTools(); + expect(reg.getVisibleTools().length).toBe(reg.getAllTools().length); + }); +});