|
| 1 | +# gograph Claude Code Hook Integration |
| 2 | + |
| 3 | +This guide shows how to manually integrate the gograph hook with your Claude Code settings. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- [bun](https://bun.sh) installed |
| 8 | +- Claude Code CLI installed and authenticated |
| 9 | +- gograph project with `gograph.yaml` configuration |
| 10 | + |
| 11 | +## Setup Steps |
| 12 | + |
| 13 | +### 1. Install Dependencies |
| 14 | + |
| 15 | +```bash |
| 16 | +# From project root |
| 17 | +bun install |
| 18 | +``` |
| 19 | + |
| 20 | +### 2. Update Claude Code Settings |
| 21 | + |
| 22 | +Add the hook configuration to your `~/.claude/settings.json`: |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "hooks": { |
| 27 | + "PreToolUse": [ |
| 28 | + { |
| 29 | + "matcher": "Write|Edit|MultiEdit", |
| 30 | + "hooks": [ |
| 31 | + { |
| 32 | + "type": "command", |
| 33 | + "command": "cd /path/to/your/gograph && bun run scripts/gograph-hook.ts" |
| 34 | + } |
| 35 | + ] |
| 36 | + } |
| 37 | + ] |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +**Important**: Replace `/path/to/your/gograph` with the actual absolute path to your gograph project root directory. |
| 43 | + |
| 44 | +### 3. Optional: Configure Global MCP Tool Permissions |
| 45 | + |
| 46 | +The hook includes `allowedTools` configuration to permit gograph MCP tools automatically. However, you can also add global permissions to your `~/.claude/settings.json` if preferred: |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "permissions": { |
| 51 | + "allow": [ |
| 52 | + "mcp__gograph__analyze_project", |
| 53 | + "mcp__gograph__get_package_structure", |
| 54 | + "mcp__gograph__query_dependencies", |
| 55 | + "mcp__gograph__get_function_info", |
| 56 | + "mcp__gograph__natural_language_query" |
| 57 | + ] |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Note: The hook automatically includes these permissions via `allowedTools` configuration, so this step is optional. |
| 63 | + |
| 64 | +### 4. Complete Settings Example |
| 65 | + |
| 66 | +Here's a minimal `~/.claude/settings.json` example: |
| 67 | + |
| 68 | +```json |
| 69 | +{ |
| 70 | + "hooks": { |
| 71 | + "PreToolUse": [ |
| 72 | + { |
| 73 | + "matcher": "Write|Edit|MultiEdit", |
| 74 | + "hooks": [ |
| 75 | + { |
| 76 | + "type": "command", |
| 77 | + "command": "cd /Users/username/projects/gograph && bun run scripts/gograph-hook.ts" |
| 78 | + } |
| 79 | + ] |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +The hook handles tool permissions automatically via the SDK's `allowedTools` configuration. |
| 87 | + |
| 88 | +## Testing the Integration |
| 89 | + |
| 90 | +### 1. Test the Hook Directly |
| 91 | + |
| 92 | +```bash |
| 93 | +cd scripts/ |
| 94 | +bun run test-hook.ts |
| 95 | +``` |
| 96 | + |
| 97 | +### 2. Test with Claude Code |
| 98 | + |
| 99 | +1. Open Claude Code in a Go project with gograph configuration |
| 100 | +2. Try editing or creating a `.go` file |
| 101 | +3. The hook should trigger and provide analysis before the operation |
| 102 | +4. Check logs at `~/.claude/gograph-hook.log` if needed |
| 103 | + |
| 104 | +## How It Works |
| 105 | + |
| 106 | +1. **Trigger**: Hook activates before `Write`, `Edit`, or `MultiEdit` operations on `.go` files |
| 107 | +2. **Analysis**: Uses Claude Code SDK with your existing session to analyze via gograph MCP |
| 108 | +3. **Output**: Provides dependency analysis, architectural recommendations, and integration guidance |
| 109 | +4. **Fallback**: If MCP analysis fails, provides static analysis based on gograph configuration |
| 110 | + |
| 111 | +## Configuration Options |
| 112 | + |
| 113 | +### Hook Matchers |
| 114 | + |
| 115 | +You can customize which operations trigger the hook: |
| 116 | + |
| 117 | +```json |
| 118 | +{ |
| 119 | + "hooks": { |
| 120 | + "PreToolUse": [ |
| 121 | + { |
| 122 | + "matcher": "Write|Edit|MultiEdit", |
| 123 | + "hooks": [ |
| 124 | + { |
| 125 | + "type": "command", |
| 126 | + "command": "cd /path/to/your/gograph && bun run scripts/gograph-hook.ts" |
| 127 | + } |
| 128 | + ] |
| 129 | + } |
| 130 | + ] |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +You can also use more specific regex patterns: |
| 136 | +- `"Write"` - Only Write operations |
| 137 | +- `"Edit|MultiEdit"` - Only Edit operations |
| 138 | +- `".*"` - All tools |
| 139 | +- `"Notebook.*"` - All notebook operations |
| 140 | + |
| 141 | +### Working Directory |
| 142 | + |
| 143 | +The hook automatically detects your project's working directory from the Claude Code session. |
| 144 | + |
| 145 | +### Permissions |
| 146 | + |
| 147 | +Without the permissions configuration, Claude Code will prompt you to approve each MCP tool use. Adding the permissions list prevents these prompts for a smoother experience. |
| 148 | + |
| 149 | +## Troubleshooting |
| 150 | + |
| 151 | +### Hook Not Triggering |
| 152 | +- Check that the path in your settings.json is correct and absolute |
| 153 | +- Ensure bun is installed and accessible |
| 154 | +- Verify the hook file is executable: `chmod +x scripts/gograph-hook.ts` |
| 155 | + |
| 156 | +### Permission Errors |
| 157 | +- Add the gograph MCP tool permissions to your settings.json |
| 158 | +- Ensure your gograph MCP server is running and connected |
| 159 | + |
| 160 | +### Analysis Failures |
| 161 | +- Check that you have a `gograph.yaml` file in your project |
| 162 | +- Verify gograph MCP tools are working: test with Claude Code directly |
| 163 | +- Check logs at `~/.claude/gograph-hook.log` |
| 164 | + |
| 165 | +## Security Considerations |
| 166 | + |
| 167 | +- The hook uses your existing Claude Code authentication session |
| 168 | +- No API keys or additional authentication required |
| 169 | +- Only processes Go files in projects with gograph configuration |
| 170 | +- Logs activity to `~/.claude/gograph-hook.log` for transparency |
| 171 | + |
| 172 | +## Uninstalling |
| 173 | + |
| 174 | +To remove the hook integration: |
| 175 | + |
| 176 | +1. Remove the hook entry from your `~/.claude/settings.json` |
| 177 | +2. Optionally remove the gograph MCP permissions |
| 178 | +3. Delete the scripts directory if no longer needed |
0 commit comments