cursor-hooks/INTEGRATION.md
This integration connects claude-mem's persistent memory system to Cursor's hook system, enabling:
┌─────────────┐
│ Cursor │
│ Agent │
└──────┬──────┘
│
│ Events (MCP, Shell, File Edits, Prompts)
│
▼
┌─────────────────────────────────────┐
│ Cursor Hooks System │
│ ┌────────────────────────────────┐ │
│ │ beforeSubmitPrompt │ │
│ │ afterMCPExecution │ │
│ │ afterShellExecution │ │
│ │ afterFileEdit │ │
│ │ stop │ │
│ └────────────────────────────────┘ │
└──────┬──────────────────────────────┘
│
│ HTTP Requests
│
▼
┌─────────────────────────────────────┐
│ Hook Scripts (Bash) │
│ ┌────────────────────────────────┐ │
│ │ session-init.sh │ │
│ │ context-inject.sh │ │
│ │ save-observation.sh │ │
│ │ save-file-edit.sh │ │
│ │ session-summary.sh │ │
│ └────────────────────────────────┘ │
└──────┬──────────────────────────────┘
│
│ HTTP API Calls
│
▼
┌─────────────────────────────────────┐
│ Claude-Mem Worker Service │
│ (Port 37777) │
│ ┌────────────────────────────────┐ │
│ │ /api/sessions/init │ │
│ │ /api/sessions/observations │ │
│ │ /api/sessions/summarize │ │
│ │ /api/context/inject │ │
│ └────────────────────────────────┘ │
└──────┬──────────────────────────────┘
│
│ Database Operations
│
▼
┌─────────────────────────────────────┐
│ SQLite Database │
│ + Chroma Vector DB │
└─────────────────────────────────────┘
User submits prompt
↓
beforeSubmitPrompt hook fires
↓
session-init.sh
├─ Extract conversation_id, project name
├─ POST /api/sessions/init
└─ Initialize session in claude-mem
↓
context-inject.sh
├─ GET /api/context/inject?project=...
└─ Fetch relevant context (for future use)
↓
Prompt proceeds to agent
Agent executes MCP tool or shell command
↓
afterMCPExecution / afterShellExecution hook fires
↓
save-observation.sh
├─ Extract tool_name, tool_input, tool_response
├─ Map to claude-mem observation format
├─ POST /api/sessions/observations
└─ Store observation in database
Agent edits file
↓
afterFileEdit hook fires
↓
save-file-edit.sh
├─ Extract file_path, edits
├─ Create "write_file" observation
├─ POST /api/sessions/observations
└─ Store file edit observation
Agent loop ends
↓
stop hook fires
↓
session-summary.sh
├─ POST /api/sessions/summarize
└─ Generate session summary for future retrieval
| Cursor Field | Claude-Mem Field | Notes |
|---|---|---|
conversation_id | contentSessionId | Stable across turns, used as primary session identifier |
generation_id | (fallback) | Used if conversation_id unavailable |
| Cursor Event | Claude-Mem Tool Name | Input Format |
|---|---|---|
afterMCPExecution | tool_name from event | tool_input as JSON |
afterShellExecution | "Bash" | {command: "..."} |
afterFileEdit | "write_file" | {file_path: "...", edits: [...]} |
| Source | Target | Notes |
|---|---|---|
workspace_roots[0] | Project name | Basename of workspace root directory |
POST /api/sessions/init - Initialize new sessionPOST /api/sessions/summarize - Generate session summaryPOST /api/sessions/observations - Store tool usage observationGET /api/context/inject?project=... - Get relevant context for injectionGET /api/readiness - Check if worker is readyLocated in ~/.claude-mem/settings.json:
CLAUDE_MEM_WORKER_PORT (default: 37777)CLAUDE_MEM_WORKER_HOST (default: 127.0.0.1)Located in hooks.json:
/api/readiness with 30 retries (6 seconds)conversation_id → use generation_idworkspace_root → use pwdcurl -s (silent)/dev/nullContext Injection: Cursor's beforeSubmitPrompt doesn't support prompt modification. Context must be retrieved via:
Transcript Access: Cursor hooks don't provide transcript paths, limiting summary quality compared to Claude Code integration.
Session Model: Uses conversation_id which may not perfectly match Claude Code's session model.
Tab Hooks: Currently only supports Agent hooks. Tab (inline completion) hooks could be added separately.
beforeTabFileRead and afterTabFileEdit hooksTest session initialization:
echo '{"conversation_id":"test-123","workspace_roots":["/tmp/test"],"prompt":"test"}' | \
~/.cursor/hooks/session-init.sh
Test observation capture:
echo '{"conversation_id":"test-123","hook_event_name":"afterMCPExecution","tool_name":"test","tool_input":{},"result_json":{}}' | \
~/.cursor/hooks/save-observation.sh
Test context retrieval:
curl "http://127.0.0.1:37777/api/context/inject?project=test"
http://localhost:37777See README.md for detailed troubleshooting steps.