ctxgraph 0.3.0__tar.gz → 0.3.2__tar.gz

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 (52) hide show
  1. {ctxgraph-0.3.0/src/ctxgraph.egg-info → ctxgraph-0.3.2}/PKG-INFO +290 -137
  2. ctxgraph-0.3.0/PKG-INFO → ctxgraph-0.3.2/README.md +763 -639
  3. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/pyproject.toml +3 -3
  4. ctxgraph-0.3.0/README.md → ctxgraph-0.3.2/src/ctxgraph.egg-info/PKG-INFO +792 -610
  5. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/setup.cfg +0 -0
  6. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/__init__.py +0 -0
  7. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/analyzers/__init__.py +0 -0
  8. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/analyzers/python/__init__.py +0 -0
  9. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/analyzers/python/importer.py +0 -0
  10. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/analyzers/python/semantic.py +0 -0
  11. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/analyzers/python/symbols.py +0 -0
  12. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/capsule/__init__.py +0 -0
  13. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/capsule/renderer.py +0 -0
  14. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/capsule/savings.py +0 -0
  15. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/cli/__init__.py +0 -0
  16. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/cli/main.py +0 -0
  17. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/clients/__init__.py +0 -0
  18. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/clients/models.py +0 -0
  19. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/config/__init__.py +0 -0
  20. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/config/init.py +0 -0
  21. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/config/providers.py +0 -0
  22. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/config/settings.py +0 -0
  23. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/exclude/__init__.py +0 -0
  24. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/exclude/patterns.py +0 -0
  25. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/graph/__init__.py +0 -0
  26. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/graph/builder.py +0 -0
  27. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/graph/models.py +0 -0
  28. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/graph/query.py +0 -0
  29. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/graph/storage.py +0 -0
  30. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/history.py +0 -0
  31. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/mcp/__init__.py +0 -0
  32. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/mcp/server.py +0 -0
  33. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/skills/__init__.py +0 -0
  34. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/view/__init__.py +0 -0
  35. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/view/visualizer.py +0 -0
  36. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/wrapper/__init__.py +0 -0
  37. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph/wrapper/claude.py +0 -0
  38. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph.egg-info/SOURCES.txt +0 -0
  39. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph.egg-info/dependency_links.txt +0 -0
  40. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph.egg-info/entry_points.txt +0 -0
  41. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph.egg-info/requires.txt +0 -0
  42. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/src/ctxgraph.egg-info/top_level.txt +0 -0
  43. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_analyzers.py +0 -0
  44. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_benchmark.py +0 -0
  45. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_capsule.py +0 -0
  46. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_config.py +0 -0
  47. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_e2e.py +0 -0
  48. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_integration.py +0 -0
  49. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_model_mode.py +0 -0
  50. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_models.py +0 -0
  51. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_query.py +0 -0
  52. {ctxgraph-0.3.0 → ctxgraph-0.3.2}/tests/test_storage.py +0 -0
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ctxgraph
3
- Version: 0.3.0
4
- Summary: AI context engine for Python cuts LLM tokens 97% via code knowledge graphs. Build, query, and generate compact context capsules for Claude, OpenAI, Ollama.
3
+ Version: 0.3.2
4
+ Summary: 97% cheaper AI coding. ctx init ctx build ctx ask. Knowledge graphs, token-efficient capsules, query history, skills, and multi-provider LLM answers (Ollama, Claude, OpenAI, Azure). Cut context costs from cents to fractions of a cent.
5
5
  Author: ctxgraph contributors
6
6
  License: MIT
7
- Keywords: code-graph,knowledge-graph,claude,code-analysis,ai-context
7
+ Keywords: code-graph,knowledge-graph,claude,code-analysis,ai-context,token-savings,ollama,openai,ctx,capsule
8
8
  Classifier: Development Status :: 3 - Alpha
9
9
  Classifier: Intended Audience :: Developers
10
10
  Classifier: License :: OSI Approved :: MIT License
@@ -29,13 +29,16 @@ Requires-Dist: ruff>=0.1.0; extra == "dev"
29
29
 
30
30
  # ctxgraph — AI Context Engine for Python
31
31
 
32
- **Slash your LLM token costs by 97%.** ctxgraph builds a multi-layer knowledge graph from your Python codebase, then generates *compact context capsules* — delivering only what your AI needs, not every line of code.
32
+ **Slash your LLM token costs by 97%.** Stop dumping entire files into AI context. ctxgraph builds a multi-layer knowledge graph from your Python codebase and generates *compact context capsules* — delivering only what your AI needs, not every line of code.
33
33
 
34
34
  ```bash
35
35
  pip install ctxgraph
36
36
 
37
- ctx build # Build knowledge graph
37
+ ctx init # Scaffold .ctxgraph with config + default skills
38
+ ctx build # Build knowledge graph (AST analysis → SQLite)
39
+ ctx ask "how does JWT auth work" # Ask questions with automatic token savings
38
40
  ctx capsule "fix JWT expiry" # 92-99% fewer tokens vs raw code
41
+ ctx history --stats # Track total tokens saved across all queries
39
42
  ccg "fix the login redirect bug" # Launch Claude with context pre-loaded
40
43
  ctx view # Interactive D3.js visualization (or --svg for static)
41
44
  ```
@@ -44,6 +47,24 @@ ctx view # Interactive D3.js visualization (or --svg f
44
47
 
45
48
  ---
46
49
 
50
+ ## Quick Start
51
+
52
+ ```bash
53
+ # 1. Initialize project
54
+ ctx init # Creates .ctxgraph/config.toml + default skills
55
+
56
+ # 2. Build the knowledge graph
57
+ ctx build # AST analysis → SQLite graph
58
+
59
+ # 3. Ask questions (requires Ollama or other LLM provider)
60
+ ctx ask "how does authentication work"
61
+
62
+ # 4. Or generate a capsule for your AI tool
63
+ ctx capsule "fix login rate limiter" --savings
64
+ ```
65
+
66
+ ---
67
+
47
68
  ## Why ctxgraph?
48
69
 
49
70
  Sending entire files to an AI is wasteful. ctxgraph analyzes your code with AST-based static analysis, stores the result in a queryable SQLite graph, and retrieves *only the relevant nodes* — compressed into a token-efficient DSL format.
@@ -83,35 +104,6 @@ Repository (.py files)
83
104
  └─────────────────────────────────────────────────────────┘
84
105
  ```
85
106
 
86
- ### Architecture
87
-
88
- ```
89
- ┌─────────┐ ┌──────────────┐ ┌──────────────┐ ┌─────────────┐
90
- │ CLI │───▶│ Analyzers │───▶│ SQLite DB │───▶│ Skills/Hist │
91
- │ typer │ │ AST-based │ │ .ctxgraph/ │ │ .ctxgraph/ │
92
- └────┬────┘ └──────────────┘ └──────┬───────┘ └─────────────┘
93
- │ │
94
- ├── ctx build ──────────────────────▶│ Graph build
95
- │ │
96
- ├── ctx capsule ◀───────────────────│ Query + BFS
97
- │ │
98
- ├── ctx query ◀─────────────────────│ Keyword search
99
- │ │
100
- ├── ctx ask ◀──────────────────────│ LLM query + savings
101
- │ │
102
- ├── ctx view ◀──────────────────────│ D3.js viz
103
- │ │
104
- ├── ctx serve ◀─────────────────────│ MCP server
105
- │ │
106
- ├── ctx init ───────────────────────│ Scaffold project
107
- │ │
108
- ├── ctx history ◀───────────────────│ Query log
109
- │ │
110
- └── ctx skill ◀─────────────────────│ Skill management
111
- │ │
112
- └── ccg wrapper ───▶ Claude Code ───┘ AI tool
113
- ```
114
-
115
107
  ---
116
108
 
117
109
  ## Token Efficiency
@@ -150,27 +142,8 @@ JSON: 426 tokens DSL: 143 tokens
150
142
 
151
143
  > **97.0% average token reduction** across 4 projects, 42 benchmark runs. The larger the project, the greater the savings.
152
144
 
153
- ### With Graph vs Without (Ollama)
154
-
155
- | Query | No Context | With ctxgraph | Δ |
156
- |-------|:----------:|:-------------:|:-:|
157
- | Calculator expression parsing | 100% | 100% | — |
158
- | Plugin registration system | 33% | **100%** | **+67pp** |
159
- | JWT authentication (web_api) | 75% | **100%** | **+25pp** |
160
- | Middleware pipeline (web_api) | 100% | 100% | — |
161
- | Circuit breaker (microsvc) | 75% | 75% | — |
162
- | Services & communication | 50% | **100%** | **+50pp** |
163
- | PipelineBuilder pattern | 100% | 75% | -25pp* |
164
- | Processor registration | 33% | **67%** | **+34pp** |
165
- | Event bus & error handling | 100% | 100% | — |
166
- > \* Without context the model gave a generic answer matching all keywords; with context it focused on actual code — more honest, more useful.
167
-
168
- **+16.7pp average coverage improvement** — better answers, concrete file names, real code structure.
169
-
170
145
  ### Token Savings Display
171
146
 
172
- Use `--savings` to see how many tokens each capsule saves:
173
-
174
147
  ```bash
175
148
  ctx capsule "user authentication" --savings
176
149
  # ┌──────────────────────────┬──────────────┐
@@ -184,37 +157,95 @@ ctx capsule "user authentication" --savings
184
157
  # └──────────────────────────┴──────────────┘
185
158
  ```
186
159
 
187
- `ctx ask` shows this automatically on every query.
160
+ `ctx ask` shows this automatically on every query. See how many tokens you save with each question.
188
161
 
189
162
  ---
190
163
 
191
164
  ## Commands
192
165
 
193
166
  ### `ctx init` — Scaffold project
167
+
194
168
  ```bash
195
- ctx init
196
- # Creates: .ctxgraph/config.toml, .ctxgraph/skills/, .ctxgraph/history.jsonl
169
+ ctx init # Default: current directory
170
+ ctx init /path/to/project # Specific path
197
171
  ```
198
- Sets up a fresh `.ctxgraph/` directory with default config and built-in skills. Idempotent — safe to run on existing projects.
172
+
173
+ Creates a `.ctxgraph/` directory with everything you need:
174
+
175
+ ```
176
+ .ctxgraph/
177
+ ├── config.toml # API provider, model, context settings
178
+ ├── history.jsonl # Query history (auto-created, auto-pruned to 1000)
179
+ └── skills/
180
+ ├── project-style.toml # Default skill: project conventions
181
+ └── field-guide.toml # Default skill: field guide
182
+ ```
183
+
184
+ Idempotent — safe to run on existing projects. Existing files are never overwritten.
199
185
 
200
186
  ### `ctx build` — Build knowledge graph
187
+
201
188
  ```bash
202
189
  ctx build # Current directory
203
190
  ctx build /path/to/project # Specific repo
204
191
  ctx build --exclude "vendor/*" # Custom exclude patterns
192
+ ctx build --provider claude # Set LLM provider for later use
193
+ ctx build --model gpt-4o # Set LLM model for later use
205
194
  ```
206
195
 
207
196
  ### `ctx ask <query>` — Ask questions via LLM
197
+
198
+ The marquee command. Generates a context capsule, sends it to your LLM provider, and shows token savings.
199
+
208
200
  ```bash
209
- ctx ask "how does JWT auth work" # Uses configured provider
201
+ ctx ask "how does JWT auth work" # Uses configured provider (default: Ollama)
210
202
  ctx ask "fix login bug" --provider claude --model claude-sonnet-4-20250514
211
- ctx ask "refactor payment flow" --skill project-style # Activate a skill
212
- ctx ask "find auth code" --graph # Show graph search results
213
- ctx ask "deep dive" --mode deep # Deep graph context
203
+ ctx ask "refactor payment flow" --skill project-style # Activate a skill as system prompt
204
+ ctx ask "find auth code" --graph # Show graph search results table
205
+ ctx ask "deep dive" --mode deep # Use deep graph context (40 nodes)
206
+ ctx ask "explain" --skill field-guide --graph # Combine skill + graph results
214
207
  ```
215
- Shows token savings automatically. Requires a running Ollama instance (or other configured provider).
216
208
 
217
- ### `ctx capsule <query>` — Generate context
209
+ **Flags:**
210
+
211
+ | Flag | Description |
212
+ |------|-------------|
213
+ | `--provider` | LLM provider: `ollama`, `claude`, `openai`, `azure`, `custom` |
214
+ | `--model` | Model name (e.g. `gpt-4o`, `claude-sonnet-4-20250514`) |
215
+ | `--skill` | Skill name to activate (e.g. `project-style`, `field-guide`) |
216
+ | `--graph` | Show ranked graph search results alongside the answer |
217
+ | `--mode` | Graph context mode: `fast`, `balanced` (default), `deep` |
218
+
219
+ **Requirements:**
220
+ - A built graph (run `ctx build` first)
221
+ - A running LLM provider (Ollama by default at `http://localhost:11434`)
222
+
223
+ **Example output:**
224
+
225
+ ```bash
226
+ $ ctx ask "how does the greeting system work"
227
+
228
+ # (Token savings table shown automatically)
229
+ ┌──────────────────────────┬──────────────┐
230
+ │ Metric │ Value │
231
+ ├──────────────────────────┼──────────────┤
232
+ │ Raw Project .py Files │ 1,234 tokens │
233
+ │ Capsule DSL │ 45 tokens │
234
+ │ JSON Equivalent │ 180 tokens │
235
+ │ Savings vs Raw │ 96.4% │
236
+ │ DSL vs JSON │ 75.0% │
237
+ └──────────────────────────┴──────────────┘
238
+
239
+ Answer:
240
+ The greeting system is implemented in `src/greet.py`. The `greet()` function takes a name
241
+ and returns a formatted greeting string. The `Greeter` class extends this with a configurable
242
+ prefix. Both use type hints and follow the project conventions.
243
+ ```
244
+
245
+ ### `ctx capsule <query>` — Generate context capsule
246
+
247
+ Generate a token-efficient DSL capsule for use with any AI tool.
248
+
218
249
  ```bash
219
250
  ctx capsule "fix JWT token validation" # Balanced (default: 20 nodes, depth 2)
220
251
  ctx capsule "fix JWT token validation" --mode fast # Fast (10 nodes, depth 1)
@@ -231,55 +262,127 @@ ctx capsule "fix auth" --skill project-style # Prepends skill context
231
262
  | `deep` | 40 | 3 | Complex refactoring, architecture |
232
263
 
233
264
  ### `ctx query <search>` — Search graph
265
+
234
266
  ```bash
235
267
  ctx query "user auth"
236
268
  ctx query "payment gateway" --mode deep
237
269
  ```
238
- Returns ranked nodes with relevance scores.
270
+
271
+ Returns ranked nodes with relevance scores, displaying type, name, path, and score.
239
272
 
240
273
  ### `ctx view` — Visualize graph
274
+
241
275
  ```bash
242
- ctx view
243
- ctx view --output graph.html
244
- ctx view --port 8080 --no-open
276
+ ctx view # Opens interactive D3.js HTML in browser
277
+ ctx view --output graph.html # Save to custom path
278
+ ctx view --svg # Generate static SVG
279
+ ctx view --no-open # Generate without opening browser
245
280
  ```
246
- Interactive D3.js force-directed HTML — no JS build tools needed.
247
281
 
248
- ### `ctx serve` — MCP server
282
+ ### `ctx history` — Query history
283
+
284
+ Review past questions, token savings, and provider usage.
285
+
249
286
  ```bash
250
- pip install ctxgraph[mcp]
251
- ctx serve
287
+ ctx history # Last 10 queries
288
+ ctx history -n 20 # Last 20
289
+ ctx history --filter "auth" # Filter by keyword in query text
290
+ ctx history --stats # Aggregate statistics
252
291
  ```
253
- Claude Desktop config:
254
- ```json
255
- {
256
- "mcpServers": {
257
- "ctxgraph": {
258
- "command": "ctx",
259
- "args": ["serve"]
260
- }
261
- }
262
- }
292
+
293
+ **Example output:**
294
+
295
+ ```bash
296
+ $ ctx history
297
+ ┌────────────┬──────────────────────────────────────────────────┬─────────┬──────────┬──────────┐
298
+ Time │ Query │ Savings │ Provider │ Skill │
299
+ ├────────────┼──────────────────────────────────────────────────┼─────────┼──────────┼──────────┤
300
+ │ 2026-06-06 │ how does the greeting system work │ 96.4% │ ollama │ - │
301
+ │ 2026-06-06 │ fix login rate limiter │ 97.1% │ ollama │ - │
302
+ │ 2026-06-06 │ refactor payment module with proper error handl… │ 95.8% │ claude │ project-style │
303
+ └────────────┴──────────────────────────────────────────────────┴─────────┴──────────┴──────────┘
304
+
305
+ $ ctx history --stats
306
+ ┌──────────────────────┬───────────┐
307
+ │ Metric │ Value │
308
+ ├──────────────────────┼───────────┤
309
+ │ Total Queries │ 24 │
310
+ │ Total Raw Tokens │ 254,088 │
311
+ │ Total Tokens Saved │ 246,465 │
312
+ │ Avg Savings │ 96.5% │
313
+ │ Provider: ollama │ 18 │
314
+ │ Provider: claude │ 6 │
315
+ └──────────────────────┴───────────┘
316
+ ```
317
+
318
+ History is stored as JSONL in `.ctxgraph/history.jsonl`. Auto-prunes to 1000 entries (oldest dropped).
319
+
320
+ ### `ctx skill` — Manage skills
321
+
322
+ Skills are reusable system prompts that prepend project knowledge to your capsule context.
323
+
324
+ ```bash
325
+ ctx skill list # Show all available skills
326
+ ctx skill show project-style # Display a skill's contents
263
327
  ```
264
- Tools: `search_graph`, `get_context_capsule`, `get_file_dependencies`, `get_project_overview`.
265
328
 
266
- ### `ctx history` — Query history
329
+ **Example:**
330
+
267
331
  ```bash
268
- ctx history # Last 10 queries
269
- ctx history -n 20 # Last 20
270
- ctx history --filter "auth" # Filter by keyword
271
- ctx history --stats # Aggregate statistics
332
+ $ ctx skill list
333
+ ┌─────────┬─────────────────┬────────────────────────────────────────────────────┐
334
+ Source │ Name │ Preview │
335
+ ├─────────┼─────────────────┼────────────────────────────────────────────────────┤
336
+ │ builtin │ project-style │ # Project Style Guide — default ctxgraph skill │
337
+ │ builtin │ field-guide │ # Project Field Guide — default ctxgraph skill │
338
+ └─────────┴─────────────────┴────────────────────────────────────────────────────┘
339
+
340
+ $ ctx skill show project-style
341
+ Skill: project-style
342
+
343
+ # Project Style Guide — default ctxgraph skill
344
+ # Activate with: ctx ask --skill project-style "..."
345
+
346
+ [about]
347
+ name = "Project Style Guide"
348
+ description = "Enforces project conventions, code style, and naming patterns"
349
+
350
+ [rules]
351
+ import_style = "absolute imports, grouped: stdlib, third-party, local"
352
+ naming = "snake_case for functions/variables, PascalCase for classes, UPPER_CASE for constants"
353
+ ...
272
354
  ```
273
- History stored in JSONL format at `.ctxgraph/history.jsonl`. Auto-prunes to 1000 entries.
274
355
 
275
- ### `ctx skill` — Manage skills
356
+ **Activating a skill:**
357
+
276
358
  ```bash
277
- ctx skill list # Show all available skills
278
- ctx skill show project-style # Display skill contents
359
+ ctx ask "refactor payment flow" --skill project-style
360
+ ctx capsule "fix auth" --skill field-guide
361
+ ```
362
+
363
+ When activated, the skill contents are prepended as a `## Project Knowledge` section before the capsule DSL.
364
+
365
+ **Creating your own skills:**
366
+
367
+ Skills are TOML files in `.ctxgraph/skills/`. Create a new file:
368
+
369
+ ```toml
370
+ # .ctxgraph/skills/my-team-rules.toml
371
+
372
+ [about]
373
+ name = "Team Conventions"
374
+ description = "Custom team coding conventions"
375
+
376
+ [rules]
377
+ testing = "must write pytest tests for all new functions"
378
+ documentation = "every public API needs a docstring with Args and Returns"
379
+ branching = "prefer early returns over nested if-else"
279
380
  ```
280
- Skills are TOML files in `.ctxgraph/skills/`. Activate with `ctx ask --skill <name>` or `ctx capsule --skill <name>`.
381
+
382
+ Now it appears in `ctx skill list` and can be activated with `--skill my-team-rules`.
281
383
 
282
384
  ### `ctx info` — Graph statistics
385
+
283
386
  ```bash
284
387
  ctx info
285
388
  # ┌────────────────────┬───────┐
@@ -291,10 +394,34 @@ ctx info
291
394
  # └────────────────────┴───────┘
292
395
  ```
293
396
 
397
+ ### `ctx serve` — MCP server
398
+
399
+ ```bash
400
+ pip install ctxgraph[mcp]
401
+ ctx serve
402
+ ```
403
+
404
+ Claude Desktop config:
405
+
406
+ ```json
407
+ {
408
+ "mcpServers": {
409
+ "ctxgraph": {
410
+ "command": "ctx",
411
+ "args": ["serve"]
412
+ }
413
+ }
414
+ }
415
+ ```
416
+
417
+ Tools: `search_graph`, `get_context_capsule`, `get_file_dependencies`, `get_project_overview`.
418
+
294
419
  ---
295
420
 
296
421
  ## Claude Wrapper (`ccg`)
297
422
 
423
+ The `ccg` command launches Claude Code with ctxgraph context pre-loaded:
424
+
298
425
  ```bash
299
426
  ccg "fix the JWT expiry bug in auth module" # Single-shot
300
427
  ccg --chat "refactor the payment flow" # Interactive session
@@ -306,7 +433,8 @@ ccg --mode deep "redesign the database schema" # Deep mode
306
433
 
307
434
  ## Configuration
308
435
 
309
- `.ctxgraph/config.toml`:
436
+ `.ctxgraph/config.toml` is auto-created by `ctx init`:
437
+
310
438
  ```toml
311
439
  [graph]
312
440
  exclude = ["legacy/*", "vendor/*"]
@@ -315,6 +443,7 @@ exclude = ["legacy/*", "vendor/*"]
315
443
  provider = "ollama" # ollama, claude, openai, azure, custom
316
444
  model = "qwen2.5-coder:7b"
317
445
  endpoint = "http://localhost:11434"
446
+ api_key = ""
318
447
 
319
448
  [context]
320
449
  mode = "balanced"
@@ -322,45 +451,46 @@ max_nodes = 20
322
451
  max_depth = 2
323
452
  ```
324
453
 
325
- | Environment Variable | Overrides |
326
- |----------------------|-----------|
327
- | `CTXGRAPH_PROVIDER` | `ai.provider` |
328
- | `CTXGRAPH_MODEL` | `ai.model` |
329
- | `CTXGRAPH_ENDPOINT` | `ai.endpoint` |
330
- | `ANTHROPIC_API_KEY` | Claude API |
331
- | `OPENAI_API_KEY` | OpenAI API |
332
- | `AZURE_OPENAI_API_KEY` | Azure OpenAI API |
454
+ ### Environment variables
455
+
456
+ | Variable | Overrides | Required For |
457
+ |----------|-----------|-------------|
458
+ | `CTXGRAPH_PROVIDER` | `ai.provider` | — |
459
+ | `CTXGRAPH_MODEL` | `ai.model` | |
460
+ | `CTXGRAPH_ENDPOINT` | `ai.endpoint` | |
461
+ | `ANTHROPIC_API_KEY` | `ai.api_key` | Claude provider |
462
+ | `OPENAI_API_KEY` | `ai.api_key` | OpenAI provider |
463
+ | `AZURE_OPENAI_API_KEY` | `ai.api_key` | Azure provider |
464
+
465
+ ### Provider examples
333
466
 
334
467
  ```bash
335
468
  # Ollama (default — no env vars needed)
336
- ctx capsule "query"
469
+ ctx ask "how does auth work"
337
470
 
338
471
  # Claude
339
- CTXGRAPH_PROVIDER=claude CTXGRAPH_MODEL=claude-sonnet-4-20250514 ctx capsule "query"
472
+ CTXGRAPH_PROVIDER=claude CTXGRAPH_MODEL=claude-sonnet-4-20250514 ctx ask "explain the architecture"
340
473
 
341
474
  # OpenAI
342
- CTXGRAPH_PROVIDER=openai CTXGRAPH_MODEL=gpt-4o ctx capsule "query"
475
+ CTXGRAPH_PROVIDER=openai CTXGRAPH_MODEL=gpt-4o ctx ask "find the bug"
343
476
 
344
477
  # Azure OpenAI
345
478
  CTXGRAPH_PROVIDER=azure \
346
479
  CTXGRAPH_MODEL=gpt-4o \
347
480
  CTXGRAPH_ENDPOINT=https://my-resource.openai.azure.com \
348
481
  AZURE_OPENAI_API_KEY=sk-... \
349
- ctx capsule "query"
482
+ ctx ask "refactor this"
350
483
 
351
484
  # Custom (OpenAI-compatible)
352
- CTXGRAPH_PROVIDER=custom CTXGRAPH_ENDPOINT=http://my-api/v1 ctx capsule "query"
485
+ CTXGRAPH_PROVIDER=custom CTXGRAPH_ENDPOINT=http://my-api/v1 ctx ask "explain"
486
+
487
+ # Per-command override (overrides both config and env vars)
488
+ ctx ask "how does this work" --provider claude --model claude-sonnet-4-20250514
353
489
  ```
354
490
 
355
491
  > **Windows (PowerShell):** Use `$env:` prefix instead:
356
492
  > ```powershell
