Created
August 9, 2025 07:59
-
-
Save na0x2c6/32cc9edfc10d505f27a2a12f850029bd to your computer and use it in GitHub Desktop.
Claude Code PreToolUse hook for WebFetch matcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import json | |
| import re | |
| import sys | |
| # Define validation rules as a list of (regex pattern, message) tuples | |
| VALIDATION_RULES = [ | |
| ( | |
| r"https://www.notion.so/", | |
| "Notion ページへのアクセスには WebFetch ツールではなく mcp__notion を利用してください", | |
| ), | |
| ( | |
| r"https://github.com/", | |
| "GitHub へのアクセスには WebFetch ツールではなく gh コマンドを利用してください", | |
| ), | |
| ] | |
| def validate_url(url: str) -> list[str]: | |
| issues = [] | |
| for pattern, message in VALIDATION_RULES: | |
| if re.search(pattern, url): | |
| issues.append(message) | |
| return issues | |
| try: | |
| input_data = json.load(sys.stdin) | |
| except json.JSONDecodeError as e: | |
| print(f"Error: Invalid JSON input: {e}", file=sys.stderr) | |
| sys.exit(1) | |
| tool_name = input_data.get("tool_name", "") | |
| tool_input = input_data.get("tool_input", {}) | |
| url = tool_input.get("url", "") | |
| if tool_name != "WebFetch": | |
| sys.exit(1) | |
| # Validate the url | |
| issues = validate_url(url) | |
| if issues: | |
| for message in issues: | |
| print(f"• {message}", file=sys.stderr) | |
| sys.exit(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment