@@ -26,6 +26,8 @@ Using local embeddings and vector search, it bridges the gap between text search
2626## Features
2727
2828- ** Semantic search** - Find code by meaning, not just keywords
29+ - ** Context graph** - Understand how code connects (calls, imports, inheritance) with graph-enhanced search
30+ - ** Session memory** - Track agent exploration state across multi-turn conversations
2931- ** AST-aware chunking** - Tree-sitter WASM for cross-platform parsing, no native compilation required
3032- ** Local embeddings** - ONNX Runtime with nomic-embed-code (768 dims, 8K context)
3133- ** Hybrid search** - Vector similarity + BM25 keyword matching
@@ -127,14 +129,80 @@ semantic_search({
127129## Architecture
128130
129131```
130- semantic_search tool ( MCP Server)
132+ MCP Server
131133├── Chunker (web-tree-sitter) → AST-aware code splitting (WASM, cross-platform)
132134├── Embedder (ONNX local) → nomic-embed-code, 768 dims
133135├── Vector DB (LanceDB) → Serverless, hybrid search
136+ ├── Context Graph (SQLite) → Structural relationships + session memory
134137├── File Watcher (chokidar) → Incremental updates
135138└── Hybrid Search → BM25 + vector + reranking
136139```
137140
141+ ## Context Graph (Opt-in)
142+
143+ The context graph adds structural awareness on top of semantic search. Enable it with:
144+
145+ ``` bash
146+ SEMANTIC_CODE_GRAPH_ENABLED=true
147+ ```
148+
149+ When enabled, the server extracts structural relationships (calls, imports, extends, implements) from the AST during indexing and stores them in a SQLite graph. This powers three additional tools.
150+
151+ ### Tool: context_query
152+
153+ Semantic search + graph neighborhood expansion. Returns search results enriched with structural context.
154+
155+ | Parameter | Type | Required | Description |
156+ | -----------| ------| ----------| -------------|
157+ | ` query ` | string | Yes | Natural language query |
158+ | ` path ` | string | No | Directory to scope the search |
159+ | ` limit ` | number | No | Maximum results (default: 10) |
160+ | ` file_pattern ` | string | No | Glob pattern to filter files |
161+ | ` depth ` | number | No | Graph traversal depth (1-3, default: 1) |
162+ | ` edge_kinds ` | string[ ] | No | Edge types to follow: calls, imports, extends, implements, exports, agent_linked |
163+ | ` session_id ` | string | No | Session ID for exploration tracking |
164+
165+ ```
166+ context_query({
167+ query: "payment processing",
168+ depth: 2,
169+ session_id: "debug-checkout"
170+ })
171+ ```
172+
173+ Returns each search result plus its graph neighbors — callers, callees, imports, and inheritance — without reading additional files.
174+
175+ ### Tool: graph_annotate
176+
177+ Leave notes on code nodes and create links between related chunks.
178+
179+ | Parameter | Type | Required | Description |
180+ | -----------| ------| ----------| -------------|
181+ | ` session_id ` | string | Yes | Session ID |
182+ | ` node_id ` | string | Yes | Chunk ID to annotate |
183+ | ` note ` | string | No | Note to attach |
184+ | ` link_to ` | string[ ] | No | Chunk IDs to create agent_linked edges to |
185+ | ` reasoning ` | string | No | Reasoning log entry |
186+
187+ ### Tool: session_summary
188+
189+ View exploration state: visited nodes, frontier, annotations, reasoning log, and graph stats.
190+
191+ | Parameter | Type | Required | Description |
192+ | -----------| ------| ----------| -------------|
193+ | ` session_id ` | string | Yes | Session ID to summarize |
194+
195+ ### Graph Configuration
196+
197+ | Variable | Description | Default |
198+ | ----------| -------------| ---------|
199+ | ` SEMANTIC_CODE_GRAPH_ENABLED ` | Enable the context graph | ` false ` |
200+ | ` SEMANTIC_CODE_GRAPH_DEPTH ` | Default BFS traversal depth (1-5) | ` 2 ` |
201+ | ` SEMANTIC_CODE_SESSION_TTL ` | Session TTL in seconds | ` 3600 ` |
202+ | ` SEMANTIC_CODE_EDGE_KINDS ` | Comma-separated edge types to follow | all types |
203+
204+ The graph degrades gracefully — if SQLite initialization fails, semantic search continues to work without graph features.
205+
138206## Supported Languages
139207
140208- TypeScript / JavaScript (including TSX/JSX)
@@ -155,6 +223,10 @@ Other languages fall back to line-based chunking.
155223| ----------| -------------| ---------|
156224| ` SEMANTIC_CODE_ROOT ` | Root directory to index | Current working directory |
157225| ` SEMANTIC_CODE_INDEX ` | Custom index storage location | ` .semantic-code/index/ ` |
226+ | ` SEMANTIC_CODE_GRAPH_ENABLED ` | Enable context graph | ` false ` |
227+ | ` SEMANTIC_CODE_GRAPH_DEPTH ` | Default graph traversal depth (1-5) | ` 2 ` |
228+ | ` SEMANTIC_CODE_SESSION_TTL ` | Session TTL in seconds | ` 3600 ` |
229+ | ` SEMANTIC_CODE_EDGE_KINDS ` | Edge types to follow (comma-separated) | all types |
158230
159231### Default Ignore Patterns
160232
@@ -194,6 +266,7 @@ Invalid inputs throw typed errors (`InvalidFilterError`, `PathTraversalError`, `
194266## Storage
195267
196268- Index location: ` .semantic-code/index/ ` (add to ` .gitignore ` )
269+ - Graph database: ` .semantic-code/index/graph.db ` (SQLite, created when graph is enabled)
197270- Model cache: ` ~/.cache/semantic-code-mcp/ `
198271- Estimated size: 3GB codebase → ~ 1.5GB index (with float16)
199272
@@ -231,21 +304,35 @@ semantic-code-mcp/
231304├── src/
232305│ ├── index.ts # MCP server entry point
233306│ ├── chunker/
234- │ │ ├── index.ts # Main chunker logic
307+ │ │ ├── index.ts # AST-aware chunker + edge extraction
235308│ │ ├── languages.ts # Language configs with WASM paths
236309│ │ └── wasm-loader.ts # WASM grammar loader with caching
237310│ ├── embedder/
238311│ │ ├── index.ts # ONNX embedding generation
239312│ │ └── model.ts # Model download & loading
313+ │ ├── graph/
314+ │ │ ├── index.ts # SQLite graph store (nodes, edges, BFS)
315+ │ │ ├── config.ts # Graph configuration from env vars
316+ │ │ ├── extractor.ts # Edge resolution (raw edges → graph edges)
317+ │ │ ├── schema.ts # SQLite DDL for graph tables
318+ │ │ ├── session.ts # In-memory session manager
319+ │ │ └── types.ts # GraphNode, GraphEdge, RawEdge types
240320│ ├── store/
241321│ │ └── index.ts # LanceDB integration
242322│ ├── search/
243323│ │ ├── index.ts # Hybrid search orchestration
244324│ │ └── reranker.ts # Cross-encoder reranking
245325│ ├── watcher/
246326│ │ └── index.ts # File watcher + incremental indexing
247- │ └── tools/
248- │ └── semantic-search.ts # MCP tool definition
327+ │ ├── tools/
328+ │ │ ├── semantic-search.ts # semantic_search tool
329+ │ │ ├── context-query.ts # context_query tool (search + graph)
330+ │ │ ├── graph-annotate.ts # graph_annotate tool
331+ │ │ └── session-summary.ts # session_summary tool
332+ │ └── utils/
333+ │ ├── logger.ts # Structured logging
334+ │ ├── validation.ts # Shared ID validation
335+ │ └── ...
249336├── grammars/ # Pre-built WASM parsers
250337├── scripts/
251338│ └── copy-grammars.js # Build script for WASM files
0 commit comments