|
1 | 1 | #!/usr/bin/env bash |
2 | | -# session-capture.sh - POST tool activity to SerialMemory API over HTTP |
| 2 | +# session-capture.sh - POST tool activity through MCP HTTP transport |
3 | 3 | # Called from PostToolUse hooks for Write/Edit/Bash |
4 | | -# Reads tool result JSON from stdin, POSTs to /api/captures/entry |
5 | | -# Designed for <100ms execution time — single HTTP POST, no MCP calls |
| 4 | +# Reads tool result JSON from stdin, sends JSON-RPC to MCP at localhost:4545 |
| 5 | +# The MCP client already has the API key — no extra env vars needed |
| 6 | +# Designed for <100ms execution time — single HTTP POST, fire-and-forget |
6 | 7 | set -euo pipefail |
7 | 8 |
|
8 | | -# SerialMemory API endpoint (set via env or default to localhost) |
9 | | -API_URL="${SERIALMEMORY_ENDPOINT:-http://localhost:5000}" |
10 | | -API_KEY="${SERIALMEMORY_API_KEY:-}" |
| 9 | +# MCP HTTP transport (always localhost, started alongside stdio) |
| 10 | +MCP_URL="${MCP_HTTP_URL:-http://localhost:4545}/mcp" |
| 11 | +MCP_TOKEN="${MCP_HTTP_TOKEN:-}" |
11 | 12 |
|
12 | 13 | # Session ID from Claude Code env var, fallback to date-based |
13 | 14 | SESSION_ID="${CLAUDE_SESSION_ID:-$(date +%Y%m%d)}" |
|
38 | 39 | RESULT="" |
39 | 40 | fi |
40 | 41 |
|
41 | | -# Timestamp in ISO 8601 |
42 | | -TS=$(date -u +"%Y-%m-%dT%H:%M:%SZ") |
43 | | - |
44 | | -# Build JSON payload |
| 42 | +# Build JSON-RPC payload for MCP tools/call |
45 | 43 | if command -v jq &>/dev/null; then |
46 | 44 | PAYLOAD=$(jq -nc \ |
47 | 45 | --arg sid "$SESSION_ID" \ |
48 | | - --arg ts "$TS" \ |
49 | 46 | --arg tool "$TOOL_NAME" \ |
50 | 47 | --arg file "$FILE_PATH" \ |
51 | 48 | --arg result "$RESULT" \ |
52 | | - '{session_id: $sid, ts: $ts, tool: $tool, file: $file, result: $result}') |
| 49 | + '{ |
| 50 | + jsonrpc: "2.0", |
| 51 | + id: 1, |
| 52 | + method: "tools/call", |
| 53 | + params: { |
| 54 | + name: "capture_entry", |
| 55 | + arguments: { |
| 56 | + session_id: $sid, |
| 57 | + tool: $tool, |
| 58 | + file: $file, |
| 59 | + result: $result |
| 60 | + } |
| 61 | + } |
| 62 | + }') |
53 | 63 | else |
54 | | - PAYLOAD="{\"session_id\":\"$SESSION_ID\",\"ts\":\"$TS\",\"tool\":\"$TOOL_NAME\",\"file\":\"$FILE_PATH\",\"result\":\"\"}" |
55 | | -fi |
56 | | - |
57 | | -# POST to API (fire-and-forget with timeout) |
58 | | -AUTH_HEADER="" |
59 | | -if [ -n "$API_KEY" ]; then |
60 | | - AUTH_HEADER="-H \"Authorization: Bearer $API_KEY\"" |
| 64 | + PAYLOAD="{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"capture_entry\",\"arguments\":{\"session_id\":\"$SESSION_ID\",\"tool\":\"$TOOL_NAME\",\"file\":\"$FILE_PATH\",\"result\":\"\"}}}" |
61 | 65 | fi |
62 | 66 |
|
| 67 | +# POST to MCP HTTP transport (fire-and-forget with 2s timeout) |
63 | 68 | if command -v curl &>/dev/null; then |
64 | | - curl -s -m 2 -X POST "${API_URL}/api/captures/entry" \ |
| 69 | + curl -s -m 2 -X POST "$MCP_URL" \ |
65 | 70 | -H "Content-Type: application/json" \ |
66 | | - ${API_KEY:+-H "Authorization: Bearer $API_KEY"} \ |
| 71 | + ${MCP_TOKEN:+-H "Authorization: Bearer $MCP_TOKEN"} \ |
67 | 72 | -d "$PAYLOAD" > /dev/null 2>&1 || true |
68 | 73 | elif command -v wget &>/dev/null; then |
69 | | - echo "$PAYLOAD" | wget -q -O /dev/null --timeout=2 \ |
| 74 | + wget -q -O /dev/null --timeout=2 \ |
70 | 75 | --header="Content-Type: application/json" \ |
71 | | - ${API_KEY:+--header="Authorization: Bearer $API_KEY"} \ |
72 | | - --post-data="$PAYLOAD" "${API_URL}/api/captures/entry" 2>/dev/null || true |
| 76 | + ${MCP_TOKEN:+--header="Authorization: Bearer $MCP_TOKEN"} \ |
| 77 | + --post-data="$PAYLOAD" "$MCP_URL" 2>/dev/null || true |
73 | 78 | fi |
74 | 79 |
|
75 | 80 | echo "Activity captured" |
0 commit comments