Skip to content

Guards: check commands inside MCP tool inputs (#2) - #9

Merged
dioptx merged 4 commits into
mainfrom
amp/mcp-input-guards
Nov 14, 2026
Merged

dioptx merged 4 commits into
mainfrom
amp/mcp-input-guards

Conversation

@dioptx

@dioptx dioptx commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Closes the second gap in #2: weft guards now see a shell command inside an mcp__* tool call the same way they see it in Bash. Before this change, MCP calls were matched by tool name only.

What guards check now

  • Command keys. Strings under command, cmd, argv, script, code, shell, program, stdin, stdin_text and similar keys are checked. Command-list keys such as commands hold separate commands. The README has the full list and the key normalisation rules.
  • Split commands. A program key is joined with sibling args, argv, arguments, subcommand or params. {"cmd":"git","argv":["push"]} is checked as git push.
  • Mixed argv. Nested lists are flattened and scalars are turned into text. {"argv":["git",["push"],"origin"]} cannot hide a push. Objects inside a command list are left out of the joined argv, but their strings are still checked. An object passed as an argument to a program fails closed.
  • Encoded JSON. String values are JSON-decoded and scanned repeatedly, up to 8 decodes. A ninth decode blocks.
  • Data stays data. Prose under keys ending in transcript/postscript and file content beside a path/file_path key are not treated as commands.
  • Fail closed. Deep nesting (more than 32 levels around a command), the shared 1 s time budget and errors inside the guard code all block with exit 2. None of them crashes the hook with exit 1.
  • Per-guard options. mcp_input: false limits a guard to MCP tool names. mcp_fields adds extra command keys for one guard.

Known false blocks (deliberate, documented)

  • Literal argument text that matches a guard pattern, e.g. {"program":"note","args":["--title","git","push"]}.
  • An object passed as an argument to a program, e.g. {"command":"vscode.open","args":[{"uri":"…"}]}. These get a generic "error while checking" block.
  • Data that needs a 9th JSON decode.
  • A file write whose path key is not path/file_path, such as relative_path or pathInProject, when the content is entirely JSON containing a matching command.
  • Structured inputs larger than about 1–2 MB, which run past the 1 s budget.

Known gaps

  • Shell input under keys outside the list. Desktop Commander's interact_with_process {pid, input} is one example; input stays data on purpose. Guards can add it with mcp_fields: ["input"].
  • Argument keys outside the list (options, positional). mcp_fields cannot add argument keys.
  • Out of scope, as for Bash: shell obfuscation and values passed through env.

Testing

  • python -m pytest -q: 2987 passed. On main the suite has 471 tests.
  • New test files: tests/test_mcp_guard_engine.py, tests/test_mcp_bundled_guards.py and tests/test_template_preview_mcp.py.
  • The split-command, encoded-JSON, mixed-argv and stdin cases, both bypass and benign, run through guard_engine.evaluate and through the real hooks/weft-pretooluse.sh.
  • One existing test changed. {"command":"git","args":["push",42]} moved from the allowed list to the blocked shapes, because the mixed-argv rule now reads it as git push 42.
  • Not verified: live Claude Code behaviour, and the real schemas of MCP servers beyond the published ones checked here (Desktop Commander, mcp-server-commands).

Amp thread: https://ampcode.com/threads/T-01a1d9bd-083a-72bf-a268-4630a34c91f2

ampagent and others added 4 commits November 14, 2026 12:16
Guards now see shell commands carried in MCP tool inputs the way they see Bash commands: command keys, split program/argument fields, nested and mixed argv lists, and repeatedly JSON-encoded strings, with fail-closed depth, decode and time limits. Includes the crew implementation and two fix rounds after judging.

Co-authored-by: Dennis Pettas <[email protected]>
…closed

A list of objects under a command key (for example a Notion code block's
rich_text) aborted the whole check with 'error while checking'. walk()
already scans those objects' strings, so the plain argv join now skips
them; objects joined to a program still fail closed.

Co-authored-by: Dennis Pettas <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1d9bd-083a-72bf-a268-4630a34c91f2
{"command":"git","args":["push",42]} encoded the round-one rule that
dropped mixed argument lists; the owner's amended rule C stringifies scalars,
so the joined command is git push 42 and must block.

Co-authored-by: Dennis Pettas <[email protected]>
Shell servers such as mcp-server-commands (run_process stdin_text) pipe
commands to a shell through stdin, which the key list missed.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d9bd-083a-72bf-a268-4630a34c91f2
Co-authored-by: Dennis Pettas <[email protected]>
@dioptx
dioptx merged commit a5baed9 into main Nov 14, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants