code-graph-context 0.1.0

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.
package/README.md ADDED
@@ -0,0 +1,870 @@
1
+ # Code Graph Context MCP Server
2
+
3
+ [![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
4
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.8-007ACC?logo=typescript&logoColor=white)](https://typescriptlang.org/)
5
+ [![Neo4j](https://img.shields.io/badge/Neo4j-5.0+-018bff?logo=neo4j&logoColor=white)](https://neo4j.com/)
6
+ [![NestJS](https://img.shields.io/badge/NestJS-Compatible-E0234E?logo=nestjs&logoColor=white)](https://nestjs.com/)
7
+ [![OpenAI](https://img.shields.io/badge/OpenAI-Powered-412991?logo=openai&logoColor=white)](https://openai.com/)
8
+ [![MCP](https://img.shields.io/badge/MCP-Server-blue)](https://modelcontextprotocol.io/)
9
+
10
+ A Model Context Protocol (MCP) server that builds rich code graphs to provide deep contextual understanding of TypeScript codebases to Large Language Models. This server parses your codebase using AST analysis, constructs a comprehensive graph representation in Neo4j, and provides intelligent querying capabilities through semantic search and graph traversal.
11
+
12
+ **Config-Driven & Extensible**: Define custom framework schemas to capture domain-specific patterns beyond the included NestJS support. The parser is fully configurable to recognize your architectural patterns, decorators, and relationships.
13
+
14
+ ## Features
15
+
16
+ - **Rich Code Graph Generation**: Parses TypeScript projects and creates detailed graph representations with AST-level precision
17
+ - **Semantic Search**: Vector-based semantic search using OpenAI embeddings to find relevant code patterns and implementations
18
+ - **Natural Language Querying**: Convert natural language questions into Cypher queries using OpenAI assistants API
19
+ - **Framework-Aware & Customizable**: Built-in NestJS schema with ability to define custom framework patterns via configuration
20
+ - **Weighted Graph Traversal**: Intelligent traversal that scores paths based on relationship importance, query relevance, and depth
21
+ - **High Performance**: Optimized Neo4j storage with vector indexing for fast retrieval
22
+ - **MCP Integration**: Seamless integration with Claude Code and other MCP-compatible tools
23
+
24
+ ## Architecture
25
+
26
+ The MCP server consists of several key components:
27
+
28
+ ### Core Components
29
+
30
+ 1. **TypeScript Parser** (`src/core/parsers/typescript-parser-v2.ts`): Uses `ts-morph` to parse TypeScript AST and extract code entities
31
+ 2. **Graph Storage** (`src/storage/neo4j/neo4j.service.ts`): Neo4j integration for storing and querying the code graph
32
+ 3. **Embeddings Service** (`src/core/embeddings/embeddings.service.ts`): OpenAI integration for semantic search capabilities
33
+ 4. **MCP Server** (`src/mcp/mcp.server.ts`): Main MCP server providing tools for code analysis
34
+
35
+ ### Graph Schema
36
+
37
+ The system uses a dual-schema approach:
38
+
39
+ - **Core Schema**: AST-level nodes (Classes, Methods, Properties, Imports, etc.)
40
+ - **Framework Schema**: Semantic interpretations (NestJS Controllers, Services, HTTP Endpoints, etc.)
41
+
42
+ ## Getting Started
43
+
44
+ ### Prerequisites
45
+
46
+ - **Node.js** >= 18
47
+ - **Neo4j** >= 5.0 with APOC plugin
48
+ - **OpenAI API Key** (for embeddings and natural language processing)
49
+ - **Docker** (recommended for Neo4j setup)
50
+
51
+ ### Installation
52
+
53
+ Choose the installation method that works best for you:
54
+
55
+ #### Option 1: Development Install (From Source)
56
+
57
+ Best for: Contributing to the project or customizing the code
58
+
59
+ 1. **Clone the repository:**
60
+ ```bash
61
+ git clone https://github.com/drewdrewH/code-graph-context.git
62
+ cd code-graph-context
63
+ ```
64
+
65
+ 2. **Install dependencies:**
66
+ ```bash
67
+ npm install
68
+ ```
69
+
70
+ 3. **Set up Neo4j using Docker:**
71
+ ```bash
72
+ docker-compose up -d
73
+ ```
74
+
75
+ This will start Neo4j with:
76
+ - Web interface: http://localhost:7474
77
+ - Bolt connection: bolt://localhost:7687
78
+ - Username: `neo4j`, Password: `PASSWORD`
79
+
80
+ 4. **Configure environment variables:**
81
+ ```bash
82
+ cp .env.example .env
83
+ # Edit .env with your configuration
84
+ ```
85
+
86
+ 5. **Build the project:**
87
+ ```bash
88
+ npm run build
89
+ ```
90
+
91
+ 6. **Add to Claude Code:**
92
+ ```bash
93
+ claude mcp add code-graph-context node /absolute/path/to/code-graph-context/dist/mcp/mcp.server.js
94
+ ```
95
+
96
+ #### Option 2: NPM Install (Global Package)
97
+
98
+ Best for: Easy setup and automatic updates
99
+
100
+ 1. **Install the package globally:**
101
+ ```bash
102
+ npm install -g code-graph-context
103
+ ```
104
+
105
+ 2. **Set up Neo4j** (choose one):
106
+
107
+ **Option A: Docker (Recommended)**
108
+ ```bash
109
+ docker run -d \
110
+ --name code-graph-neo4j \
111
+ -p 7474:7474 -p 7687:7687 \
112
+ -e NEO4J_AUTH=neo4j/PASSWORD \
113
+ -e NEO4J_PLUGINS='["apoc"]' \
114
+ neo4j:5.15
115
+ ```
116
+
117
+ **Option B: Neo4j Desktop**
118
+ - Download from [neo4j.com/download](https://neo4j.com/download/)
119
+ - Install APOC plugin
120
+ - Start database
121
+
122
+ **Option C: Neo4j Aura (Cloud)**
123
+ - Create free account at [neo4j.com/cloud/aura](https://neo4j.com/cloud/platform/aura-graph-database/)
124
+ - Note your connection URI and credentials
125
+
126
+ 3. **Add to Claude Code:**
127
+ ```bash
128
+ claude mcp add code-graph-context code-graph-context
129
+ ```
130
+
131
+ Then configure in your MCP config file (`~/.config/claude/config.json`):
132
+ ```json
133
+ {
134
+ "mcpServers": {
135
+ "code-graph-context": {
136
+ "command": "code-graph-context",
137
+ "env": {
138
+ "OPENAI_API_KEY": "sk-your-key-here",
139
+ "NEO4J_URI": "bolt://localhost:7687",
140
+ "NEO4J_USER": "neo4j",
141
+ "NEO4J_PASSWORD": "PASSWORD"
142
+ }
143
+ }
144
+ }
145
+ }
146
+ ```
147
+
148
+ **Note:** The env vars can be configured for any Neo4j instance - local, Docker, cloud (Aura), or enterprise.
149
+
150
+ ## Tool Usage Guide & Sequential Workflows
151
+
152
+ ### Sequential Tool Usage Patterns
153
+
154
+ The MCP tools are designed to work together in powerful workflows. Here are the most effective patterns:
155
+
156
+ #### Pattern 1: Discovery → Focus → Deep Dive
157
+ ```mermaid
158
+ graph LR
159
+ A[search_codebase] --> B[traverse_from_node] --> C[traverse_from_node with skip]
160
+ A --> D[traverse_from_node] --> E[traverse_from_node deeper]
161
+ ```
162
+
163
+ #### Pattern 2: Broad Search → Targeted Analysis
164
+ 1. **Start Broad**: Use `search_codebase` to find relevant starting points
165
+ 2. **Focus**: Use `traverse_from_node` to explore specific relationships
166
+ 3. **Paginate**: Use `skip` parameter to explore different sections of the graph
167
+
168
+ ### Tool Deep Dive
169
+
170
+ #### 1. `search_codebase` - Your Starting Point
171
+ **Purpose**: Semantic search using vector embeddings to find the most relevant code nodes.
172
+
173
+ **Response Structure**: Returns normalized JSON using JSON:API pattern to eliminate duplication:
174
+ - **nodes**: Map of unique nodes (stored once, referenced by ID)
175
+ - **depths**: Array of depth levels with relationship chains
176
+ - **Source Code**: Included by default (truncated to 1000 chars: first 500 + last 500)
177
+ - **Statistics**: Total connections, unique files, max depth
178
+
179
+ **Real Response Example**:
180
+ ```json
181
+ // Query: "JWT token validation"
182
+ // Returns:
183
+ {
184
+ "totalConnections": 22,
185
+ "uniqueFiles": 2,
186
+ "maxDepth": 3,
187
+ "startNodeId": "MethodDeclaration:697d2c96-1f91-4894-985d-1eece117b72b",
188
+ "nodes": {
189
+ "MethodDeclaration:697d2c96-1f91-4894-985d-1eece117b72b": {
190
+ "id": "MethodDeclaration:697d2c96-1f91-4894-985d-1eece117b72b",
191
+ "type": "MethodDeclaration",
192
+ "filePath": "/packages/jwt-validation/src/lib/jwt.strategy.ts",
193
+ "name": "validate",
194
+ "sourceCode": "validate(payload: EmJwtPayload): EmJwtPayload {\n ...\n\n... [truncated] ...\n\n return payload;\n}",
195
+ "hasMore": true,
196
+ "truncated": 1250
197
+ },
198
+ "ClassDeclaration:abc-123": {
199
+ "id": "ClassDeclaration:abc-123",
200
+ "type": "Service",
201
+ "filePath": "/packages/jwt-validation/src/lib/jwt.strategy.ts",
202
+ "name": "JwtStrategy"
203
+ }
204
+ },
205
+ "depths": [
206
+ {
207
+ "depth": 1,
208
+ "count": 8,
209
+ "chains": [
210
+ {
211
+ "via": "HAS_MEMBER",
212
+ "direction": "INCOMING",
213
+ "count": 1,
214
+ "nodeIds": ["ClassDeclaration:abc-123"]
215
+ },
216
+ {
217
+ "via": "HAS_PARAMETER",
218
+ "direction": "OUTGOING",
219
+ "count": 2,
220
+ "nodeIds": ["Parameter:xyz-456", "Parameter:def-789"]
221
+ }
222
+ ]
223
+ },
224
+ {
225
+ "depth": 2,
226
+ "count": 14,
227
+ "chains": [
228
+ {
229
+ "via": "HAS_MEMBER → INJECTS",
230
+ "direction": "INCOMING",
231
+ "count": 3,
232
+ "nodeIds": ["Service:auth-service", "Service:user-service", "Repository:user-repo"],
233
+ "hasMore": 2
234
+ }
235
+ ]
236
+ }
237
+ ]
238
+ }
239
+ ```
240
+
241
+ **Key Features**:
242
+ - **JSON:API Normalization**: Nodes stored once in `nodes` map, referenced by ID to eliminate duplication
243
+ - **Source Code Truncation**: Max 1000 chars per snippet (first 500 + last 500 chars)
244
+ - **Relationship Chains**: Shows full path like "HAS_MEMBER → INJECTS → USES_REPOSITORY"
245
+ - **Direction Indicators**: OUTGOING (what this calls), INCOMING (who calls this)
246
+
247
+ **Pro Tips**:
248
+ - Use specific domain terms: "JWT token validation" vs "authentication"
249
+ - Start with limit=1-3 for initial exploration to avoid token limits
250
+ - Look for node IDs in `nodes` map to use with `traverse_from_node`
251
+ - Check `truncated` field to see how many bytes were hidden from source code
252
+
253
+ #### 2. `traverse_from_node` - Deep Relationship Exploration
254
+ **Purpose**: Explore all connections from a specific node with precise control over depth and pagination.
255
+
256
+ **Response Structure**: Identical JSON:API format to search_codebase:
257
+ - **Focused Traversal**: Starts from your specified node
258
+ - **Depth Control**: Configurable max depth (1-10, default 3)
259
+ - **Pagination**: Skip parameter for exploring large graphs in chunks
260
+ - **Source Code Included by Default**: Set `includeCode: false` for structure-only view
261
+
262
+ **Real Response Example**:
263
+ ```json
264
+ // Starting from a Service class
265
+ // maxDepth: 2, skip: 0, includeCode: true
266
+ {
267
+ "totalConnections": 15,
268
+ "uniqueFiles": 4,
269
+ "maxDepth": 2,
270
+ "startNodeId": "ClassDeclaration:credit-check-service",
271
+ "nodes": {
272
+ "ClassDeclaration:credit-check-service": {
273
+ "id": "ClassDeclaration:credit-check-service",
274
+ "type": "Service",
275
+ "filePath": "/src/modules/credit/credit-check.service.ts",
276
+ "name": "CreditCheckService",
277
+ "sourceCode": "@Injectable([CreditCheckRepository, OscilarClient])\nexport class CreditCheckService {\n ...\n\n... [truncated] ...\n\n}",
278
+ "truncated": 3200
279
+ },
280
+ "Repository:credit-check-repo": {
281
+ "id": "Repository:credit-check-repo",
282
+ "type": "Repository",
283
+ "filePath": "/src/modules/credit/credit-check.repository.ts",
284
+ "name": "CreditCheckRepository"
285
+ }
286
+ },
287
+ "depths": [
288
+ {
289
+ "depth": 1,
290
+ "count": 5,
291
+ "chains": [
292
+ {
293
+ "via": "INJECTS",
294
+ "direction": "OUTGOING",
295
+ "count": 2,
296
+ "nodeIds": ["Repository:credit-check-repo", "VendorClient:oscilar"]
297
+ },
298
+ {
299
+ "via": "HAS_MEMBER",
300
+ "direction": "OUTGOING",
301
+ "count": 3,
302
+ "nodeIds": ["Method:processCheck", "Method:getResult", "Method:rerun"]
303
+ }
304
+ ]
305
+ },
306
+ {
307
+ "depth": 2,
308
+ "count": 10,
309
+ "chains": [
310
+ {
311
+ "via": "INJECTS → USES_DAL",
312
+ "direction": "OUTGOING",
313
+ "count": 1,
314
+ "nodeIds": ["DAL:application-dal"]
315
+ }
316
+ ]
317
+ }
318
+ ]
319
+ }
320
+ ```
321
+
322
+ **Parameters**:
323
+ - `nodeId` (required): Node ID from search_codebase results
324
+ - `maxDepth` (default: 3): Traversal depth (1-10)
325
+ - `skip` (default: 0): Pagination offset
326
+ - `includeCode` (default: **true**): Include source code snippets
327
+ - `summaryOnly` (default: false): Just file paths and statistics
328
+ - `direction` (default: BOTH): Filter by OUTGOING/INCOMING/BOTH
329
+ - `relationshipTypes` (optional): Filter by specific relationships like ["INJECTS", "USES_REPOSITORY"]
330
+
331
+ **Pagination Strategy**:
332
+ ```typescript
333
+ // Note: Pagination removed in recent commits - all results returned
334
+ // Use depth and relationship filtering instead
335
+ traverse_from_node({
336
+ nodeId,
337
+ maxDepth: 2,
338
+ relationshipTypes: ["INJECTS"] // Focus on dependency injection only
339
+ })
340
+ ```
341
+
342
+ #### 3. `parse_typescript_project` - Graph Generation
343
+ **Purpose**: Parse a TypeScript/NestJS project and build the initial graph database.
344
+
345
+ ```typescript
346
+ // Full project parsing
347
+ await mcp.call('parse_typescript_project', {
348
+ projectPath: '/path/to/your/nestjs/project',
349
+ tsconfigPath: '/path/to/your/nestjs/project/tsconfig.json',
350
+ clearExisting: true // Recommended: clear previous data
351
+ });
352
+
353
+ // Response: Success confirmation with node/edge counts
354
+ "✅ SUCCESS: Parsed 2,445 nodes and 4,892 edges. Graph imported to Neo4j."
355
+ ```
356
+
357
+ **Performance Notes**:
358
+ - Large projects (>1000 files) may take several minutes
359
+ - Embedding generation adds significant time but enables semantic search
360
+ - Use `clearExisting: true` to avoid duplicate data
361
+
362
+ #### 4. `test_neo4j_connection` - Health Check
363
+ **Purpose**: Verify database connectivity and APOC plugin availability.
364
+
365
+ ```typescript
366
+ // Simple health check
367
+ await mcp.call('test_neo4j_connection');
368
+
369
+ // Response indicates database status
370
+ "Neo4j connected: Connected! at 2025-07-25T19:48:42.676Z
371
+ APOC plugin available with 438 functions"
372
+ ```
373
+
374
+ ### Workflow Examples
375
+
376
+ #### Example 1: Understanding Authentication Flow
377
+ ```typescript
378
+ // Step 1: Find authentication-related code
379
+ const searchResult = await mcp.call('search_codebase', {
380
+ query: 'JWT token validation authentication',
381
+ limit: 2
382
+ });
383
+
384
+ // Step 2: Extract node ID from most relevant result
385
+ const nodeId = "MethodDeclaration:697d2c96-1f91-4894-985d-1eece117b72b";
386
+
387
+ // Step 3: Explore immediate relationships
388
+ const immediateConnections = await mcp.call('traverse_from_node', {
389
+ nodeId,
390
+ maxDepth: 2,
391
+ skip: 0
392
+ });
393
+
394
+ // Step 4: Go deeper to understand full authentication chain
395
+ const deepConnections = await mcp.call('traverse_from_node', {
396
+ nodeId,
397
+ maxDepth: 4,
398
+ skip: 0
399
+ });
400
+
401
+ // Step 5: Explore different connection branches
402
+ const alternateConnections = await mcp.call('traverse_from_node', {
403
+ nodeId,
404
+ maxDepth: 3,
405
+ skip: 10 // Skip first 10 to see different connections
406
+ });
407
+ ```
408
+
409
+ #### Example 2: API Endpoint Analysis
410
+ ```typescript
411
+ // Step 1: Search for controller endpoints
412
+ const controllerSearch = await mcp.call('search_codebase', {
413
+ query: 'HTTP controller endpoints routes POST GET',
414
+ limit: 1
415
+ });
416
+
417
+ // Step 2: Find a controller node ID from results
418
+ const controllerNodeId = "ClassDeclaration:controller-uuid";
419
+
420
+ // Step 3: Explore what endpoints this controller exposes
421
+ const endpoints = await mcp.call('traverse_from_node', {
422
+ nodeId: controllerNodeId,
423
+ maxDepth: 2,
424
+ skip: 0
425
+ });
426
+
427
+ // Step 4: For each endpoint found, explore its dependencies
428
+ const endpointNodeId = "MethodDeclaration:endpoint-uuid";
429
+ const endpointDeps = await mcp.call('traverse_from_node', {
430
+ nodeId: endpointNodeId,
431
+ maxDepth: 3,
432
+ skip: 0
433
+ });
434
+ ```
435
+
436
+ #### Example 3: Service Dependency Mapping
437
+ ```typescript
438
+ // Step 1: Find a specific service
439
+ const serviceSearch = await mcp.call('search_codebase', {
440
+ query: 'UserService injectable dependency injection',
441
+ limit: 1
442
+ });
443
+
444
+ // Step 2: Map all its dependencies (what it injects)
445
+ const serviceDeps = await mcp.call('traverse_from_node', {
446
+ nodeId: "ClassDeclaration:user-service-uuid",
447
+ maxDepth: 2,
448
+ skip: 0
449
+ });
450
+
451
+ // Step 3: Find what depends on this service (reverse relationships)
452
+ const serviceDependents = await mcp.call('search_codebase', {
453
+ query: 'UserService injection constructor parameter',
454
+ limit: 5
455
+ });
456
+ ```
457
+
458
+ ### Advanced Usage Tips
459
+
460
+ #### Understanding Response Format (JSON:API Normalization)
461
+
462
+ **Key Insight**: All responses use JSON:API pattern to eliminate duplication by storing each node once and referencing by ID.
463
+
464
+ **How to Read Responses**:
465
+ 1. **Start with `nodes` map**: All unique nodes are stored here once
466
+ 2. **Look at `depths` array**: Shows how nodes are connected at each depth level
467
+ 3. **Follow `nodeIds` references**: Use IDs to look up full node data in `nodes` map
468
+ 4. **Check `truncated` field**: Indicates how many bytes of source code were hidden
469
+
470
+ **Example Reading Pattern**:
471
+ ```typescript
472
+ const response = await search_codebase({ query: "authentication" });
473
+
474
+ // 1. Get overview statistics
475
+ console.log(`Found ${response.totalConnections} connections across ${response.uniqueFiles} files`);
476
+
477
+ // 2. Examine the starting node
478
+ const startNode = response.nodes[response.startNodeId];
479
+ console.log(`Starting from: ${startNode.name} in ${startNode.filePath}`);
480
+
481
+ // 3. Explore first depth level
482
+ const firstDepth = response.depths[0];
483
+ firstDepth.chains.forEach(chain => {
484
+ console.log(`Via ${chain.via}: ${chain.count} connections (${chain.direction})`);
485
+
486
+ // 4. Look up actual node details
487
+ chain.nodeIds.forEach(nodeId => {
488
+ const node = response.nodes[nodeId];
489
+ console.log(` - ${node.name} (${node.type})`);
490
+ });
491
+ });
492
+ ```
493
+
494
+ #### Managing Large Responses
495
+ - **Start Small**: Use `limit: 1-3` for initial searches
496
+ - **Relationship Filtering**: Use `relationshipTypes` to focus on specific connections
497
+ - **Structure-Only View**: Set `includeCode: false` to exclude source code snippets
498
+ - **Summary Mode**: Use `summaryOnly: true` for just file paths and statistics
499
+
500
+ #### Efficient Graph Exploration
501
+ - **Breadth First**: Start with low `maxDepth` (1-2) to get overview
502
+ - **Depth Second**: Increase `maxDepth` (3-5) for detailed analysis
503
+ - **Direction Filtering**: Use `direction: "OUTGOING"` or `"INCOMING"` to focus exploration
504
+ - **Source Code on Demand**: Source code included by default but truncated to 1000 chars
505
+
506
+ #### Weighted Traversal
507
+
508
+ The `search_codebase` tool uses **weighted traversal** by default (`useWeightedTraversal: true`) to intelligently prioritize which relationships to explore. This produces more relevant results by scoring each node at every depth level.
509
+
510
+ **How Scoring Works**:
511
+
512
+ Each potential path is scored using three factors multiplied together:
513
+
514
+ 1. **Edge Weight** (0.0-1.0): How important is this relationship type?
515
+ - Critical (0.9-0.95): `INJECTS`, `EXPOSES`, `ROUTES_TO` - core architectural relationships
516
+ - High (0.8-0.88): `EXTENDS`, `IMPLEMENTS`, `USES_REPOSITORY` - important semantic links
517
+ - Medium (0.5-0.6): `IMPORTS`, `EXPORTS`, `HAS_MEMBER` - structural relationships
518
+ - Low (0.3-0.4): `DECORATED_WITH`, `HAS_PARAMETER` - metadata relationships
519
+
520
+ 2. **Node Similarity**: Cosine similarity between the node's embedding and your query embedding. Nodes semantically related to your search rank higher.
521
+
522
+ 3. **Depth Penalty**: Exponential decay (default 0.85 per level). Closer nodes are preferred:
523
+ - Depth 1: 1.0 (no penalty)
524
+ - Depth 2: 0.85
525
+ - Depth 3: 0.72
526
+
527
+ **When to Disable**:
528
+ ```typescript
529
+ // Use standard traversal for exhaustive exploration
530
+ search_codebase({
531
+ query: "...",
532
+ useWeightedTraversal: false
533
+ })
534
+ ```
535
+
536
+ #### Performance Optimization
537
+ - **Token Efficiency**: JSON:API normalization eliminates duplicate nodes in responses
538
+ - **Code Truncation**: Source code limited to 1000 chars (first 500 + last 500) to prevent token overflow
539
+ - **Memory**: Large traversals may hit Neo4j memory limits (increase heap size if needed)
540
+ - **Caching**: Node IDs are persistent; save interesting ones for later exploration
541
+
542
+ ## Available MCP Tools
543
+
544
+ ### Core Tools
545
+
546
+ | Tool | Description | Parameters | Use Case |
547
+ |------|-------------|------------|----------|
548
+ | `hello` | Test tool that says hello | None | Verify MCP connection |
549
+ | `test_neo4j_connection` | Test Neo4j connection and APOC plugin | None | Health check before operations |
550
+
551
+ ### Parsing Tools
552
+
553
+ | Tool | Description | Parameters | Use Case |
554
+ |------|-------------|------------|----------|
555
+ | `parse_typescript_project` | Parse TypeScript/NestJS project into graph | `projectPath`, `tsconfigPath`, `clearExisting?` | Initial setup: build the graph database |
556
+
557
+ ### Search & Exploration Tools
558
+
559
+ | Tool | Description | Parameters | Best For |
560
+ |------|-------------|------------|----------|
561
+ | `search_codebase` | **Vector-based semantic search** - Find most relevant code using OpenAI embeddings | `query`, `limit?`, `useWeightedTraversal?` (default: true) | **Starting point** for code exploration. Uses weighted scoring for intelligent traversal |
562
+ | `traverse_from_node` | **Focused graph traversal** - Explore specific relationships from a known node | `nodeId` (string), `maxDepth?` (1-10, default: 3), `skip?` (default: 0) | **Deep diving** into specific code relationships. Pagination for large graphs |
563
+ | `natural_language_to_cypher` | **AI-powered query generation** - Convert natural language to Cypher queries using GPT-4 | `query` (string) | **Advanced queries** - currently requires OpenAI assistant setup |
564
+
565
+ ### Tool Selection Guide
566
+
567
+ **Start Here**: `search_codebase`
568
+ - Use when you don't know specific node IDs
569
+ - Best for exploring new codebases
570
+ - Returns rich context with code snippets
571
+
572
+ **Go Deeper**: `traverse_from_node`
573
+ - Use when you have specific node IDs from search results
574
+ - Perfect for understanding relationships and dependencies
575
+ - Use `skip` parameter for pagination through large result sets
576
+
577
+ **Advanced**: `natural_language_to_cypher`
578
+ - Requires additional OpenAI assistant configuration
579
+ - Best for complex queries beyond simple search/traversal
580
+ - Currently in development - may require setup
581
+
582
+ ## Claude Code Integration Tips
583
+
584
+ ### Guiding Tool Usage with claude.md
585
+
586
+ You can add a `claude.md` file to your repository root to help Claude Code understand when to use these MCP tools effectively. Here are some useful patterns:
587
+
588
+ #### Trigger Word Hints
589
+
590
+ ```markdown
591
+ ## Code Search Tools
592
+
593
+ **Use `search_codebase` for:**
594
+ - "Where is...", "Find...", "Show me [specific thing]"
595
+ - Example: "Where is the authentication middleware?"
596
+
597
+ **Use `natural_language_to_cypher` for:**
598
+ - "List all...", "How many...", "Count..."
599
+ - Example: "List all API controllers"
600
+
601
+ **Use `traverse_from_node` for:**
602
+ - Deep dependency analysis after initial search
603
+ - "What depends on X?", "Trace the flow..."
604
+ ```
605
+
606
+ #### Weighted Traversal Hints
607
+
608
+ ```markdown
609
+ **Use `useWeightedTraversal: true` for:**
610
+ - Service/Controller classes with many dependencies
611
+ - Queries with depth > 3 or limit > 10
612
+ - Cleaner, more relevant results
613
+
614
+ **Recommended settings:**
615
+ - Default: `limit: 15, maxDepth: 5, snippetLength: 900`
616
+ - Simple lookups: `limit: 5, maxDepth: 2`
617
+ ```
618
+
619
+ #### Framework-Specific Patterns
620
+
621
+ Document your custom node types and relationships so Claude knows what to search for:
622
+
623
+ ```markdown
624
+ **Custom Node Types:**
625
+ - `PaymentProcessor` - Payment integrations
626
+ - `EmailTemplate` - Email templates
627
+
628
+ **Custom Relationships:**
629
+ - `PROCESSES_PAYMENT` - Service → PaymentProcessor
630
+ - `SENDS_EMAIL` - Service → EmailTemplate
631
+ ```
632
+
633
+ #### Common Query Examples
634
+
635
+ ```markdown
636
+ **Finding authentication:**
637
+ search_codebase({ query: "JWT authentication middleware" })
638
+
639
+ **Tracing dependencies:**
640
+ traverse_from_node({ nodeId: "...", direction: "OUTGOING", maxDepth: 5 })
641
+
642
+ **Impact analysis:**
643
+ traverse_from_node({ nodeId: "...", direction: "INCOMING", maxDepth: 4 })
644
+ ```
645
+
646
+ ## Framework Support
647
+
648
+ ### NestJS Framework Schema
649
+
650
+ The server provides deep understanding of NestJS patterns:
651
+
652
+ #### Node Types
653
+ - **Controllers**: HTTP endpoint handlers with route analysis
654
+ - **Services**: Business logic providers with dependency injection mapping
655
+ - **Modules**: Application structure with import/export relationships
656
+ - **Guards**: Authentication and authorization components
657
+ - **Pipes**: Request validation and transformation
658
+ - **Interceptors**: Request/response processing middleware
659
+ - **DTOs**: Data transfer objects with validation decorators
660
+ - **Entities**: Database models with relationship mapping
661
+
662
+ #### Relationship Types
663
+ - **Module System**: `MODULE_IMPORTS`, `MODULE_PROVIDES`, `MODULE_EXPORTS`
664
+ - **Dependency Injection**: `INJECTS`, `PROVIDED_BY`
665
+ - **HTTP API**: `EXPOSES`, `ACCEPTS`, `RESPONDS_WITH`
666
+ - **Security**: `GUARDED_BY`, `TRANSFORMED_BY`, `INTERCEPTED_BY`
667
+
668
+ ### Example Graph Structure
669
+
670
+ ```
671
+ ┌─────────────────┐ EXPOSES ┌──────────────────┐
672
+ │ UserController│──────────────→│ POST /users │
673
+ │ @Controller │ │ @Post() │
674
+ └─────────────────┘ └──────────────────┘
675
+ │ │
676
+ INJECTS ACCEPTS
677
+ ↓ ↓
678
+ ┌─────────────────┐ ┌──────────────────┐
679
+ │ UserService │ │ CreateUserDto │
680
+ │ @Injectable │ │ @IsString() │
681
+ └─────────────────┘ └──────────────────┘
682
+
683
+ MANAGES
684
+
685
+ ┌─────────────────┐
686
+ │ User Entity │
687
+ │ @Entity() │
688
+ └─────────────────┘
689
+ ```
690
+
691
+ ## Configuration
692
+
693
+ ### Environment Variables
694
+
695
+ | Variable | Description | Default |
696
+ |----------|-------------|---------|
697
+ | `OPENAI_API_KEY` | OpenAI API key for embeddings and LLM | Required |
698
+ | `OPENAI_ASSISTANT_ID` | Reuse existing OpenAI assistant | Optional |
699
+ | `NEO4J_URI` | Neo4j database URI | `bolt://localhost:7687` |
700
+ | `NEO4J_USER` | Neo4j username | `neo4j` |
701
+ | `NEO4J_PASSWORD` | Neo4j password | `PASSWORD` |
702
+
703
+ ### Parse Options
704
+
705
+ Customize parsing behavior:
706
+
707
+ ```typescript
708
+ const parseOptions = {
709
+ includePatterns: ['**/*.ts', '**/*.tsx'],
710
+ excludePatterns: [
711
+ 'node_modules/',
712
+ 'dist/',
713
+ 'coverage/',
714
+ '.d.ts',
715
+ '.spec.ts',
716
+ '.test.ts'
717
+ ],
718
+ maxFiles: 1000,
719
+ frameworkSchemas: [NESTJS_FRAMEWORK_SCHEMA]
720
+ };
721
+ ```
722
+
723
+ ## Limitations
724
+
725
+ ### Current Limitations
726
+
727
+ 1. **Language Support**: Currently supports TypeScript/NestJS only
728
+ 2. **Framework Support**: Primary focus on NestJS patterns
729
+ 3. **File Size**: Large files (>10MB) may cause parsing performance issues
730
+ 4. **Memory Usage**: Graph generation is memory-intensive for very large projects
731
+ 5. **Vector Search**: Requires OpenAI API for semantic search functionality
732
+ 6. **Real-time Updates**: No file watching - requires manual re-parsing for code changes
733
+ 7. **Response Size**: Large graph traversals can exceed token limits (25,000 tokens max)
734
+ 8. **Neo4j Memory**: Database memory limits can cause query failures on large graphs
735
+
736
+ ### Performance Considerations
737
+
738
+ - **Large Projects**: Projects with >10,000 files may require increased memory allocation
739
+ - **Graph Traversal**: Deep traversals (>5 levels) may be slow for highly connected graphs
740
+ - **Embedding Generation**: Initial parsing with embeddings can take several minutes for large codebases
741
+ - **Neo4j Memory**: Recommend at least 4GB RAM allocation for Neo4j with large graphs
742
+
743
+ ### Known Issues
744
+
745
+ 1. **Complex Type Inference**: Advanced TypeScript type gymnastics may not be fully captured
746
+ 2. **Dynamic Imports**: Runtime module loading not tracked in static analysis
747
+ 3. **Decorator Arguments**: Complex decorator argument patterns may not be fully parsed
748
+ 4. **Monorepo Support**: Limited support for complex monorepo structures
749
+
750
+ ## Troubleshooting
751
+
752
+ ### Common Issues
753
+
754
+ #### Neo4j Connection Failed
755
+ ```bash
756
+ # Check if Neo4j is running
757
+ docker ps | grep neo4j
758
+
759
+ # Check Neo4j logs
760
+ docker logs codebase-neo4j
761
+
762
+ # Verify APOC plugin
763
+ curl -u neo4j:PASSWORD http://localhost:7474/db/neo4j/tx/commit \
764
+ -H "Content-Type: application/json" \
765
+ -d '{"statements":[{"statement":"CALL apoc.help(\"apoc\") YIELD name RETURN count(name) as count"}]}'
766
+ ```
767
+
768
+ #### Neo4j Memory Issues
769
+ If you encounter errors like "allocation of an extra X MiB would use more than the limit":
770
+
771
+ ```bash
772
+ # Increase Neo4j memory limits in docker-compose.yml
773
+ NEO4J_server_memory_heap_max__size=8G
774
+ NEO4J_server_memory_pagecache_size=4G
775
+ NEO4J_dbms_memory_transaction_total_max=8G
776
+
777
+ # Restart Neo4j
778
+ docker-compose restart neo4j
779
+ ```
780
+
781
+ #### Token Limit Exceeded
782
+ If responses exceed 25,000 tokens:
783
+
784
+ ```typescript
785
+ // Reduce limit parameter
786
+ search_codebase({ query: "...", limit: 1 })
787
+
788
+ // Use pagination with skip
789
+ traverse_from_node({ nodeId: "...", maxDepth: 2, skip: 0 })
790
+ traverse_from_node({ nodeId: "...", maxDepth: 2, skip: 20 })
791
+ ```
792
+
793
+ #### OpenAI API Issues
794
+ ```bash
795
+ # Test API key
796
+ curl https://api.openai.com/v1/models \
797
+ -H "Authorization: Bearer $OPENAI_API_KEY"
798
+
799
+ # Check embedding model availability
800
+ curl https://api.openai.com/v1/models/text-embedding-3-large \
801
+ -H "Authorization: Bearer $OPENAI_API_KEY"
802
+ ```
803
+
804
+ #### Parsing Failures
805
+ ```bash
806
+ # Check TypeScript configuration
807
+ npx tsc --noEmit --project /path/to/tsconfig.json
808
+
809
+ # Verify file permissions
810
+ ls -la /path/to/project
811
+
812
+ # Check memory usage during parsing
813
+ node --max-old-space-size=8192 dist/mcp/mcp.server.js
814
+ ```
815
+
816
+ ### Debug Mode
817
+
818
+ Enable detailed logging:
819
+
820
+ ```bash
821
+ export DEBUG=mcp:*
822
+ export NODE_ENV=development
823
+ ```
824
+
825
+ ## Contributing
826
+
827
+ 1. Fork the repository
828
+ 2. Create a feature branch: `git checkout -b feature/amazing-feature`
829
+ 3. Commit your changes: `git commit -m 'Add amazing feature'`
830
+ 4. Push to the branch: `git push origin feature/amazing-feature`
831
+ 5. Open a Pull Request
832
+
833
+ ### Development Setup
834
+
835
+ ```bash
836
+ # Install dependencies
837
+ npm install
838
+
839
+ # Run in development mode
840
+ npm run dev
841
+
842
+ # Run tests
843
+ npm test
844
+
845
+ # Lint code
846
+ npm run lint
847
+
848
+ # Format code
849
+ npm run format
850
+ ```
851
+
852
+ ## License
853
+
854
+ This project is proprietary software. All rights reserved - see the [LICENSE](LICENSE) file for details.
855
+
856
+ ## Acknowledgments
857
+
858
+ - [Model Context Protocol](https://modelcontextprotocol.io/) by Anthropic
859
+ - [Neo4j](https://neo4j.com/) for graph database technology
860
+ - [ts-morph](https://ts-morph.com/) for TypeScript AST manipulation
861
+ - [OpenAI](https://openai.com/) for embeddings and natural language processing
862
+ - [NestJS](https://nestjs.com/) for the framework patterns and conventions
863
+
864
+ ## Support
865
+
866
+ - Create an [Issue](https://github.com/drewdrewH/code-graph-context/issues) for bug reports or feature requests
867
+ - Join the [MCP Discord](https://discord.gg/mcp) for community support
868
+ - Check the [MCP Documentation](https://modelcontextprotocol.io/docs) for MCP-specific questions
869
+
870
+ ---