Initial commit

This commit is contained in:
Zhongwei Li
2025-11-29 18:26:43 +08:00
commit 0784802e81
8 changed files with 263 additions and 0 deletions

34
hooks/hooks.json Normal file
View File

@@ -0,0 +1,34 @@
{
"description": "Web search and Tavily integration hooks",
"hooks": {
"PreToolUse": [
{
"matcher": "WebFetch",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks/scripts/webfetch_to_tavily_extract.py"
}
]
},
{
"matcher": "WebSearch",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks/scripts/websearch_to_tavily_search.py"
}
]
},
{
"matcher": "mcp__tavily__tavily-extract",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks/scripts/tavily_extract_to_advanced.py"
}
]
}
]
}
}

View File

@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""
PreToolUse hook: intercept mcp__tavily__tavily-extract
- Block GitHub URLs and suggest using GitHub MCP tools instead
- Otherwise, upgrade extract_depth to "advanced"
"""
import json
import sys
try:
data = json.load(sys.stdin)
tool_input = data["tool_input"]
urls = tool_input.get("urls", [])
# Check for GitHub URLs
github_domains = ("github.com", "raw.githubusercontent.com", "gist.github.com")
github_urls = [url for url in urls if any(domain in url for domain in github_domains)]
if github_urls:
# Block and suggest GitHub MCP tools
print(json.dumps({
"systemMessage": "GitHub URL detected in Tavily extract tool. AI is directed to use GitHub MCP tools instead.",
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": "GitHub URL detected. Please use GitHub MCP tools (mcp__github__*) for more robust data retrieval."
},
}, separators=(',', ':')))
sys.exit(2)
# Always ensure extract_depth="advanced" for non-GitHub URLs
tool_input["extract_depth"] = "advanced"
# Allow the call to proceed
print(json.dumps({
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "allow",
"permissionDecisionReason": "Automatically upgrading Tavily extract to advanced mode for better content extraction"
}
}, separators=(',', ':')))
sys.exit(0)
except (KeyError, json.JSONDecodeError) as err:
print(f"hook-error: {err}", file=sys.stderr)
sys.exit(1)

View File

@@ -0,0 +1,23 @@
#!/usr/bin/env python3
"""
PreToolUse hook: intercept WebFetch → suggest using tavily-extract instead
"""
import json
import sys
try:
data = json.load(sys.stdin)
url = data["tool_input"]["url"]
except (KeyError, json.JSONDecodeError) as err:
print(f"hook-error: {err}", file=sys.stderr)
sys.exit(1)
print(json.dumps({
"systemMessage": "WebFetch detected. AI is directed to use Tavily extract instead.",
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": f"Please use mcp__tavily__tavily-extract with urls: ['{url}'] and extract_depth: 'advanced'"
}
}, separators=(',', ':')))
sys.exit(0)

View File

@@ -0,0 +1,24 @@
#!/usr/bin/env python3
"""
PreToolUse hook: intercept WebSearch → suggest using Tavily search instead
"""
import json
import sys
try:
data = json.load(sys.stdin)
tool_input = data["tool_input"]
query = tool_input["query"]
except (KeyError, json.JSONDecodeError) as err:
print(f"hook-error: {err}", file=sys.stderr)
sys.exit(1)
print(json.dumps({
"systemMessage": "WebSearch detected. AI is directed to use Tavily search instead.",
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": f"Please use mcp__tavily__tavily-search with query: '{query}'"
}
}, separators=(',', ':')))
sys.exit(0)