smart-context-mcp 1.0.4 → 1.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 CHANGED
@@ -1,710 +1,230 @@
1
1
  # smart-context-mcp
2
2
 
3
+ MCP server that reduces AI agent token usage by 90% with intelligent context compression.
4
+
3
5
  [![npm version](https://badge.fury.io/js/smart-context-mcp.svg)](https://www.npmjs.com/package/smart-context-mcp)
4
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
7
 
6
- **MCP server that reduces AI agent token usage by 90% and improves response quality.**
7
-
8
- Instead of reading entire files and repeating context, this MCP provides 8 focused tools that compress, rank, and maintain context efficiently.
9
-
10
- ## Why use this?
11
-
12
- **Problem:** AI agents waste tokens reading full files, repeating context, and searching inefficiently.
13
-
14
- **Solution:** This MCP reduces token usage by **~90%** in real projects while improving response quality.
15
-
16
- **Real metrics from production use:**
17
- - 14.5M tokens → 1.6M tokens (89.87% reduction)
18
- - 3,666 successful calls across the original 7 core tools
19
- - Compression ratios: 3x to 46x depending on tool
20
-
21
- ## Quick Start (2 commands)
8
+ ## Installation
22
9
 
23
10
  ```bash
24
11
  npm install smart-context-mcp
25
12
  npx smart-context-init --target .
26
13
  ```
27
14
 
28
- That's it. Restart your AI client (Cursor, Codex, Claude Desktop) and the tools are available.
15
+ Restart your AI client. Tools are immediately available.
29
16
 
30
- **Important:** The init command automatically sets the correct project-root env var in the generated configs, so the MCP server runs from your project root. This works for standalone projects, monorepos, and nested workspaces.
17
+ ## What it does
31
18
 
32
- ## What you get
19
+ Replaces inefficient file reading and searching with 12 specialized tools:
33
20
 
34
- Eight focused tools that work automatically:
21
+ | Tool | Purpose | Savings |
22
+ |------|---------|---------|
23
+ | `smart_read` | Read files in outline/signatures mode | 90% |
24
+ | `smart_read_batch` | Read multiple files in one call | 90% |
25
+ | `smart_search` | Intent-aware code search with ranking | 95% |
26
+ | `smart_context` | One-call context builder | 85% |
27
+ | `smart_summary` | Session state management | 98% |
28
+ | `smart_turn` | Turn orchestration | - |
29
+ | `smart_metrics` | Token usage inspection | - |
30
+ | `smart_shell` | Safe command execution | 94% |
31
+ | `build_index` | Symbol index builder | - |
32
+ | `warm_cache` | File preloading (5x faster cold start) | - |
33
+ | `git_blame` | Function-level code attribution | - |
34
+ | `cross_project` | Multi-project context | - |
35
35
 
36
- - `smart_read`: compact file summaries instead of full file dumps (3x compression)
37
- - `smart_read_batch`: read multiple files in one call — reduces round-trip latency
38
- - `smart_search`: ripgrep-first code search with intent-aware ranking (21x compression)
39
- - `smart_context`: one-call context planner — search + read + graph expansion
40
- - `smart_summary`: maintain compressed conversation state across sessions (46x compression)
41
- - `smart_turn`: one-call turn orchestration for start/end context recovery and checkpointing
42
- - `smart_metrics`: inspect saved token metrics and recent usage through MCP
43
- - `smart_shell`: safe diagnostic shell execution with restricted commands (18x compression)
44
- - `build_index`: lightweight symbol index for faster lookups and smarter ranking
36
+ ## Real Metrics
45
37
 
46
- **Strongest in:** Modern web/backend codebases (JS/TS, React, Next.js, Node.js, Python, Go, Rust), infra repos (Terraform, Docker, YAML)
38
+ Production usage: **14.5M tokens 1.6M tokens** (89.87% reduction)
47
39
 
48
- ## Example: Before vs After
40
+ ## Core Tools
49
41
 
50
- ### Without this MCP
51
- ```
52
- Agent: Let me read auth.js...
53
- [Reads 4,000 tokens of full file]
42
+ ### smart_read
54
43
 
55
- Agent: Let me search for "jwt validation"...
56
- [Returns 10,000 tokens of grep results]
44
+ Read files without full content:
57
45
 
58
- Agent: [Next turn] What were we doing?
59
- [Repeats 5,000 tokens of context]
60
-
61
- Total: ~19,000 tokens
62
- ```
46
+ ```javascript
47
+ // Outline mode: structure only (~400 tokens vs 4000)
48
+ { filePath: 'src/server.js', mode: 'outline' }
63
49
 
64
- ### With this MCP
50
+ // Extract specific function
51
+ { filePath: 'src/auth.js', mode: 'symbol', symbol: 'validateToken' }
65
52
  ```
66
- Agent: Let me use smart_read on auth.js...
67
- [Returns 500 tokens of signatures]
68
-
69
- Agent: Let me use smart_search for "jwt validation"...
70
- [Returns 400 tokens of ranked snippets]
71
53
 
72
- Agent: [Next turn] Let me get the context...
73
- [smart_summary returns 100 tokens]
54
+ **Modes**: `outline`, `signatures`, `symbol`, `range`, `full`
74
55
 
75
- Total: ~1,000 tokens (95% reduction)
76
- ```
56
+ ### smart_search
77
57
 
78
- ## Quick start
58
+ Intent-aware search with ranking:
79
59
 
80
- ```bash
81
- npm install smart-context-mcp
82
- npx smart-context-init --target .
60
+ ```javascript
61
+ { query: 'authentication', intent: 'debug' } // Prioritizes errors, logs
62
+ { query: 'UserModel', intent: 'implementation' } // Prioritizes source
83
63
  ```
84
64
 
85
- This installs the MCP server and generates client configs for Cursor, Codex, Qwen, and Claude Code. Open the project with your IDE/agent and the server starts automatically.
86
- If the target is a git repository, `smart-context-init` also installs an idempotent `pre-commit` hook that blocks commits when `.devctx/state.sqlite` is staged, tracked, or not properly ignored.
87
- For Claude Code, `smart-context-init` also generates `.claude/settings.json` with native hooks so devctx context recovery and turn-end enforcement run automatically.
88
-
89
- ## Binaries
90
-
91
- The package exposes five binaries:
92
-
93
- - `smart-context-headless`
94
- - `smart-context-server`
95
- - `smart-context-init`
96
- - `smart-context-report`
97
- - `smart-context-protect`
65
+ **Intents**: `implementation`, `debug`, `tests`, `config`, `docs`, `explore`
98
66
 
99
- Start the MCP server against the current project:
67
+ ### smart_context
100
68
 
101
- ```bash
102
- smart-context-server
103
- ```
69
+ Get everything for a task in one call:
104
70
 
105
- Start it against another repository:
106
-
107
- ```bash
108
- smart-context-server --project-root /path/to/target-repo
71
+ ```javascript
72
+ {
73
+ task: 'Fix authentication bug',
74
+ detail: 'balanced', // minimal | balanced | deep
75
+ maxTokens: 8000
76
+ }
109
77
  ```
110
78
 
111
- ## Generate client configs
79
+ Returns: relevant files + compressed content + symbol details + graph relationships
112
80
 
113
- Generate MCP config files for a target project:
81
+ ### smart_summary
114
82
 
115
- ```bash
116
- smart-context-init --target /path/to/project
117
- ```
83
+ Maintain session state:
118
84
 
119
- Limit the generated clients if needed:
85
+ ```javascript
86
+ // Start
87
+ { action: 'update', update: { goal: 'Implement OAuth', status: 'in_progress' }}
120
88
 
121
- ```bash
122
- smart-context-init --target /path/to/project --clients cursor,codex,qwen,claude
89
+ // Resume
90
+ { action: 'get' }
123
91
  ```
124
92
 
125
- Override the command used in generated configs:
126
-
127
- ```bash
128
- smart-context-init --target /path/to/project --command node --args '["./tools/devctx/src/mcp-server.js"]'
129
- ```
93
+ ## New Features
130
94
 
131
- ## Metrics
95
+ ### Diff-Aware Context
132
96
 
133
- Each tool call persists token metrics to the target repo by default in:
97
+ Analyze git changes intelligently:
134
98
 
135
- ```bash
136
- .devctx/state.sqlite
99
+ ```javascript
100
+ { task: 'Review changes', diff: 'main' }
137
101
  ```
138
102
 
139
- SQLite is now the primary project-local store for persisted context and metrics. Running `smart-context-init` also adds `.devctx/` to the target repo's `.gitignore` idempotently.
103
+ Returns changed files prioritized by impact + related files (importers, tests).
140
104
 
141
- When an active session exists, metrics entries automatically inherit its `sessionId`, so you can inspect savings per task with `smart_metrics`.
105
+ ### Context Prediction
142
106
 
143
- Show a quick report:
107
+ Learn from usage and predict needed files:
144
108
 
145
- ```bash
146
- smart-context-report
147
- ```
148
-
149
- Show JSON output or inspect a legacy/custom JSONL file explicitly:
150
-
151
- ```bash
152
- smart-context-report --json
153
- smart-context-report --file ./.devctx/metrics.jsonl
109
+ ```javascript
110
+ { task: 'Implement auth', prefetch: true }
154
111
  ```
155
112
 
156
- Example output:
113
+ After 3+ similar tasks: 40-60% fewer round-trips, 15-20% additional savings.
157
114
 
158
- ```text
159
- devctx metrics report
115
+ ### Cache Warming
160
116
 
161
- File: /path/to/repo/.devctx/state.sqlite
162
- Source: sqlite
163
- Entries: 148
164
- Raw tokens: 182,340
165
- Final tokens: 41,920
166
- Saved tokens: 140,420 (77.01%)
117
+ Eliminate cold-start latency:
167
118
 
168
- By tool:
169
- smart_context count=42 raw=96,200 final=24,180 saved=72,020 (74.86%)
170
- smart_read count=71 raw=52,810 final=9,940 saved=42,870 (81.18%)
171
- smart_search count=35 raw=33,330 final=7,800 saved=25,530 (76.59%)
119
+ ```javascript
120
+ { incremental: true, warmCache: true }
172
121
  ```
173
122
 
174
- If you need JSONL compatibility for external tooling, set `DEVCTX_METRICS_FILE` or pass `--file`.
175
-
176
- ## Usage per client
177
-
178
- After installing and running `smart-context-init`, each client picks up the server automatically:
179
-
180
- ### Cursor
181
-
182
- Open the project in Cursor. The MCP server starts automatically. Enable it in **Cursor Settings > MCP** if needed. All eight tools are available in Agent mode.
123
+ First query: 250ms 50ms (5x faster).
183
124
 
184
- ### Codex CLI
125
+ ### Git Blame
185
126
 
186
- ```bash
187
- cd /path/to/your-project
188
- codex
189
- ```
127
+ Function-level attribution:
190
128
 
191
- Codex reads `.codex/config.toml` and starts the MCP server on launch.
129
+ ```javascript
130
+ // Who wrote each function?
131
+ { mode: 'symbol', filePath: 'src/server.js' }
192
132
 
193
- ### Claude Code
133
+ // Find code by author
134
+ { mode: 'author', authorQuery: 'alice@example.com' }
194
135
 
195
- ```bash
196
- cd /path/to/your-project
197
- claude
136
+ // Recent changes
137
+ { mode: 'recent', daysBack: 7 }
198
138
  ```
199
139
 
200
- Claude Code reads `.mcp.json` from the project root and `.claude/settings.json` for native hook automation.
201
-
202
- ### Codex/Qwen headless fallback
140
+ ### Cross-Project Context
203
141
 
204
- When a client does not expose native per-turn hooks, use `smart-context-headless` to wrap a headless CLI run and force `smart_turn(start)` plus a closing checkpoint around that invocation.
142
+ Work across monorepos:
205
143
 
206
- Examples:
144
+ ```javascript
145
+ // Search all projects
146
+ { mode: 'search', query: 'AuthService' }
207
147
 
208
- ```bash
209
- smart-context-headless --client codex --prompt "Finish the runtime repo-safety docs" -- codex exec
210
- smart-context-headless --client qwen --prompt "Review the persisted session and propose the next step" -- qwen -p
148
+ // Find symbol across projects
149
+ { mode: 'symbol', symbolName: 'validateToken' }
211
150
  ```
212
151
 
213
- This is the current automation path for non-Claude CLI agents. GUI clients without hook support still rely on generated rules plus `smart_turn`.
214
-
215
- ### Qwen Code
216
-
217
- Open the project in Qwen Code. The MCP server starts from `.qwen/settings.json`.
218
-
219
- ## Agent rules
152
+ Requires `.devctx-projects.json` config.
220
153
 
221
- `smart-context-init` generates agent rules that instruct AI agents to prefer devctx tools over their built-in equivalents. This is what makes agents use `smart_read` in outline/signatures mode instead of reading full files.
154
+ ## Supported Languages
222
155
 
223
- ### Intent-based workflows
156
+ **AST parsing**: JavaScript, TypeScript, JSX, TSX
224
157
 
225
- The `intent` parameter in `smart_search` and `smart_context` adjusts ranking and suggests optimal workflows:
158
+ **Heuristic**: Python, Go, Rust, Java, C#, Kotlin, PHP, Swift
226
159
 
227
- | Intent | Ranking priority | Suggested workflow |
228
- |--------|-----------------|-------------------|
229
- | `debug` | Error messages, stack traces, logs | Search error → read signatures → inspect symbol → smart_shell |
230
- | `implementation` | Source files, changed files | Read outline/signatures → focus on changed symbols |
231
- | `tests` | Test files, spec files | Find tests → read symbol of function under test |
232
- | `config` | Config files, env vars, YAML/JSON | Find settings → read full config files |
233
- | `explore` | Entry points, main modules | Directory structure → outlines of key modules |
160
+ **Structural**: Shell, Terraform, HCL, Dockerfile, SQL, JSON, YAML, TOML
234
161
 
235
- ### Generated files per client
162
+ ## Client Support
236
163
 
237
- - **Cursor**: `.cursor/rules/devctx.mdc` (always-apply rule)
238
- - **Codex**: `AGENTS.md` (devctx section with sentinel markers)
239
- - **Claude Code**: `CLAUDE.md` (devctx section with sentinel markers) and `.claude/settings.json` (native hooks)
164
+ - Cursor (`.cursor/mcp.json`)
165
+ - Codex CLI (`.codex/config.toml`)
166
+ - Claude Code (`.mcp.json` + `.claude/settings.json`)
167
+ - Qwen Code (`.qwen/settings.json`)
240
168
 
241
- The generated files are idempotent — running `smart-context-init` again updates the devctx sections and Claude hook entries without duplicating them. Existing content in `AGENTS.md`, `CLAUDE.md`, and `.claude/settings.json` is preserved.
242
-
243
- ## Use against another repo
244
-
245
- By default, `devctx` works against the repo where it is installed. You can point it at another repo without modifying that target project:
246
-
247
- ```bash
248
- node ./src/mcp-server.js --project-root /path/to/target-repo
249
- ```
250
-
251
- or:
252
-
253
- ```bash
254
- DEVCTX_PROJECT_ROOT=/path/to/target-repo node ./src/mcp-server.js
255
- ```
256
-
257
- or (recommended for MCP clients and generated configs):
169
+ ## Commands
258
170
 
259
171
  ```bash
260
- DEVCTX_PROJECT_ROOT=/path/to/target-repo node ./src/mcp-server.js
261
- ```
262
-
263
- Legacy configs that still set `MCP_PROJECT_ROOT` remain supported for backward compatibility.
264
-
265
- `smart-context-init` automatically sets `DEVCTX_PROJECT_ROOT` in the generated client configs (`.cursor/mcp.json`, `.codex/config.toml`, `.mcp.json`, `.qwen/settings.json`), so the MCP server always launches from the correct project context, even in monorepos or when installed globally.
266
-
267
- ## What it is good at
268
-
269
- | Level | Languages / Stack | Use cases |
270
- |-------|------------------|-----------|
271
- | **Strong** | JS/TS, React, Next.js, Node.js, Python | Modern web apps, monorepos, backend services, scripts |
272
- | **Strong** | Terraform, Docker, YAML, shell, SQL | Infra/platform repos, config-heavy codebases |
273
- | **Good** | Go, Rust, Java, C#/.NET, Kotlin, PHP, Swift | Services, libraries, Android/iOS, Laravel/Symfony |
274
- | **Partial** | Enterprise Java/C# with heavy frameworks | Generated code, polyglot monorepos needing semantic ranking |
275
- | **Limited** | Ruby, Elixir, Scala | Deep semantic understanding required, general shell needs |
276
-
277
- ## Tool behavior
278
-
279
- ### `smart_read`
280
-
281
- Modes:
282
-
283
- - `outline` — compact structural summary (~90% token savings)
284
- - `signatures` — exported API surface only
285
- - `range` — specific line range with line numbers (`startLine`, `endLine`)
286
- - `symbol` — extract function/class/method by name; accepts a string or an array for batch extraction
287
- - `full` — file content capped at 12k chars, with truncation marker when needed
288
-
289
- The `symbol` mode supports nested methods (class methods, object methods), interface signatures, and multiline function signatures across all supported languages.
290
-
291
- Cross-file symbol context:
292
-
293
- - Pass `context: true` with `symbol` mode to include callers, tests, and referenced types from the dependency graph
294
- - Callers: files that import the current file and reference the symbol (via graph + ripgrep)
295
- - Tests: test files related to the current file that mention the symbol
296
- - Types: type/interface names referenced in the symbol definition that exist in the index
297
- - Requires `build_index` for graph data; without it, the definition is returned with an empty context and a hint
298
- - Response includes `context: { callers, tests, types }` with counts, `graphCoverage: { imports, tests }` (`full|partial|none`), and `contextHints` if applicable
299
- - `graphCoverage` indicates how reliable cross-file context is: `full` for JS/TS/Python/Go (imports resolved), `partial` for C#/Kotlin/PHP/Swift (imports extracted but namespace-based), `none` for other languages
300
-
301
- Token budget mode:
302
-
303
- - Pass `maxTokens` to let the tool auto-select the most detailed mode that fits the budget
304
- - Cascade order: `full` -> `outline` -> `signatures` -> truncated
305
- - If the requested mode (or default `outline`) exceeds the budget, the tool falls back to a more compact mode automatically
306
- - `range` and `symbol` modes do not cascade but will truncate by tokens if needed
307
- - When the mode changes, the response includes `chosenMode` (the mode actually used) and `budgetApplied: true`
308
-
309
- Responses are cached in memory per session. If the same file+mode is requested again and the file's `mtime` has not changed, the cached result is returned without re-parsing. The response includes `cached: true` when served from cache.
310
-
311
- Every response includes a `confidence` block:
312
-
313
- ```json
314
- { "parser": "ast|heuristic|fallback|raw", "truncated": false, "cached": false }
315
- ```
316
-
317
- Additional metadata: `indexHint` (symbol mode), `chosenMode`/`budgetApplied` (token budget), `graphCoverage` (symbol+context mode).
318
-
319
- **Example response (outline mode):**
320
-
321
- ```json
322
- {
323
- "mode": "outline",
324
- "parser": "ast",
325
- "truncated": false,
326
- "cached": false,
327
- "tokens": 245,
328
- "confidence": { "parser": "ast", "truncated": false, "cached": false },
329
- "content": "import express from 'express';\nexport class AuthMiddleware { ... }\nexport function requireRole(role: string) { ... }"
330
- }
331
- ```
332
-
333
- Current support:
334
-
335
- - First-class (AST): JS, JSX, TS, TSX
336
- - Heuristic: Python, Go, Rust, Java, C#, Kotlin, PHP, Swift, shell, Terraform, HCL, Dockerfile, SQL, JSON, TOML, YAML
337
- - Fallback: plain-text structural extraction for unsupported formats
338
-
339
- ### `smart_read_batch`
340
-
341
- Read multiple files in one MCP call. Reduces round-trip latency for common patterns like "read the outline of these 5 files".
342
-
343
- Parameters:
344
-
345
- - `files` (required, max 20) — array of items, each with:
346
- - `path` (required) — file path
347
- - `mode` (optional) — `outline`, `signatures`, `full`, `range`, `symbol`
348
- - `symbol`, `startLine`, `endLine` (optional) — as in `smart_read`
349
- - `maxTokens` (optional) — per-file token budget with automatic mode cascade
350
- - `maxTokens` (optional) — global token budget; stops reading more files once exceeded (at least 1 file is always read)
351
-
352
- Response:
353
-
354
- ```json
355
- {
356
- "results": [
357
- { "filePath": "...", "mode": "outline", "parser": "ast", "truncated": false, "content": "..." },
358
- { "filePath": "...", "mode": "signatures", "parser": "heuristic", "truncated": false, "content": "..." }
359
- ],
360
- "metrics": { "totalTokens": 450, "filesRead": 2, "filesSkipped": 0, "totalSavingsPct": 88 }
361
- }
362
- ```
363
-
364
- ### `smart_search`
365
-
366
- - Uses embedded ripgrep via `@vscode/ripgrep`
367
- - Falls back to filesystem walking if rg is unavailable or fails
368
- - Groups matches by file, ranks results to reduce noise
369
- - Optional `intent` parameter adjusts ranking: `implementation`, `debug`, `tests`, `config`, `docs`, `explore`
370
- - When a symbol index exists (via `build_index`), files with matching definitions get +50 ranking bonus, and related files (importers, tests, neighbors) get +25 graph boost
371
- - Index is loaded from `projectRoot`, so subdirectory searches still benefit from the project-level index
372
- - Returns `confidence` block: `{ "level": "high", "indexFreshness": "fresh" }`
373
-
374
- **Example response:**
375
-
376
- ```json
377
- {
378
- "engine": "rg",
379
- "retrievalConfidence": "high",
380
- "indexFreshness": "fresh",
381
- "confidence": { "level": "high", "indexFreshness": "fresh" },
382
- "sourceBreakdown": { "textMatch": 7, "indexBoost": 2, "graphBoost": 1 },
383
- "results": [
384
- { "file": "src/auth/middleware.js", "matches": 3, "rank": 150, "preview": "export class AuthMiddleware { ..." }
385
- ]
386
- }
387
- ```
388
-
389
- ### `smart_context`
390
-
391
- One-call context planner. Instead of the manual cycle of `smart_search` → `smart_read` → `smart_read` → ..., `smart_context` receives a task description and returns curated context in a single response.
392
-
393
- **Pipeline:**
394
-
395
- ```
396
- task input → intent detection → search/diff → graph expansion → smart_read_batch → symbol extraction → response
397
- ```
398
-
399
- **Parameters:**
400
- - `task` (required) — natural language description (e.g., `"debug the auth flow in AuthMiddleware"`)
401
- - `intent` (optional) — override auto-detected intent
402
- - `detail` (optional) — `minimal` | `balanced` (default) | `deep`
403
- - `maxTokens` (optional, default 8000) — token budget
404
- - `entryFile` (optional) — guarantee specific file inclusion
405
- - `diff` (optional) — `true` (vs HEAD) or git ref (`"main"`) to scope to changed files only
406
- - `include` (optional) — `["content","graph","hints","symbolDetail"]` to control response fields
407
-
408
- **Detail modes:**
409
-
410
- | Mode | Behavior | Use when |
411
- |------|----------|----------|
412
- | `minimal` | Index-first: paths, roles, evidence, signatures, symbol previews (no file reads) | Fastest exploration, budget-constrained |
413
- | `balanced` | Batch read with smart compression (outline/signatures) | Default, most tasks |
414
- | `deep` | Full content reads | Deep investigation, debugging |
415
-
416
- **How it works:**
417
-
418
- 1. **Search or diff**: Extracts queries from task and runs `smart_search`, OR runs `git diff` when `diff` parameter provided
419
- 2. **Graph expansion**: Expands top results via relational graph (imports, importedBy, tests, neighbors)
420
- 3. **Read strategy**: Index-first mode (no file reads) OR batch read mode using `smart_read_batch` with role-based compression
421
- 4. **Symbol extraction**: Detects identifiers in task and extracts focused symbol details
422
- 5. **Deduplication**: In `minimal` mode, omits redundant outline when `symbolDetail` covers same file
423
- 6. **Assembly**: Returns curated context with `reasonIncluded` / `evidence` per item, graph summary, hints, and confidence block
424
-
425
- Diff mode is ideal for PR review and debugging recent changes — reads only changed files plus their tests and dependencies.
426
-
427
- Example response:
428
-
429
- ```json
430
- {
431
- "task": "debug AuthMiddleware",
432
- "intent": "debug",
433
- "indexFreshness": "fresh",
434
- "confidence": { "indexFreshness": "fresh", "graphCoverage": { "imports": "full", "tests": "full" } },
435
- "context": [
436
- { "file": "src/auth/middleware.js", "role": "primary", "readMode": "outline", "reasonIncluded": "Matched task search: AuthMiddleware", "evidence": [{ "type": "searchHit", "query": "AuthMiddleware", "rank": 1 }, { "type": "symbolMatch", "symbols": ["AuthMiddleware"] }], "symbols": ["AuthMiddleware", "requireRole"], "symbolPreviews": [{ "name": "AuthMiddleware", "kind": "class", "signature": "export class AuthMiddleware", "snippet": "export class AuthMiddleware { ..." }], "content": "..." },
437
- { "file": "tests/auth.test.js", "role": "test", "readMode": "signatures", "reasonIncluded": "Test for src/auth/middleware.js", "evidence": [{ "type": "testOf", "via": "src/auth/middleware.js" }], "content": "..." },
438
- { "file": "src/utils/jwt.js", "role": "dependency", "readMode": "signatures", "reasonIncluded": "Imported by src/auth/middleware.js", "evidence": [{ "type": "dependencyOf", "via": "src/auth/middleware.js" }], "content": "..." },
439
- { "file": "src/auth/middleware.js", "role": "symbolDetail", "readMode": "symbol", "reasonIncluded": "Focused symbol detail: AuthMiddleware", "evidence": [{ "type": "symbolDetail", "symbols": ["AuthMiddleware"] }], "content": "..." }
440
- ],
441
- "graph": {
442
- "primaryImports": ["src/utils/jwt.js"],
443
- "tests": ["tests/auth.test.js"],
444
- "dependents": [],
445
- "neighbors": ["src/utils/logger.js"]
446
- },
447
- "graphCoverage": { "imports": "full", "tests": "full" },
448
- "metrics": { "totalTokens": 1200, "filesIncluded": 4, "filesEvaluated": 8, "savingsPct": 82 },
449
- "hints": ["Inspect symbols with smart_read: verifyJwt, createJwt"]
450
- }
451
- ```
452
-
453
- `graphCoverage` indicates how complete the relational context is: `full` for JS/TS/Python/Go (imports resolved to local files), `partial` for C#/Kotlin/PHP/Swift (imports extracted but namespace-based), `none` for other languages. When files from multiple languages are included, the level reflects the weakest coverage.
454
-
455
- File roles: `primary` (search hits or changed files), `test` (related test files), `dependency` (imports), `dependent` (importedBy), `symbolDetail` (extracted symbol bodies). Each item also includes `reasonIncluded` and structured `evidence` so the agent knows why it was selected.
456
-
457
- When using diff mode, the response includes a `diffSummary`:
458
-
459
- ```json
460
- {
461
- "diffSummary": { "ref": "main", "totalChanged": 5, "included": 3, "skippedDeleted": 1 }
462
- }
463
- ```
464
-
465
- ### `smart_summary`
466
-
467
- Maintain compressed conversation state across sessions. Solves the context-loss problem when resuming work after hours or days.
468
-
469
- **Actions:**
470
-
471
- | Action | Purpose | Returns |
472
- |--------|---------|---------|
473
- | `get` | Retrieve current, explicit, or auto-resolved session | Resume summary (≤500 tokens) + compression metadata |
474
- | `update` | Create or replace session | New session with compressed state |
475
- | `append` | Add to existing session | Merged session state |
476
- | `auto_append` | Add only when something meaningful changed | Merged session state or skipped no-op result |
477
- | `checkpoint` | Event-driven orchestration for persistence decisions | Persisted update or skipped event with decision metadata |
478
- | `reset` | Clear session | Confirmation |
479
- | `list_sessions` | Show all available sessions | Array of sessions with metadata |
480
- | `compact` | Apply retention/compaction to SQLite state | Counts for pruned sessions, events, and metrics |
481
- | `cleanup_legacy` | Inspect or remove imported JSON/JSONL artifacts | Dry-run or deletion report |
482
-
483
- **Parameters:**
484
- - `action` (required) — one of the actions above
485
- - `sessionId` (optional) — session identifier; auto-generated from `goal` if omitted. Pass `"auto"` to accept the recommended recent session when multiple candidates exist.
486
- - `update` (required for update/append/auto_append/checkpoint) — object with:
487
- - `goal`: primary objective
488
- - `status`: current state (`planning` | `in_progress` | `blocked` | `completed`)
489
- - `pinnedContext`: critical context that should survive compression when possible
490
- - `unresolvedQuestions`: open questions that matter for the next turn
491
- - `currentFocus`: current work area in one short phrase
492
- - `whyBlocked`: blocker summary when status is `blocked`
493
- - `completed`: array of completed steps
494
- - `decisions`: array of key decisions with rationale
495
- - `blockers`: array of current blockers
496
- - `nextStep`: immediate next action
497
- - `touchedFiles`: array of modified files
498
- - `maxTokens` (optional, default 500) — hard cap on summary size
499
- - `event` (optional for `checkpoint`) — one of `manual`, `milestone`, `decision`, `blocker`, `status_change`, `file_change`, `task_switch`, `task_complete`, `session_end`, `read_only`, `heartbeat`
500
- - `force` (optional, default false) — override a suppressed checkpoint event
501
- - `retentionDays` (optional, default 30) — used by `compact`
502
- - `keepLatestEventsPerSession` (optional, default 20) — used by `compact`
503
- - `keepLatestMetrics` (optional, default 1000) — used by `compact`
504
- - `vacuum` (optional, default false) — run SQLite `VACUUM` after deletions during `compact`
505
- - `apply` (optional, default false) — required to actually delete files during `cleanup_legacy`
506
-
507
- `update` replaces the stored session state for that `sessionId`, so omitted fields are cleared. Use `append` when you want to keep existing state and add progress incrementally. Use `auto_append` when the caller may fire checkpoint saves often and you want the tool to skip no-op updates automatically. Use `checkpoint` when the caller has a meaningful event and wants the tool to decide whether that event deserves persistence.
508
-
509
- **Storage:**
510
- - Session state, session events, summary cache, and metrics persist in `.devctx/state.sqlite`
511
- - Legacy `.devctx/sessions/*.json`, `.devctx/sessions/active.json`, and `.devctx/metrics.jsonl` are imported idempotently when present
512
- - `compact` enforces retention without deleting the active session
513
- - `cleanup_legacy` is dry-run by default and only deletes imported legacy artifacts when `apply: true`
514
-
515
- **Auto-resume behavior:**
516
- - `get` returns the active session immediately when `active.json` exists
517
- - If there is no active session, `get` auto-resumes the best saved session when there is a single clear candidate
518
- - If multiple recent sessions are plausible, `get` returns ordered `candidates` plus `recommendedSessionId`
519
- - Passing `sessionId: "auto"` accepts that recommendation and restores it as the active session
520
-
521
- **Resume summary fields:**
522
- - `status` and `nextStep` are preserved with highest priority
523
- - `pinnedContext` and `unresolvedQuestions` preserve critical context and open questions
524
- - `currentFocus` and `whyBlocked` are included when relevant
525
- - `recentCompleted`, `keyDecisions`, and `hotFiles` are derived from the persisted state
526
- - `completedCount`, `decisionsCount`, and `touchedFilesCount` preserve activity scale cheaply
527
- - Empty fields are omitted to save tokens
528
-
529
- **Response metadata:**
530
- - `schemaVersion`: persisted session schema version
531
- - `truncated`: whether the resume summary had to be compressed
532
- - `compressionLevel`: `none` | `trimmed` | `reduced` | `status_only`
533
- - `omitted`: fields dropped from the resume summary to fit the token budget
534
- - `repoSafety`: git hygiene signal for `.devctx/state.sqlite` (`isIgnored`, `isTracked`, `isStaged`, warnings, recommended actions)
535
- - mutating actions (`update`, `append`, `auto_append`, `checkpoint`, `reset`, `compact`) are blocked at runtime when `.devctx/state.sqlite` is tracked or staged
536
-
537
- **Compression strategy:**
538
- - Keeps the persisted session state intact and compresses only the resume summary
539
- - Prioritizes `nextStep`, `status`, and active blockers over history
540
- - Deduplicates repeated completed steps, decisions, and touched files
541
- - Uses token-aware reduction until the summary fits `maxTokens`
542
-
543
- **Example workflow:**
544
-
545
- ```javascript
546
- // Start of work session
547
- smart_summary({ action: "get" })
548
- // → retrieves last active session or auto-resumes the best saved session
549
-
550
- // After implementing auth middleware
551
- smart_summary({
552
- action: "checkpoint",
553
- event: "milestone",
554
- update: {
555
- completed: ["auth middleware"],
556
- decisions: ["JWT with 1h expiry, refresh tokens in Redis"],
557
- touchedFiles: ["src/middleware/auth.js"],
558
- nextStep: "add role-based access control"
559
- }
560
- })
561
-
562
- // Monday after weekend - resume work
563
- smart_summary({ action: "get" })
564
- // → full context restored, continue from nextStep
565
-
566
- // List all sessions
567
- smart_summary({ action: "list_sessions" })
568
- // → see all available sessions, pick one to resume
569
-
570
- // Inspect git safety for project-local state from any smart_summary response
571
- smart_summary({ action: "get" })
572
- // → repoSafety warns if .devctx/state.sqlite is tracked or not ignored
573
-
574
- // Suppress noisy read-only exploration checkpoints
575
- smart_summary({
576
- action: "checkpoint",
577
- event: "read_only",
578
- update: { currentFocus: "inspect auth flow" }
579
- })
580
- // → skipped=true, no event persisted
581
-
582
- // Compact old SQLite events while keeping recent history
583
- smart_summary({ action: "compact", retentionDays: 30, keepLatestEventsPerSession: 20, keepLatestMetrics: 1000 })
584
-
585
- // Inspect what legacy files are safe to remove
586
- smart_summary({ action: "cleanup_legacy" })
587
-
588
- // Remove imported legacy JSON/JSONL artifacts explicitly
589
- smart_summary({ action: "cleanup_legacy", apply: true })
590
- ```
591
-
592
- ### `smart_metrics`
172
+ # Start server
173
+ smart-context-server
593
174
 
594
- Inspect token metrics recorded in project-local SQLite storage without leaving MCP.
175
+ # Against another repo
176
+ smart-context-server --project-root /path/to/repo
595
177
 
596
- - Returns aggregated totals, savings percentage, and per-tool breakdowns
597
- - Supports `window`: `24h` | `7d` | `30d` | `all`
598
- - Supports filtering by `tool`
599
- - Supports filtering by `sessionId`, including `sessionId: "active"`
600
- - Includes `latestEntries` so an agent can explain recent savings without parsing storage manually
601
- - Includes `overheadTokens` and `overheadTools` so hook/wrapper context cost stays measurable against the savings
602
- - When `.devctx/state.sqlite` is tracked or staged, metric writes are skipped and reads fall back to a temporary read-only snapshot with `sideEffectsSuppressed: true`
178
+ # Generate configs
179
+ smart-context-init --target /path/to/project
603
180
 
604
- **Example workflow:**
181
+ # View metrics
182
+ smart-context-report
605
183
 
606
- ```javascript
607
- smart_metrics({ window: "7d", sessionId: "active" })
608
- // → totals and recent entries for the current task/session
184
+ # Verify features
185
+ npm run verify
609
186
  ```
610
187
 
611
- ### `smart_turn`
612
-
613
- Orchestrate the start or end of a meaningful agent turn with one MCP call.
188
+ ## Storage
614
189
 
615
- - `phase: "start"` rehydrates context, classifies whether the current prompt aligns with persisted work, and can auto-create a planning session for a substantial new task
616
- - `phase: "end"` writes a checkpoint through `smart_summary` and can optionally include compact metrics
617
- - Designed to make context usage almost mandatory without forcing the agent to chain `smart_summary(get)` and `smart_summary(checkpoint)` manually on every turn
618
- - Claude Code can invoke this automatically through generated native hooks on `SessionStart`, `UserPromptSubmit`, `PostToolUse`, and `Stop`
619
- - Non-Claude CLI clients can approximate the same flow with `smart-context-headless`, which wraps one headless agent invocation around `smart_turn(start)` and `smart_turn(end)`
190
+ Data stored in `.devctx/`:
191
+ - `index.json` - Symbol index
192
+ - `state.sqlite` - Sessions, metrics, patterns
193
+ - `metrics.jsonl` - Legacy fallback
620
194
 
621
- **Example workflow:**
622
-
623
- ```javascript
624
- smart_turn({
625
- phase: "start",
626
- prompt: "Finish runtime repo-safety enforcement for smart metrics",
627
- ensureSession: true
628
- })
629
- // → summary + continuity classification + repoSafety
630
-
631
- smart_turn({
632
- phase: "end",
633
- event: "milestone",
634
- update: {
635
- completed: ["Finished smart metrics repo-safety enforcement"],
636
- nextStep: "Update docs and run the full suite"
637
- }
638
- })
639
- // → checkpoint result + optional compact metrics
195
+ Add to `.gitignore`:
640
196
  ```
641
-
642
- ### `build_index`
643
-
644
- - Builds a lightweight symbol index for the project (functions, classes, methods, types, etc.)
645
- - Supports JS/TS (via TypeScript AST), Python, Go, Rust, Java, C#, Kotlin, PHP, Swift
646
- - Extracts imports/exports and builds a dependency graph with `import` and `testOf` edges
647
- - Test files are linked to source files via import analysis and naming conventions
648
- - Index stored per-project in `.devctx/index.json`, invalidated by file mtime
649
- - Each symbol includes a condensed `signature` (one line, max 200 chars) and a short `snippet` preview so agents can inspect likely definitions without opening files
650
- - Accelerates `smart_search` (symbol + graph ranking) and `smart_read` symbol mode (line hints)
651
- - Pass `incremental=true` to only reindex files with changed mtime — much faster for large repos (10k+ files). Falls back to full rebuild if no prior index exists.
652
- - Incremental response includes `reindexed`, `removed`, `unchanged` counts
653
- - Run once after checkout or when many files changed; not required but recommended for large projects
654
-
655
- ### `smart_shell`
656
-
657
- - Runs only allowlisted diagnostic commands
658
- - Executes from the effective project root
659
- - Blocks shell operators and unsafe commands by design
660
-
661
- ## Evaluations (repo development only)
662
-
663
- The eval harness and corpora are available in the [source repository](https://github.com/Arrayo/devctx-mcp-mvp) but are **not included in the npm package**. Clone the repo to run evaluations.
664
-
665
- ```bash
666
- cd tools/devctx
667
- npm run eval
668
- npm run eval -- --baseline
669
- npm run eval:self
670
- npm run eval:context
671
- npm run eval:both
672
- npm run eval:report
197
+ .devctx/
673
198
  ```
674
199
 
675
- Commands:
676
- - `eval` — synthetic corpus with index + intent
677
- - `eval -- --baseline` — baseline without index/intent
678
- - `eval:self` — self-eval against the real devctx repo
679
- - `eval:context` — evaluate smart_context alongside search
680
- - `eval:both` — search + context evaluation
681
- - `eval:report` — scorecard with delta vs baseline
682
-
683
- The harness supports `--root=` and `--corpus=` for evaluating against any repo with custom task corpora. Use `--tool=search|context|both` to control which tools are evaluated. When `--tool=context`, pass/fail is determined by `smart_context` precision; when `--tool=both`, both search and context must pass.
200
+ ## Requirements
684
201
 
685
- Metrics include: P@5, P@10, Recall, wrong-file rate, retrieval honesty, follow-up reads, tokens-to-success, latency p50/p95, confidence calibration (accuracy, over-confident rate, under-confident rate), and smart_context metrics when applicable. smart_context reporting now includes precision, explanation coverage (`reasonIncluded` + `evidence`), preview coverage (`symbolPreviews`), and preview symbol recall. Token metrics (`totalTokens`) reflect the full JSON payload, not just content blocks.
202
+ - Node.js 18+ (22+ for SQLite features)
203
+ - Git (for diff and blame features)
686
204
 
687
- ## Notes
205
+ ## Documentation
688
206
 
689
- - `@vscode/ripgrep` provides a bundled `rg` binary, so a system install is not required.
690
- - Persistent context and metrics live in `<projectRoot>/.devctx/state.sqlite`.
691
- - `DEVCTX_METRICS_FILE` is now an explicit compatibility override for JSONL-based workflows and reports.
692
- - Symbol index stored in `<projectRoot>/.devctx/index.json` when `build_index` is used.
693
- - Legacy session JSON files in `<projectRoot>/.devctx/sessions/` are imported idempotently when present.
694
- - This package is a navigation and diagnostics layer, not a full semantic code intelligence system.
207
+ Full documentation in [GitHub repository](https://github.com/Arrayo/devctx-mcp-mvp):
695
208
 
696
- ## Repository
209
+ - [STREAMING.md](https://github.com/Arrayo/devctx-mcp-mvp/blob/main/STREAMING.md) - Progress notifications
210
+ - [CONTEXT-PREDICTION.md](https://github.com/Arrayo/devctx-mcp-mvp/blob/main/CONTEXT-PREDICTION.md) - File prediction
211
+ - [DIFF-AWARE.md](https://github.com/Arrayo/devctx-mcp-mvp/blob/main/DIFF-AWARE.md) - Change analysis
212
+ - [CACHE-WARMING.md](https://github.com/Arrayo/devctx-mcp-mvp/blob/main/CACHE-WARMING.md) - Cold-start optimization
213
+ - [GIT-BLAME.md](https://github.com/Arrayo/devctx-mcp-mvp/blob/main/GIT-BLAME.md) - Code attribution
214
+ - [CROSS-PROJECT.md](https://github.com/Arrayo/devctx-mcp-mvp/blob/main/CROSS-PROJECT.md) - Multi-project support
697
215
 
698
- Source repository and full project documentation:
216
+ ## Links
699
217
 
700
- - https://github.com/Arrayo/devctx-mcp-mvp
218
+ - [GitHub](https://github.com/Arrayo/devctx-mcp-mvp)
219
+ - [npm](https://www.npmjs.com/package/smart-context-mcp)
220
+ - [Issues](https://github.com/Arrayo/devctx-mcp-mvp/issues)
701
221
 
702
222
  ## Author
703
223
 
704
224
  **Francisco Caballero Portero**
705
- Email: fcp1978@hotmail.com
706
- GitHub: [@Arrayo](https://github.com/Arrayo)
225
+ fcp1978@hotmail.com
226
+ [@Arrayo](https://github.com/Arrayo)
707
227
 
708
228
  ## License
709
229
 
710
- MIT License - see [LICENSE](LICENSE) file for details.
230
+ MIT