Block MCP Tool Calls Outside an Allowlist

The blunt, simple version of MCP governance: rather than trusting whatever's configured in settings.json for this session, this hook maintains its own independent allowlist and blocks anything outside it — useful as a belt-and-suspenders control alongside the trust-dialog Claude Code already shows.

MCP & Config Safety Event: PreToolUse Claude Code Open source · Free

1. Save this as the hook script

#!/usr/bin/env python3
"""PreToolUse hook — blocks mcp__ tool calls to servers outside an
allowlist. Wire this to matcher "mcp__.*"."""
import json, sys

data = json.load(sys.stdin)
tool_name = data.get("tool_name", "")

ALLOWED_PREFIXES = ("mcp__edgar_mcp__", "mcp__portfolio_risk_mcp__", "mcp__accounting_mcp__")

if tool_name.startswith("mcp__") and not tool_name.startswith(ALLOWED_PREFIXES):
    print(f"BLOCKED: {tool_name} is not on the MCP server allowlist.", file=sys.stderr)
    sys.exit(2)

sys.exit(0)

Save to a path inside your project, e.g. .claude/hooks/block-mcp-calls-outside-allowlist.sh, then chmod +x it.

2. Wire it into .claude/settings.json

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "mcp__.*",
        "hooks": [
          {"type": "command", "command": "python3 .claude/hooks/block-mcp-calls-outside-allowlist.py"}
        ]
      }
    ]
  }
}

Before you use this

A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.

Related hooks

← All hooks
FEEDBACK