@colbymchenry/codegraph 0.3.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +681 -641
  3. package/dist/bin/codegraph.d.ts +7 -2
  4. package/dist/bin/codegraph.d.ts.map +1 -1
  5. package/dist/bin/codegraph.js +360 -140
  6. package/dist/bin/codegraph.js.map +1 -1
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js +43 -10
  9. package/dist/config.js.map +1 -1
  10. package/dist/context/index.d.ts +17 -4
  11. package/dist/context/index.d.ts.map +1 -1
  12. package/dist/context/index.js +182 -15
  13. package/dist/context/index.js.map +1 -1
  14. package/dist/db/index.d.ts.map +1 -1
  15. package/dist/db/index.js +21 -0
  16. package/dist/db/index.js.map +1 -1
  17. package/dist/db/migrations.d.ts +1 -1
  18. package/dist/db/migrations.d.ts.map +1 -1
  19. package/dist/db/migrations.js +19 -12
  20. package/dist/db/migrations.js.map +1 -1
  21. package/dist/db/queries.d.ts +32 -1
  22. package/dist/db/queries.d.ts.map +1 -1
  23. package/dist/db/queries.js +271 -118
  24. package/dist/db/queries.js.map +1 -1
  25. package/dist/db/schema.sql +163 -149
  26. package/dist/directory.d.ts +13 -1
  27. package/dist/directory.d.ts.map +1 -1
  28. package/dist/directory.js +85 -19
  29. package/dist/directory.js.map +1 -1
  30. package/dist/errors.d.ts +1 -1
  31. package/dist/errors.d.ts.map +1 -1
  32. package/dist/errors.js +7 -1
  33. package/dist/errors.js.map +1 -1
  34. package/dist/extraction/grammars.d.ts +9 -4
  35. package/dist/extraction/grammars.d.ts.map +1 -1
  36. package/dist/extraction/grammars.js +133 -65
  37. package/dist/extraction/grammars.js.map +1 -1
  38. package/dist/extraction/index.d.ts +6 -0
  39. package/dist/extraction/index.d.ts.map +1 -1
  40. package/dist/extraction/index.js +209 -44
  41. package/dist/extraction/index.js.map +1 -1
  42. package/dist/extraction/tree-sitter.d.ts +67 -0
  43. package/dist/extraction/tree-sitter.d.ts.map +1 -1
  44. package/dist/extraction/tree-sitter.js +980 -38
  45. package/dist/extraction/tree-sitter.js.map +1 -1
  46. package/dist/graph/traversal.d.ts.map +1 -1
  47. package/dist/graph/traversal.js +6 -2
  48. package/dist/graph/traversal.js.map +1 -1
  49. package/dist/index.d.ts +6 -38
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +91 -73
  52. package/dist/index.js.map +1 -1
  53. package/dist/installer/banner.js +7 -7
  54. package/dist/installer/claude-md-template.js +32 -32
  55. package/dist/installer/config-writer.d.ts +9 -0
  56. package/dist/installer/config-writer.d.ts.map +1 -1
  57. package/dist/installer/config-writer.js +126 -17
  58. package/dist/installer/config-writer.js.map +1 -1
  59. package/dist/installer/index.d.ts.map +1 -1
  60. package/dist/installer/index.js +11 -9
  61. package/dist/installer/index.js.map +1 -1
  62. package/dist/mcp/index.d.ts +14 -3
  63. package/dist/mcp/index.d.ts.map +1 -1
  64. package/dist/mcp/index.js +109 -29
  65. package/dist/mcp/index.js.map +1 -1
  66. package/dist/mcp/tools.d.ts +66 -1
  67. package/dist/mcp/tools.d.ts.map +1 -1
  68. package/dist/mcp/tools.js +442 -49
  69. package/dist/mcp/tools.js.map +1 -1
  70. package/dist/mcp/transport.d.ts.map +1 -1
  71. package/dist/mcp/transport.js +2 -0
  72. package/dist/mcp/transport.js.map +1 -1
  73. package/dist/resolution/frameworks/index.d.ts +1 -0
  74. package/dist/resolution/frameworks/index.d.ts.map +1 -1
  75. package/dist/resolution/frameworks/index.js +5 -1
  76. package/dist/resolution/frameworks/index.js.map +1 -1
  77. package/dist/resolution/frameworks/svelte.d.ts +9 -0
  78. package/dist/resolution/frameworks/svelte.d.ts.map +1 -0
  79. package/dist/resolution/frameworks/svelte.js +268 -0
  80. package/dist/resolution/frameworks/svelte.js.map +1 -0
  81. package/dist/resolution/import-resolver.d.ts.map +1 -1
  82. package/dist/resolution/import-resolver.js +3 -4
  83. package/dist/resolution/import-resolver.js.map +1 -1
  84. package/dist/resolution/index.d.ts +15 -2
  85. package/dist/resolution/index.d.ts.map +1 -1
  86. package/dist/resolution/index.js +153 -13
  87. package/dist/resolution/index.js.map +1 -1
  88. package/dist/resolution/name-matcher.d.ts.map +1 -1
  89. package/dist/resolution/name-matcher.js +7 -20
  90. package/dist/resolution/name-matcher.js.map +1 -1
  91. package/dist/resolution/types.d.ts +4 -0
  92. package/dist/resolution/types.d.ts.map +1 -1
  93. package/dist/search/query-utils.d.ts +25 -0
  94. package/dist/search/query-utils.d.ts.map +1 -0
  95. package/dist/search/query-utils.js +124 -0
  96. package/dist/search/query-utils.js.map +1 -0
  97. package/dist/sentry.d.ts +22 -0
  98. package/dist/sentry.d.ts.map +1 -0
  99. package/dist/sentry.js +159 -0
  100. package/dist/sentry.js.map +1 -0
  101. package/dist/sync/index.d.ts +4 -2
  102. package/dist/sync/index.d.ts.map +1 -1
  103. package/dist/sync/index.js +3 -5
  104. package/dist/sync/index.js.map +1 -1
  105. package/dist/types.d.ts +7 -1
  106. package/dist/types.d.ts.map +1 -1
  107. package/dist/types.js +13 -0
  108. package/dist/types.js.map +1 -1
  109. package/dist/utils.d.ts +89 -2
  110. package/dist/utils.d.ts.map +1 -1
  111. package/dist/utils.js +252 -3
  112. package/dist/utils.js.map +1 -1
  113. package/dist/vectors/embedder.d.ts +1 -1
  114. package/dist/vectors/embedder.d.ts.map +1 -1
  115. package/dist/vectors/embedder.js +2 -2
  116. package/dist/vectors/embedder.js.map +1 -1
  117. package/dist/vectors/search.d.ts.map +1 -1
  118. package/dist/vectors/search.js +33 -32
  119. package/dist/vectors/search.js.map +1 -1
  120. package/package.json +74 -67
  121. package/scripts/patch-tree-sitter-dart.js +112 -0
  122. package/scripts/postinstall.js +71 -68
  123. package/dist/sync/git-hooks.d.ts +0 -66
  124. package/dist/sync/git-hooks.d.ts.map +0 -1
  125. package/dist/sync/git-hooks.js +0 -281
  126. package/dist/sync/git-hooks.js.map +0 -1