357
- > $env:CTXGRAPH_PROVIDER = "azure"; $env:CTXGRAPH_MODEL = "gpt-4o"; ctx capsule "query"
358
- > ```
359
- > Or set them once per session:
360
- > ```powershell
361
- > $env:CTXGRAPH_PROVIDER = "azure"
362
- > $env:AZURE_OPENAI_API_KEY = "sk-..."
363
- > ctx capsule "query"
493
+ > $env:CTXGRAPH_PROVIDER = "azure"; $env:CTXGRAPH_MODEL = "gpt-4o"; ctx ask "query"
364
494
  > ```
365
495
 
366
496
  ---
@@ -368,6 +498,7 @@ CTXGRAPH_PROVIDER=custom CTXGRAPH_ENDPOINT=http://my-api/v1 ctx capsule "query"
368
498
  ## Use Cases
369
499
 
370
500
  ### Debug a failing test
501
+
371
502
  ```bash
372
503
  ctx build
373
504
  ctx capsule "test_user_login is failing with auth error" --mode deep
@@ -375,17 +506,35 @@ ctx capsule "test_user_login is failing with auth error" --mode deep
375
506
  # [F]src/auth/login.py
376
507
  # [C]AuthService
377
508
  # [DEP] auth/login.py → core/database.py, auth/session.py
509
+
510
+ # Or ask directly:
511
+ ctx ask "test_user_login is failing" --mode deep
512
+ # → Explains the issue with file references and suggests fixes
378
513
  ```
379
514
 
380
515
  ### Understand a new codebase
516
+
381
517
  ```bash
518
+ ctx build
382
519
  ctx capsule "project architecture" --overview
383
520
  ccg --chat "explain the overall architecture and data flow"
521
+
522
+ # Or explore with skills active:
523
+ ctx ask "explain the architecture" --skill field-guide
384
524
  ```
385
525
 
386
526
  ### Refactor across modules
527
+
387
528
  ```bash
388
529
  ctx capsule "extract payment processing into separate module" --mode deep
530
+ ctx ask "plan the payment module extraction" --skill project-style --mode deep
531
+ ```
532
+
533
+ ### Track your LLM usage
534
+
535
+ ```bash
536
+ ctx history --stats
537
+ # Shows total queries, tokens saved, avg savings, provider breakdown
389
538
  ```
390
539
 
391
540
  ---
@@ -433,6 +582,27 @@ for node, score in results:
433
582
 
434
583
  > **Tip:** `build_graph` is a one-time setup. In production, run `ctx build` during CI/deployment and let your app code only call `get_storage` + `render_capsule`.
435
584
 
585
+ ### Compose with skill context
586
+
587
+ ```python
588
+ from ctxgraph.skills import load_skill
589
+
590
+ storage = get_storage(Path("./my_project"))
591
+ skill_text = load_skill(Path("./my_project"), "project-style")
592
+ capsule = render_capsule(storage, "fix auth", max_nodes=20, skill_context=skill_text)
593
+ # Capsule now has "## Project Knowledge" section prepended
594
+ ```
595
+
596
+ ### Compute token savings
597
+
598
+ ```python
599
+ from ctxgraph.capsule.savings import compute_savings
600
+
601
+ savings = compute_savings(Path("./my_project"), capsule_text)
602
+ print(f"Saved {savings['savings_pct']}% tokens")
603
+ print(f"DSL is {savings['dsl_vs_json']}% more efficient than JSON")
604
+ ```
605
+
436
606
  ### LangChain
437
607
 
438
608
  Pass the capsule as context in your prompt template. The LLM gets exactly the files, classes, and dependencies it needs — no token waste.
@@ -441,13 +611,10 @@ Pass the capsule as context in your prompt template. The LLM gets exactly the fi
441
611
  from pathlib import Path
442
612
  from langchain_openai import ChatOpenAI
443
613
  from langchain_core.prompts import ChatPromptTemplate
444
- from ctxgraph.graph.builder import get_storage # graph already built
614
+ from ctxgraph.graph.builder import get_storage
445
615
  from ctxgraph.capsule.renderer import render_capsule
446
616
 
447
- # Load existing graph (zero build time)
448
617
  storage = get_storage(Path("./my_project"))
449
-
450
- # Generate capsule for the question
451
618
  context = render_capsule(storage, "login rate limiter", max_nodes=15)
452
619
 
453
620
  prompt = ChatPromptTemplate.from_messages([
@@ -462,13 +629,11 @@ print(response.invoke({
462
629
  "context": context,
463
630
  "question": "Where is the rate limiter applied in the login flow?",
464
631
  }))
465
- # → "The rate limiter is in src/auth/middleware.py at line 42.
466
- # It wraps the login endpoint with a 5req/min limit per IP."
467
632
  ```
468
633
 
469
634
  ### LangGraph
470
635
 
471
- Expose ctxgraph as a tool the agent calls on-demand. The agent fetches context only when it hits a code-related question.
636
+ Expose ctxgraph as a tool the agent calls on-demand.
472
637
 
