mcp-toolcall-policy 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kim Mawla
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,349 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcp-toolcall-policy
3
+ Version: 0.1.0
4
+ Summary: An execution-free policy decision engine for MCP tool calls, with a Claude Code PreToolUse hook adapter. It decides allow, deny, or ask from a YAML policy. It never runs tools or MCP servers.
5
+ Keywords: mcp,model-context-protocol,policy,claude-code,hooks,security
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Environment :: Console
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Operating System :: POSIX
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Topic :: Security
16
+ Classifier: Typing :: Typed
17
+ Requires-Dist: pyyaml>=6.0.3
18
+ Requires-Python: >=3.12
19
+ Project-URL: Repository, https://github.com/kimmawla/mcp-toolcall-policy
20
+ Project-URL: Issues, https://github.com/kimmawla/mcp-toolcall-policy/issues
21
+ Project-URL: Changelog, https://github.com/kimmawla/mcp-toolcall-policy/blob/main/CHANGELOG.md
22
+ Description-Content-Type: text/markdown
23
+
24
+ # mcp-toolcall-policy
25
+
26
+ > An execution-free policy decision engine for MCP tool calls, with a Claude Code PreToolUse hook.
27
+
28
+ Given an MCP tool call (tool name and arguments) and a YAML policy, it returns `allow`, `deny`, or
29
+ `ask` with the rule id and reason. Unmatched calls are denied by default. Anything it cannot parse or
30
+ check is denied with a fixed reason code.
31
+
32
+ **Status: v0.1.0, alpha.** Nothing here is a security guarantee.
33
+
34
+ "allow" means "a rule in your policy permits this call". It is not a safety verdict.
35
+
36
+ ## What it is and is not
37
+
38
+ - **Scope: MCP tool calls only.** The hook governs calls to MCP tools (`mcp__...`). Built-in tools
39
+ such as Bash, Edit, Write, Read, and NotebookEdit are not covered. They never reach the hook, and
40
+ the engine makes no decision about them. That includes edits to the policy file and to Claude
41
+ Code's settings (see [Hardening](#hardening)).
42
+ - **It is a decision engine.** It reads a policy and one tool call and returns a decision. It never
43
+ executes, imports, installs, connects to, or fetches anything named in a policy or a tool call.
44
+ - **It is not a proxy.** It does not sit between Claude Code and an MCP server, and it never sees
45
+ what a server does with the arguments.
46
+ - **The hook is the enforcement point.** Claude Code runs `mcp-toolcall-policy hook` before each
47
+ matching MCP tool call and acts on its answer. If the hook is not wired up, or fails without
48
+ `"onFailure": "block"`, nothing is enforced (see Limitations).
49
+ - **The MCP server is for authoring only.** The separate package
50
+ [`mcp-toolcall-policy-mcp`](server/README.md) lets you lint a pasted policy and test it against
51
+ pasted hook inputs. It enforces nothing on other tools.
52
+
53
+ ## Quick start
54
+
55
+ Steps 1 to 4 are the **minimal example**: the policy is in the project and the hook can sit in
56
+ project settings. That setup is easy to try, but the agent can edit it. For real use, follow
57
+ [Hardening](#hardening) as well.
58
+
59
+ ### 1. Install
60
+
61
+ ```bash
62
+ uv tool install mcp-toolcall-policy==0.1.0
63
+ ```
64
+
65
+ This puts `mcp-toolcall-policy` on your `PATH`. The hook needs the command to resolve on `PATH`
66
+ when Claude Code starts it. The engine depends on PyYAML only.
67
+
68
+ ### 2. Write a policy
69
+
70
+ `.claude/toolcall-policy.yaml` in your project:
71
+
72
+ ```yaml
73
+ version: 1
74
+ settings:
75
+ home: /home/zqvex
76
+ rules:
77
+ - id: files-read-project
78
+ effect: allow
79
+ server: zqvex-files
80
+ tool: read_file
81
+ when:
82
+ - {arg: path, path_under: ["/home/zqvex/project"]}
83
+ reason: Reads inside the project.
84
+ - id: files-write-ask
85
+ effect: ask
86
+ server: zqvex-files
87
+ tool: write_file
88
+ when:
89
+ - {arg: path, path_under: ["/home/zqvex/project"]}
90
+ - {arg: content, max_len: 65536}
91
+ reason: Writes need a prompt.
92
+ - id: no-delete
93
+ effect: deny
94
+ server: "*"
95
+ tool: delete_file
96
+ reason: Deletes are never permitted.
97
+ ```
98
+
99
+ Everything else is denied (`_default`). The full format is in
100
+ [docs/spec-policy.md](docs/spec-policy.md).
101
+
102
+ ### 3. Validate and test
103
+
104
+ ```bash
105
+ mcp-toolcall-policy validate --strict .claude/toolcall-policy.yaml
106
+ mcp-toolcall-policy test --policy .claude/toolcall-policy.yaml scenarios.yaml --require-coverage
107
+ ```
108
+
109
+ `validate --strict` fails on warnings such as a wildcard allow rule with no conditions, or a rule
110
+ that can never decide. `test` runs scenario files (spec section 8.1) and reports rules no scenario
111
+ reached. `explain --policy FILE < input.json` prints a per-rule trace for one hook input.
112
+
113
+ ### 4. Wire the Claude Code PreToolUse hook
114
+
115
+ In `.claude/settings.json` (project) or `~/.claude/settings.json` (user):
116
+
117
+ ```json
118
+ {
119
+ "hooks": {
120
+ "PreToolUse": [
121
+ {
122
+ "matcher": "^mcp__",
123
+ "hooks": [
124
+ {
125
+ "type": "command",
126
+ "command": "mcp-toolcall-policy",
127
+ "args": ["hook", "--policy", "${CLAUDE_PROJECT_DIR}/.claude/toolcall-policy.yaml"],
128
+ "onFailure": "block"
129
+ }
130
+ ]
131
+ }
132
+ ]
133
+ }
134
+ }
135
+ ```
136
+
137
+ Each part of this entry was checked against the
138
+ [Claude Code hooks reference](https://code.claude.com/docs/en/hooks) on 2026-11-21. Details are in
139
+ [docs/hook-contract.md](docs/hook-contract.md).
140
+
141
+ - **`"matcher": "^mcp__"`:** a matcher with characters like `^`, `.` or `*` is an unanchored
142
+ JavaScript regular expression, so the `^` makes it match only tool names that start with `mcp__`.
143
+ - **Exec form (`command` plus `args`):** the command runs with no shell, and
144
+ `${CLAUDE_PROJECT_DIR}` (the project root where the session started) is passed as one argument.
145
+ - **`"onFailure": "block"`:** per the hooks reference, a missing binary, crash, or timeout then
146
+ blocks the call. **Always set it.** The reference gives Claude Code v2.1.295 as the minimum
147
+ version for this field. That number is reported, not verified here.
148
+ - **Timeout:** the default for `command` hooks is 600 seconds. The hook decides in well under one.
149
+ - **`mcp_server` input field (Claude Code v2.1.274 or later):** older versions don't send it, so
150
+ only servers named in your policy resolve.
151
+
152
+ **Fail-closed requires `onFailure`.** In a smoke test (2026-11-28), a hook command that could not
153
+ start, with no `onFailure`, made Claude Code show "Executable not found in $PATH", and the tool call
154
+ **ran** (fail open). With `"onFailure": "block"` the same call was blocked. To check your setup,
155
+ point `command` at a name that does not exist (for example `zqvex-no-such-command`), make one MCP
156
+ call, confirm it is blocked, then set `command` back. Details are in
157
+ [docs/smoke-test.md](docs/smoke-test.md).
158
+
159
+ Optional: add `"--log", "${CLAUDE_PROJECT_DIR}/.claude/toolcall-decisions.jsonl"` to `args` for a
160
+ hash-chained decision log. The log never contains argument values. Check it with
161
+ `mcp-toolcall-policy log-verify FILE`.
162
+
163
+ **What you see on a denial (observed 2026-11-27, not documented):** Claude Code shows every denial
164
+ from this hook as a "hook error", with the reason text. The model sees the same reason. A policy
165
+ denial and a failed hook look alike, so use `explain` to see which rule fired. For a locally added
166
+ server, `mcp_server.source` came through as `"local"`. Details are in
167
+ [docs/hook-contract.md](docs/hook-contract.md#observed-behavior-smoke-test-2026-11-27).
168
+
169
+ ### Hardening
170
+
171
+ In the minimal example the agent can turn enforcement off. It reads which rule denied a call. With
172
+ Edit, Write, or Bash it can then rewrite the policy, the log, or the project settings that hold the
173
+ hook. Those built-in tools never reach the hook. See
174
+ [docs/threat-model.md](docs/threat-model.md#agent-tampering-with-policy-and-hook-configuration).
175
+
176
+ The settings and permission syntax below was checked against the Claude Code
177
+ [settings](https://code.claude.com/docs/en/settings),
178
+ [permissions](https://code.claude.com/docs/en/permissions) and
179
+ [hooks](https://code.claude.com/docs/en/hooks) docs, fetched 2026-11-27. Paths use invented
180
+ `zqvex` names; replace `/home/zqvex` with your home directory.
181
+
182
+ **1. Keep the policy outside the project, read-only to you.** Install it as another owner (here
183
+ root), so the account that runs Claude Code can read it but not write it:
184
+
185
+ ```bash
186
+ sudo install -d -o root -m 0755 /usr/local/etc/mcp-toolcall-policy
187
+ sudo install -o root -m 0644 toolcall-policy.yaml /usr/local/etc/mcp-toolcall-policy/
188
+ ```
189
+
190
+ Changing the policy then needs `sudo`, which the agent shouldn't have. The log can't be made
191
+ read-only, because the hook runs as you and appends to it. Keep it outside the project and store a
192
+ copy of its head hash somewhere the agent can't write (`log-verify --expected-head`).
193
+
194
+ **2. Configure the hook in user or managed settings, not project settings.** Put it in
195
+ `~/.claude/settings.json`, with absolute paths instead of `${CLAUDE_PROJECT_DIR}`:
196
+
197
+ ```json
198
+ {
199
+ "hooks": {
200
+ "PreToolUse": [
201
+ {
202
+ "matcher": "^mcp__",
203
+ "hooks": [
204
+ {
205
+ "type": "command",
206
+ "command": "mcp-toolcall-policy",
207
+ "args": [
208
+ "hook",
209
+ "--policy", "/usr/local/etc/mcp-toolcall-policy/toolcall-policy.yaml",
210
+ "--log", "/home/zqvex/.local/state/mcp-toolcall-policy/decisions.jsonl"
211
+ ],
212
+ "onFailure": "block"
213
+ }
214
+ ]
215
+ }
216
+ ]
217
+ }
218
+ }
219
+ ```
220
+
221
+ The `args` vector is passed with no shell, so `~` is not expanded. I did not verify whether `$HOME`
222
+ or `${HOME}` is expanded there, so the example spells the path out.
223
+
224
+ User settings are still a file you, and so the agent, can write. Managed settings are stronger.
225
+ They live at `/Library/Application Support/ClaudeCode/managed-settings.json` (macOS),
226
+ `/etc/claude-code/managed-settings.json` (Linux and WSL), or
227
+ `C:\Program Files\ClaudeCode\managed-settings.json` (Windows), and they take precedence over every
228
+ other scope. In managed settings, `allowManagedHooksOnly` blocks user, project, local, and plugin
229
+ hooks, so only managed hooks run. If you set it, the hook entry must be in managed settings too.
230
+ The docs name the key but I did not confirm its value format verbatim, so check the
231
+ [managed settings page](https://code.claude.com/docs/en/managed-settings) before relying on it.
232
+
233
+ **3. Deny edits to the policy and log paths.** Add `permissions.deny` rules to the same user (or
234
+ managed) settings file:
235
+
236
+ ```json
237
+ {
238
+ "permissions": {
239
+ "deny": [
240
+ "Edit(//usr/local/etc/mcp-toolcall-policy/**)",
241
+ "Edit(~/.local/state/mcp-toolcall-policy/**)",
242
+ "Edit(~/.claude/settings.json)"
243
+ ]
244
+ }
245
+ }
246
+ ```
247
+
248
+ Each of these points comes straight from the permissions docs:
249
+ - A leading `//` is an absolute path, and `~/` is your home directory. A single leading `/` anchors
250
+ at the settings file's location, not the filesystem root.
251
+ - `Edit` rules apply to all built-in tools that edit files, including Write and NotebookEdit. A
252
+ `Write(path)` rule is accepted but never matched, so always use `Edit(...)`.
253
+ - The third rule protects the settings file that holds the hook and these rules. That goes beyond
254
+ the policy and log paths, but without it the agent can remove the other two rules.
255
+ - Deny rules are evaluated before ask and allow rules. List keys such as `permissions.deny` are
256
+ combined across settings files, so a project file can't drop them.
257
+ - In Bash, deny rules cover redirection targets and file commands Claude Code recognizes (`cat`,
258
+ `sed`, `tee`, ...). They don't cover "arbitrary subprocesses that read or write files indirectly,
259
+ like a Python or Node script". This is why step 1, file ownership, matters most.
260
+
261
+ ## Commands
262
+
263
+ ```
264
+ mcp-toolcall-policy hook --policy FILE [--log FILE] # PreToolUse hook (stdin -> stdout)
265
+ mcp-toolcall-policy validate FILE [--strict] # errors and warnings
266
+ mcp-toolcall-policy test --policy FILE SCENARIO... # expected decisions and rule coverage
267
+ mcp-toolcall-policy explain --policy FILE < input.json # per-rule trace
268
+ mcp-toolcall-policy check --policy FILE < input.json # exit 0 allow, 3 ask, 2 deny
269
+ mcp-toolcall-policy log-verify FILE [--expected-head HEX]
270
+ mcp-toolcall-policy --version
271
+ ```
272
+
273
+ Only `hook` writes to stdout. Every other command reports on stderr.
274
+
275
+ ## Limitations (v0.1)
276
+
277
+ These match [spec section 9](docs/spec-policy.md#9-limitations-v01).
278
+
279
+ - **Enforcement depends on the host.** If the hook fails to start, Claude Code's behavior governs:
280
+ without `"onFailure": "block"` the call ran (observed 2026-11-28, `docs/smoke-test.md`). Per the
281
+ hooks reference, a crash or timeout behaves the same (documented, not verified). The minimum
282
+ version for `onFailure`, v2.1.295, is reported, not verified.
283
+ A matcher that doesn't cover a tool means no decision for that tool.
284
+ - **Built-in tools are not covered.** `^mcp__` doesn't match Bash, Edit, Write, or other built-in
285
+ tools, so the agent can use them to edit the policy, log, or settings (see Hardening).
286
+ - **Denial reasons reach the model.** Claude Code shows `deny` reasons to Claude, and (observed
287
+ 2026-11-27) to the user as a "hook error". Rule ids and `reason` text tell the agent which rule
288
+ fired.
289
+ - **`mcp_server.source` is logged but can't be used in rules.** Claude Code documents its value set
290
+ as open, so rules that name a server trust the configured name.
291
+ - **Name resolution:** calls resolve through `mcp_server.name` or through server names declared in
292
+ the policy. Tool names are never split on `__`, and anything that doesn't resolve is denied by
293
+ default. Claude Code before v2.1.274 doesn't send `mcp_server`.
294
+ - **Inputs that fail closed (deny) rather than being supported:**
295
+ - nested arguments under a conditioned key
296
+ - Windows-style paths
297
+ - non-ASCII or punycode hosts
298
+ - URL path matching
299
+ - IPv6 hosts that embed an IPv4 address (IPv4-mapped, IPv4-compatible including `::1`, NAT64,
300
+ 6to4)
301
+ - **Paths are lexical only.** Symlinks are not resolved.
302
+ - **Path matching is case-sensitive.** On a case-insensitive filesystem (the macOS default), a path
303
+ that differs from a deny root only in case can reach the file the deny rule names. The same goes
304
+ for characters a filesystem folds but NFC doesn't, such as long s (U+017F). Write deny roots in
305
+ every plausible case form, or prefer allow-listing.
306
+ - **`host_in` ignores ports.** Any port on an allowed host matches.
307
+ - **The decision log is tamper-evident, not tamper-proof.** Its file locking is POSIX-only, and it
308
+ never contains argument values.
309
+
310
+ ## Evaluation
311
+
312
+ The engine is tested against a bypass corpus of policies plus hook inputs with expected decisions
313
+ (`tests/fixtures/bypass/`). Results are counts only. Full method and limits are in
314
+ [docs/eval-heldout.md](docs/eval-heldout.md).
315
+
316
+ | Set | Cases | As expected | Not as expected |
317
+ |---|---|---|---|
318
+ | In-sample (developed against; written with the engine) | 88 | 88 | 0 |
319
+ | Held-out, first run (frozen before the run, run once, no tuning) | 39 | 39 | 0 |
320
+ | Held-out, regression run after spec-gap fixes (not an independent result) | 39 | 39 | 0 |
321
+
322
+ - **In-sample versus held-out:**
323
+ - The in-sample cases were written alongside the engine and are expected to pass.
324
+ - The held-out expectations were derived from the spec text by Claude in a separate chat
325
+ session, not the one that wrote the engine. The maintainer reviewed them, and they were then
326
+ hashed, committed, and run once.
327
+ - The held-out set was used up by that first run. Later runs on it are regression checks only.
328
+ - **Shared author:** the spec and the engine have the same author. Passing cases show agreement
329
+ with the spec, not that the spec covers every bypass.
330
+ - **Spec gaps:** three are recorded in [docs/spec-gaps.md](docs/spec-gaps.md):
331
+
332
+ | Gap | Status |
333
+ |---|---|
334
+ | Case-insensitive filesystems | Documented limitation |
335
+ | IPv4-embedded IPv6 hosts | Fixed: they fail closed |
336
+ | Ports not restricted by `host_in` | Documented limitation |
337
+
338
+ - **Scope:** the corpus is not exhaustive. Results apply to these cases only.
339
+
340
+ ## Docs
341
+
342
+ - Policy format and semantics: [docs/spec-policy.md](docs/spec-policy.md)
343
+ - Hook I/O and the settings entry: [docs/hook-contract.md](docs/hook-contract.md)
344
+ - Threat model and non-goals: [docs/threat-model.md](docs/threat-model.md)
345
+ - Release steps: [docs/release-checklist.md](docs/release-checklist.md)
346
+
347
+ ## License
348
+
349
+ MIT