package/README.md CHANGED
@@ -1,641 +1,681 @@
1
- <div align="center">
2
-
3
- # 🔮 CodeGraph
4
-
5
- ### Supercharge Claude Code with Semantic Code Intelligence
6
-
7
- **30% fewer tokens • 25% fewer tool calls • 100% local**
8
-
9
- [![npm version](https://img.shields.io/npm/v/@colbymchenry/codegraph.svg)](https://www.npmjs.com/package/@colbymchenry/codegraph)
10
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
11
- [![Node.js](https://img.shields.io/badge/Node.js-18+-green.svg)](https://nodejs.org/)
12
-
13
- <br />
14
-
15
- ### Get Started
16
-
17
- ```bash
18
- npx @colbymchenry/codegraph
19
- ```
20
-
21
- <sub>Interactive installer configures Claude Code automatically</sub>
22
-
23
- </div>
24
-
25
- ---
26
-
27
- ## 🚀 Why CodeGraph?
28
-
29
- When you ask Claude Code to work on a complex task, it spawns **Explore agents** that scan your codebase using grep, glob, and file reads. These agents consume tokens with every tool call.
30
-
31
- **CodeGraph gives those agents a semantic knowledge graph** — pre-indexed symbol relationships, call graphs, and code structure. Instead of scanning files, agents query the graph instantly.
32
-
33
- ### 📊 Benchmark Results
34
-
35
- We ran the same complex task 3 times with and without CodeGraph:
36
-
37
- | Metric | Without CodeGraph | With CodeGraph | Improvement |
38
- |--------|-------------------|----------------|-------------|
39
- | **Explore tokens** | 157.8k | 111.7k | **29% fewer** |
40
- | **Per-agent tokens** | 74.0k | 46.4k | **37% fewer** |
41
- | **Tool calls** | 60 | 45 | **25% fewer** |
42
- | **Main context usage** | 28.7% | 24.0% | **4.7% less** |
43
-
44
- <details>
45
- <summary><strong>Full benchmark data</strong></summary>
46
-
47
- **With CodeGraph:**
48
- | Test | Agents | Tool Uses | Explore Tokens | Plan Tokens | Time |
49
- |------|--------|-----------|----------------|-------------|------|
50
- | 1 | 3 | 54 | 149.7k | 76.4k | 1m 43s |
51
- | 2 | 2 | 41 | 102.1k | 74.8k | 1m 29s |
52
- | 3 | 2 | 40 | 83.3k | 63.3k | 1m 25s |
53
- | **Avg** | **2.3** | **45** | **111.7k** | **71.5k** | **1m 32s** |
54
-
55
- **Without CodeGraph:**
56
- | Test | Agents | Tool Uses | Explore Tokens | Plan Tokens | Time |
57
- |------|--------|-----------|----------------|-------------|------|
58
- | 1 | 3 | 74 | 177.3k | 80.5k | 1m 54s |
59
- | 2 | 2 | 55 | 149.3k | 64.0k | 1m 27s |
60
- | 3 | 2 | 51 | 146.7k | 62.3k | 1m 17s |
61
- | **Avg** | **2.3** | **60** | **157.8k** | **68.9k** | **1m 33s** |
62
-
63
- </details>
64
-
65
- ### 🔄 How It Works
66
-
67
- ```
68
- ┌─────────────────────────────────────────────────────────────────┐
69
- │ Claude Code │
70
- │ │
71
- │ "Implement user authentication" │
72
- │ │ │
73
-
74
- ┌─────────────────┐ ┌─────────────────┐
75
- │ Explore Agent │ ──── Explore Agent │ │
76
- └────────┬────────┘ └────────┬────────┘
77
- │ │ │ │
78
- └───────────┼────────────────────────┼─────────────────────────────┘
79
-
80
- ▼ ▼
81
- ┌───────────────────────────────────────────────────────────────────┐
82
- │ CodeGraph MCP Server │
83
- ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
84
- │ │ Search │ │ Callers │ │ Context │ │
85
- │ │ "auth" │ │ "login()" │ │ for task │ │
86
- └──────┬──────┘ └──────┬──────┘ └──────┬──────┘
87
- │ │ │
88
- └────────────────┼────────────────┘
89
-
90
- ┌───────────────────────┐
91
- SQLite Graph DB
92
- • 387 symbols │ │
93
- • 1,204 edges │ │
94
- │ • Instant lookups │
95
- └───────────────────────┘
96
- └───────────────────────────────────────────────────────────────────┘
97
- ```
98
-
99
- **Without CodeGraph:** Explore agents use `grep`, `glob`, and `Read` to scan files → many API calls, high token usage
100
-
101
- **With CodeGraph:** Explore agents query the graph via MCP tools → instant results, local processing, fewer tokens
102
-
103
- ---
104
-
105
- ## Key Features
106
-
107
- <table>
108
- <tr>
109
- <td width="33%" valign="top">
110
-
111
- ### 🧠 Smart Context Building
112
- One tool call returns everything Claude needs—entry points, related symbols, and code snippets. No more expensive exploration agents.
113
-
114
- </td>
115
- <td width="33%" valign="top">
116
-
117
- ### 🔍 Semantic Search
118
- Find code by meaning, not just text. Search for "authentication" and find `login`, `validateToken`, `AuthService`—even with different naming conventions.
119
-
120
- </td>
121
- <td width="33%" valign="top">
122
-
123
- ### 📈 Impact Analysis
124
- Know exactly what breaks before you change it. Trace callers, callees, and the full impact radius of any symbol.
125
-
126
- </td>
127
- </tr>
128
- <tr>
129
- <td width="33%" valign="top">
130
-
131
- ### 🌍 15+ Languages
132
- TypeScript, JavaScript, Python, Go, Rust, Java, C#, PHP, Ruby, C, C++, Swift, Kotlin—all with the same API.
133
-
134
- </td>
135
- <td width="33%" valign="top">
136
-
137
- ### 🔒 100% Local
138
- No data leaves your machine. No API keys. No external services. Everything runs on your local SQLite database.
139
-
140
- </td>
141
- <td width="33%" valign="top">
142
-
143
- ### ⚡ Always Fresh
144
- Git hooks automatically sync the index on every commit. Your code intelligence is always up to date.
145
-
146
- </td>
147
- </tr>
148
- </table>
149
-
150
- ---
151
-
152
- ## 🎯 Quick Start
153
-
154
- ### 1. Run the Installer
155
-
156
- ```bash
157
- npx @colbymchenry/codegraph
158
- ```
159
-
160
- The interactive installer will:
161
- - Configure the MCP server in `~/.claude.json`
162
- - Set up auto-allow permissions for CodeGraph tools
163
- - Add global instructions to `~/.claude/CLAUDE.md` (teaches Claude how to use CodeGraph)
164
- - Optionally initialize your current project
165
-
166
- ### 2. Restart Claude Code
167
-
168
- Restart Claude Code for the MCP server to load.
169
-
170
- ### 3. Initialize Projects
171
-
172
- For each project you want to use CodeGraph with:
173
-
174
- ```bash
175
- cd your-project
176
- codegraph init -i
177
- ```
178
-
179
- That's it! Claude Code will now use CodeGraph tools automatically when a `.codegraph/` directory exists.
180
-
181
- <details>
182
- <summary><strong>Manual Setup (Alternative)</strong></summary>
183
-
184
- If you prefer manual configuration:
185
-
186
- **Install globally:**
187
- ```bash
188
- npm install -g @colbymchenry/codegraph
189
- ```
190
-
191
- **Add to `~/.claude.json`:**
192
- ```json
193
- {
194
- "mcpServers": {
195
- "codegraph": {
196
- "type": "stdio",
197
- "command": "codegraph",
198
- "args": ["serve", "--mcp"]
199
- }
200
- }
201
- }
202
- ```
203
-
204
- **Add to `~/.claude/settings.json` (optional, for auto-allow):**
205
- ```json
206
- {
207
- "permissions": {
208
- "allow": [
209
- "mcp__codegraph__codegraph_search",
210
- "mcp__codegraph__codegraph_context",
211
- "mcp__codegraph__codegraph_callers",
212
- "mcp__codegraph__codegraph_callees",
213
- "mcp__codegraph__codegraph_impact",
214
- "mcp__codegraph__codegraph_node",
215
- "mcp__codegraph__codegraph_status"
216
- ]
217
- }
218
- }
219
- ```
220
-
221
- </details>
222
-
223
- <details>
224
- <summary><strong>Global Instructions Reference</strong></summary>
225
-
226
- The installer automatically adds these instructions to `~/.claude/CLAUDE.md`. This is provided here for reference:
227
-
228
- ```markdown
229
- ## CodeGraph
230
-
231
- CodeGraph builds a semantic knowledge graph of codebases for faster, smarter code exploration.
232
-
233
- ### If `.codegraph/` exists in the project
234
-
235
- **Use codegraph tools for faster exploration.** These tools provide instant lookups via the code graph instead of scanning files:
236
-
237
- | Tool | Use For |
238
- |------|---------|
239
- | `codegraph_search` | Find symbols by name (functions, classes, types) |
240
- | `codegraph_context` | Get relevant code context for a task |
241
- | `codegraph_callers` | Find what calls a function |
242
- | `codegraph_callees` | Find what a function calls |
243
- | `codegraph_impact` | See what's affected by changing a symbol |
244
- | `codegraph_node` | Get details + source code for a symbol |
245
-
246
- **When spawning Explore agents in a codegraph-enabled project:**
247
-
248
- Tell the Explore agent to use codegraph tools for faster exploration.
249
-
250
- **For quick lookups in the main session:**
251
- - Use `codegraph_search` instead of grep for finding symbols
252
- - Use `codegraph_callers`/`codegraph_callees` to trace code flow
253
- - Use `codegraph_impact` before making changes to see what's affected
254
-
255
- ### If `.codegraph/` does NOT exist
256
-
257
- At the start of a session, ask the user if they'd like to initialize CodeGraph:
258
-
259
- "I notice this project doesn't have CodeGraph initialized. Would you like me to run `codegraph init -i` to build a code knowledge graph?"
260
- ```
261
-
262
- </details>
263
-
264
- ---
265
-
266
- ## 📋 Requirements
267
-
268
- - Node.js >= 18.0.0
269
-
270
- ---
271
-
272
- ## 💻 CLI Usage
273
-
274
- ```bash
275
- codegraph # Run interactive installer
276
- codegraph install # Run interactive installer (explicit)
277
- codegraph init [path] # Initialize in a project
278
- codegraph index [path] # Full index
279
- codegraph sync [path] # Incremental update
280
- codegraph status [path] # Show statistics
281
- codegraph query <search> # Search symbols
282
- codegraph context <task> # Build context for AI
283
- codegraph hooks install # Install git auto-sync hook
284
- codegraph serve --mcp # Start MCP server
285
- ```
286
-
287
- ## 📖 CLI Commands
288
-
289
- ### `codegraph` / `codegraph install`
290
-
291
- Run the interactive installer for Claude Code integration. Configures MCP server and permissions automatically.
292
-
293
- ```bash
294
- codegraph # Run installer (when no args)
295
- codegraph install # Run installer (explicit)
296
- npx @colbymchenry/codegraph # Run via npx (no global install needed)
297
- ```
298
-
299
- The installer will:
300
- 1. Ask for installation location (global `~/.claude` or local `./.claude`)
301
- 2. Configure the MCP server in `claude.json`
302
- 3. Optionally set up auto-allow permissions
303
- 4. Add global instructions to `~/.claude/CLAUDE.md` (teaches Claude how to use CodeGraph)
304
- 5. For local installs: initialize and index the current project
305
-
306
- ### `codegraph init [path]`
307
-
308
- Initialize CodeGraph in a project directory. Creates a `.codegraph/` directory with the database and configuration.
309
-
310
- ```bash
311
- codegraph init # Initialize in current directory
312
- codegraph init /path/to/project # Initialize in specific directory
313
- codegraph init --index # Initialize and immediately index
314
- codegraph init --no-hooks # Skip git hook installation
315
- ```
316
-
317
- ### `codegraph index [path]`
318
-
319
- Index all files in the project. Extracts functions, classes, methods, and their relationships.
320
-
321
- ```bash
322
- codegraph index # Index current directory
323
- codegraph index --force # Force full re-index
324
- codegraph index --quiet # Suppress progress output
325
- ```
326
-
327
- ### `codegraph sync [path]`
328
-
329
- Incrementally sync changes since the last index. Only processes added, modified, or removed files.
330
-
331
- ```bash
332
- codegraph sync # Sync current directory
333
- codegraph sync --quiet # Suppress output
334
- ```
335
-
336
- ### `codegraph status [path]`
337
-
338
- Show index status and statistics.
339
-
340
- ```bash
341
- codegraph status
342
- ```
343
-
344
- Output includes:
345
- - Files indexed, nodes, edges
346
- - Nodes by kind (functions, classes, methods, etc.)
347
- - Files by language
348
- - Pending changes (if any)
349
- - Git hook status
350
-
351
- ### `codegraph query <search>`
352
-
353
- Search for symbols in the codebase by name.
354
-
355
- ```bash
356
- codegraph query "authenticate" # Search for symbols
357
- codegraph query "User" --kind class # Filter by kind
358
- codegraph query "process" --limit 20 # Limit results
359
- codegraph query "validate" --json # Output as JSON
360
- ```
361
-
362
- ### `codegraph context <task>`
363
-
364
- Build relevant code context for a task. Uses semantic search to find entry points, then expands through the graph to find related code.
365
-
366
- ```bash
367
- codegraph context "fix checkout bug"
368
- codegraph context "add user authentication" --format json
369
- codegraph context "refactor payment service" --max-nodes 30
370
- ```
371
-
372
- ### `codegraph hooks`
373
-
374
- Manage git hooks for automatic syncing.
375
-
376
- ```bash
377
- codegraph hooks install # Install post-commit hook
378
- codegraph hooks remove # Remove hook
379
- codegraph hooks status # Check if hook is installed
380
- ```
381
-
382
- ### `codegraph serve`
383
-
384
- Start CodeGraph as an MCP server for AI assistants.
385
-
386
- ```bash
387
- codegraph serve # Show MCP configuration help
388
- codegraph serve --mcp # Start MCP server (stdio)
389
- codegraph serve --mcp --path /project # Specify project path
390
- ```
391
-
392
- ## 🔌 MCP Tools Reference
393
-
394
- When running as an MCP server, CodeGraph exposes these tools to AI assistants. **These tools are designed to be used by Claude's Explore agents** for faster, more efficient codebase exploration.
395
-
396
- ### `codegraph_context`
397
-
398
- Build context for a specific task. Good for focused queries.
399
-
400
- ```
401
- codegraph_context(task: "fix checkout validation bug", maxNodes: 20)
402
- ```
403
-
404
- ### `codegraph_search`
405
-
406
- Quick symbol search by name. Returns locations only.
407
-
408
- ```
409
- codegraph_search(query: "UserService", kind: "class", limit: 10)
410
- ```
411
-
412
- ### `codegraph_callers` / `codegraph_callees`
413
-
414
- Find what calls a function, or what a function calls.
415
-
416
- ```
417
- codegraph_callers(symbol: "validatePayment", limit: 20)
418
- codegraph_callees(symbol: "processOrder", limit: 20)
419
- ```
420
-
421
- ### `codegraph_impact`
422
-
423
- Analyze what code would be affected by changing a symbol.
424
-
425
- ```
426
- codegraph_impact(symbol: "UserService", depth: 2)
427
- ```
428
-
429
- ### `codegraph_node`
430
-
431
- Get details about a specific symbol. Use `includeCode: true` only when needed.
432
-
433
- ```
434
- codegraph_node(symbol: "authenticate", includeCode: true)
435
- ```
436
-
437
- ### `codegraph_status`
438
-
439
- Check index health and statistics.
440
-
441
- ### How It Works With Claude Code
442
-
443
- Claude's **Explore agents** use these tools instead of grep/glob/Read for faster exploration:
444
-
445
- | Without CodeGraph | With CodeGraph | Benefit |
446
- |-------------------|----------------|---------|
447
- | `grep -r "auth"` | `codegraph_search("auth")` | Instant symbol lookup |
448
- | Multiple `Read` calls | `codegraph_context(task)` | Related code in one call |
449
- | Manual file tracing | `codegraph_callers/callees` | Call graph traversal |
450
- | Guessing impact | `codegraph_impact(symbol)` | Know what breaks |
451
-
452
- This hybrid approach gives you **~30% fewer tokens** and **~25% fewer tool calls** while letting Claude's native agents handle synthesis.
453
-
454
- ## 📚 Library Usage
455
-
456
- CodeGraph can also be used as a library in your Node.js applications:
457
-
458
- ```typescript
459
- import CodeGraph from '@colbymchenry/codegraph';
460
-
461
- // Initialize a new project
462
- const cg = await CodeGraph.init('/path/to/project');
463
-
464
- // Or open an existing one
465
- const cg = await CodeGraph.open('/path/to/project');
466
-
467
- // Index with progress callback
468
- await cg.indexAll({
469
- onProgress: (progress) => {
470
- console.log(`${progress.phase}: ${progress.current}/${progress.total}`);
471
- }
472
- });
473
-
474
- // Search for symbols
475
- const results = cg.searchNodes('UserService');
476
-
477
- // Get callers of a function
478
- const node = results[0].node;
479
- const callers = cg.getCallers(node.id);
480
-
481
- // Build context for a task
482
- const context = await cg.buildContext('fix login bug', {
483
- maxNodes: 20,
484
- includeCode: true,
485
- format: 'markdown'
486
- });
487
-
488
- // Get impact radius
489
- const impact = cg.getImpactRadius(node.id, 2);
490
-
491
- // Sync changes
492
- const syncResult = await cg.sync();
493
-
494
- // Clean up
495
- cg.close();
496
- ```
497
-
498
- ## ⚙️ How It Works
499
-
500
- ### 1. Extraction
501
-
502
- CodeGraph uses [tree-sitter](https://tree-sitter.github.io/) to parse source code into ASTs. Language-specific queries (`.scm` files) extract:
503
-
504
- - **Nodes**: Functions, methods, classes, interfaces, types, variables
505
- - **Edges**: Calls, imports, extends, implements, returns_type
506
-
507
- Each node gets a unique ID based on its kind, file path, name, and line number.
508
-
509
- ### 2. Storage
510
-
511
- All data is stored in a local SQLite database (`.codegraph/codegraph.db`):
512
-
513
- - **nodes** table: All code entities with metadata
514
- - **edges** table: Relationships between nodes
515
- - **files** table: File tracking for incremental updates
516
- - **node_vectors** / **vector_map**: Embeddings for semantic search (using sqlite-vss)
517
-
518
- ### 3. Reference Resolution
519
-
520
- After extraction, CodeGraph resolves references:
521
-
522
- 1. Match function calls to function definitions
523
- 2. Resolve imports to their source files
524
- 3. Link class inheritance and interface implementations
525
- 4. Apply framework-specific patterns (Express routes, etc.)
526
-
527
- ### 4. Semantic Search
528
-
529
- CodeGraph uses local embeddings (via [@xenova/transformers](https://github.com/xenova/transformers.js)) to enable semantic search:
530
-
531
- 1. Code symbols are embedded using a transformer model
532
- 2. Queries are embedded and compared using cosine similarity
533
- 3. Results are ranked by relevance
534
-
535
- ### 5. Graph Queries
536
-
537
- The graph structure enables powerful queries:
538
-
539
- - **Callers/Callees**: Direct call relationships
540
- - **Impact Radius**: BFS traversal to find all potentially affected code
541
- - **Dependencies**: What a symbol depends on
542
- - **Dependents**: What depends on a symbol
543
-
544
- ### 6. Context Building
545
-
546
- When you request context for a task:
547
-
548
- 1. Semantic search finds relevant entry points
549
- 2. Graph traversal expands to related code
550
- 3. Code snippets are extracted
551
- 4. Results are formatted for AI consumption
552
-
553
- ## ⚙️ Configuration
554
-
555
- The `.codegraph/config.json` file controls indexing behavior:
556
-
557
- ```json
558
- {
559
- "version": 1,
560
- "projectName": "my-project",
561
- "languages": ["typescript", "javascript"],
562
- "exclude": [
563
- "node_modules/**",
564
- "dist/**",
565
- "build/**",
566
- "*.min.js"
567
- ],
568
- "frameworks": ["express", "react"],
569
- "maxFileSize": 1048576,
570
- "gitHooksEnabled": true
571
- }
572
- ```
573
-
574
- ### Options
575
-
576
- | Option | Description | Default |
577
- |--------|-------------|---------|
578
- | `languages` | Languages to index (auto-detected if empty) | `[]` |
579
- | `exclude` | Glob patterns to ignore | `["node_modules/**", ...]` |
580
- | `frameworks` | Framework hints for better resolution | `[]` |
581
- | `maxFileSize` | Skip files larger than this (bytes) | `1048576` (1MB) |
582
- | `gitHooksEnabled` | Enable git hook installation | `true` |
583
-
584
- ## 🌐 Supported Languages
585
-
586
- | Language | Extension | Status |
587
- |----------|-----------|--------|
588
- | TypeScript | `.ts`, `.tsx` | Full support |
589
- | JavaScript | `.js`, `.jsx`, `.mjs` | Full support |
590
- | Python | `.py` | Full support |
591
- | Go | `.go` | Full support |
592
- | Rust | `.rs` | Full support |
593
- | Java | `.java` | Full support |
594
- | C# | `.cs` | Full support |
595
- | PHP | `.php` | Full support |
596
- | Ruby | `.rb` | Full support |
597
- | C | `.c`, `.h` | Full support |
598
- | C++ | `.cpp`, `.hpp`, `.cc` | Full support |
599
- | Swift | `.swift` | Basic support |
600
- | Kotlin | `.kt` | Basic support |
601
-
602
- ## 🔧 Troubleshooting
603
-
604
- ### "CodeGraph not initialized"
605
-
606
- Run `codegraph init` in your project directory first.
607
-
608
- ### Indexing is slow
609
-
610
- - Check if `node_modules` or other large directories are excluded
611
- - Use `--quiet` flag to reduce console output overhead
612
- - Consider increasing `maxFileSize` if you have large files to skip
613
-
614
- ### MCP server not connecting
615
-
616
- 1. Ensure the project is initialized and indexed
617
- 2. Check the path in your MCP configuration is correct
618
- 3. Verify `codegraph serve --mcp` works from the command line
619
- 4. Check Claude Code logs for connection errors
620
-
621
- ### Missing symbols in search
622
-
623
- - Run `codegraph sync` to pick up recent changes
624
- - Check if the file's language is supported
625
- - Verify the file isn't excluded by config patterns
626
-
627
- ---
628
-
629
- ## 📄 License
630
-
631
- MIT
632
-
633
- ---
634
-
635
- <div align="center">
636
-
637
- **Made for the Claude Code community** 🤖
638
-
639
- [Report Bug](https://github.com/colbymchenry/codegraph/issues) · [Request Feature](https://github.com/colbymchenry/codegraph/issues)
640
-
641
- </div>
1
+ <div align="center">
2
+
3
+ # 🔮 CodeGraph
4
+
5
+ ### Supercharge Claude Code with Semantic Code Intelligence
6
+
7
+ **30% fewer tokens • 25% fewer tool calls • 100% local**
8
+
9
+ [![npm version](https://img.shields.io/npm/v/@colbymchenry/codegraph.svg)](https://www.npmjs.com/package/@colbymchenry/codegraph)
10
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
11
+ [![Node.js](https://img.shields.io/badge/Node.js-18+-green.svg)](https://nodejs.org/)
12
+
13
+ [![Windows](https://img.shields.io/badge/Windows-supported-blue.svg)](#)
14
+ [![macOS](https://img.shields.io/badge/macOS-supported-blue.svg)](#)
15
+ [![Linux](https://img.shields.io/badge/Linux-supported-blue.svg)](#)
16
+
17
+ <br />
18
+
19
+ ### Get Started
20
+
21
+ ```bash
22
+ npx @colbymchenry/codegraph
23
+ ```
24
+
25
+ <sub>Interactive installer configures Claude Code automatically</sub>
26
+
27
+ </div>
28
+
29
+ ---
30
+
31
+ ## 🚀 Why CodeGraph?
32
+
33
+ When you ask Claude Code to work on a complex task, it spawns **Explore agents** that scan your codebase using grep, glob, and file reads. These agents consume tokens with every tool call.
34
+
35
+ **CodeGraph gives those agents a semantic knowledge graph** pre-indexed symbol relationships, call graphs, and code structure. Instead of scanning files, agents query the graph instantly.
36
+
37
+ ### 📊 Benchmark Results
38
+
39
+ We ran the same complex task 3 times with and without CodeGraph:
40
+
41
+ | Metric | Without CodeGraph | With CodeGraph | Improvement |
42
+ |--------|-------------------|----------------|-------------|
43
+ | **Explore tokens** | 157.8k | 111.7k | **29% fewer** |
44
+ | **Per-agent tokens** | 74.0k | 46.4k | **37% fewer** |
45
+ | **Tool calls** | 60 | 45 | **25% fewer** |
46
+ | **Main context usage** | 28.7% | 24.0% | **4.7% less** |
47
+
48
+ <details>
49
+ <summary><strong>Full benchmark data</strong></summary>
50
+
51
+ **With CodeGraph:**
52
+ | Test | Agents | Tool Uses | Explore Tokens | Plan Tokens | Time |
53
+ |------|--------|-----------|----------------|-------------|------|
54
+ | 1 | 3 | 54 | 149.7k | 76.4k | 1m 43s |
55
+ | 2 | 2 | 41 | 102.1k | 74.8k | 1m 29s |
56
+ | 3 | 2 | 40 | 83.3k | 63.3k | 1m 25s |
57
+ | **Avg** | **2.3** | **45** | **111.7k** | **71.5k** | **1m 32s** |
58
+
59
+ **Without CodeGraph:**
60
+ | Test | Agents | Tool Uses | Explore Tokens | Plan Tokens | Time |
61
+ |------|--------|-----------|----------------|-------------|------|
62
+ | 1 | 3 | 74 | 177.3k | 80.5k | 1m 54s |
63
+ | 2 | 2 | 55 | 149.3k | 64.0k | 1m 27s |
64
+ | 3 | 2 | 51 | 146.7k | 62.3k | 1m 17s |
65
+ | **Avg** | **2.3** | **60** | **157.8k** | **68.9k** | **1m 33s** |
66
+
67
+ </details>
68
+
69
+ ### 🔄 How It Works
70
+
71
+ ```
72
+ ┌─────────────────────────────────────────────────────────────────┐
73
+ Claude Code
74
+
75
+ "Implement user authentication"
76
+
77
+
78
+ │ ┌─────────────────┐ ┌─────────────────┐ │
79
+ Explore Agent │ ──── │ Explore Agent │ │
80
+ │ └────────┬────────┘ └────────┬────────┘ │
81
+ │ │ │ │
82
+ └───────────┼────────────────────────┼─────────────────────────────┘
83
+
84
+ ▼ ▼
85
+ ┌───────────────────────────────────────────────────────────────────┐
86
+ CodeGraph MCP Server
87
+ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
88
+ Search │ │ Callers │ │ Context │ │
89
+ "auth" │ │ "login()" │ │ for task │ │
90
+ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘
91
+
92
+ └────────────────┼────────────────┘
93
+
94
+ ┌───────────────────────┐
95
+ │ SQLite Graph DB │
96
+ │ │ • 387 symbols │ │
97
+ │ │ • 1,204 edges │ │
98
+ │ │ • Instant lookups │ │
99
+ │ └───────────────────────┘ │
100
+ └───────────────────────────────────────────────────────────────────┘
101
+ ```
102
+
103
+ **Without CodeGraph:** Explore agents use `grep`, `glob`, and `Read` to scan files → many API calls, high token usage
104
+
105
+ **With CodeGraph:** Explore agents query the graph via MCP tools → instant results, local processing, fewer tokens
106
+
107
+ ---
108
+
109
+ ## Key Features
110
+
111
+ <table>
112
+ <tr>
113
+ <td width="33%" valign="top">
114
+
115
+ ### 🧠 Smart Context Building
116
+ One tool call returns everything Claude needs—entry points, related symbols, and code snippets. No more expensive exploration agents.
117
+
118
+ </td>
119
+ <td width="33%" valign="top">
120
+
121
+ ### 🔍 Semantic Search
122
+ Find code by meaning, not just text. Search for "authentication" and find `login`, `validateToken`, `AuthService`—even with different naming conventions.
123
+
124
+ </td>
125
+ <td width="33%" valign="top">
126
+
127
+ ### 📈 Impact Analysis
128
+ Know exactly what breaks before you change it. Trace callers, callees, and the full impact radius of any symbol.
129
+
130
+ </td>
131
+ </tr>
132
+ <tr>
133
+ <td width="33%" valign="top">
134
+
135
+ ### 🌍 17+ Languages
136
+ TypeScript, JavaScript, Python, Go, Rust, Java, C#, PHP, Ruby, C, C++, Swift, Kotlin, Dart, Svelte, Liquid—all with the same API.
137
+
138
+ </td>
139
+ <td width="33%" valign="top">
140
+
141
+ ### 🔒 100% Local
142
+ No data leaves your machine. No API keys. No external services. Everything runs on your local SQLite database.
143
+
144
+ </td>
145
+ <td width="33%" valign="top">
146
+
147
+ ### ⚡ Always Fresh
148
+ Claude Code hooks automatically sync the index as you work. Your code intelligence is always up to date.
149
+
150
+ </td>
151
+ </tr>
152
+ </table>
153
+
154
+ ---
155
+
156
+ ## 🎯 Quick Start
157
+
158
+ ### 1. Run the Installer
159
+
160
+ ```bash
161
+ npx @colbymchenry/codegraph
162
+ ```
163
+
164
+ The interactive installer will:
165
+ - Configure the MCP server in `~/.claude.json`
166
+ - Set up auto-allow permissions for CodeGraph tools
167
+ - Add global instructions to `~/.claude/CLAUDE.md` (teaches Claude how to use CodeGraph)
168
+ - Install Claude Code hooks for automatic index syncing
169
+ - Optionally initialize your current project
170
+
171
+ ### 2. Restart Claude Code
172
+
173
+ Restart Claude Code for the MCP server to load.
174
+
175
+ ### 3. Initialize Projects
176
+
177
+ For each project you want to use CodeGraph with:
178
+
179
+ ```bash
180
+ cd your-project
181
+ codegraph init -i
182
+ ```
183
+
184
+ That's it! Claude Code will now use CodeGraph tools automatically when a `.codegraph/` directory exists.
185
+
186
+ <details>
187
+ <summary><strong>Manual Setup (Alternative)</strong></summary>
188
+
189
+ If you prefer manual configuration:
190
+
191
+ **Install globally:**
192
+ ```bash
193
+ npm install -g @colbymchenry/codegraph
194
+ ```
195
+
196
+ **Add to `~/.claude.json`:**
197
+ ```json
198
+ {
199
+ "mcpServers": {
200
+ "codegraph": {
201
+ "type": "stdio",
202
+ "command": "codegraph",
203
+ "args": ["serve", "--mcp"]
204
+ }
205
+ }
206
+ }
207
+ ```
208
+
209
+ **Add to `~/.claude/settings.json` (optional, for auto-allow):**
210
+ ```json
211
+ {
212
+ "permissions": {
213
+ "allow": [
214
+ "mcp__codegraph__codegraph_search",
215
+ "mcp__codegraph__codegraph_context",
216
+ "mcp__codegraph__codegraph_callers",
217
+ "mcp__codegraph__codegraph_callees",
218
+ "mcp__codegraph__codegraph_impact",
219
+ "mcp__codegraph__codegraph_node",
220
+ "mcp__codegraph__codegraph_status",
221
+ "mcp__codegraph__codegraph_files"
222
+ ]
223
+ }
224
+ }
225
+ ```
226
+
227
+ </details>
228
+
229
+ <details>
230
+ <summary><strong>Global Instructions Reference</strong></summary>
231
+
232
+ The installer automatically adds these instructions to `~/.claude/CLAUDE.md`. This is provided here for reference:
233
+
234
+ ```markdown
235
+ ## CodeGraph
236
+
237
+ CodeGraph builds a semantic knowledge graph of codebases for faster, smarter code exploration.
238
+
239
+ ### If `.codegraph/` exists in the project
240
+
241
+ **Use codegraph tools for faster exploration.** These tools provide instant lookups via the code graph instead of scanning files:
242
+
243
+ | Tool | Use For |
244
+ |------|---------|
245
+ | `codegraph_search` | Find symbols by name (functions, classes, types) |
246
+ | `codegraph_context` | Get relevant code context for a task |
247
+ | `codegraph_callers` | Find what calls a function |
248
+ | `codegraph_callees` | Find what a function calls |
249
+ | `codegraph_impact` | See what's affected by changing a symbol |
250
+ | `codegraph_node` | Get details + source code for a symbol |
251
+ | `codegraph_files` | Get project file structure from the index |
252
+
253
+ **When spawning Explore agents in a codegraph-enabled project:**
254
+
255
+ Tell the Explore agent to use codegraph tools for faster exploration.
256
+
257
+ **For quick lookups in the main session:**
258
+ - Use `codegraph_search` instead of grep for finding symbols
259
+ - Use `codegraph_callers`/`codegraph_callees` to trace code flow
260
+ - Use `codegraph_impact` before making changes to see what's affected
261
+
262
+ ### If `.codegraph/` does NOT exist
263
+
264
+ At the start of a session, ask the user if they'd like to initialize CodeGraph:
265
+
266
+ "I notice this project doesn't have CodeGraph initialized. Would you like me to run `codegraph init -i` to build a code knowledge graph?"
267
+ ```
268
+
269
+ </details>
270
+
271
+ ---
272
+
273
+ ## 📋 Requirements
274
+
275
+ - Node.js >= 18.0.0
276
+
277
+ ---
278
+
279
+ ## 💻 CLI Usage
280
+
281
+ ```bash
282
+ codegraph # Run interactive installer
283
+ codegraph install # Run interactive installer (explicit)
284
+ codegraph init [path] # Initialize in a project
285
+ codegraph uninit [path] # Remove CodeGraph from a project
286
+ codegraph index [path] # Full index
287
+ codegraph sync [path] # Incremental update
288
+ codegraph status [path] # Show statistics
289
+ codegraph query <search> # Search symbols
290
+ codegraph files [path] # Show project file structure
291
+ codegraph context <task> # Build context for AI
292
+ codegraph serve --mcp # Start MCP server
293
+ ```
294
+
295
+ ## 📖 CLI Commands
296
+
297
+ ### `codegraph` / `codegraph install`
298
+
299
+ Run the interactive installer for Claude Code integration. Configures MCP server and permissions automatically.
300
+
301
+ ```bash
302
+ codegraph # Run installer (when no args)
303
+ codegraph install # Run installer (explicit)
304
+ npx @colbymchenry/codegraph # Run via npx (no global install needed)
305
+ ```
306
+
307
+ The installer will:
308
+ 1. Ask for installation location (global `~/.claude` or local `./.claude`)
309
+ 2. Configure the MCP server in `claude.json`
310
+ 3. Optionally set up auto-allow permissions
311
+ 4. Add global instructions to `~/.claude/CLAUDE.md` (teaches Claude how to use CodeGraph)
312
+ 5. Install Claude Code hooks for automatic index syncing
313
+ 6. For local installs: initialize and index the current project
314
+
315
+ ### `codegraph init [path]`
316
+
317
+ Initialize CodeGraph in a project directory. Creates a `.codegraph/` directory with the database and configuration.
318
+
319
+ ```bash
320
+ codegraph init # Initialize in current directory
321
+ codegraph init /path/to/project # Initialize in specific directory
322
+ codegraph init --index # Initialize and immediately index
323
+ ```
324
+
325
+ ### `codegraph uninit [path]`
326
+
327
+ Remove CodeGraph from a project. Deletes the `.codegraph/` directory and all indexed data.
328
+
329
+ ```bash
330
+ codegraph uninit # Remove from current directory
331
+ codegraph uninit --force # Skip confirmation prompt
332
+ ```
333
+
334
+ ### `codegraph index [path]`
335
+
336
+ Index all files in the project. Extracts functions, classes, methods, and their relationships.
337
+
338
+ ```bash
339
+ codegraph index # Index current directory
340
+ codegraph index --force # Force full re-index
341
+ codegraph index --quiet # Suppress progress output
342
+ ```
343
+
344
+ ### `codegraph sync [path]`
345
+
346
+ Incrementally sync changes since the last index. Only processes added, modified, or removed files.
347
+
348
+ ```bash
349
+ codegraph sync # Sync current directory
350
+ codegraph sync --quiet # Suppress output
351
+ ```
352
+
353
+ ### `codegraph status [path]`
354
+
355
+ Show index status and statistics.
356
+
357
+ ```bash
358
+ codegraph status
359
+ ```
360
+
361
+ Output includes:
362
+ - Files indexed, nodes, edges
363
+ - Nodes by kind (functions, classes, methods, etc.)
364
+ - Files by language
365
+ - Pending changes (if any)
366
+
367
+ ### `codegraph query <search>`
368
+
369
+ Search for symbols in the codebase by name.
370
+
371
+ ```bash
372
+ codegraph query "authenticate" # Search for symbols
373
+ codegraph query "User" --kind class # Filter by kind
374
+ codegraph query "process" --limit 20 # Limit results
375
+ codegraph query "validate" --json # Output as JSON
376
+ ```
377
+
378
+ ### `codegraph files [path]`
379
+
380
+ Show the project file structure from the index. Faster than filesystem scanning since it reads from the indexed data.
381
+
382
+ ```bash
383
+ codegraph files # Show file tree
384
+ codegraph files --format flat # Simple list
385
+ codegraph files --format grouped # Group by language
386
+ codegraph files --filter src/components # Filter by directory
387
+ codegraph files --pattern "*.test.ts" # Filter by glob pattern
388
+ codegraph files --max-depth 2 # Limit tree depth
389
+ codegraph files --no-metadata # Hide language/symbol counts
390
+ codegraph files --json # Output as JSON
391
+ ```
392
+
393
+ ### `codegraph context <task>`
394
+
395
+ Build relevant code context for a task. Uses semantic search to find entry points, then expands through the graph to find related code.
396
+
397
+ ```bash
398
+ codegraph context "fix checkout bug"
399
+ codegraph context "add user authentication" --format json
400
+ codegraph context "refactor payment service" --max-nodes 30
401
+ ```
402
+
403
+ ### `codegraph serve`
404
+
405
+ Start CodeGraph as an MCP server for AI assistants.
406
+
407
+ ```bash
408
+ codegraph serve # Show MCP configuration help
409
+ codegraph serve --mcp # Start MCP server (stdio)
410
+ codegraph serve --mcp --path /project # Specify project path
411
+ ```
412
+
413
+ ## 🔌 MCP Tools Reference
414
+
415
+ When running as an MCP server, CodeGraph exposes these tools to AI assistants. **These tools are designed to be used by Claude's Explore agents** for faster, more efficient codebase exploration.
416
+
417
+ ### `codegraph_context`
418
+
419
+ Build context for a specific task. Good for focused queries.
420
+
421
+ ```
422
+ codegraph_context(task: "fix checkout validation bug", maxNodes: 20)
423
+ ```
424
+
425
+ ### `codegraph_search`
426
+
427
+ Quick symbol search by name. Returns locations only.
428
+
429
+ ```
430
+ codegraph_search(query: "UserService", kind: "class", limit: 10)
431
+ ```
432
+
433
+ ### `codegraph_callers` / `codegraph_callees`
434
+
435
+ Find what calls a function, or what a function calls.
436
+
437
+ ```
438
+ codegraph_callers(symbol: "validatePayment", limit: 20)
439
+ codegraph_callees(symbol: "processOrder", limit: 20)
440
+ ```
441
+
442
+ ### `codegraph_impact`
443
+
444
+ Analyze what code would be affected by changing a symbol.
445
+
446
+ ```
447
+ codegraph_impact(symbol: "UserService", depth: 2)
448
+ ```
449
+
450
+ ### `codegraph_node`
451
+
452
+ Get details about a specific symbol. Use `includeCode: true` only when needed.
453
+
454
+ ```
455
+ codegraph_node(symbol: "authenticate", includeCode: true)
456
+ ```
457
+
458
+ ### `codegraph_files`
459
+
460
+ Get the project file structure from the index. Faster than filesystem scanning.
461
+
462
+ ```
463
+ codegraph_files(path: "src/components", format: "tree", includeMetadata: true)
464
+ ```
465
+
466
+ ### `codegraph_status`
467
+
468
+ Check index health and statistics.
469
+
470
+ ### How It Works With Claude Code
471
+
472
+ Claude's **Explore agents** use these tools instead of grep/glob/Read for faster exploration:
473
+
474
+ | Without CodeGraph | With CodeGraph | Benefit |
475
+ |-------------------|----------------|---------|
476
+ | `grep -r "auth"` | `codegraph_search("auth")` | Instant symbol lookup |
477
+ | Multiple `Read` calls | `codegraph_context(task)` | Related code in one call |
478
+ | Manual file tracing | `codegraph_callers/callees` | Call graph traversal |
479
+ | Guessing impact | `codegraph_impact(symbol)` | Know what breaks |
480
+ | `Glob`/`find` scanning | `codegraph_files(path)` | Indexed file structure |
481
+
482
+ This hybrid approach gives you **~30% fewer tokens** and **~25% fewer tool calls** while letting Claude's native agents handle synthesis.
483
+
484
+ ## 📚 Library Usage
485
+
486
+ CodeGraph can also be used as a library in your Node.js applications:
487
+
488
+ ```typescript
489
+ import CodeGraph from '@colbymchenry/codegraph';
490
+
491
+ // Initialize a new project
492
+ const cg = await CodeGraph.init('/path/to/project');
493
+
494
+ // Or open an existing one
495
+ const cg = await CodeGraph.open('/path/to/project');
496
+
497
+ // Index with progress callback
498
+ await cg.indexAll({
499
+ onProgress: (progress) => {
500
+ console.log(`${progress.phase}: ${progress.current}/${progress.total}`);
501
+ }
502
+ });
503
+
504
+ // Search for symbols
505
+ const results = cg.searchNodes('UserService');
506
+
507
+ // Get callers of a function
508
+ const node = results[0].node;
509
+ const callers = cg.getCallers(node.id);
510
+
511
+ // Build context for a task
512
+ const context = await cg.buildContext('fix login bug', {
513
+ maxNodes: 20,
514
+ includeCode: true,
515
+ format: 'markdown'
516
+ });
517
+
518
+ // Get impact radius
519
+ const impact = cg.getImpactRadius(node.id, 2);
520
+
521
+ // Sync changes
522
+ const syncResult = await cg.sync();
523
+
524
+ // Clean up
525
+ cg.close();
526
+ ```
527
+
528
+ ## ⚙️ How It Works
529
+
530
+ ### 1. Extraction
531
+
532
+ CodeGraph uses [tree-sitter](https://tree-sitter.github.io/) to parse source code into ASTs. Language-specific queries (`.scm` files) extract:
533
+
534
+ - **Nodes**: Functions, methods, classes, interfaces, types, variables
535
+ - **Edges**: Calls, imports, extends, implements, returns_type
536
+
537
+ Each node gets a unique ID based on its kind, file path, name, and line number.
538
+
539
+ ### 2. Storage
540
+
541
+ All data is stored in a local SQLite database (`.codegraph/codegraph.db`):
542
+
543
+ - **nodes** table: All code entities with metadata
544
+ - **edges** table: Relationships between nodes
545
+ - **files** table: File tracking for incremental updates
546
+ - **unresolved_refs** table: References pending resolution
547
+ - **vectors** table: Embeddings stored as BLOBs for semantic search
548
+ - **nodes_fts**: FTS5 virtual table for full-text search
549
+ - **schema_versions** table: Schema version tracking
550
+ - **project_metadata** table: Project-level key-value metadata
551
+
552
+ ### 3. Reference Resolution
553
+
554
+ After extraction, CodeGraph resolves references:
555
+
556
+ 1. Match function calls to function definitions
557
+ 2. Resolve imports to their source files
558
+ 3. Link class inheritance and interface implementations
559
+ 4. Apply framework-specific patterns (Express routes, etc.)
560
+
561
+ ### 4. Semantic Search
562
+
563
+ CodeGraph uses local embeddings (via [@xenova/transformers](https://github.com/xenova/transformers.js)) to enable semantic search:
564
+
565
+ 1. Code symbols are embedded using a transformer model
566
+ 2. Queries are embedded and compared using cosine similarity
567
+ 3. Results are ranked by relevance
568
+
569
+ ### 5. Graph Queries
570
+
571
+ The graph structure enables powerful queries:
572
+
573
+ - **Callers/Callees**: Direct call relationships
574
+ - **Impact Radius**: BFS traversal to find all potentially affected code
575
+ - **Dependencies**: What a symbol depends on
576
+ - **Dependents**: What depends on a symbol
577
+
578
+ ### 6. Context Building
579
+
580
+ When you request context for a task:
581
+
582
+ 1. Semantic search finds relevant entry points
583
+ 2. Graph traversal expands to related code
584
+ 3. Code snippets are extracted
585
+ 4. Results are formatted for AI consumption
586
+
587
+ ## ⚙️ Configuration
588
+
589
+ The `.codegraph/config.json` file controls indexing behavior:
590
+
591
+ ```json
592
+ {
593
+ "version": 1,
594
+ "languages": ["typescript", "javascript"],
595
+ "exclude": [
596
+ "node_modules/**",
597
+ "dist/**",
598
+ "build/**",
599
+ "*.min.js"
600
+ ],
601
+ "frameworks": [],
602
+ "maxFileSize": 1048576,
603
+ "extractDocstrings": true,
604
+ "trackCallSites": true,
605
+ "enableEmbeddings": false
606
+ }
607
+ ```
608
+
609
+ ### Options
610
+
611
+ | Option | Description | Default |
612
+ |--------|-------------|---------|
613
+ | `languages` | Languages to index (auto-detected if empty) | `[]` |
614
+ | `exclude` | Glob patterns to ignore | `["node_modules/**", ...]` |
615
+ | `frameworks` | Framework hints for better resolution | `[]` |
616
+ | `maxFileSize` | Skip files larger than this (bytes) | `1048576` (1MB) |
617
+ | `extractDocstrings` | Whether to extract docstrings from code | `true` |
618
+ | `trackCallSites` | Whether to track call site locations | `true` |
619
+ | `enableEmbeddings` | Enable semantic search embeddings | `false` |
620
+
621
+ ## 🌐 Supported Languages
622
+
623
+ | Language | Extension | Status |
624
+ |----------|-----------|--------|
625
+ | TypeScript | `.ts`, `.tsx` | Full support |
626
+ | JavaScript | `.js`, `.jsx`, `.mjs` | Full support |
627
+ | Python | `.py` | Full support |
628
+ | Go | `.go` | Full support |
629
+ | Rust | `.rs` | Full support |
630
+ | Java | `.java` | Full support |
631
+ | C# | `.cs` | Full support |
632
+ | PHP | `.php` | Full support |
633
+ | Ruby | `.rb` | Full support |
634
+ | C | `.c`, `.h` | Full support |
635
+ | C++ | `.cpp`, `.hpp`, `.cc` | Full support |
636
+ | Swift | `.swift` | Basic support |
637
+ | Kotlin | `.kt`, `.kts` | Basic support |
638
+ | Dart | `.dart` | Full support |
639
+ | Svelte | `.svelte` | Full support (script extraction, Svelte 5 runes, SvelteKit routes) |
640
+ | Liquid | `.liquid` | Full support |
641
+
642
+ ## 🔧 Troubleshooting
643
+
644
+ ### "CodeGraph not initialized"
645
+
646
+ Run `codegraph init` in your project directory first.
647
+
648
+ ### Indexing is slow
649
+
650
+ - Check if `node_modules` or other large directories are excluded
651
+ - Use `--quiet` flag to reduce console output overhead
652
+ - Consider increasing `maxFileSize` if you have large files to skip
653
+
654
+ ### MCP server not connecting
655
+
656
+ 1. Ensure the project is initialized and indexed
657
+ 2. Check the path in your MCP configuration is correct
658
+ 3. Verify `codegraph serve --mcp` works from the command line
659
+ 4. Check Claude Code logs for connection errors
660
+
661
+ ### Missing symbols in search
662
+
663
+ - Run `codegraph sync` to pick up recent changes
664
+ - Check if the file's language is supported
665
+ - Verify the file isn't excluded by config patterns
666
+
667
+ ---
668
+
669
+ ## 📄 License
670
+
671
+ MIT
672
+
673
+ ---
674
+
675
+ <div align="center">
676
+
677
+ **Made for the Claude Code community** 🤖
678
+
679
+ [Report Bug](https://github.com/colbymchenry/codegraph/issues) · [Request Feature](https://github.com/colbymchenry/codegraph/issues)
680
+
681
+ </div>