473
638
  ```python
474
639
  from pathlib import Path
@@ -479,17 +644,13 @@ from langchain_core.tools import tool
479
644
  from ctxgraph.graph.builder import get_storage
480
645
  from ctxgraph.capsule.renderer import render_capsule
481
646
 
482
- # Pre-built graph — loaded instantly
483
647
  _storage = get_storage(Path("./my_project"))
484
648
 
485
649
  @tool
486
650
  def code_context(task: str) -> str:
487
- """Fetch relevant source code for a development task.
488
- Use this whenever the user asks about implementation details,
489
- bug fixes, or architecture in the codebase."""
651
+ """Fetch relevant source code for a development task."""
490
652
  return render_capsule(_storage, task, max_nodes=20)
491
653
 
492
- # --- Build LangGraph ---
493
654
  tools = [code_context]
494
655
  model = ChatOpenAI(model="gpt-4o", temperature=0).bind_tools(tools)
495
656
 
@@ -508,7 +669,6 @@ graph.add_edge("tools", "agent")
508
669
 
509
670
  app = graph.compile()
510
671
 
511
- # --- Run ---
512
672
  for chunk in app.stream({"messages": [("user", "How does payment retry work?")]}):
513
673
  for node, vals in chunk.items():
514
674
  msg = vals["messages"][0]
@@ -516,12 +676,8 @@ for chunk in app.stream({"messages": [("user", "How does payment retry work?")]}
516
676
  print(f"[{node}]: {msg.content[:300]}")
517
677
  ```
518
678
 
519
- When the user asks about code, the agent calls `code_context("payment retry")`, gets back a capsule with `[F]src/payment/retry.py`, `[F]src/payment/processor.py`, and their dependency edges, then answers with those files in context.
520
-
521
679
  ### OpenAI Agents SDK
522
680
 
523
- Same pattern — ctxgraph is a function tool the agent invokes.
524
-
525
681
  ```python
526
682
  from pathlib import Path
527
683
  from agents import Agent, Runner, function_tool
@@ -532,8 +688,7 @@ _storage = get_storage(Path("./my_project"))
532
688
 
533
689
  @function_tool
534
690
  def fetch_code_context(task_description: str) -> str:
535
- """Retrieve code context from the project's knowledge graph.
536
- Provide a task description like 'JWT auth middleware' or 'payment processor'."""
691
+ """Retrieve code context from the project's knowledge graph."""
537
692
  return render_capsule(_storage, task_description, max_nodes=20)
538
693
 
539
694
  agent = Agent(
@@ -549,8 +704,6 @@ print(result.final_output)
549
704
 
550
705
  ### Azure OpenAI (direct client)
551
706
 
552
- For Azure OpenAI or any OpenAI-compatible endpoint, inject the capsule directly into the system message.
553
-
554
707
  ```python
555
708
  import os
556
709
  from openai import AzureOpenAI
@@ -568,7 +721,7 @@ client = AzureOpenAI(
568
721
  )
569
722
 
570
723
  response = client.chat.completions.create(
571
- model="gpt-4o", # deployment name
724
+ model="gpt-4o",
572
725
  messages=[
573
726
  {"role": "system", "content": f"You are a senior developer. Code context:\n\n{context}"},
574
727
  {"role": "user", "content": "How do I add a new event handler?"},
@@ -585,12 +738,13 @@ print(response.choices[0].message.content)
585
738
  git clone https://github.com/shashi3070/ctxgraph.git
586
739
  cd ctxgraph
587
740
  pip install -e ".[dev]"
588
- pytest
741
+ pytest # 88+ tests
589
742
  python benchmarks/run_benchmarks.py
590
743
  python benchmarks/run_ollama_comparison.py # Requires local Ollama
591
744
  ```
592
745
 
593
746
  ### Project Structure
747
+
594
748
  ```
595
749
  src/ctxgraph/
596
750
  ├── cli/main.py — Typer CLI (9 commands)
@@ -607,7 +761,6 @@ src/ctxgraph/
607
761
  │ ├── symbols.py — AST class/function/method analysis
608
762
  │ └── semantic.py — Docstring summarization
609
763
  ├── config/
610
- │ ├── __init__.py
611
764
  │ ├── init.py — Project scaffold (.ctxgraph dir)
612
765
  │ ├── settings.py — TOML/JSON/env config loading
613
766
  │ └── providers.py — Ollama, Claude, OpenAI clients