codebase-brain-mcp 0.1.0__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.
- codebase_brain_mcp-0.1.0/LICENSE +21 -0
- codebase_brain_mcp-0.1.0/PKG-INFO +293 -0
- codebase_brain_mcp-0.1.0/README.md +267 -0
- codebase_brain_mcp-0.1.0/pyproject.toml +98 -0
- codebase_brain_mcp-0.1.0/pyproject.toml.orig +70 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/__init__.py +8 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/__main__.py +118 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/analysis.py +427 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/brain.py +395 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/chunking.py +196 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/config.py +58 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/embeddings.py +79 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/evals.py +372 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/history.py +214 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/py.typed +0 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/remote.py +112 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/server.py +415 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/sources.py +186 -0
- codebase_brain_mcp-0.1.0/src/codebase_brain_mcp/store.py +349 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Noman Nazir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: codebase-brain-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An MCP server that gives AI agents a cited, searchable memory of a codebase.
|
|
5
|
+
Keywords: mcp,model-context-protocol,rag,code-search,ai-agents,claude,git
|
|
6
|
+
Author: Noman Nazir
|
|
7
|
+
Author-email: Noman Nazir <nomannazir69@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Topic :: Software Development
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Requires-Dist: fastembed>=0.9.1
|
|
19
|
+
Requires-Dist: mcp>=2.3.0
|
|
20
|
+
Requires-Dist: numpy>=2.5.4
|
|
21
|
+
Requires-Python: >=3.12
|
|
22
|
+
Project-URL: Homepage, https://github.com/Nomannazir/codebase-brain-mcp
|
|
23
|
+
Project-URL: Repository, https://github.com/Nomannazir/codebase-brain-mcp
|
|
24
|
+
Project-URL: Issues, https://github.com/Nomannazir/codebase-brain-mcp/issues
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Codebase Brain MCP
|
|
28
|
+
|
|
29
|
+
<!-- mcp-name: io.github.Nomannazir/codebase-brain-mcp -->
|
|
30
|
+
|
|
31
|
+
[](https://github.com/Nomannazir/codebase-brain-mcp/actions/workflows/ci.yml)
|
|
32
|
+
[](https://github.com/Nomannazir/codebase-brain-mcp/blob/main/LICENSE)
|
|
33
|
+

|
|
34
|
+
|
|
35
|
+
An [MCP](https://modelcontextprotocol.io) server that gives AI assistants a **cited, searchable memory of a
|
|
36
|
+
codebase**, including its history. Point it at a local folder or a public git URL. Claude, Cursor or any MCP client
|
|
37
|
+
can then answer questions about the code with exact `path:start-end` citations.
|
|
38
|
+
|
|
39
|
+

|
|
40
|
+
|
|
41
|
+
*Claude Code asks why `TimestampSigner.unsign` looks the way it does, what breaks if it changes, and whether its
|
|
42
|
+
docs are current. Codebase Brain answers with commits, callers, tests and doc findings, all cited. (Waiting time
|
|
43
|
+
sped up.)*
|
|
44
|
+
|
|
45
|
+
## The problem
|
|
46
|
+
|
|
47
|
+
AI coding assistants are good at writing new code but struggle to understand code that already exists:
|
|
48
|
+
|
|
49
|
+
- **They can't see the whole project.** A real codebase has thousands of files. The assistant reads a few and
|
|
50
|
+
guesses the rest, so it misses the function that matters or invents one that doesn't exist.
|
|
51
|
+
- **You can't check their answers.** "Expiry is handled in the auth module" is no use if you can't see where. Without
|
|
52
|
+
file and line references, you have to trust the answer or re-read the code yourself.
|
|
53
|
+
- **They don't know why code is the way it is.** The code shows *what* it does. The reasons ("we added this check
|
|
54
|
+
because it crashed on Windows") live in git history and pull requests, which assistants never read. So they
|
|
55
|
+
"clean up" code that exists for a reason and bring old bugs back.
|
|
56
|
+
- **They don't know what a change will break,** or whether the documentation still matches the code.
|
|
57
|
+
- **Many tools send your code to someone else's server** to make it searchable.
|
|
58
|
+
|
|
59
|
+
## What Codebase Brain gives you
|
|
60
|
+
|
|
61
|
+
A plug-in for any AI assistant that turns a codebase into something it can actually look things up in:
|
|
62
|
+
|
|
63
|
+
- **Answers with proof.** Every result points to the exact file and lines, and the assistant can open them.
|
|
64
|
+
- **Search that understands code.** It finds code whether you type the exact name (`calculate_invoice_total`) or
|
|
65
|
+
describe it in plain words ("how are invoices totalled?").
|
|
66
|
+
- **The story behind the code.** For any function, it shows every change made to it, who made it, why (from the
|
|
67
|
+
commit message and pull request), and what the change was.
|
|
68
|
+
- **What breaks if you change something.** Everything that uses a function, directly or indirectly, and the tests
|
|
69
|
+
that cover it.
|
|
70
|
+
- **Out-of-date docs, found automatically.** Documentation that mentions functions, files or settings that no longer
|
|
71
|
+
exist, or shows examples that would no longer work.
|
|
72
|
+
- **Private by design.** Everything runs on your machine. No API keys, and your code never leaves your computer.
|
|
73
|
+
|
|
74
|
+
## Why it's different (technical)
|
|
75
|
+
|
|
76
|
+
- **Code-aware chunking.** Python is split by function, class and method using the real syntax tree, Markdown by
|
|
77
|
+
heading. Each chunk knows its symbol (`TimestampSigner.unsign`) and exact lines, so citations point at real code.
|
|
78
|
+
- **Hybrid retrieval.** SQLite FTS5 BM25 (with `camelCase` / `snake_case` identifier splitting) plus local
|
|
79
|
+
embeddings, merged with reciprocal-rank fusion. Exact names and fuzzy questions both work.
|
|
80
|
+
- **Local and free.** Embeddings run on your machine (`fastembed`, ONNX). No API keys, and no code leaves your
|
|
81
|
+
computer.
|
|
82
|
+
- **Incremental.** Re-indexing only re-embeds files whose content changed. On a 42-file repo, a re-index with no
|
|
83
|
+
changes takes about 2 seconds.
|
|
84
|
+
- **History-aware.** `explain_history` follows a function's lines back through git (`git log -L`), shows who wrote
|
|
85
|
+
the current lines, and links the pull requests behind each change. So agents can answer *"why is it built this
|
|
86
|
+
way?"*, not just *"what does it do?"*.
|
|
87
|
+
- **Impact analysis.** `impact_of_change` lists everything that uses a function, directly and transitively, plus the
|
|
88
|
+
tests that exercise it, before an agent changes anything.
|
|
89
|
+
- **Doc-drift detection.** `find_doc_drift` checks Markdown and reStructuredText docs against the code. It finds
|
|
90
|
+
files, functions, settings and flags that don't exist; examples passing parameters a function doesn't take; imports
|
|
91
|
+
that would fail; and sections last edited long before the code they describe changed. Each finding cites the doc
|
|
92
|
+
section and suggests a fix ("Was it renamed to `checkout_total`?").
|
|
93
|
+
- **Agent-friendly output.** Structured tool results, read-only tool annotations, progress notifications while
|
|
94
|
+
indexing, and clear error messages the model can act on.
|
|
95
|
+
|
|
96
|
+
## Tools
|
|
97
|
+
|
|
98
|
+
| Tool | What it does |
|
|
99
|
+
|---|---|
|
|
100
|
+
| `index_repo(source, name?)` | Index a local path or an `https://` git URL (shallow clone). Reports progress. |
|
|
101
|
+
| `ask_codebase(question, repo?, k?)` | The most relevant code and docs as cited sources, plus related commits, with instructions to answer only from them. |
|
|
102
|
+
| `search(query, repo?, k?, path_prefix?, kinds?)` | Raw hybrid search, filterable by path and chunk kind. |
|
|
103
|
+
| `explain_history(symbol? \| path, start_line?, end_line?, repo?)` | Every commit that changed a function or line range, newest first, with the relevant diff, current authorship, and PR titles and descriptions (GitHub). |
|
|
104
|
+
| `search_history(query, repo?, k?)` | Find commits by what their messages say. |
|
|
105
|
+
| `impact_of_change(symbol, path?, depth?, repo?)` | Direct and indirect users of a function or class, and the tests involved. |
|
|
106
|
+
| `find_doc_drift(path_prefix?, check_history?, repo?)` | Documentation that no longer matches the code, by severity (high, medium, low). |
|
|
107
|
+
| `read_lines(path, start, end, repo?)` | Open the exact lines behind a citation. Refuses paths outside the repo. |
|
|
108
|
+
| `list_repos()` | What's indexed, at which commit, and in which languages. |
|
|
109
|
+
|
|
110
|
+
Also included: the resource `brain://repos`, and the prompt `answer_with_citations`.
|
|
111
|
+
|
|
112
|
+
## Install
|
|
113
|
+
|
|
114
|
+
Codebase Brain runs on your own machine, so there's no server or account, and it's free. With
|
|
115
|
+
[uv](https://docs.astral.sh/uv/):
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# Claude Code
|
|
119
|
+
claude mcp add codebase-brain -- uvx codebase-brain-mcp
|
|
120
|
+
|
|
121
|
+
# Any MCP client (Claude Desktop, Cursor, ...): add this server to its config
|
|
122
|
+
{ "command": "uvx", "args": ["codebase-brain-mcp"] }
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Quick start
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
uv sync
|
|
129
|
+
uv run codebase-brain index https://github.com/pallets/itsdangerous
|
|
130
|
+
uv run codebase-brain search "how is the signature timestamp checked for expiry"
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
0.0313 src/itsdangerous/timed.py:72-158 method TimestampSigner.unsign
|
|
135
|
+
0.0311 docs/timed.rst:1-28 code
|
|
136
|
+
0.0301 src/itsdangerous/timed.py:170-228 class TimedSerializer
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Ask why it looks like that. `explain_history(symbol="TimestampSigner.unsign")` returns its line-by-line history:
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
src/itsdangerous/timed.py:72-158
|
|
143
|
+
3edfbbbc 2022-03-24 handle OverflowError in timed unsign on 32-bit systems
|
|
144
|
+
177196d3 2022-03-09 catch OSError on Windows
|
|
145
|
+
37f09970 2022-03-08 catching year overflow ValueError
|
|
146
|
+
c30678d1 2019-05-31 don't allow timestamps from the future
|
|
147
|
+
910a7a94 2020-04-17 datetimes are timezone-aware utc
|
|
148
|
+
...
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Use it from Claude Code
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
claude mcp add codebase-brain -- uv run --directory /path/to/codebase-brain-mcp codebase-brain
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Run it as a remote server (Streamable HTTP)
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
export CODEBASE_BRAIN_TOKEN="$(python -c 'import secrets; print(secrets.token_urlsafe(32))')"
|
|
161
|
+
uv run codebase-brain serve --transport streamable-http --host 0.0.0.0 --port 8765
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Clients connect to `http://<host>:8765/mcp` with `Authorization: Bearer <token>`. `GET /healthz` is open for load
|
|
165
|
+
balancers. Remote mode is locked down by default:
|
|
166
|
+
|
|
167
|
+
- **No token, no public port.** The server refuses to listen beyond localhost without a token of at least 24
|
|
168
|
+
characters. Tokens are compared in constant time.
|
|
169
|
+
- **Remote clients can't read the server's disk.** Only public `https://` git URLs can be indexed, not local paths,
|
|
170
|
+
unless you set `CODEBASE_BRAIN_ALLOW_LOCAL_PATHS=1`.
|
|
171
|
+
- **Big repos are refused** above `CODEBASE_BRAIN_MAX_FILES` (default 20,000), and every git command has a timeout.
|
|
172
|
+
- **DNS-rebinding protection** is on for localhost, and for any hosts listed in `CODEBASE_BRAIN_ALLOWED_HOSTS`.
|
|
173
|
+
|
|
174
|
+
### Docker
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
docker build -t codebase-brain-mcp .
|
|
178
|
+
docker run -p 8765:8765 -e CODEBASE_BRAIN_TOKEN="$CODEBASE_BRAIN_TOKEN" -v brain-data:/data codebase-brain-mcp
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The image runs as a non-root user and includes the embedding model, so it starts fast and needs no download at
|
|
182
|
+
runtime. It has a health check, and keeps indexes in the `/data` volume. Indexing uses about 1.2 GB of memory, so
|
|
183
|
+
give the container 2 GB.
|
|
184
|
+
|
|
185
|
+
### Configuration
|
|
186
|
+
|
|
187
|
+
| Variable | Default | |
|
|
188
|
+
|---|---|---|
|
|
189
|
+
| `CODEBASE_BRAIN_HOME` | `~/.codebase-brain` | Indexes and clones |
|
|
190
|
+
| `CODEBASE_BRAIN_EMBEDDER` | `fastembed` | `hash` gives an offline, deterministic embedder (used in tests) |
|
|
191
|
+
| `CODEBASE_BRAIN_EMBED_MODEL` | `BAAI/bge-small-en-v1.5` | Any fastembed text model |
|
|
192
|
+
| `CODEBASE_BRAIN_MAX_FILE_BYTES` | `512000` | Larger files are skipped |
|
|
193
|
+
| `GITHUB_TOKEN` | unset | Optional. Raises GitHub's API rate limit for PR lookups |
|
|
194
|
+
| `CODEBASE_BRAIN_FETCH_PRS` | `1` | Set to `0` to never call the GitHub API |
|
|
195
|
+
| `CODEBASE_BRAIN_TOKEN` | unset | Bearer token for HTTP mode (required unless bound to localhost) |
|
|
196
|
+
| `CODEBASE_BRAIN_ALLOWED_HOSTS` | unset | Comma-separated `Host` values to accept (DNS-rebinding protection) |
|
|
197
|
+
| `CODEBASE_BRAIN_ALLOW_LOCAL_PATHS` | `1` (stdio) / `0` (HTTP) | Whether `index_repo` may read local directories |
|
|
198
|
+
| `CODEBASE_BRAIN_MAX_FILES` | `20000` | Refuse repos with more indexable files |
|
|
199
|
+
| `PORT` | `8765` | HTTP port (platforms such as Cloud Run set this) |
|
|
200
|
+
|
|
201
|
+
### Doc drift, tested honestly
|
|
202
|
+
|
|
203
|
+
On a well-maintained project (itsdangerous), `find_doc_drift` reports no false alarms. It only reports three
|
|
204
|
+
"worth a look" sections whose docs date from 2020 while the code they describe changed in 2024–2025. On docs with
|
|
205
|
+
planted mistakes, it catches every one: a moved file, a renamed function (with the right suggestion), an unknown
|
|
206
|
+
setting and flag, a keyword argument the function doesn't take, and an import that would fail. Changelogs are skipped,
|
|
207
|
+
because they're supposed to mention removed things.
|
|
208
|
+
|
|
209
|
+
## Evaluation
|
|
210
|
+
|
|
211
|
+
Does search actually put the right code in front of the model? `codebase-brain eval` answers that with a benchmark.
|
|
212
|
+
The benchmark has 32 questions about [pallets/itsdangerous](https://github.com/pallets/itsdangerous), pinned to
|
|
213
|
+
commit `672971d`, each with the code or docs a good answer must come from. The questions are in
|
|
214
|
+
[`evals/datasets/itsdangerous.toml`](https://github.com/Nomannazir/codebase-brain-mcp/blob/main/evals/datasets/itsdangerous.toml). They were written by reading the code
|
|
215
|
+
before any results were measured, and haven't been changed since.
|
|
216
|
+
|
|
217
|
+
| Mode | Hit@1 | Hit@3 | Hit@5 | Hit@10 | MRR@10 (95% CI) | nDCG@10 | p50 latency |
|
|
218
|
+
|---|---|---|---|---|---|---|---|
|
|
219
|
+
| **hybrid** (default) | 56% | **88%** | **88%** | 91% | **0.697** (0.56–0.82) | **0.735** | 5 ms |
|
|
220
|
+
| keyword only (BM25) | 56% | 75% | 78% | 84% | 0.667 (0.52–0.80) | 0.699 | <1 ms |
|
|
221
|
+
| vector only (bge-small) | 56% | 72% | 75% | 94% | 0.658 (0.51–0.80) | 0.701 | 4 ms |
|
|
222
|
+
|
|
223
|
+
| Hit@5 by question type | describes behaviour (14) | about the docs (7) | names the code (11) |
|
|
224
|
+
|---|---|---|---|
|
|
225
|
+
| hybrid | **71%** | **100%** | 100% |
|
|
226
|
+
| keyword only | 64% | 71% | 100% |
|
|
227
|
+
| vector only | 57% | 71% | 100% |
|
|
228
|
+
|
|
229
|
+
**What this shows:**
|
|
230
|
+
- **Hybrid finds a right answer in the top 3 for 88% of questions,** against 75% and 72% for either method alone.
|
|
231
|
+
The gain comes from questions written in plain words and questions about the docs. When the question names the
|
|
232
|
+
code, every mode finds it.
|
|
233
|
+
- **It's honest about uncertainty.** With 32 questions, the confidence intervals for MRR overlap, so hybrid is better
|
|
234
|
+
*on this set* but not yet proven in general. Per question, hybrid beats keyword-only on 8 questions and loses on 4.
|
|
235
|
+
- **It has known misses.** Four questions describing behaviour in very different words from the code don't reach
|
|
236
|
+
the top 5. They're listed in [`evals/results/itsdangerous.md`](https://github.com/Nomannazir/codebase-brain-mcp/blob/main/evals/results/itsdangerous.md) and are the next
|
|
237
|
+
thing to improve. A larger, held-out question set will come first, so any tuning isn't fitted to these 32.
|
|
238
|
+
|
|
239
|
+
Reproduce it (the first run downloads the repo and the embedding model):
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
uv run codebase-brain eval evals/datasets/itsdangerous.toml --out evals/results
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
How it's measured:
|
|
246
|
+
- **Hit@k:** a relevant result is in the top k.
|
|
247
|
+
- **MRR:** 1 / rank of the first relevant result.
|
|
248
|
+
- **nDCG@10:** each target is credited once, so duplicate chunks don't inflate the score.
|
|
249
|
+
- **Confidence intervals:** 95% bootstrap over questions, seeded.
|
|
250
|
+
- **Relevance:** a result is relevant if it's in the target file and, when the target names a symbol, is that symbol
|
|
251
|
+
or one of its methods.
|
|
252
|
+
- **Dataset rot:** the harness fails if a target no longer exists at the pinned commit.
|
|
253
|
+
|
|
254
|
+
## How it works
|
|
255
|
+
|
|
256
|
+
```
|
|
257
|
+
source ─► resolve (local dir / shallow https clone) ─► list files (.gitignore-aware, skips binaries and lockfiles)
|
|
258
|
+
─► chunk (ast / headings / line windows) ─► embed (fastembed) ─► SQLite: chunks + vectors + FTS5
|
|
259
|
+
query ─► BM25 top 50 ┐
|
|
260
|
+
─► vector top 50 ┴─► reciprocal-rank fusion ─► filters ─► cited sources
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Security
|
|
264
|
+
|
|
265
|
+
- Clones are blobless (`--filter=blob:none`): full history, with file contents fetched only when needed.
|
|
266
|
+
- Commit authors are shown by name only; email addresses are never stored or returned.
|
|
267
|
+
- Only `https://` clone URLs are accepted. Git runs with `GIT_ALLOW_PROTOCOL=https:file` and no terminal prompts.
|
|
268
|
+
- `read_lines` resolves paths and refuses anything outside the indexed repository.
|
|
269
|
+
- Repo names are validated, so they can't escape the index directory.
|
|
270
|
+
- All query tools are marked read-only.
|
|
271
|
+
- In HTTP mode: a required bearer token (constant-time check), git URLs only, repo size limits, DNS-rebinding
|
|
272
|
+
protection, and a non-root container.
|
|
273
|
+
|
|
274
|
+
## Development
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
uv run pytest # 54 tests, offline (hash embedder, temporary git repos, in-process MCP client)
|
|
278
|
+
uv run ruff check . # lint
|
|
279
|
+
uv run ruff format . # format
|
|
280
|
+
uv run pyright # types
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Roadmap
|
|
284
|
+
|
|
285
|
+
- [x] Git history and PR discussions: `explain_history`, `search_history`
|
|
286
|
+
- [x] Symbol references and transitive dependents: `impact_of_change`
|
|
287
|
+
- [x] `find_doc_drift`: Markdown and reStructuredText, signatures, imports, settings, flags, staleness
|
|
288
|
+
- [ ] tree-sitter chunking for more languages
|
|
289
|
+
- [x] Retrieval evals with confidence intervals (`codebase-brain eval`)
|
|
290
|
+
- [ ] Larger held-out eval set, then retrieval tuning (reranking, query expansion)
|
|
291
|
+
- [x] Streamable HTTP with bearer-token auth, Docker image
|
|
292
|
+
- [ ] Hosted demo, PyPI package, MCP Registry listing
|
|
293
|
+
- [ ] OpenTelemetry tracing
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
# Codebase Brain MCP
|
|
2
|
+
|
|
3
|
+
<!-- mcp-name: io.github.Nomannazir/codebase-brain-mcp -->
|
|
4
|
+
|
|
5
|
+
[](https://github.com/Nomannazir/codebase-brain-mcp/actions/workflows/ci.yml)
|
|
6
|
+
[](https://github.com/Nomannazir/codebase-brain-mcp/blob/main/LICENSE)
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
An [MCP](https://modelcontextprotocol.io) server that gives AI assistants a **cited, searchable memory of a
|
|
10
|
+
codebase**, including its history. Point it at a local folder or a public git URL. Claude, Cursor or any MCP client
|
|
11
|
+
can then answer questions about the code with exact `path:start-end` citations.
|
|
12
|
+
|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
*Claude Code asks why `TimestampSigner.unsign` looks the way it does, what breaks if it changes, and whether its
|
|
16
|
+
docs are current. Codebase Brain answers with commits, callers, tests and doc findings, all cited. (Waiting time
|
|
17
|
+
sped up.)*
|
|
18
|
+
|
|
19
|
+
## The problem
|
|
20
|
+
|
|
21
|
+
AI coding assistants are good at writing new code but struggle to understand code that already exists:
|
|
22
|
+
|
|
23
|
+
- **They can't see the whole project.** A real codebase has thousands of files. The assistant reads a few and
|
|
24
|
+
guesses the rest, so it misses the function that matters or invents one that doesn't exist.
|
|
25
|
+
- **You can't check their answers.** "Expiry is handled in the auth module" is no use if you can't see where. Without
|
|
26
|
+
file and line references, you have to trust the answer or re-read the code yourself.
|
|
27
|
+
- **They don't know why code is the way it is.** The code shows *what* it does. The reasons ("we added this check
|
|
28
|
+
because it crashed on Windows") live in git history and pull requests, which assistants never read. So they
|
|
29
|
+
"clean up" code that exists for a reason and bring old bugs back.
|
|
30
|
+
- **They don't know what a change will break,** or whether the documentation still matches the code.
|
|
31
|
+
- **Many tools send your code to someone else's server** to make it searchable.
|
|
32
|
+
|
|
33
|
+
## What Codebase Brain gives you
|
|
34
|
+
|
|
35
|
+
A plug-in for any AI assistant that turns a codebase into something it can actually look things up in:
|
|
36
|
+
|
|
37
|
+
- **Answers with proof.** Every result points to the exact file and lines, and the assistant can open them.
|
|
38
|
+
- **Search that understands code.** It finds code whether you type the exact name (`calculate_invoice_total`) or
|
|
39
|
+
describe it in plain words ("how are invoices totalled?").
|
|
40
|
+
- **The story behind the code.** For any function, it shows every change made to it, who made it, why (from the
|
|
41
|
+
commit message and pull request), and what the change was.
|
|
42
|
+
- **What breaks if you change something.** Everything that uses a function, directly or indirectly, and the tests
|
|
43
|
+
that cover it.
|
|
44
|
+
- **Out-of-date docs, found automatically.** Documentation that mentions functions, files or settings that no longer
|
|
45
|
+
exist, or shows examples that would no longer work.
|
|
46
|
+
- **Private by design.** Everything runs on your machine. No API keys, and your code never leaves your computer.
|
|
47
|
+
|
|
48
|
+
## Why it's different (technical)
|
|
49
|
+
|
|
50
|
+
- **Code-aware chunking.** Python is split by function, class and method using the real syntax tree, Markdown by
|
|
51
|
+
heading. Each chunk knows its symbol (`TimestampSigner.unsign`) and exact lines, so citations point at real code.
|
|
52
|
+
- **Hybrid retrieval.** SQLite FTS5 BM25 (with `camelCase` / `snake_case` identifier splitting) plus local
|
|
53
|
+
embeddings, merged with reciprocal-rank fusion. Exact names and fuzzy questions both work.
|
|
54
|
+
- **Local and free.** Embeddings run on your machine (`fastembed`, ONNX). No API keys, and no code leaves your
|
|
55
|
+
computer.
|
|
56
|
+
- **Incremental.** Re-indexing only re-embeds files whose content changed. On a 42-file repo, a re-index with no
|
|
57
|
+
changes takes about 2 seconds.
|
|
58
|
+
- **History-aware.** `explain_history` follows a function's lines back through git (`git log -L`), shows who wrote
|
|
59
|
+
the current lines, and links the pull requests behind each change. So agents can answer *"why is it built this
|
|
60
|
+
way?"*, not just *"what does it do?"*.
|
|
61
|
+
- **Impact analysis.** `impact_of_change` lists everything that uses a function, directly and transitively, plus the
|
|
62
|
+
tests that exercise it, before an agent changes anything.
|
|
63
|
+
- **Doc-drift detection.** `find_doc_drift` checks Markdown and reStructuredText docs against the code. It finds
|
|
64
|
+
files, functions, settings and flags that don't exist; examples passing parameters a function doesn't take; imports
|
|
65
|
+
that would fail; and sections last edited long before the code they describe changed. Each finding cites the doc
|
|
66
|
+
section and suggests a fix ("Was it renamed to `checkout_total`?").
|
|
67
|
+
- **Agent-friendly output.** Structured tool results, read-only tool annotations, progress notifications while
|
|
68
|
+
indexing, and clear error messages the model can act on.
|
|
69
|
+
|
|
70
|
+
## Tools
|
|
71
|
+
|
|
72
|
+
| Tool | What it does |
|
|
73
|
+
|---|---|
|
|
74
|
+
| `index_repo(source, name?)` | Index a local path or an `https://` git URL (shallow clone). Reports progress. |
|
|
75
|
+
| `ask_codebase(question, repo?, k?)` | The most relevant code and docs as cited sources, plus related commits, with instructions to answer only from them. |
|
|
76
|
+
| `search(query, repo?, k?, path_prefix?, kinds?)` | Raw hybrid search, filterable by path and chunk kind. |
|
|
77
|
+
| `explain_history(symbol? \| path, start_line?, end_line?, repo?)` | Every commit that changed a function or line range, newest first, with the relevant diff, current authorship, and PR titles and descriptions (GitHub). |
|
|
78
|
+
| `search_history(query, repo?, k?)` | Find commits by what their messages say. |
|
|
79
|
+
| `impact_of_change(symbol, path?, depth?, repo?)` | Direct and indirect users of a function or class, and the tests involved. |
|
|
80
|
+
| `find_doc_drift(path_prefix?, check_history?, repo?)` | Documentation that no longer matches the code, by severity (high, medium, low). |
|
|
81
|
+
| `read_lines(path, start, end, repo?)` | Open the exact lines behind a citation. Refuses paths outside the repo. |
|
|
82
|
+
| `list_repos()` | What's indexed, at which commit, and in which languages. |
|
|
83
|
+
|
|
84
|
+
Also included: the resource `brain://repos`, and the prompt `answer_with_citations`.
|
|
85
|
+
|
|
86
|
+
## Install
|
|
87
|
+
|
|
88
|
+
Codebase Brain runs on your own machine, so there's no server or account, and it's free. With
|
|
89
|
+
[uv](https://docs.astral.sh/uv/):
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
# Claude Code
|
|
93
|
+
claude mcp add codebase-brain -- uvx codebase-brain-mcp
|
|
94
|
+
|
|
95
|
+
# Any MCP client (Claude Desktop, Cursor, ...): add this server to its config
|
|
96
|
+
{ "command": "uvx", "args": ["codebase-brain-mcp"] }
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Quick start
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
uv sync
|
|
103
|
+
uv run codebase-brain index https://github.com/pallets/itsdangerous
|
|
104
|
+
uv run codebase-brain search "how is the signature timestamp checked for expiry"
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
0.0313 src/itsdangerous/timed.py:72-158 method TimestampSigner.unsign
|
|
109
|
+
0.0311 docs/timed.rst:1-28 code
|
|
110
|
+
0.0301 src/itsdangerous/timed.py:170-228 class TimedSerializer
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Ask why it looks like that. `explain_history(symbol="TimestampSigner.unsign")` returns its line-by-line history:
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
src/itsdangerous/timed.py:72-158
|
|
117
|
+
3edfbbbc 2022-03-24 handle OverflowError in timed unsign on 32-bit systems
|
|
118
|
+
177196d3 2022-03-09 catch OSError on Windows
|
|
119
|
+
37f09970 2022-03-08 catching year overflow ValueError
|
|
120
|
+
c30678d1 2019-05-31 don't allow timestamps from the future
|
|
121
|
+
910a7a94 2020-04-17 datetimes are timezone-aware utc
|
|
122
|
+
...
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Use it from Claude Code
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
claude mcp add codebase-brain -- uv run --directory /path/to/codebase-brain-mcp codebase-brain
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Run it as a remote server (Streamable HTTP)
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
export CODEBASE_BRAIN_TOKEN="$(python -c 'import secrets; print(secrets.token_urlsafe(32))')"
|
|
135
|
+
uv run codebase-brain serve --transport streamable-http --host 0.0.0.0 --port 8765
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Clients connect to `http://<host>:8765/mcp` with `Authorization: Bearer <token>`. `GET /healthz` is open for load
|
|
139
|
+
balancers. Remote mode is locked down by default:
|
|
140
|
+
|
|
141
|
+
- **No token, no public port.** The server refuses to listen beyond localhost without a token of at least 24
|
|
142
|
+
characters. Tokens are compared in constant time.
|
|
143
|
+
- **Remote clients can't read the server's disk.** Only public `https://` git URLs can be indexed, not local paths,
|
|
144
|
+
unless you set `CODEBASE_BRAIN_ALLOW_LOCAL_PATHS=1`.
|
|
145
|
+
- **Big repos are refused** above `CODEBASE_BRAIN_MAX_FILES` (default 20,000), and every git command has a timeout.
|
|
146
|
+
- **DNS-rebinding protection** is on for localhost, and for any hosts listed in `CODEBASE_BRAIN_ALLOWED_HOSTS`.
|
|
147
|
+
|
|
148
|
+
### Docker
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
docker build -t codebase-brain-mcp .
|
|
152
|
+
docker run -p 8765:8765 -e CODEBASE_BRAIN_TOKEN="$CODEBASE_BRAIN_TOKEN" -v brain-data:/data codebase-brain-mcp
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The image runs as a non-root user and includes the embedding model, so it starts fast and needs no download at
|
|
156
|
+
runtime. It has a health check, and keeps indexes in the `/data` volume. Indexing uses about 1.2 GB of memory, so
|
|
157
|
+
give the container 2 GB.
|
|
158
|
+
|
|
159
|
+
### Configuration
|
|
160
|
+
|
|
161
|
+
| Variable | Default | |
|
|
162
|
+
|---|---|---|
|
|
163
|
+
| `CODEBASE_BRAIN_HOME` | `~/.codebase-brain` | Indexes and clones |
|
|
164
|
+
| `CODEBASE_BRAIN_EMBEDDER` | `fastembed` | `hash` gives an offline, deterministic embedder (used in tests) |
|
|
165
|
+
| `CODEBASE_BRAIN_EMBED_MODEL` | `BAAI/bge-small-en-v1.5` | Any fastembed text model |
|
|
166
|
+
| `CODEBASE_BRAIN_MAX_FILE_BYTES` | `512000` | Larger files are skipped |
|
|
167
|
+
| `GITHUB_TOKEN` | unset | Optional. Raises GitHub's API rate limit for PR lookups |
|
|
168
|
+
| `CODEBASE_BRAIN_FETCH_PRS` | `1` | Set to `0` to never call the GitHub API |
|
|
169
|
+
| `CODEBASE_BRAIN_TOKEN` | unset | Bearer token for HTTP mode (required unless bound to localhost) |
|
|
170
|
+
| `CODEBASE_BRAIN_ALLOWED_HOSTS` | unset | Comma-separated `Host` values to accept (DNS-rebinding protection) |
|
|
171
|
+
| `CODEBASE_BRAIN_ALLOW_LOCAL_PATHS` | `1` (stdio) / `0` (HTTP) | Whether `index_repo` may read local directories |
|
|
172
|
+
| `CODEBASE_BRAIN_MAX_FILES` | `20000` | Refuse repos with more indexable files |
|
|
173
|
+
| `PORT` | `8765` | HTTP port (platforms such as Cloud Run set this) |
|
|
174
|
+
|
|
175
|
+
### Doc drift, tested honestly
|
|
176
|
+
|
|
177
|
+
On a well-maintained project (itsdangerous), `find_doc_drift` reports no false alarms. It only reports three
|
|
178
|
+
"worth a look" sections whose docs date from 2020 while the code they describe changed in 2024–2025. On docs with
|
|
179
|
+
planted mistakes, it catches every one: a moved file, a renamed function (with the right suggestion), an unknown
|
|
180
|
+
setting and flag, a keyword argument the function doesn't take, and an import that would fail. Changelogs are skipped,
|
|
181
|
+
because they're supposed to mention removed things.
|
|
182
|
+
|
|
183
|
+
## Evaluation
|
|
184
|
+
|
|
185
|
+
Does search actually put the right code in front of the model? `codebase-brain eval` answers that with a benchmark.
|
|
186
|
+
The benchmark has 32 questions about [pallets/itsdangerous](https://github.com/pallets/itsdangerous), pinned to
|
|
187
|
+
commit `672971d`, each with the code or docs a good answer must come from. The questions are in
|
|
188
|
+
[`evals/datasets/itsdangerous.toml`](https://github.com/Nomannazir/codebase-brain-mcp/blob/main/evals/datasets/itsdangerous.toml). They were written by reading the code
|
|
189
|
+
before any results were measured, and haven't been changed since.
|
|
190
|
+
|
|
191
|
+
| Mode | Hit@1 | Hit@3 | Hit@5 | Hit@10 | MRR@10 (95% CI) | nDCG@10 | p50 latency |
|
|
192
|
+
|---|---|---|---|---|---|---|---|
|
|
193
|
+
| **hybrid** (default) | 56% | **88%** | **88%** | 91% | **0.697** (0.56–0.82) | **0.735** | 5 ms |
|
|
194
|
+
| keyword only (BM25) | 56% | 75% | 78% | 84% | 0.667 (0.52–0.80) | 0.699 | <1 ms |
|
|
195
|
+
| vector only (bge-small) | 56% | 72% | 75% | 94% | 0.658 (0.51–0.80) | 0.701 | 4 ms |
|
|
196
|
+
|
|
197
|
+
| Hit@5 by question type | describes behaviour (14) | about the docs (7) | names the code (11) |
|
|
198
|
+
|---|---|---|---|
|
|
199
|
+
| hybrid | **71%** | **100%** | 100% |
|
|
200
|
+
| keyword only | 64% | 71% | 100% |
|
|
201
|
+
| vector only | 57% | 71% | 100% |
|
|
202
|
+
|
|
203
|
+
**What this shows:**
|
|
204
|
+
- **Hybrid finds a right answer in the top 3 for 88% of questions,** against 75% and 72% for either method alone.
|
|
205
|
+
The gain comes from questions written in plain words and questions about the docs. When the question names the
|
|
206
|
+
code, every mode finds it.
|
|
207
|
+
- **It's honest about uncertainty.** With 32 questions, the confidence intervals for MRR overlap, so hybrid is better
|
|
208
|
+
*on this set* but not yet proven in general. Per question, hybrid beats keyword-only on 8 questions and loses on 4.
|
|
209
|
+
- **It has known misses.** Four questions describing behaviour in very different words from the code don't reach
|
|
210
|
+
the top 5. They're listed in [`evals/results/itsdangerous.md`](https://github.com/Nomannazir/codebase-brain-mcp/blob/main/evals/results/itsdangerous.md) and are the next
|
|
211
|
+
thing to improve. A larger, held-out question set will come first, so any tuning isn't fitted to these 32.
|
|
212
|
+
|
|
213
|
+
Reproduce it (the first run downloads the repo and the embedding model):
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
uv run codebase-brain eval evals/datasets/itsdangerous.toml --out evals/results
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
How it's measured:
|
|
220
|
+
- **Hit@k:** a relevant result is in the top k.
|
|
221
|
+
- **MRR:** 1 / rank of the first relevant result.
|
|
222
|
+
- **nDCG@10:** each target is credited once, so duplicate chunks don't inflate the score.
|
|
223
|
+
- **Confidence intervals:** 95% bootstrap over questions, seeded.
|
|
224
|
+
- **Relevance:** a result is relevant if it's in the target file and, when the target names a symbol, is that symbol
|
|
225
|
+
or one of its methods.
|
|
226
|
+
- **Dataset rot:** the harness fails if a target no longer exists at the pinned commit.
|
|
227
|
+
|
|
228
|
+
## How it works
|
|
229
|
+
|
|
230
|
+
```
|
|
231
|
+
source ─► resolve (local dir / shallow https clone) ─► list files (.gitignore-aware, skips binaries and lockfiles)
|
|
232
|
+
─► chunk (ast / headings / line windows) ─► embed (fastembed) ─► SQLite: chunks + vectors + FTS5
|
|
233
|
+
query ─► BM25 top 50 ┐
|
|
234
|
+
─► vector top 50 ┴─► reciprocal-rank fusion ─► filters ─► cited sources
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Security
|
|
238
|
+
|
|
239
|
+
- Clones are blobless (`--filter=blob:none`): full history, with file contents fetched only when needed.
|
|
240
|
+
- Commit authors are shown by name only; email addresses are never stored or returned.
|
|
241
|
+
- Only `https://` clone URLs are accepted. Git runs with `GIT_ALLOW_PROTOCOL=https:file` and no terminal prompts.
|
|
242
|
+
- `read_lines` resolves paths and refuses anything outside the indexed repository.
|
|
243
|
+
- Repo names are validated, so they can't escape the index directory.
|
|
244
|
+
- All query tools are marked read-only.
|
|
245
|
+
- In HTTP mode: a required bearer token (constant-time check), git URLs only, repo size limits, DNS-rebinding
|
|
246
|
+
protection, and a non-root container.
|
|
247
|
+
|
|
248
|
+
## Development
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
uv run pytest # 54 tests, offline (hash embedder, temporary git repos, in-process MCP client)
|
|
252
|
+
uv run ruff check . # lint
|
|
253
|
+
uv run ruff format . # format
|
|
254
|
+
uv run pyright # types
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## Roadmap
|
|
258
|
+
|
|
259
|
+
- [x] Git history and PR discussions: `explain_history`, `search_history`
|
|
260
|
+
- [x] Symbol references and transitive dependents: `impact_of_change`
|
|
261
|
+
- [x] `find_doc_drift`: Markdown and reStructuredText, signatures, imports, settings, flags, staleness
|
|
262
|
+
- [ ] tree-sitter chunking for more languages
|
|
263
|
+
- [x] Retrieval evals with confidence intervals (`codebase-brain eval`)
|
|
264
|
+
- [ ] Larger held-out eval set, then retrieval tuning (reranking, query expansion)
|
|
265
|
+
- [x] Streamable HTTP with bearer-token auth, Docker image
|
|
266
|
+
- [ ] Hosted demo, PyPI package, MCP Registry listing
|
|
267
|
+
- [ ] OpenTelemetry tracing
|