Rate-Limit Market-Data MCP Queries
Many market-data feeds are contractually rate-limited, and a runaway agent loop can blow through a monthly query cap in minutes. This enforces a local sliding-window limit independently of whatever the data vendor's own API returns, so the overage is caught before the bill arrives.
1. Save this as the hook script
#!/usr/bin/env python3
"""PreToolUse hook — blocks a market-data MCP tool call once more than N
calls have happened in the last 60 seconds. Wire to matcher
"mcp__market_data_mcp__.*"."""
import json, sys, time
from pathlib import Path
data = json.load(sys.stdin)
LIMIT_PER_MINUTE = 30
state_path = Path(data.get("cwd", ".")) / ".claude" / "market_data_call_times.txt"
now = time.time()
times = []
if state_path.exists():
times = [float(t) for t in state_path.read_text().splitlines() if t.strip()]
times = [t for t in times if now - t < 60]
if len(times) >= LIMIT_PER_MINUTE:
print(f"BLOCKED: {len(times)} market-data calls in the last 60s, "
f"at the {LIMIT_PER_MINUTE}/min contractual limit.", file=sys.stderr)
sys.exit(2)
times.append(now)
state_path.parent.mkdir(parents=True, exist_ok=True)
state_path.write_text("\n".join(str(t) for t in times))
sys.exit(0)
Save to a path inside your project, e.g. .claude/hooks/rate-limit-market-data-mcp-queries.sh, then chmod +x it.
2. Wire it into .claude/settings.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "mcp__market_data_mcp__.*",
"hooks": [
{"type": "command", "command": "python3 .claude/hooks/rate-limit-market-data-mcp-queries.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