codebase-snapshot 0.20.12 → 0.21.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/CHANGELOG.md +44 -6
- package/README.md +46 -19
- package/SECURITY.md +2 -2
- package/dist/bin.js +12173 -9823
- package/dist/{chunk-TUKY6KLH.js → chunk-TDPYLX7X.js} +6312 -4472
- package/dist/cli.cjs +12074 -9744
- package/dist/cli.d.cts +76 -2
- package/dist/cli.d.ts +76 -2
- package/dist/cli.js +549 -14
- package/dist/{generate-worker-DMzNbsAW.d.cts → generate-worker-D56JeXwx.d.cts} +2 -2
- package/dist/{generate-worker-CRMESc0b.d.ts → generate-worker-DakxZs-l.d.ts} +2 -2
- package/dist/index.cjs +6292 -4466
- package/dist/index.d.cts +281 -63
- package/dist/index.d.ts +281 -63
- package/dist/index.js +1 -1
- package/dist/mcp.cjs +10486 -4393
- package/dist/mcp.d.cts +26 -11
- package/dist/mcp.d.ts +26 -11
- package/dist/mcp.js +1 -1
- package/dist/{types-C5ytHMYz.d.ts → types-D5XYzksd.d.cts} +50 -2
- package/dist/{types-C5ytHMYz.d.cts → types-D5XYzksd.d.ts} +50 -2
- package/docs/about.md +86 -0
- package/docs/agent.md +28 -6
- package/docs/answer.md +99 -48
- package/docs/benchmarks.md +34 -34
- package/docs/brief.md +80 -81
- package/docs/cli.md +103 -27
- package/docs/codebase-token-budget.md +14 -13
- package/docs/configuration.md +1 -1
- package/docs/contents.md +66 -0
- package/docs/deploy.md +2 -2
- package/docs/faq.md +112 -0
- package/docs/glossary.md +152 -0
- package/docs/health.md +1 -1
- package/docs/how-to-feed-a-repo-to-claude.md +6 -0
- package/docs/impact.md +102 -0
- package/docs/install.md +4 -3
- package/docs/llm-codebase-context.md +166 -0
- package/docs/mcp-server-for-codebase.md +155 -0
- package/docs/mcp.md +81 -82
- package/docs/migration.md +83 -85
- package/docs/output.md +1 -3
- package/docs/security-review.md +20 -9
- package/docs/slice.md +1 -1
- package/docs/tools.md +159 -185
- package/docs/troubleshooting.md +11 -11
- package/docs/vs-code2prompt.md +7 -0
- package/docs/vs-gitingest.md +7 -0
- package/docs/vs-repomix.md +10 -1
- package/docs/workspace.md +1 -1
- package/package.json +9 -2
package/docs/glossary.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Codebase snapshot and LLM context glossary
|
|
2
|
+
|
|
3
|
+
The words this project uses, defined plainly. Where a term has a narrower meaning in the code, the
|
|
4
|
+
narrower meaning is the one that applies.
|
|
5
|
+
|
|
6
|
+
### Codebase snapshot
|
|
7
|
+
|
|
8
|
+
A bounded, indexed copy of a repository prepared for a language model: Markdown parts containing
|
|
9
|
+
the source, plus a manifest that records every included file, every skipped path, and every file
|
|
10
|
+
whose body was shortened. A snapshot is not a git commit and not a backup; it is context with an
|
|
11
|
+
audit trail.
|
|
12
|
+
|
|
13
|
+
### Context window
|
|
14
|
+
|
|
15
|
+
The maximum number of tokens a model can read and generate in one request. A 128k context window
|
|
16
|
+
does not mean 128,000 tokens of source code: the system prompt, the question, the snapshot and the
|
|
17
|
+
reply all share the same budget.
|
|
18
|
+
|
|
19
|
+
### Token
|
|
20
|
+
|
|
21
|
+
A unit of text a model processes. Tokens are not words or characters. English prose is often around
|
|
22
|
+
four characters per token; source code is denser because identifiers, punctuation and indentation
|
|
23
|
+
tokenise less efficiently. A token count is therefore model-specific, which is why this tool
|
|
24
|
+
reports an estimate and enforces a byte ceiling rather than pretending to know a tokenizer's exact
|
|
25
|
+
answer.
|
|
26
|
+
|
|
27
|
+
### Token budget
|
|
28
|
+
|
|
29
|
+
The amount of context a run is allowed to produce. codebase-snapshot expresses it as maxPartBytes
|
|
30
|
+
because the partitioner writes bytes, and reports the estimated tokens beside it. The budget is a
|
|
31
|
+
ceiling, not a target.
|
|
32
|
+
|
|
33
|
+
### Part
|
|
34
|
+
|
|
35
|
+
One Markdown file written by the partitioner. A part contains a fixed header, a file listing and
|
|
36
|
+
one or more file sections, and never exceeds its byte budget. Parts are the unit a chat destination
|
|
37
|
+
attaches, which is why the prompt does delivery arithmetic instead of asking a model to read a
|
|
38
|
+
single enormous file.
|
|
39
|
+
|
|
40
|
+
### Manifest
|
|
41
|
+
|
|
42
|
+
The JSON index at the root of a snapshot. It records the configuration digest, file entries with
|
|
43
|
+
layer, kind, language, part and hash, skipped paths with reasons, part hashes, artifact hashes, git
|
|
44
|
+
state and counts. The manifest is the contract every reader trusts; validate compares it with the
|
|
45
|
+
tree.
|
|
46
|
+
|
|
47
|
+
### Drift
|
|
48
|
+
|
|
49
|
+
The difference between what a snapshot records and what the repository contains now. Drift includes
|
|
50
|
+
changed bytes, added and deleted files, changed ignore or configuration rules, damaged parts and
|
|
51
|
+
stale reports. validate reports drift and exits non-zero.
|
|
52
|
+
|
|
53
|
+
### Fingerprint
|
|
54
|
+
|
|
55
|
+
A hash of the snapshot's content and configuration, recorded in the manifest and printed by the
|
|
56
|
+
CLI. Two runs over unchanged input produce the same fingerprint. It is a summary, not a signature;
|
|
57
|
+
signing is a separate operation.
|
|
58
|
+
|
|
59
|
+
### MCP
|
|
60
|
+
|
|
61
|
+
Model Context Protocol: a standard way for an AI client to discover and call tools and read
|
|
62
|
+
resources exposed by a local or remote server. codebase-snapshot serves seven tools; six are
|
|
63
|
+
read-only and one writes a snapshot. See [MCP server for a codebase](mcp-server-for-codebase.md).
|
|
64
|
+
|
|
65
|
+
### Tool
|
|
66
|
+
|
|
67
|
+
A callable operation with a JSON Schema input and structured output. Tools are listed by the MCP
|
|
68
|
+
server, have a cost class, and are the unit an agent decides to call. Resources are the other half
|
|
69
|
+
of the protocol: URI-addressed content such as snapshot://manifest.
|
|
70
|
+
|
|
71
|
+
### Resource
|
|
72
|
+
|
|
73
|
+
A URI-addressed, read-only document exposed over MCP. This server publishes the manifest, the
|
|
74
|
+
prompt, the parts, reports, the contract and the skill. Resources are listed before they are read,
|
|
75
|
+
so a missing artifact is not advertised as if it existed.
|
|
76
|
+
|
|
77
|
+
### LLM context
|
|
78
|
+
|
|
79
|
+
The information a model is given before it answers. Good context is relevant, bounded, attributable
|
|
80
|
+
and current. More context is not automatically better: a 1M-token dump can bury the one file that
|
|
81
|
+
matters and leave no room for the answer.
|
|
82
|
+
|
|
83
|
+
### Repository packing
|
|
84
|
+
|
|
85
|
+
Collecting a repository into one document or a small set of documents for a model. Packing is a
|
|
86
|
+
transport decision; it says nothing about whether the content is current, complete or safe to
|
|
87
|
+
share. Those properties have to be added deliberately.
|
|
88
|
+
|
|
89
|
+
### Retrieval-augmented generation (RAG)
|
|
90
|
+
|
|
91
|
+
A pattern where a system retrieves relevant passages from an index and puts them in the model's
|
|
92
|
+
context at question time. Repository packing is the opposite trade-off: it prepares a stable,
|
|
93
|
+
inspectable artefact ahead of time. The two can be combined; codebase-snapshot's MCP tools are
|
|
94
|
+
closer to bounded retrieval over a local index.
|
|
95
|
+
|
|
96
|
+
### Agent experience (AX)
|
|
97
|
+
|
|
98
|
+
Whether an agent can fetch, read and act on a site or tool without a human translating for it.
|
|
99
|
+
Static HTML, stable URLs, a machine-readable contract, bounded responses and explicit errors are
|
|
100
|
+
AX. The [AX is the New AEO](https://arxiv.org/abs/2609.34951) study finds that agent-ready sites are
|
|
101
|
+
recommended more often and answered from their own pages more often.
|
|
102
|
+
|
|
103
|
+
### Generative engine optimization (GEO) and answer engine optimization (AEO)
|
|
104
|
+
|
|
105
|
+
The practice of making content discoverable, citable and useful in AI-generated answers. The
|
|
106
|
+
credible parts are the old parts: unique first-hand content, clear structure, verifiable facts and
|
|
107
|
+
a readable site. The unreliable parts are paid placements, synthetic listicles and claims that a
|
|
108
|
+
special file guarantees inclusion.
|
|
109
|
+
|
|
110
|
+
### llms.txt
|
|
111
|
+
|
|
112
|
+
A proposed Markdown index for language models, with a short summary and links to the pages an agent
|
|
113
|
+
should read. It is useful as an agent-facing table of contents. Google has said that special AI
|
|
114
|
+
text files are not required for its AI features, so this project treats llms.txt as documentation
|
|
115
|
+
infrastructure, not as a ranking switch.
|
|
116
|
+
|
|
117
|
+
### Secret redaction
|
|
118
|
+
|
|
119
|
+
Replacing a matched credential-shaped value with a marker before it reaches an artifact. Redaction
|
|
120
|
+
is span-based: the surrounding line and the file's structure stay readable, while the value does
|
|
121
|
+
not. It is defence in depth, not a guarantee that every possible secret shape is known.
|
|
122
|
+
|
|
123
|
+
### Byte budget
|
|
124
|
+
|
|
125
|
+
A hard ceiling on the bytes a part or artifact may contain. Bytes are what the writer controls, so
|
|
126
|
+
the budget is enforceable without a tokenizer. The tool estimates tokens from bytes for planning,
|
|
127
|
+
but the ceiling that can fail a run is the byte ceiling.
|
|
128
|
+
|
|
129
|
+
### Preview and omission
|
|
130
|
+
|
|
131
|
+
A preview is a bounded head and tail of a file that did not fit. An omission range records the
|
|
132
|
+
lines that were not emitted. The rule is that nothing disappears without a reason: a shortened file
|
|
133
|
+
is listed in incomplete[], with its reason and ranges, and verifyInMemory fails the run if the
|
|
134
|
+
accounting does not add up.
|
|
135
|
+
|
|
136
|
+
### Symbol-pruned impact
|
|
137
|
+
|
|
138
|
+
A reverse-import analysis that follows the exported symbol a change actually touches, rather than
|
|
139
|
+
every file that imports the module. It is conservative where the symbol cannot be determined and
|
|
140
|
+
reports the limits of its answer. See [impact reports](impact.md).
|
|
141
|
+
|
|
142
|
+
### Workspace
|
|
143
|
+
|
|
144
|
+
A repository whose packages are snapshotted separately but recorded in one WORKSPACE.json index.
|
|
145
|
+
A workspace run preserves each member's real path and local budget, and reports a missing member
|
|
146
|
+
rather than silently skipping it.
|
|
147
|
+
|
|
148
|
+
## Related
|
|
149
|
+
|
|
150
|
+
- [All documentation](contents.md)
|
|
151
|
+
- [FAQ](faq.md)
|
|
152
|
+
- [How many tokens is a codebase?](codebase-token-budget.md)
|
package/docs/health.md
CHANGED
|
@@ -152,7 +152,7 @@ codebase-snapshot health --all-members --cwd /repo
|
|
|
152
152
|
|
|
153
153
|
## In the MCP server
|
|
154
154
|
|
|
155
|
-
`
|
|
155
|
+
`snapshot_validate` with `mode: "audit"` returns the same report as structured output, so an agent can call it before
|
|
156
156
|
trusting a snapshot it was handed. Pass `deep: true` for the source replay:
|
|
157
157
|
|
|
158
158
|
```json
|
|
@@ -214,3 +214,9 @@ context for the code that is being asked about.
|
|
|
214
214
|
- [Configuration](configuration.md) — every setting and its default
|
|
215
215
|
- [Snapshot output](output.md) — what each artifact contains
|
|
216
216
|
- [Comparing with other tools](vs-repomix.md)
|
|
217
|
+
|
|
218
|
+
## Related
|
|
219
|
+
|
|
220
|
+
- [LLM codebase context](llm-codebase-context.md) — what context is and why a raw dump fails.
|
|
221
|
+
- [MCP server for a codebase](mcp-server-for-codebase.md) — the live alternative when the client supports MCP.
|
|
222
|
+
- [All documentation](contents.md) — every guide and reference.
|
package/docs/impact.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Impact reports
|
|
2
|
+
|
|
3
|
+
`codebase-snapshot impact` answers one question before a change is opened for review: **what does
|
|
4
|
+
this change reach, and which tests are candidates?** It is not a code graph for browsing. It is a
|
|
5
|
+
bounded, evidence-carrying report built from the same import resolver the generator uses, with a
|
|
6
|
+
`trust` envelope that says how fresh and complete the answer is.
|
|
7
|
+
|
|
8
|
+
## Run it
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
codebase-snapshot impact --diff HEAD
|
|
12
|
+
codebase-snapshot impact --paths src/impact.ts,src/cli.ts --format markdown
|
|
13
|
+
codebase-snapshot impact --diff origin/main --tests-paths
|
|
14
|
+
codebase-snapshot impact --diff HEAD --run
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
With neither `--diff` nor `--paths`, the base is `HEAD`. The change set includes committed,
|
|
18
|
+
staged, unstaged and untracked changes relative to the ref. A ref beginning with `-` is refused;
|
|
19
|
+
`shell: false` is used for every git invocation.
|
|
20
|
+
|
|
21
|
+
The report works without a generated snapshot. A current `manifest.json` is reused, changed and
|
|
22
|
+
new files are overlaid in memory, and a repository with no snapshot is scanned in memory. Nothing
|
|
23
|
+
is written into the repository. `--no-live` requires the persisted snapshot instead.
|
|
24
|
+
|
|
25
|
+
## What is in the report
|
|
26
|
+
|
|
27
|
+
| Field | Meaning |
|
|
28
|
+
| :-- | :-- |
|
|
29
|
+
| `changed[]` | Path, whether it is in the snapshot, whether it is deleted, and why it is absent when it is. |
|
|
30
|
+
| `affected[]` | One entry per impacted file: `distance`, `via`, the exact `specifier`, `confidence` (how the edge resolved), `impactKind` and `matchedSymbols`. |
|
|
31
|
+
| `tests[]` | Candidate tests, with the files they cover. These are import relationships, never runtime coverage. |
|
|
32
|
+
| `unresolved[]` | Repository-internal imports that did not resolve. Bare packages and Node built-ins are outside the repository and are not listed. |
|
|
33
|
+
| `routesAffected[]` | Route files in the changed or affected set, with the methods their summaries recorded. |
|
|
34
|
+
| `prunedByBarrel` | Barrel edges rejected because they do not consume the changed symbol. |
|
|
35
|
+
| `cycles` | Import cycles in the impacted subgraph, with `--include-cycles`. |
|
|
36
|
+
| `imports` | Outgoing dependencies of changed files, with `--include-imports`. |
|
|
37
|
+
| `trust` | Index source, freshness, coverage, redaction and omission counts, and named partial reasons. |
|
|
38
|
+
| `truncated` / `limits` | Whether `--max-results` cut the list, and the bounds that applied. |
|
|
39
|
+
|
|
40
|
+
Test files are excluded from `affected[]` and listed in `tests[]`. A file that changed from full to
|
|
41
|
+
preview is still a file: the node is present, the body may not be.
|
|
42
|
+
|
|
43
|
+
## How an edge is decided
|
|
44
|
+
|
|
45
|
+
1. The changed file is resolved against the manifest. A deleted path, an unknown path, a non-TypeScript
|
|
46
|
+
file and a paths-only run seed a wildcard. A `--diff` run maps the new-side hunks to the exported
|
|
47
|
+
declarations whose line spans they overlap; an edit outside every declaration widens to a wildcard.
|
|
48
|
+
2. Reverse importers are walked. The walk reads the importing file's `importBindings` and
|
|
49
|
+
`reExportBindings`, not just the specifier:
|
|
50
|
+
- `import { foo as f }` matches the original `foo`;
|
|
51
|
+
- `export * from "./a"` propagates the symbol set;
|
|
52
|
+
- `export { foo as publicFoo }` maps `foo` to `publicFoo`;
|
|
53
|
+
- `export * as ns` propagates `ns`;
|
|
54
|
+
- CommonJS destructured `require()` and dynamic `import()` are file-level edges;
|
|
55
|
+
- a pure barrel is traversed but not reported as an affected consumer.
|
|
56
|
+
3. If a barrel edge matches nothing in the incoming symbol set, it is pruned and counted. A consumer
|
|
57
|
+
of a *different* export is not reported.
|
|
58
|
+
4. Direct or transitive importers are reported with the exact hop that reached them. Tests are collected
|
|
59
|
+
when their import consumes the impacted symbol set.
|
|
60
|
+
|
|
61
|
+
The compiler pass behind `--precise` is bounded to 2,500 files and four seconds. It follows
|
|
62
|
+
`getAliasedSymbol` through aliases and returns only path/line/name references, never source text. Above
|
|
63
|
+
the cap or past the deadline the report stays partial and says why; it never silently falls back.
|
|
64
|
+
|
|
65
|
+
## Tests
|
|
66
|
+
|
|
67
|
+
`tests[]` is a **candidate list**. A candidate test imports a changed or affected file. It may not
|
|
68
|
+
exercise the changed behaviour, and no coverage claim is made. `--tests-paths` prints one path per
|
|
69
|
+
line for a CI step. `--run` invokes the repository's own runner:
|
|
70
|
+
|
|
71
|
+
- Vitest: `node node_modules/vitest/vitest.mjs related <changed...> --run`
|
|
72
|
+
- Jest: `node node_modules/jest/bin/jest.js --findRelatedTests <changed...> --runInBand`
|
|
73
|
+
- node:test: `node --test <candidate tests>`
|
|
74
|
+
|
|
75
|
+
No package manager, shell or network is used. A change to a build, test or workspace configuration
|
|
76
|
+
adds a warning that the selective run may be incomplete; run the full suite too.
|
|
77
|
+
|
|
78
|
+
## Output
|
|
79
|
+
|
|
80
|
+
`--format json` is the stable contract and writes one JSON document to stdout. Text goes to stderr,
|
|
81
|
+
as every human-facing command does. `--format markdown` writes a PR-ready report to stdout;
|
|
82
|
+
`--format pr-comment` is accepted by `brief`, which is the page-oriented command.
|
|
83
|
+
|
|
84
|
+
Exit `0` when the report was produced, `1` when the live index cannot be built or an explicit
|
|
85
|
+
`--no-live` manifest is missing, and `2` for a usage error.
|
|
86
|
+
|
|
87
|
+
## What it does not claim
|
|
88
|
+
|
|
89
|
+
- It is not runtime coverage, a call graph, or a security review.
|
|
90
|
+
- It does not resolve bare package specifiers to `node_modules`, or Node built-ins.
|
|
91
|
+
- It does not see a dependency created through a dynamic string the parser cannot evaluate; such a
|
|
92
|
+
path is absent rather than guessed.
|
|
93
|
+
- A manifest generated from a dirty tree may describe bytes that have already moved. The `trust`
|
|
94
|
+
envelope names that condition; `validate` is the separate proof.
|
|
95
|
+
|
|
96
|
+
→ [CLI reference](cli.md) · [Impact in MCP](tools.md) · [Brief](brief.md)
|
|
97
|
+
|
|
98
|
+
## Related
|
|
99
|
+
|
|
100
|
+
- [Briefs](brief.md) — turn an impact report into a review pack.
|
|
101
|
+
- [LLM codebase context](llm-codebase-context.md) — why symbol-pruned context beats a repository dump.
|
|
102
|
+
- [All documentation](contents.md) — every guide and reference.
|
package/docs/install.md
CHANGED
|
@@ -115,9 +115,10 @@ it exists — run:
|
|
|
115
115
|
npx codebase-snapshot skill --install
|
|
116
116
|
```
|
|
117
117
|
|
|
118
|
-
That writes `.
|
|
119
|
-
`.cursor/rules/codebase-snapshot.mdc`, and adds
|
|
120
|
-
|
|
118
|
+
That writes `.agents/skills/codebase-snapshot/SKILL.md`,
|
|
119
|
+
`.claude/skills/codebase-snapshot/SKILL.md` and `.cursor/rules/codebase-snapshot.mdc`, and adds
|
|
120
|
+
all three to `.gitignore` so the install leaves nothing untracked. Add `--with-mcp` to merge a
|
|
121
|
+
server entry into `.mcp.json`. It does not touch `AGENTS.md`: that file is your project's own agent
|
|
121
122
|
contract. To put the skill there, print it and paste it —
|
|
122
123
|
`npx codebase-snapshot skill >> AGENTS.md`. See
|
|
123
124
|
[Using this with an AI agent](agent.md).
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# LLM codebase context, explained
|
|
2
|
+
|
|
3
|
+
**LLM codebase context is the part of a repository a model is allowed to see when it answers a
|
|
4
|
+
question about that repository.** It has to be selected, bounded, readable and current. Copying
|
|
5
|
+
every file into a prompt is the simplest version, and usually the worst one: it spends the context
|
|
6
|
+
window on files the question never needed and gives the model no way to tell whether the copy is
|
|
7
|
+
stale.
|
|
8
|
+
|
|
9
|
+
This guide covers what "codebase context" means, why raw dumps fail, the four practical approaches,
|
|
10
|
+
and a repeatable way to build context with codebase-snapshot.
|
|
11
|
+
|
|
12
|
+
## What is LLM codebase context?
|
|
13
|
+
|
|
14
|
+
A model has no ambient access to your repository. Everything it knows about the code arrives in the
|
|
15
|
+
request: the system prompt, the conversation, and whatever files or retrieval results the client
|
|
16
|
+
attaches. That attached material is the codebase context.
|
|
17
|
+
|
|
18
|
+
Good codebase context has four properties:
|
|
19
|
+
|
|
20
|
+
- **Relevant.** It contains the files and symbols that can answer the question.
|
|
21
|
+
- **Bounded.** It fits the window with room for instructions and a reply.
|
|
22
|
+
- **Attributable.** The model can name the file and line a claim came from.
|
|
23
|
+
- **Current.** It is possible to tell whether the context still matches the working tree.
|
|
24
|
+
|
|
25
|
+
A codebase snapshot is one way to produce all four at once. It is not the only way, and it is not
|
|
26
|
+
always the right way. A small bug in one file is better handled by opening the file than by packing
|
|
27
|
+
the repository.
|
|
28
|
+
|
|
29
|
+
## Why a raw codebase dump fails
|
|
30
|
+
|
|
31
|
+
The arithmetic is unforgiving. A token is roughly 3.5 bytes of source code, so a 1 MB repository is
|
|
32
|
+
about 285,000 tokens before any instructions. A 128k window cannot hold it; a 200k window can hold
|
|
33
|
+
the code but not much of a conversation. Real applications are five to twenty times larger than a
|
|
34
|
+
small library.
|
|
35
|
+
|
|
36
|
+
The failure is not only size. A dump is usually:
|
|
37
|
+
|
|
38
|
+
1. **Unfiltered.** Vendored dependencies, generated files, lockfiles and test fixtures ride along
|
|
39
|
+
because the packer copies what it finds.
|
|
40
|
+
2. **Undifferentiated.** The model sees every file with the same weight. The one module the
|
|
41
|
+
question depends on is buried among hundreds that are irrelevant.
|
|
42
|
+
3. **Unaccounted.** A packer that silently skips or truncates a file leaves the model unaware that
|
|
43
|
+
its picture has a hole. The answer sounds complete and is not.
|
|
44
|
+
4. **Undated.** The model cannot know whether the snapshot was generated five minutes or five
|
|
45
|
+
months ago. A stale snapshot is worse than no snapshot because it reads as current.
|
|
46
|
+
|
|
47
|
+
The tooling answer is not "write a better prompt". It is to produce a smaller, indexed artefact and
|
|
48
|
+
to make freshness checkable.
|
|
49
|
+
|
|
50
|
+
## Four ways to give a model codebase context
|
|
51
|
+
|
|
52
|
+
| Approach | What it is good at | Where it breaks |
|
|
53
|
+
| :-- | :-- | :-- |
|
|
54
|
+
| Paste files by hand | One file or one symbol; complete control | Does not scale past a handful of files; easy to miss a caller |
|
|
55
|
+
| Pack the repository | A stable artefact attached to one conversation; offline and inspectable | Size; stale copies; no principled choice of what matters |
|
|
56
|
+
| Bounded retrieval over a local index (MCP) | A question answered from the few files that matter; live edits | Needs a client that speaks MCP; the index has to be built |
|
|
57
|
+
| Code graph or embedding index | Cross-repo and semantic questions at scale | Infrastructure, embeddings and a server; overkill for one repository |
|
|
58
|
+
|
|
59
|
+
Most teams end up combining the second and third: a packed snapshot for a first, broad review, and
|
|
60
|
+
a live MCP server for follow-up questions. The rest of this page shows the packed route, because it
|
|
61
|
+
is the one that works in a fresh clone and in any chat client that accepts attachments.
|
|
62
|
+
|
|
63
|
+
## Build codebase context with codebase-snapshot
|
|
64
|
+
|
|
65
|
+
### 1. Generate the snapshot
|
|
66
|
+
|
|
67
|
+
npx codebase-snapshot generate
|
|
68
|
+
|
|
69
|
+
The default writes ./snapshot with Markdown parts, a manifest, a prompt and reports. The run is
|
|
70
|
+
deterministic: unchanged input produces byte-identical parts and the same fingerprint. The parts
|
|
71
|
+
never exceed the configured byte budget.
|
|
72
|
+
|
|
73
|
+
### 2. Read the prompt before the parts
|
|
74
|
+
|
|
75
|
+
PROMPT.md states the fingerprint, the commit, the parts, what was left out and how to attach the
|
|
76
|
+
files. Paste it as the first message. It is the instruction sheet that turns a pile of Markdown into
|
|
77
|
+
context the model can use.
|
|
78
|
+
|
|
79
|
+
### 3. Attach only what fits
|
|
80
|
+
|
|
81
|
+
A hosted chat often accepts ten files. A 12-part snapshot cannot be delivered whole. Either raise
|
|
82
|
+
--max-part-bytes so the same content becomes fewer parts, or scope the run with include/exclude
|
|
83
|
+
patterns to the directories the question is about. The prompt's delivery section does the
|
|
84
|
+
arithmetic for the current run.
|
|
85
|
+
|
|
86
|
+
### 4. Ask a bounded question
|
|
87
|
+
|
|
88
|
+
"Using the attached snapshot, trace how authentication reaches the database. Cite the file and line
|
|
89
|
+
for each hop; if the answer is not in the attached files, say what is missing." The second clause is
|
|
90
|
+
what stops a model from filling a gap with a plausible invention.
|
|
91
|
+
|
|
92
|
+
### 5. Validate before reusing the context
|
|
93
|
+
|
|
94
|
+
npx codebase-snapshot validate
|
|
95
|
+
|
|
96
|
+
validate re-hashes the tree and exits non-zero on drift. Put it in CI or a pre-commit hook. When it
|
|
97
|
+
fails, regenerate; do not keep asking questions against a snapshot that no longer describes the
|
|
98
|
+
code.
|
|
99
|
+
|
|
100
|
+
For a live alternative, run the MCP server and let the client call the tools:
|
|
101
|
+
|
|
102
|
+
npx codebase-snapshot mcp
|
|
103
|
+
|
|
104
|
+
The read tools use the working tree by default, so an uncommitted edit is visible without a
|
|
105
|
+
regeneration. See [MCP server for a codebase](mcp-server-for-codebase.md).
|
|
106
|
+
|
|
107
|
+
## How much codebase context fits?
|
|
108
|
+
|
|
109
|
+
Use the destination, not the tool's default. The snapshot budget should leave room for the system
|
|
110
|
+
prompt, the question and the reply.
|
|
111
|
+
|
|
112
|
+
| Window | Practical context ceiling | What that means |
|
|
113
|
+
| :-- | --: | :-- |
|
|
114
|
+
| 32k | ~20k tokens | One small module or a slice, not a repository |
|
|
115
|
+
| 128k | ~80k tokens | One 300 KB part, or a scoped folder |
|
|
116
|
+
| 200k | ~130k tokens | Two or three parts, or a larger slice |
|
|
117
|
+
| 1M | ~700k tokens | A medium repository whole, with room for analysis |
|
|
118
|
+
|
|
119
|
+
The estimates are conservative on purpose. A 128k window filled to 120k leaves no space for a
|
|
120
|
+
useful answer, and models often perform worse when the relevant material is diluted.
|
|
121
|
+
|
|
122
|
+
## Keep context current
|
|
123
|
+
|
|
124
|
+
A snapshot is a moment, not a subscription. The repository moves; the file on disk does not. The
|
|
125
|
+
controls that make freshness visible are:
|
|
126
|
+
|
|
127
|
+
- **Fingerprint.** The manifest records a content fingerprint; two runs over the same input agree.
|
|
128
|
+
- **Validate.** The command compares the snapshot with the tree and exits non-zero on drift.
|
|
129
|
+
- **Git state.** The manifest records the branch, commit and dirty state at generation time.
|
|
130
|
+
- **Live index.** The MCP read tools overlay uncommitted edits and new files, and say so in a trust
|
|
131
|
+
envelope.
|
|
132
|
+
|
|
133
|
+
Read [Stale context is your bug](stale-context-is-your-bug.md) for the CI pattern.
|
|
134
|
+
|
|
135
|
+
## What leaves the repository?
|
|
136
|
+
|
|
137
|
+
The CLI and the MCP server run locally. There is no account, upload or telemetry. Generated
|
|
138
|
+
artifacts contain redacted source, not your credentials: sensitive paths are excluded by default,
|
|
139
|
+
and secret-shaped values in ordinary files are replaced with a marker. The audit report names the
|
|
140
|
+
file and line but not the value.
|
|
141
|
+
|
|
142
|
+
Redaction is not a guarantee. If you need a formal data-handling statement, read the
|
|
143
|
+
[security review](security-review.md), which lists the residual risks instead of hiding them.
|
|
144
|
+
|
|
145
|
+
## How to choose a codebase context tool
|
|
146
|
+
|
|
147
|
+
Ask five questions:
|
|
148
|
+
|
|
149
|
+
1. **Does it fit the destination?** A tool that produces one 40 MB file has not solved the
|
|
150
|
+
attachment problem.
|
|
151
|
+
2. **Does it account for omissions?** Every skipped or shortened file should have a reason.
|
|
152
|
+
3. **Can it prove freshness?** A hash or a validate command, not a promise.
|
|
153
|
+
4. **Does it handle secrets?** Default exclusions, span redaction and an audit.
|
|
154
|
+
5. **Can an agent use it without a human?** A machine-readable contract and bounded tools, or at
|
|
155
|
+
least static pages it can fetch.
|
|
156
|
+
|
|
157
|
+
Those five questions are also the design brief for codebase-snapshot. The comparisons with
|
|
158
|
+
[repomix](vs-repomix.md), [gitingest](vs-gitingest.md) and [code2prompt](vs-code2prompt.md) apply the
|
|
159
|
+
same questions honestly.
|
|
160
|
+
|
|
161
|
+
## Related
|
|
162
|
+
|
|
163
|
+
- [How to feed a repository to Claude or ChatGPT](how-to-feed-a-repo-to-claude.md)
|
|
164
|
+
- [How many tokens is a codebase?](codebase-token-budget.md)
|
|
165
|
+
- [Budgeting LLM context](how-to-context-budget.md)
|
|
166
|
+
- [All documentation](contents.md)
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# MCP server for a codebase
|
|
2
|
+
|
|
3
|
+
**An MCP server for a codebase lets an AI client ask a local process for the few files, symbols or
|
|
4
|
+
impact facts a question needs, instead of attaching the whole repository.** codebase-snapshot ships
|
|
5
|
+
one: seven tools (six read-only) over stdio or Streamable HTTP, a live index that sees uncommitted
|
|
6
|
+
edits, and a persistent snapshot when you need a shared artefact.
|
|
7
|
+
|
|
8
|
+
This guide covers what the server exposes, how to wire it into Claude, Cursor, Claude Code or any
|
|
9
|
+
generic MCP client, what it can read, and where its limits are.
|
|
10
|
+
|
|
11
|
+
## Why a local MCP server instead of a packed file
|
|
12
|
+
|
|
13
|
+
A packed file is a good first move: it is inspectable, portable and works in a chat that only
|
|
14
|
+
accepts attachments. It has two costs. Every follow-up question either re-attaches the same file or
|
|
15
|
+
starts a new conversation, and the copy is frozen at the moment it was written.
|
|
16
|
+
|
|
17
|
+
A local MCP server changes the interaction:
|
|
18
|
+
|
|
19
|
+
- The client asks for **one** file, symbol or impact report instead of receiving all of them.
|
|
20
|
+
- The index is **live by default**: changed files are overlaid in memory, new files are indexed, and
|
|
21
|
+
a repository with no snapshot is scanned without writing one.
|
|
22
|
+
- Responses are **bounded**: a read is capped at 64 KiB, a resource at 512 KiB, a tool result at 64
|
|
23
|
+
KiB, and a text search at 4 MiB.
|
|
24
|
+
- Every structured response carries a **trust envelope** naming the source, freshness, coverage,
|
|
25
|
+
redaction count and any reason the answer is partial.
|
|
26
|
+
|
|
27
|
+
The trade-off is that the client must speak MCP. If it cannot, generate a snapshot and attach it.
|
|
28
|
+
|
|
29
|
+
## Start the server
|
|
30
|
+
|
|
31
|
+
npx codebase-snapshot mcp
|
|
32
|
+
|
|
33
|
+
The server serves the directory it was started in. --cwd points it elsewhere; that directory is the
|
|
34
|
+
only one it reads. Add --http to listen on loopback instead of stdio.
|
|
35
|
+
|
|
36
|
+
A generic client needs a command and arguments. The surrounding shape belongs to the client; this
|
|
37
|
+
is the mcpServers form used by Claude Desktop and Cursor:
|
|
38
|
+
|
|
39
|
+
{
|
|
40
|
+
"mcpServers": {
|
|
41
|
+
"codebase-snapshot": {
|
|
42
|
+
"command": "npx",
|
|
43
|
+
"args": ["-y", "codebase-snapshot", "mcp", "--cwd", "/absolute/path/to/repo"]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Claude Code registers the same server from a shell:
|
|
49
|
+
|
|
50
|
+
claude mcp add codebase-snapshot -- npx -y codebase-snapshot mcp --cwd /absolute/path/to/repo
|
|
51
|
+
|
|
52
|
+
On Windows, wrap the command as cmd /c npx with the same arguments. npx is a .cmd shim there, and a
|
|
53
|
+
client that spawns without a shell cannot execute it directly.
|
|
54
|
+
|
|
55
|
+
## The seven tools
|
|
56
|
+
|
|
57
|
+
| Tool | Cost | Answers |
|
|
58
|
+
| :-- | :-- | :-- |
|
|
59
|
+
| snapshot_impact | medium | What a change reaches: changed files, symbol-pruned importers, routes, candidate tests, cycles |
|
|
60
|
+
| snapshot_context | medium | The minimum redacted, line-numbered context for a question or file list inside a token budget |
|
|
61
|
+
| snapshot_locate | cheap | Paths, symbols, routes, entrypoints or bounded redacted text in one call |
|
|
62
|
+
| snapshot_read | medium | A line window of one redacted file, never a sensitive path |
|
|
63
|
+
| snapshot_validate | cheap | Status, drift, or an integrity and secret audit of the persisted snapshot |
|
|
64
|
+
| snapshot_explain | expensive | Why a path is included, excluded, sensitive, ignored, binary or truncated |
|
|
65
|
+
| snapshot_generate | expensive | Write a snapshot, or preview one with dryRun: true. The only writable tool |
|
|
66
|
+
|
|
67
|
+
Six tools are declared readOnlyHint, which most clients use to run them without asking. The full
|
|
68
|
+
schemas, cost classes and example calls are in the [MCP tool reference](tools.md).
|
|
69
|
+
|
|
70
|
+
## What a call looks like
|
|
71
|
+
|
|
72
|
+
A question about a change, with candidate tests:
|
|
73
|
+
|
|
74
|
+
{
|
|
75
|
+
"name": "snapshot_impact",
|
|
76
|
+
"arguments": {
|
|
77
|
+
"paths": ["src/impact.ts"],
|
|
78
|
+
"includeCycles": true,
|
|
79
|
+
"includeSnippets": true
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
A bounded reading list before a task:
|
|
84
|
+
|
|
85
|
+
{
|
|
86
|
+
"name": "snapshot_context",
|
|
87
|
+
"arguments": {
|
|
88
|
+
"question": "how does impact pruning work",
|
|
89
|
+
"maxTokens": 12000,
|
|
90
|
+
"linesPerFile": 100
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
A read of one file:
|
|
95
|
+
|
|
96
|
+
{
|
|
97
|
+
"name": "snapshot_read",
|
|
98
|
+
"arguments": { "path": "src/impact.ts", "fromLine": 1, "lines": 120 }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
## Live index versus persisted snapshot
|
|
102
|
+
|
|
103
|
+
Read-only tools are live by default. They reuse a current manifest.json, overlay changed and new
|
|
104
|
+
files in memory when the tree has moved, or scan the confined working tree when no snapshot exists.
|
|
105
|
+
They never write into the repository. Pass "live": false to require the persisted snapshot; the
|
|
106
|
+
trust envelope then says freshness is unknown rather than claiming the file is current.
|
|
107
|
+
|
|
108
|
+
This distinction matters for agents. A stale persisted snapshot is still useful if the client knows
|
|
109
|
+
it is stale; it is dangerous if the envelope says current. The server only claims current when it
|
|
110
|
+
has compared the tree, which is what snapshot_validate drift mode is for.
|
|
111
|
+
|
|
112
|
+
## Large repositories and monorepos
|
|
113
|
+
|
|
114
|
+
The server bounds its own work. A text search stops at 4 MiB; a TypeScript compiler pass stops at
|
|
115
|
+
2,500 files and four seconds; a resource above 512 KiB is refused rather than truncated. When a cap
|
|
116
|
+
is hit, the response says so in trust.reasons or in the precise coverage field.
|
|
117
|
+
|
|
118
|
+
For a monorepo, point --cwd at the member that contains the code you are asking about, or generate a
|
|
119
|
+
workspace snapshot and pass its outDir. A workspace run writes one snapshot per member plus a
|
|
120
|
+
WORKSPACE.json index. The MCP server does not silently merge members: a tool call names the
|
|
121
|
+
repository it serves.
|
|
122
|
+
|
|
123
|
+
## Security boundary
|
|
124
|
+
|
|
125
|
+
- The server reads only the served root. --root can add directories explicitly; a path outside them
|
|
126
|
+
is refused.
|
|
127
|
+
- Sensitive paths are never readable through snapshot_read, even when they exist on disk.
|
|
128
|
+
- A snapshot generated with --include-sensitive-files is refused by the MCP transport and by serve.
|
|
129
|
+
- HTTP binds loopback by default, requires a bearer token for /api and generates one on first run.
|
|
130
|
+
A non-loopback bind without a token is refused.
|
|
131
|
+
- Remote ingestion is HTTPS-only, refuses private addresses and does not follow redirects.
|
|
132
|
+
- Secrets are redacted in emitted parts and checked again by the audit.
|
|
133
|
+
|
|
134
|
+
The full threat model, including the residual risks (executable configs, DNS rebinding, prompt
|
|
135
|
+
injection inside repository content), is in the [security review](security-review.md).
|
|
136
|
+
|
|
137
|
+
## Limits and when not to use it
|
|
138
|
+
|
|
139
|
+
- **The client must speak MCP.** A chat that only accepts attachments cannot use the server.
|
|
140
|
+
- **Heuristic languages.** TypeScript and JavaScript get the deepest symbol support; the other
|
|
141
|
+
language profiles classify and index but do not promise compiler-grade references.
|
|
142
|
+
- **No write tools beyond generate.** The server cannot edit, refactor or run tests; it is a
|
|
143
|
+
read-and-generate surface.
|
|
144
|
+
- **Not a code graph.** It follows imports and declaration spans, not a semantic graph of every
|
|
145
|
+
relationship. The impact report says where it is conservative.
|
|
146
|
+
|
|
147
|
+
If you need a single portable artefact, use generate. If you need a live question-answer loop and
|
|
148
|
+
your client supports MCP, use the server. Most teams use both.
|
|
149
|
+
|
|
150
|
+
## Related
|
|
151
|
+
|
|
152
|
+
- [MCP tools reference](tools.md) — every schema and example call
|
|
153
|
+
- [MCP protocol details](mcp.md) — transports, sessions, headers and limits
|
|
154
|
+
- [LLM codebase context](llm-codebase-context.md) — the broader guide
|
|
155
|
+
- [All documentation](contents.md)
|