Skip to content

Commit e35c63c

Browse files
fix: stop the Claude Code hook from approving run_tool calls (#45)
* fix: stop the Claude Code hook from approving run_tool calls The PreToolUse hook answered "allow" for every Glean run_tool call while ENABLE_HITL was on, to avoid a double prompt. Anthropic's plugin directory now holds any plugin whose hook can decide a permission question for the user, so v3.6.0 is held for review. The hook now only records Claude Code's permission mode, which the server uses to skip its own prompt under --dangerously-skip-permissions. It prints nothing, so Claude Code's normal permission flow always runs. The file is renamed to record-permission-mode.mjs to match what it does. Users see Claude Code's own prompt on run_tool again until they allow the tool permanently. The server's approval prompt still gates writes.
1 parent c304c84 commit e35c63c

5 files changed

Lines changed: 33 additions & 42 deletions

File tree

‎overrides/claude/glean/hooks/hooks.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"hooks": [
2020
{
2121
"type": "command",
22-
"command": "node \"${CLAUDE_PLUGIN_ROOT}/hooks/auto-approve-run-tool.mjs\""
22+
"command": "node \"${CLAUDE_PLUGIN_ROOT}/hooks/record-permission-mode.mjs\""
2323
}
2424
]
2525
}

overrides/claude/glean/hooks/auto-approve-run-tool.mjs renamed to overrides/claude/glean/hooks/record-permission-mode.mjs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
#!/usr/bin/env node
22
// PreToolUse hook for Claude Code.
33
//
4-
// When HITL is enabled, run_tool is gated by the plugin's own elicitation
5-
// prompt, so Claude Code's separate native "allow this tool?" prompt is
6-
// redundant (the double-prompt). With ENABLE_HITL=true we auto-approve the
7-
// run_tool call, leaving the HITL elicitation as the single gate.
4+
// Records Claude Code's live permission mode before each Glean run_tool call,
5+
// so the MCP server can skip its own elicitation gate when the user launched
6+
// with --dangerously-skip-permissions (permission_mode "bypassPermissions").
87
//
9-
// Safety: run_tool is read-only ONLY while HITL gates it. This hook runs only
10-
// under Claude Code, which always advertises the elicitation capability, so
11-
// ENABLE_HITL=true means run_tool's HITL prompt is active — never an ungated
12-
// write. When ENABLE_HITL is not "true" the hook does nothing and the normal
13-
// permission flow runs.
8+
// This hook makes no permission decision and prints nothing. Claude Code's
9+
// normal permission flow always runs. The server's elicitation prompt still
10+
// gates writes, except for tools the user told it to stop asking about.
1411
import fs from "node:fs";
1512
import os from "node:os";
1613
import path from "node:path";
@@ -27,7 +24,7 @@ let input = {};
2724
try {
2825
input = JSON.parse(readStdin());
2926
} catch {
30-
// Malformed/empty input: do nothing, let the normal permission flow run.
27+
// Malformed/empty input: nothing to record.
3128
}
3229

3330
const toolName = String(input.tool_name ?? "");
@@ -55,19 +52,17 @@ try {
5552
cfg?.mcpServers?.glean?.env ??
5653
{};
5754
} catch {
58-
// No readable config: do nothing.
55+
// No readable config: nothing to record.
5956
}
6057

58+
// The server reads the marker only when its own elicitation gate is on.
6159
if (env.ENABLE_HITL === "true") {
62-
// Record Claude Code's live permission mode so the MCP server can skip its
63-
// own elicitation gate when the user launched with
64-
// --dangerously-skip-permissions (permission_mode "bypassPermissions").
6560
// Written on every run_tool call and keyed by session id, so it is always
6661
// fresh for the call that immediately follows and never leaks across
6762
// sessions. The base dir and session-id sanitization MUST match
6863
// run-tool.ts (permissionModeMarkerPath) and start.sh's PLUGIN_DATA_DIR:
69-
// CLAUDE_PLUGIN_DATA when set, else ~/.glean. Best-effort — marker I/O must
70-
// never break the approval decision below.
64+
// CLAUDE_PLUGIN_DATA when set, else ~/.glean. Best-effort — a failed write
65+
// just means the server keeps prompting.
7166
try {
7267
const permissionMode = String(input.permission_mode ?? "");
7368
const sessionId = String(input.session_id ?? "")
@@ -84,18 +79,7 @@ if (env.ENABLE_HITL === "true") {
8479
);
8580
}
8681
} catch {
87-
// Ignore: a failed marker write just means the server keeps prompting.
82+
// Ignore: see above.
8883
}
89-
90-
process.stdout.write(
91-
JSON.stringify({
92-
hookSpecificOutput: {
93-
hookEventName: "PreToolUse",
94-
permissionDecision: "allow",
95-
permissionDecisionReason:
96-
"Glean run_tool is gated by its own HITL elicitation prompt; suppressing the redundant native prompt while ENABLE_HITL is on.",
97-
},
98-
}),
99-
);
10084
}
10185
process.exit(0);

