#!/bin/bash # Post Tool Use Hook for Claude Code Monitor # Captures tool execution events and sends them to the backend # Read JSON from stdin INPUT=$(cat) # Extract fields using jq SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // "unknown"') TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // null') TOOL_INPUT=$(echo "$INPUT" | jq -c '.tool_input // {}') TOOL_OUTPUT=$(echo "$INPUT" | jq -r '.tool_response // null') TIMESTAMP=$(echo "$INPUT" | jq -r '.timestamp // (now | tonumber)') SUCCESS=$(echo "$INPUT" | jq -r '.success // true') # Build payload for backend PAYLOAD=$(jq -n \ --arg session_id "$SESSION_ID" \ --arg event_type "PostToolUse" \ --arg tool_name "$TOOL_NAME" \ --argjson tool_input "$TOOL_INPUT" \ --arg tool_output "$TOOL_OUTPUT" \ --arg timestamp "$TIMESTAMP" \ --argjson success "$SUCCESS" \ '{ session_id: $session_id, event_type: $event_type, tool_name: $tool_name, tool_input: $tool_input, tool_output: $tool_output, timestamp: ($timestamp | tonumber), success: $success }') # Send to backend API (asynchronous, non-blocking) # Priority: 1) Config file, 2) Environment variable, 3) Default localhost if [ -f ~/.claude/monitor_url ]; then MONITOR_URL=$(cat ~/.claude/monitor_url) else MONITOR_URL="${CLAUDE_MONITOR_URL:-http://localhost:8000}" fi curl -X POST \ -H "Content-Type: application/json" \ -d "$PAYLOAD" \ "${MONITOR_URL}/api/events" \ --max-time 2 \ --silent \ --show-error \ > /dev/null 2>&1 & # Exit immediately (don't wait for curl) exit 0