Skip to main content

megabrain

megabrain

Ask a codebase a question. Get the exact code back.

PyPI MIT No LLM in the retrieval path MCP ready


Point megabrain at a repo and ask "how does auth work" in plain English. It finds all the related code — in ~200 ms, using no LLM, just math on embeddings — and an LLM narrates a walkthrough with the real code spliced in from disk. Nothing is invented: every line shown is copied verbatim.

Use it from the terminal, as an MCP server inside Claude Code, or as a Python library.

Quickstart — the easy path, no API keys

Everything runs on your machine: ask narrates on your Claude Code subscription, embeddings run locally on Ollama. No cloud keys.

pip install 'megabrain[claude]'                      # engine + Claude Code narration

ollama pull nomic-embed-text                          # local embeddings, one time
export MEGABRAIN_EMBED_BASE_URL=http://localhost:11434/v1
export MEGABRAIN_EMBED_MODEL=nomic-embed-text

megabrain index ~/your/repo                           # once — incremental after
megabrain ask   ~/your/repo "how does auth work end to end"

ask uses your logged-in claude CLI (free on your plan); embeddings never leave your machine. No OpenRouter, no Anthropic key.

Inside Claude Code

Register it as an MCP server and research any indexed repo without leaving Claude Code:

claude mcp add megabrain -- python3 -m megabrain.mcp_server

Then use megabrain_ask / megabrain_query instead of grep + Read chains — one call replaces minutes of file-crawling. Tools: megabrain_ask (narrated walkthrough), megabrain_query (raw code map, no LLM), megabrain_get, megabrain_chunks, megabrain_index.

Commands

megabrain index  ~/repo                          # build / update the index
megabrain ask    ~/repo "how does X work"        # narrated walkthrough + real code
megabrain query  ~/repo "retry logic"            # raw code map, no LLM (~200 ms)
megabrain get    ~/repo src/x.py --symbol Foo    # one file or symbol
megabrain serve-api ~/repo                       # long-running HTTP API (warm state)

Scope to a sub-folder (~/repo/src/auth), search several repos at once (~/a,~/b), and the index auto-refreshes when files change on disk.

Rather use the cloud?

No Claude Code or Ollama? One key runs everything through OpenRouter — embeddings and narration — with sensible defaults:

export OPENROUTER_API_KEY=...
megabrain ask ~/repo "how does X work"

megabrain auto-picks the narrator: Claude when its SDK is installed, otherwise OpenRouter. Embeddings always go through OpenRouter or a local endpoint (Anthropic has no embeddings API). The full provider matrix — native APIs, hybrid, fully-local GPU — is in docs/ARCHITECTURE.md.

How it works

stage what happens
index code is split over its syntax tree (whole functions / classes, never arbitrary line windows), embedded once, stored in SQLite. Incremental by hash.
query no LLM — your question is embedded and matched by vector similarity. Returns every related file in ~200 ms; nothing is dropped.
ask one LLM call narrates the answer and cites code as [[k]]; the engine replaces each citation with the verbatim block from disk. The model can only point at code, never rewrite it — so nothing is hallucinated. Broad questions fan out into parallel sub-agents, then a parent synthesizes.

Languages: Python · JS/TS · Markdown built in; Ruby · Go · Rust · PHP with pip install 'megabrain[languages]'.

See it live

bernardocastro.dev/megabrain — search 7 popular open-source repos and watch the engine rank the files and pick the exact code chunks, live. Or run it locally: python examples/webui/server.py.

Learn more

  • docs/ARCHITECTURE.md — the full design, the locked rules, and the measurements behind them
  • examples/ — programmatic API · a custom .sql chunker · the web demo
  • CONTRIBUTING.md — the best first PR is a new language

MIT · github.com/bernatch22/megabrain

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

megabrain-0.5.0.tar.gz (103.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

megabrain-0.5.0-py3-none-any.whl (87.4 kB view details)

Uploaded Python 3

File details

Details for the file megabrain-0.5.0.tar.gz.

File metadata

  • Download URL: megabrain-0.5.0.tar.gz
  • Upload date:
  • Size: 103.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for megabrain-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e3874400582b212c262a2d7c4daf904fe79b3a321e458e34fa1d2a1b1b8331f4
MD5 7444caf95412a621a849dfba826b3457
BLAKE2b-256 7912f52ffcb7273abef0d18ff8dfe79e28778e099ecb25ab6553073b1dd0fcb4

See more details on using hashes here.

File details

Details for the file megabrain-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: megabrain-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 87.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for megabrain-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c7fe8685af74320969b2403e76657528db046f083b090fa8ecf69f3a77a2c7a
MD5 325a4f75a89b84d2594add8e0cad81e3
BLAKE2b-256 d632781e7917f798cc91544edd6be56bfa2305411939441afdc0f5abb876093b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page