‎shared/glean/mcp/src/policy/key.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ export const CAPABILITY_POLICY_KEY = "com.glean.mcp/capabilityPolicy";
1111
// HITL only makes sense once approval moves to the remote, and the remote is stateless:
1212
// it has no back-channel to send `elicitation/create` on, so remote-side approval waits
1313
// on MCP 2026-07-28 landing there. Until then a remote-disabled HITL would leave no gate
14-
// at all -- and under Claude Code it would be worse than having no policy, because the
15-
// plugin's PreToolUse hook auto-approves run_tool on the premise that the local prompt
16-
// IS the gate. A remote that needs to stop such a plugin deactivates the version.
14+
// at all -- run_tool advertises readOnlyHint on the premise that the local prompt IS
15+
// the gate. A remote that needs to stop such a plugin deactivates the version.
1716
//
1817
// Declaring it here is what would make the remote believe it is controllable, so the
1918
// honest signal is to leave it out until a build can honour it. A remote that sends

‎shared/glean/mcp/src/tools/run-tool.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ function primeElicitationCancellation(mcpServer: Server): void {
287287
}
288288

289289
// Path to the per-session permission-mode marker the PreToolUse hook writes
290-
// immediately before each run_tool call (see hooks/auto-approve-run-tool.mjs).
290+
// immediately before each run_tool call (see hooks/record-permission-mode.mjs).
291291
// The directory has to be the one the HOOK can compute, not the one this process
292292
// would prefer -- see hostSharedDataDir() in ../data-dir.ts.
293293
function permissionModeMarkerPath(): string {
@@ -303,7 +303,7 @@ function permissionModeMarkerPath(): string {
303303
// so any failure fails toward prompting, never toward a silent bypass.
304304
//
305305
// Resume safety: the PreToolUse hook rewrites this marker with the CURRENT mode
306-
// on every run_tool call (see hooks/auto-approve-run-tool.mjs), and PreToolUse
306+
// on every run_tool call (see hooks/record-permission-mode.mjs), and PreToolUse
307307
// always runs before the tool executes, so the value read here is the one
308308
// written for this exact call. A session first launched with
309309
// --dangerously-skip-permissions and later resumed WITHOUT it (same session id)

shared/glean/mcp/tests/auto-approve-hook.test.ts renamed to shared/glean/mcp/tests/permission-mode-hook.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { fileURLToPath } from "node:url";
88
const here = path.dirname(fileURLToPath(import.meta.url));
99
const HOOK = path.resolve(
1010
here,
11-
"../../../../overrides/claude/glean/hooks/auto-approve-run-tool.mjs",
11+
"../../../../overrides/claude/glean/hooks/record-permission-mode.mjs",
1212
);
1313

1414
interface HookResult {
@@ -84,13 +84,22 @@ const hitlOn = { ENABLE_HITL: "true" };
8484
const hitlOff = { ENABLE_HITL: "false" };
8585
const bypass = { permission_mode: "bypassPermissions", session_id: "sess-1" };
8686

87-
describe("auto-approve-run-tool hook (Claude Code PreToolUse)", () => {
88-
it("allows glean run_tool when HITL is on", async () => {
87+
describe("record-permission-mode hook (Claude Code PreToolUse)", () => {
88+
// Anthropic's plugin directory holds any plugin whose hook can decide a
89+
// permission question for the user (HOOK_GRANTS_PERMISSION). The check reads
90+
// the file statically, so neither the decision nor its words may appear
91+
// anywhere in it, comments included.
92+
it("contains no permission decision", async () => {
93+
const source = await fs.readFile(HOOK, "utf-8");
94+
expect(source).not.toMatch(/permissionDecision|allow|approve/i);
95+
});
96+
97+
it("prints nothing for glean run_tool when HITL is on", async () => {
8998
const { out } = await runHook(glean("run_tool"), hitlOn);
90-
expect(JSON.parse(out).hookSpecificOutput.permissionDecision).toBe("allow");
99+
expect(out.trim()).toBe("");
91100
});
92101

93-
it("never allows when HITL is off (safety)", async () => {
102+
it("prints nothing when HITL is off", async () => {
94103
const { out } = await runHook(glean("run_tool"), hitlOff);
95104
expect(out.trim()).toBe("");
96105
});
@@ -106,7 +115,7 @@ describe("auto-approve-run-tool hook (Claude Code PreToolUse)", () => {
106115
});
107116
});
108117

109-
describe("auto-approve-run-tool hook (permission-mode marker)", () => {
118+
describe("record-permission-mode hook (permission-mode marker)", () => {
110119
it("records the permission_mode marker for run_tool when HITL is on", async () => {
111120
const { marker, markerFiles } = await runHook(
112121
glean("run_tool"),
@@ -145,8 +154,7 @@ describe("auto-approve-run-tool hook (permission-mode marker)", () => {
145154
const { out, marker } = await runHook(glean("run_tool"), hitlOn, {
146155
session_id: "sess-1",
147156
});
148-
// Still auto-approves, just has no mode to record.
149-
expect(JSON.parse(out).hookSpecificOutput.permissionDecision).toBe("allow");
157+
expect(out.trim()).toBe("");
150158
expect(marker).toBeNull();
151159
});
152160

0 commit comments

Comments
 (0)