From 7dd52d7146ff7f0184849934729063b3bd516c3e Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Thu, 5 Nov 2026 00:20:01 +0530 Subject: [PATCH 1/3] fix(plugin): pass plugin settings to the MCP server under accepted names --- CHANGELOG.md | 1 + plugin/.mcp.json | 4 +-- plugin/scripts/_capture.mjs | 22 ++++++++---- plugin/scripts/plugin-bridge.mjs | 29 +++++++++++++--- plugin/scripts/pre-compact.mjs | 22 ++++++++---- plugin/scripts/pre-tool-use.mjs | 22 ++++++++---- plugin/scripts/session-start.mjs | 22 ++++++++---- src/hooks/_endpoint.ts | 26 +++++++++----- src/mcp/plugin-bridge.ts | 4 +-- src/mcp/rest-proxy.ts | 9 ++--- test/codex-plugin.test.ts | 4 +-- test/mcp-surface-default.test.ts | 4 +-- test/plugin-bridge-secret.test.ts | 56 +++++++++++++++++++++++++++++++ 13 files changed, 170 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f97f668cfa..4f1df6403a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - pi includes recalled lessons in search and can read a full lesson by ID, scoped to its project (#1605 by @YeChenglin8888). - `npm run build` works under the Windows `cmd.exe` shell (#1362 by @mattamundson). - `.env.example` names the Agent SDK route and documents `MAX_OBS_PER_SESSION` as a hard per-session cap (#1675, from #1503 by @Rex57). +- The Claude Code plugin passes its URL and secret settings to the MCP server as `CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL` and `CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET`, the names the plugin directory accepts. An empty URL setting no longer hides `AGENTMEMORY_URL` from the environment, and an empty secret setting still never sends `AGENTMEMORY_SECRET` from the environment. ### Known issues diff --git a/plugin/.mcp.json b/plugin/.mcp.json index bf4f05ef50..6c46a916b1 100644 --- a/plugin/.mcp.json +++ b/plugin/.mcp.json @@ -4,8 +4,8 @@ "command": "node", "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/plugin-bridge.mjs"], "env": { - "AGENTMEMORY_URL": "${user_config.agentmemory_url}", - "AGENTMEMORY_SECRET": "${user_config.agentmemory_secret}" + "CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL": "${user_config.agentmemory_url}", + "CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET": "${user_config.agentmemory_secret}" } } } diff --git a/plugin/scripts/_capture.mjs b/plugin/scripts/_capture.mjs index 2799eaf7da..83bc5d0d17 100644 --- a/plugin/scripts/_capture.mjs +++ b/plugin/scripts/_capture.mjs @@ -93,12 +93,24 @@ function canonicalSecretPath() { //#region src/hooks/_endpoint.ts const OPTION_PREFIX = "CLAUDE_PLUGIN_OPTION_"; const DEFAULT_URL = "http://localhost:3111"; +function filled(value) { + const trimmed = value?.trim(); + if (!trimmed || trimmed.startsWith("${") && trimmed.endsWith("}")) return void 0; + return value; +} function hookSetting(name, env = process.env) { const optionKey = `${OPTION_PREFIX}${name}`; - const option = env[optionKey]?.trim(); + const option = filled(env[optionKey])?.trim(); if (option) return option; if (name === "AGENTMEMORY_SECRET" && optionKey in env) return void 0; - return env[name]; + return filled(env[name]); +} +function resolveSettingSecret(url, env = process.env) { + const secretEnv = { ...env }; + const explicit = hookSetting("AGENTMEMORY_SECRET", env); + if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; + else secretEnv["AGENTMEMORY_SECRET"] = explicit; + return resolveClientSecret(url, secretEnv); } function secretTransportAllowed(url) { try { @@ -109,11 +121,7 @@ function secretTransportAllowed(url) { } function resolveHookEndpoint(env = process.env) { const url = hookSetting("AGENTMEMORY_URL", env) || DEFAULT_URL; - const secretEnv = { ...env }; - const explicit = hookSetting("AGENTMEMORY_SECRET", env); - if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; - else secretEnv["AGENTMEMORY_SECRET"] = explicit; - const secret = resolveClientSecret(url, secretEnv); + const secret = resolveSettingSecret(url, env); const blocked = !secretTransportAllowed(url); return { url, diff --git a/plugin/scripts/plugin-bridge.mjs b/plugin/scripts/plugin-bridge.mjs index 8c54d77745..224fc672a3 100644 --- a/plugin/scripts/plugin-bridge.mjs +++ b/plugin/scripts/plugin-bridge.mjs @@ -92,12 +92,31 @@ function canonicalSecretPath() { return join(homedir(), ".agentmemory", "secret"); } //#endregion +//#region src/hooks/_endpoint.ts +const OPTION_PREFIX = "CLAUDE_PLUGIN_OPTION_"; +function filled(value) { + const trimmed = value?.trim(); + if (!trimmed || trimmed.startsWith("${") && trimmed.endsWith("}")) return void 0; + return value; +} +function hookSetting(name, env = process.env) { + const optionKey = `${OPTION_PREFIX}${name}`; + const option = filled(env[optionKey])?.trim(); + if (option) return option; + if (name === "AGENTMEMORY_SECRET" && optionKey in env) return void 0; + return filled(env[name]); +} +function resolveSettingSecret(url, env = process.env) { + const secretEnv = { ...env }; + const explicit = hookSetting("AGENTMEMORY_SECRET", env); + if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; + else secretEnv["AGENTMEMORY_SECRET"] = explicit; + return resolveClientSecret(url, secretEnv); +} +//#endregion //#region src/mcp/rest-proxy.ts function resolveEnvOrEmpty(name) { - const raw = process.env[name]; - if (!raw) return ""; - if (raw.startsWith("${") && raw.endsWith("}")) return ""; - return raw; + return hookSetting(name) ?? ""; } //#endregion //#region src/mcp/transport.ts @@ -307,7 +326,7 @@ function createPluginBridge() { "[::1]" ].includes(base.hostname) || /^127(?:\.\d{1,3}){3}$/.test(base.hostname); function resolveSecret() { - const secret = resolveClientSecret(base.href); + const secret = resolveSettingSecret(base.href); if (secret && base.protocol !== "https:" && !loopback) throw new Error("AGENTMEMORY_URL requires HTTPS when AGENTMEMORY_SECRET is set, except for loopback URLs."); return secret; } diff --git a/plugin/scripts/pre-compact.mjs b/plugin/scripts/pre-compact.mjs index 107ec36c41..925b4a415b 100755 --- a/plugin/scripts/pre-compact.mjs +++ b/plugin/scripts/pre-compact.mjs @@ -102,12 +102,24 @@ function canonicalSecretPath() { //#region src/hooks/_endpoint.ts const OPTION_PREFIX = "CLAUDE_PLUGIN_OPTION_"; const DEFAULT_URL = "http://localhost:3111"; +function filled(value) { + const trimmed = value?.trim(); + if (!trimmed || trimmed.startsWith("${") && trimmed.endsWith("}")) return void 0; + return value; +} function hookSetting(name, env = process.env) { const optionKey = `${OPTION_PREFIX}${name}`; - const option = env[optionKey]?.trim(); + const option = filled(env[optionKey])?.trim(); if (option) return option; if (name === "AGENTMEMORY_SECRET" && optionKey in env) return void 0; - return env[name]; + return filled(env[name]); +} +function resolveSettingSecret(url, env = process.env) { + const secretEnv = { ...env }; + const explicit = hookSetting("AGENTMEMORY_SECRET", env); + if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; + else secretEnv["AGENTMEMORY_SECRET"] = explicit; + return resolveClientSecret(url, secretEnv); } function secretTransportAllowed(url) { try { @@ -118,11 +130,7 @@ function secretTransportAllowed(url) { } function resolveHookEndpoint(env = process.env) { const url = hookSetting("AGENTMEMORY_URL", env) || DEFAULT_URL; - const secretEnv = { ...env }; - const explicit = hookSetting("AGENTMEMORY_SECRET", env); - if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; - else secretEnv["AGENTMEMORY_SECRET"] = explicit; - const secret = resolveClientSecret(url, secretEnv); + const secret = resolveSettingSecret(url, env); const blocked = !secretTransportAllowed(url); return { url, diff --git a/plugin/scripts/pre-tool-use.mjs b/plugin/scripts/pre-tool-use.mjs index f92eb24e79..80b6b7aa75 100755 --- a/plugin/scripts/pre-tool-use.mjs +++ b/plugin/scripts/pre-tool-use.mjs @@ -92,12 +92,24 @@ function canonicalSecretPath() { //#region src/hooks/_endpoint.ts const OPTION_PREFIX = "CLAUDE_PLUGIN_OPTION_"; const DEFAULT_URL = "http://localhost:3111"; +function filled(value) { + const trimmed = value?.trim(); + if (!trimmed || trimmed.startsWith("${") && trimmed.endsWith("}")) return void 0; + return value; +} function hookSetting(name, env = process.env) { const optionKey = `${OPTION_PREFIX}${name}`; - const option = env[optionKey]?.trim(); + const option = filled(env[optionKey])?.trim(); if (option) return option; if (name === "AGENTMEMORY_SECRET" && optionKey in env) return void 0; - return env[name]; + return filled(env[name]); +} +function resolveSettingSecret(url, env = process.env) { + const secretEnv = { ...env }; + const explicit = hookSetting("AGENTMEMORY_SECRET", env); + if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; + else secretEnv["AGENTMEMORY_SECRET"] = explicit; + return resolveClientSecret(url, secretEnv); } function secretTransportAllowed(url) { try { @@ -108,11 +120,7 @@ function secretTransportAllowed(url) { } function resolveHookEndpoint(env = process.env) { const url = hookSetting("AGENTMEMORY_URL", env) || DEFAULT_URL; - const secretEnv = { ...env }; - const explicit = hookSetting("AGENTMEMORY_SECRET", env); - if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; - else secretEnv["AGENTMEMORY_SECRET"] = explicit; - const secret = resolveClientSecret(url, secretEnv); + const secret = resolveSettingSecret(url, env); const blocked = !secretTransportAllowed(url); return { url, diff --git a/plugin/scripts/session-start.mjs b/plugin/scripts/session-start.mjs index 60c1920ad8..52f4483ea0 100755 --- a/plugin/scripts/session-start.mjs +++ b/plugin/scripts/session-start.mjs @@ -93,12 +93,24 @@ function canonicalSecretPath() { //#region src/hooks/_endpoint.ts const OPTION_PREFIX = "CLAUDE_PLUGIN_OPTION_"; const DEFAULT_URL = "http://localhost:3111"; +function filled(value) { + const trimmed = value?.trim(); + if (!trimmed || trimmed.startsWith("${") && trimmed.endsWith("}")) return void 0; + return value; +} function hookSetting(name, env = process.env) { const optionKey = `${OPTION_PREFIX}${name}`; - const option = env[optionKey]?.trim(); + const option = filled(env[optionKey])?.trim(); if (option) return option; if (name === "AGENTMEMORY_SECRET" && optionKey in env) return void 0; - return env[name]; + return filled(env[name]); +} +function resolveSettingSecret(url, env = process.env) { + const secretEnv = { ...env }; + const explicit = hookSetting("AGENTMEMORY_SECRET", env); + if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; + else secretEnv["AGENTMEMORY_SECRET"] = explicit; + return resolveClientSecret(url, secretEnv); } function secretTransportAllowed(url) { try { @@ -109,11 +121,7 @@ function secretTransportAllowed(url) { } function resolveHookEndpoint(env = process.env) { const url = hookSetting("AGENTMEMORY_URL", env) || DEFAULT_URL; - const secretEnv = { ...env }; - const explicit = hookSetting("AGENTMEMORY_SECRET", env); - if (explicit === void 0) delete secretEnv["AGENTMEMORY_SECRET"]; - else secretEnv["AGENTMEMORY_SECRET"] = explicit; - const secret = resolveClientSecret(url, secretEnv); + const secret = resolveSettingSecret(url, env); const blocked = !secretTransportAllowed(url); return { url, diff --git a/src/hooks/_endpoint.ts b/src/hooks/_endpoint.ts index 892d23cd23..64ec98f288 100644 --- a/src/hooks/_endpoint.ts +++ b/src/hooks/_endpoint.ts @@ -9,15 +9,29 @@ export interface HookEndpoint { blocked: boolean; } +function filled(value: string | undefined): string | undefined { + const trimmed = value?.trim(); + if (!trimmed || (trimmed.startsWith("${") && trimmed.endsWith("}"))) return undefined; + return value; +} + export function hookSetting( - name: "AGENTMEMORY_URL" | "AGENTMEMORY_SECRET", + name: string, env: NodeJS.ProcessEnv = process.env, ): string | undefined { const optionKey = `${OPTION_PREFIX}${name}`; - const option = env[optionKey]?.trim(); + const option = filled(env[optionKey])?.trim(); if (option) return option; if (name === "AGENTMEMORY_SECRET" && optionKey in env) return undefined; - return env[name]; + return filled(env[name]); +} + +export function resolveSettingSecret(url: string, env: NodeJS.ProcessEnv = process.env): string { + const secretEnv: NodeJS.ProcessEnv = { ...env }; + const explicit = hookSetting("AGENTMEMORY_SECRET", env); + if (explicit === undefined) delete secretEnv["AGENTMEMORY_SECRET"]; + else secretEnv["AGENTMEMORY_SECRET"] = explicit; + return resolveClientSecret(url, secretEnv); } export function secretTransportAllowed(url: string): boolean { @@ -30,11 +44,7 @@ export function secretTransportAllowed(url: string): boolean { export function resolveHookEndpoint(env: NodeJS.ProcessEnv = process.env): HookEndpoint { const url = hookSetting("AGENTMEMORY_URL", env) || DEFAULT_URL; - const secretEnv: NodeJS.ProcessEnv = { ...env }; - const explicit = hookSetting("AGENTMEMORY_SECRET", env); - if (explicit === undefined) delete secretEnv["AGENTMEMORY_SECRET"]; - else secretEnv["AGENTMEMORY_SECRET"] = explicit; - const secret = resolveClientSecret(url, secretEnv); + const secret = resolveSettingSecret(url, env); const blocked = !secretTransportAllowed(url); return { url, secret: blocked ? "" : secret, blocked }; } diff --git a/src/mcp/plugin-bridge.ts b/src/mcp/plugin-bridge.ts index 0196788103..1ec1c56f36 100644 --- a/src/mcp/plugin-bridge.ts +++ b/src/mcp/plugin-bridge.ts @@ -1,7 +1,7 @@ import { pathToFileURL } from "node:url"; import { realpathSync } from "node:fs"; import { VERSION } from "../version.js"; -import { resolveClientSecret } from "../secret-store.js"; +import { resolveSettingSecret } from "../hooks/_endpoint.js"; import { resolveEnvOrEmpty } from "./rest-proxy.js"; import { createStdioTransport, JsonRpcError, type RequestHandler } from "./transport.js"; @@ -17,7 +17,7 @@ export function createPluginBridge(): RequestHandler { const loopback = ["localhost", "localhost.", "[::1]"].includes(base.hostname) || /^127(?:\.\d{1,3}){3}$/.test(base.hostname); function resolveSecret(): string { - const secret = resolveClientSecret(base.href); + const secret = resolveSettingSecret(base.href); if (secret && base.protocol !== "https:" && !loopback) { throw new Error("AGENTMEMORY_URL requires HTTPS when AGENTMEMORY_SECRET is set, except for loopback URLs."); } diff --git a/src/mcp/rest-proxy.ts b/src/mcp/rest-proxy.ts index b70f28ff0a..9fcf752778 100644 --- a/src/mcp/rest-proxy.ts +++ b/src/mcp/rest-proxy.ts @@ -1,4 +1,4 @@ -import { resolveClientSecret } from "../secret-store.js"; +import { hookSetting, resolveSettingSecret } from "../hooks/_endpoint.js"; const DEFAULT_URL = "http://localhost:3111"; const DEFAULT_HEALTH_PROBE_TIMEOUT_MS = 2_000; const CALL_TIMEOUT_MS = 15_000; @@ -53,10 +53,7 @@ let probeInFlight: { url: string; forceProxy: boolean; promise: Promise // (DNS failure). Strip any literal placeholder we see so the fallback // engages instead. export function resolveEnvOrEmpty(name: string): string { - const raw = process.env[name]; - if (!raw) return ""; - if (raw.startsWith("${") && raw.endsWith("}")) return ""; - return raw; + return hookSetting(name) ?? ""; } function baseUrl(): string { @@ -74,7 +71,7 @@ function baseUrl(): string { } function authHeader(url: string): Record { - const secret = resolveClientSecret(url); + const secret = resolveSettingSecret(url); return secret ? { authorization: `Bearer ${secret}` } : {}; } diff --git a/test/codex-plugin.test.ts b/test/codex-plugin.test.ts index 0789cd8169..55bb167f22 100644 --- a/test/codex-plugin.test.ts +++ b/test/codex-plugin.test.ts @@ -82,8 +82,8 @@ describe("Codex plugin manifest (developers.openai.com/codex/plugins)", () => { }>(join(pluginRoot, ".mcp.json")); expect(mcp.mcpServers.agentmemory?.env).toEqual({ - AGENTMEMORY_URL: "${user_config.agentmemory_url}", - AGENTMEMORY_SECRET: "${user_config.agentmemory_secret}", + CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL: "${user_config.agentmemory_url}", + CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET: "${user_config.agentmemory_secret}", }); expect(mcp.mcpServers.agentmemory?.command).toBe("node"); expect(mcp.mcpServers.agentmemory?.args).toEqual([ diff --git a/test/mcp-surface-default.test.ts b/test/mcp-surface-default.test.ts index ff741ab853..3fa3985f20 100644 --- a/test/mcp-surface-default.test.ts +++ b/test/mcp-surface-default.test.ts @@ -56,8 +56,8 @@ describe("MCP tool surface default", () => { command: "node", args: ["${CLAUDE_PLUGIN_ROOT}/scripts/plugin-bridge.mjs"], env: { - AGENTMEMORY_URL: "${user_config.agentmemory_url}", - AGENTMEMORY_SECRET: "${user_config.agentmemory_secret}", + CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL: "${user_config.agentmemory_url}", + CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET: "${user_config.agentmemory_secret}", }, }); const manifest = JSON.parse(readFileSync("plugin/.claude-plugin/plugin.json", "utf-8")); diff --git a/test/plugin-bridge-secret.test.ts b/test/plugin-bridge-secret.test.ts index 3d282e450c..dac9679174 100644 --- a/test/plugin-bridge-secret.test.ts +++ b/test/plugin-bridge-secret.test.ts @@ -75,3 +75,59 @@ describe("plugin bridge uses the daemon's local credential resolution", () => { }, ); }); + +function requestedUrl() { + return String(vi.mocked(fetch).mock.lastCall?.[0]); +} + +describe("plugin bridge reads Claude Code plugin settings", () => { + it("prefers the plugin URL and secret settings over the environment", async () => { + vi.stubEnv("AGENTMEMORY_URL", "https://env.example"); + vi.stubEnv("AGENTMEMORY_SECRET", "env-test-secret"); + vi.stubEnv("CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL", "https://chosen.example"); + vi.stubEnv("CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET", "chosen-test-secret"); + await createPluginBridge()("tools/list", {}); + expect(requestedUrl()).toBe("https://chosen.example/agentmemory/mcp/tools"); + expect(authorization()).toBe("Bearer chosen-test-secret"); + }); + + it("falls back to AGENTMEMORY_URL when the URL setting is empty", async () => { + vi.stubEnv("AGENTMEMORY_URL", "https://env.example"); + vi.stubEnv("CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL", ""); + await createPluginBridge()("tools/list", {}); + expect(requestedUrl()).toBe("https://env.example/agentmemory/mcp/tools"); + }); + + it("uses http://localhost:3111 when both URL sources are empty", async () => { + vi.stubEnv("AGENTMEMORY_URL", ""); + vi.stubEnv("CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL", ""); + await createPluginBridge()("tools/list", {}); + expect(requestedUrl()).toBe("http://localhost:3111/agentmemory/mcp/tools"); + }); + + it("does not send the environment secret when the secret setting is empty", async () => { + vi.stubEnv("AGENTMEMORY_URL", "https://env.example"); + vi.stubEnv("AGENTMEMORY_SECRET", "env-test-secret"); + vi.stubEnv("CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET", ""); + await createPluginBridge()("tools/list", {}); + expect(authorization()).toBeUndefined(); + }); + + it("still reads the local secret for a loopback URL when the secret setting is empty", async () => { + store("secret", "generated-test-secret\n"); + vi.stubEnv("AGENTMEMORY_SECRET", "env-test-secret"); + vi.stubEnv("CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET", ""); + await createPluginBridge()("tools/list", {}); + expect(authorization()).toBe("Bearer generated-test-secret"); + }); + + it("ignores unsubstituted placeholders in the plugin settings", async () => { + vi.stubEnv("AGENTMEMORY_URL", "https://env.example"); + vi.stubEnv("AGENTMEMORY_SECRET", "env-test-secret"); + vi.stubEnv("CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL", "${user_config.agentmemory_url}"); + vi.stubEnv("CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET", "${user_config.agentmemory_secret}"); + await createPluginBridge()("tools/list", {}); + expect(requestedUrl()).toBe("https://env.example/agentmemory/mcp/tools"); + expect(authorization()).toBeUndefined(); + }); +}); From f037d4011335cea3574407e82078c5e8227dd7c3 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Thu, 5 Nov 2026 00:20:01 +0530 Subject: [PATCH 2/3] fix(plugin): run Claude Code hooks in exec form --- CHANGELOG.md | 4 ++++ plugin/hooks/hooks.json | 36 +++++++++++++++++++---------- scripts/release-gate/run.mjs | 12 ++++++---- src/cli/connect/codex-hooks.ts | 28 +++++++++++----------- test/claude-code-with-hooks.test.ts | 23 ++++++++++++++---- test/codex-plugin.test.ts | 25 ++++++++++++++------ 6 files changed, 88 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f1df6403a..3bb91beba8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - `.env.example` names the Agent SDK route and documents `MAX_OBS_PER_SESSION` as a hard per-session cap (#1675, from #1503 by @Rex57). - The Claude Code plugin passes its URL and secret settings to the MCP server as `CLAUDE_PLUGIN_OPTION_AGENTMEMORY_URL` and `CLAUDE_PLUGIN_OPTION_AGENTMEMORY_SECRET`, the names the plugin directory accepts. An empty URL setting no longer hides `AGENTMEMORY_URL` from the environment, and an empty secret setting still never sends `AGENTMEMORY_SECRET` from the environment. +### Changed + +- The Claude Code plugin hooks run in exec form (`command` plus `args`), so the plugin path reaches Node as one argument with no shell quoting. The plugin and `connect claude-code --with-hooks` now need Claude Code 2.1.139 or later. + ### Known issues - On Windows, `agentmemory stop` takes about 60 s and each start opens empty console windows for the engine and workers. Both come from iii (iii-hq/iii#2430). diff --git a/plugin/hooks/hooks.json b/plugin/hooks/hooks.json index a13c99736c..4c7aa13abf 100644 --- a/plugin/hooks/hooks.json +++ b/plugin/hooks/hooks.json @@ -5,7 +5,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/session-start.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/session-start.mjs"] } ] } @@ -15,7 +16,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/prompt-submit.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/prompt-submit.mjs"] } ] } @@ -26,7 +28,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/pre-tool-use.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/pre-tool-use.mjs"] } ] } @@ -36,7 +39,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/post-tool-use.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/post-tool-use.mjs"] } ] } @@ -46,7 +50,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/post-tool-failure.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/post-tool-failure.mjs"] } ] } @@ -56,7 +61,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/pre-compact.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/pre-compact.mjs"] } ] } @@ -66,7 +72,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/subagent-start.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/subagent-start.mjs"] } ] } @@ -76,7 +83,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/subagent-stop.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/subagent-stop.mjs"] } ] } @@ -86,7 +94,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/notification.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/notification.mjs"] } ] } @@ -96,7 +105,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/task-completed.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/task-completed.mjs"] } ] } @@ -106,7 +116,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/stop.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/stop.mjs"] } ] } @@ -116,7 +127,8 @@ "hooks": [ { "type": "command", - "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/session-end.mjs\"" + "command": "node", + "args": ["${CLAUDE_PLUGIN_ROOT}/scripts/session-end.mjs"] } ] } diff --git a/scripts/release-gate/run.mjs b/scripts/release-gate/run.mjs index d5daad27c2..a49e2e1727 100644 --- a/scripts/release-gate/run.mjs +++ b/scripts/release-gate/run.mjs @@ -365,7 +365,7 @@ class Gate { for (const entry of entries) { if (entry.matcher && toolName && !new RegExp(`^(?:${entry.matcher})$`).test(toolName)) continue; if (entry.matcher && !toolName) continue; - for (const h of entry.hooks ?? []) if (h.type === "command") commands.push(h.command); + for (const h of entry.hooks ?? []) if (h.type === "command") commands.push(h); } return commands; } @@ -791,11 +791,15 @@ class Instance { const commands = gate.hookCommands(event, payload.tool_name); assert(commands.length > 0, `no bundled ${event} hook matches ${payload.tool_name ?? "this event"}`); const results = []; - for (const command of commands) { - const r = await run("sh", ["-c", command], { + const pluginRoot = join(gate.pkgDir, "plugin"); + for (const handler of commands) { + const [file, argv] = handler.args + ? [handler.command, handler.args.map((arg) => arg.replaceAll("${CLAUDE_PLUGIN_ROOT}", pluginRoot))] + : ["sh", ["-c", handler.command]]; + const r = await run(file, argv, { env: { ...gate.baseEnv(this.home), - CLAUDE_PLUGIN_ROOT: join(gate.pkgDir, "plugin"), + CLAUDE_PLUGIN_ROOT: pluginRoot, CLAUDE_PROJECT_DIR: this.project, AGENTMEMORY_URL: `http://127.0.0.1:${this.port}`, AGENTMEMORY_DATA_DIR: this.dataDir, diff --git a/src/cli/connect/codex-hooks.ts b/src/cli/connect/codex-hooks.ts index 554443f3f1..0b239b3fc3 100644 --- a/src/cli/connect/codex-hooks.ts +++ b/src/cli/connect/codex-hooks.ts @@ -2,7 +2,7 @@ import { existsSync, readFileSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; -type HookHandler = { type: string; command?: string; [key: string]: unknown }; +type HookHandler = { type: string; command?: string; args?: string[]; [key: string]: unknown }; type HookEntry = { matcher?: string; hooks: HookHandler[] }; export type HookManifest = { hooks: Record }; @@ -70,13 +70,11 @@ export function buildMergedHooks( for (const [event, entries] of Object.entries(ours.hooks)) { const resolvedEntries: HookEntry[] = entries.map((entry) => { const next: HookEntry = { - hooks: entry.hooks.map((handler) => ({ - ...handler, - command: handler.command?.replace( - /\$\{CLAUDE_PLUGIN_ROOT\}/g, - () => pluginRoot, - ), - })), + hooks: entry.hooks.map((handler) => { + const resolved: HookHandler = { ...handler, command: withPluginRoot(handler.command, pluginRoot) }; + if (handler.args) resolved.args = handler.args.map((arg) => withPluginRoot(arg, pluginRoot)!); + return resolved; + }), }; if (entry.matcher !== undefined) next.matcher = entry.matcher; return next; @@ -87,12 +85,16 @@ export function buildMergedHooks( return out; } +function withPluginRoot(value: string | undefined, pluginRoot: string): string | undefined { + return value?.replace(/\$\{CLAUDE_PLUGIN_ROOT\}/g, () => pluginRoot); +} + function isAgentmemoryHandler(handler: HookHandler, scriptsDir: string): boolean { - return ( - typeof handler.command === "string" && - normalizePathForCommandMatch(handler.command).includes( - normalizePathForCommandMatch(scriptsDir), - ) + const parts = [handler.command, ...(Array.isArray(handler.args) ? handler.args : [])]; + return parts.some( + (part) => + typeof part === "string" && + normalizePathForCommandMatch(part).includes(normalizePathForCommandMatch(scriptsDir)), ); } diff --git a/test/claude-code-with-hooks.test.ts b/test/claude-code-with-hooks.test.ts index bf60374742..e3d35af552 100644 --- a/test/claude-code-with-hooks.test.ts +++ b/test/claude-code-with-hooks.test.ts @@ -15,13 +15,15 @@ describe("buildMergedHooks against plugin/hooks/hooks.json (Claude Code)", () => expect(findPluginRoot()).toBe(PLUGIN_ROOT); }); - it("rewrites ${CLAUDE_PLUGIN_ROOT} to absolute pluginRoot in every command", () => { + it("rewrites ${CLAUDE_PLUGIN_ROOT} to absolute pluginRoot in every exec-form argument", () => { const merged = buildMergedHooks(null, PLUGIN_ROOT, "hooks.json"); for (const entries of Object.values(merged.hooks)) { for (const entry of entries) { for (const handler of entry.hooks) { - expect(handler.command).not.toContain("${CLAUDE_PLUGIN_ROOT}"); - expect(handler.command).toContain(`${PLUGIN_ROOT}/scripts/`); + expect(handler.command).toBe("node"); + expect(handler.args).toHaveLength(1); + expect(handler.args![0]).not.toContain("${CLAUDE_PLUGIN_ROOT}"); + expect(handler.args![0]).toContain(`${PLUGIN_ROOT}/scripts/`); } } } @@ -57,7 +59,7 @@ describe("buildMergedHooks against plugin/hooks/hooks.json (Claude Code)", () => expect( sessionStart.some((e) => e.hooks.some((h) => - h.command.includes(`${PLUGIN_ROOT}/scripts/session-start.mjs`), + h.args?.includes(`${PLUGIN_ROOT}/scripts/session-start.mjs`), ), ), ).toBe(true); @@ -75,6 +77,19 @@ describe("buildMergedHooks against plugin/hooks/hooks.json (Claude Code)", () => ]); }); + it("replaces an earlier shell-form agentmemory install with the exec form", () => { + const existing: HookManifest = { + hooks: { + SessionStart: [ + { hooks: [{ type: "command", command: `node "${PLUGIN_ROOT}/scripts/session-start.mjs"` }] }, + ], + }, + }; + expect(buildMergedHooks(existing, PLUGIN_ROOT, "hooks.json").hooks["SessionStart"]).toEqual( + buildMergedHooks(null, PLUGIN_ROOT, "hooks.json").hooks["SessionStart"], + ); + }); + it("re-install strips previous agentmemory entries (idempotent)", () => { const first = buildMergedHooks(null, PLUGIN_ROOT, "hooks.json"); const second = buildMergedHooks(first, PLUGIN_ROOT, "hooks.json"); diff --git a/test/codex-plugin.test.ts b/test/codex-plugin.test.ts index 55bb167f22..7051d05f26 100644 --- a/test/codex-plugin.test.ts +++ b/test/codex-plugin.test.ts @@ -9,7 +9,7 @@ function readJson(path: string): T { return JSON.parse(readFileSync(path, "utf-8")) as T; } -type HookHandler = { type: string; command: string }; +type HookHandler = { type: string; command: string; args?: string[] }; type HookEntry = { hooks: HookHandler[] }; function hookCommands(path: string): string[] { @@ -21,13 +21,24 @@ function hookCommands(path: string): string[] { describe("Plugin hook manifests", () => { it("quote plugin script paths so roots with spaces stay intact", () => { - for (const manifest of ["hooks.json", "hooks.codex.json"]) { - const commands = hookCommands(join(pluginRoot, "hooks", manifest)); - expect(commands.length, `${manifest} should contain hook commands`).toBeGreaterThan(0); + const commands = hookCommands(join(pluginRoot, "hooks", "hooks.codex.json")); + expect(commands.length, "hooks.codex.json should contain hook commands").toBeGreaterThan(0); - for (const command of commands) { - expect(command).toMatch(/^node "\$\{CLAUDE_PLUGIN_ROOT\}\/scripts\/[^\s"]+\.mjs"$/); - } + for (const command of commands) { + expect(command).toMatch(/^node "\$\{CLAUDE_PLUGIN_ROOT\}\/scripts\/[^\s"]+\.mjs"$/); + } + }); + + it("runs Claude Code hooks in exec form with the plugin script as one argument", () => { + const manifest = readJson<{ hooks: Record }>(join(pluginRoot, "hooks", "hooks.json")); + const handlers = Object.values(manifest.hooks).flatMap((entries) => entries.flatMap((entry) => entry.hooks)); + expect(handlers.length).toBeGreaterThan(0); + + for (const handler of handlers) { + expect(handler.command).toBe("node"); + expect(handler.args).toHaveLength(1); + expect(handler.args?.[0]).toMatch(/^\$\{CLAUDE_PLUGIN_ROOT\}\/scripts\/[\w-]+\.mjs$/); + expect(existsSync(join(pluginRoot, handler.args![0]!.replace("${CLAUDE_PLUGIN_ROOT}/", "")))).toBe(true); } }); }); From 1d726c4b80fbe418596e8f7644fd124d870582cc Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Thu, 5 Nov 2026 00:20:01 +0530 Subject: [PATCH 3/3] docs(readme): describe where the plugin reads a remote server's URL and secret --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d31332571..458c1bde15 100644 --- a/README.md +++ b/README.md @@ -483,7 +483,7 @@ agentmemory connect claude-code --with-hooks ``` This merges the same hook commands into `~/.claude/settings.json` with absolute paths resolved to the bundled `plugin/` directory of the currently installed `@agentmemory/agentmemory` package. Re-run the command after upgrading agentmemory to refresh the paths. User entries in the same file are preserved; only previous agentmemory entries are replaced. Using the `/plugin install` path remains the recommended approach. -For remote or protected deployments, launch Claude Code with `AGENTMEMORY_URL` and `AGENTMEMORY_SECRET` set. The plugin passes both values through to its bundled MCP server; when `AGENTMEMORY_URL` is empty, the MCP shim uses `http://localhost:3111`. +For a remote server, enter its `https://` URL and secret in the plugin settings that Claude Code asks for when you enable the plugin. The hooks and the bundled MCP server read both from there. With the URL setting empty, they use `AGENTMEMORY_URL` from your environment, then `http://localhost:3111`; with the secret setting empty, they send only the local server's own secret, and only to a loopback address. ### Codex CLI (Codex plugin platform)