Skip to main content
Glama

CodeBrain

An MCP server that lets Claude Code offload bulk work to a local LLM running on your own hardware.

Status Stack License


What this is (and isn't)

Is: A Model Context Protocol (MCP) server that Claude Code registers as a sub-agent backend. When a session includes the kind of task a 14B local coder model handles well — generating 50 event templates, polishing 20 React components, drafting boilerplate — Claude Code calls into CodeBrain instead of spending its own output tokens. The local model does the bulk draft, Claude reviews and applies.

Is not: A Claude replacement. The reasoning, architecture decisions, debugging, and anything where "close enough" isn't good enough stays with Claude. CodeBrain is a Claude-offloader, not a Claude-competitor.

Why: Large-volume content and polish work burns through Claude's context and rate limits fast. A local model you can run unlimited costs nothing extra per call and keeps the high-value context free for the hard parts of the session.

Related MCP server: ollama-mcp

Status

Phases 1–4 complete, Phase 5 deferred. Nine tools exposed, .brain/context.md passthrough live, per-file brain summaries scanner, verifier loop, consensus decoding. MCP integration verified in a real Claude Code session. Phase 5 (RAG) was explicitly scoped as "only if needed" and current use doesn't show cross-file search as a bottleneck, so it stays deferred.

How it works

Claude Code session                     CodeBrain MCP server              Local machine
─────────────────────      stdio       ───────────────────                ─────────────
Claude delegates a         ────────►   codebrain_generate()     ────►    Ollama HTTP
bulk / polish task                     codebrain_explain()                (localhost:11434)
                                       codebrain_status()                      │
                                                                                ▼
                                                                        Qwen2.5-Coder 14B
                                                                              (GPU)
Claude reviews,            ◄────────   tool result string        ◄────    streamed response
applies, or pushes back

Nine tools are exposed today:

Tool

When Claude would reach for it

codebrain_generate(prompt, system, use_brain)

Bulk content, boilerplate, repetitive transformations, first drafts

codebrain_batch_generate(prompts, system, use_brain)

N prompts with one shared system message, serial execution, index-stable errors so one failure doesn't abort the batch

codebrain_polish(text, instructions, use_brain)

Targeted transform over existing text — shorten, rephrase, translate, tighten. Auto-retries on no-op output.

codebrain_explain(code, question)

Quick read-only explanations without burning Claude context

codebrain_generate_verified(prompt, min_words, max_words, must_match, max_retries)

Generation with deterministic verifier loop: word-count / regex-schema checks, tightened-instruction retry on violation

codebrain_consensus_generate(prompt, n)

N candidates + judge call → best single output. Use on high-variance tasks.

codebrain_init(root, force)

One-shot repo onboarding: detects stack, writes .brain/context.md template

codebrain_scan_file(path, force)

Generate or refresh one <source>.brain summary file

codebrain_scan_repo(root, force, extensions, exclude_dirs)

Walk + scan a tree; hash-gated, per-file failures don't abort the batch

codebrain_status()

Check which models are installed locally

The use_brain flag on generation tools automatically prepends .brain/context.md from the current working directory to the system prompt, so project-specific context travels with every call without Claude having to pass it manually.

Requirements

  • Python 3.11+

  • Ollama — download for your OS. Tested with Ollama on Windows native, talking over localhost:11434.

  • A coder model pulled locally:

    ollama pull qwen2.5-coder:14b

    ~9 GB download. Fits in 12 GB VRAM at Q5. Other models work too (DeepSeek-Coder, Qwen3 if available) — set via CODEBRAIN_MODEL env var.

  • Claude Code CLI on the machine that will call the server (obviously).

Install

git clone <this repo> CodeBrain
cd CodeBrain
python -m venv .venv
.venv\Scripts\activate                         # on Windows
# source .venv/bin/activate                    # on macOS / Linux
pip install -e .

Configure Claude Code

Add CodeBrain to your Claude Code MCP config. On Windows, that's usually ~/.claude.json (adjust path to where you cloned):

{
  "mcpServers": {
    "codebrain": {
      "command": "C:\\Users\\YOU\\Desktop\\CodeBrain\\.venv\\Scripts\\python.exe",
      "args": ["-m", "codebrain"]
    }
  }
}

Restart any Claude Code session — the five codebrain_* tools should now appear in the available-tools list.

Keep brain files in sync automatically

Once you've run codebrain_init on a repo and scanned it with codebrain_scan_repo, you probably want brain files to refresh automatically whenever Claude edits source. Two pieces wire that up:

1. Project CLAUDE.md snippet — tell Claude to read brain files before opening source:

## Brain files

This repo has per-file `.brain` summaries next to each source file.
Before reading a full source file, read its `<path>.brain` sibling first.
Only open the source when the brain file is insufficient for the task.

2. PostToolUse hook — regenerate the brain after every Edit/Write.

Add to .claude/settings.json in the repo root:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "python -c \"import asyncio, json, sys; from codebrain.brain_scanner import scan_file; d = json.load(sys.stdin); p = d.get('tool_input', {}).get('file_path'); p and p.endswith(('.py', '.ts', '.tsx', '.js', '.jsx', '.java', '.go', '.rs')) and print(asyncio.run(scan_file(p)))\""
          }
        ]
      }
    ]
  }
}

The hook inspects the edited path, skips non-source files via the extension filter, and kicks off a scan. Hash-gated: unchanged files don't hit Qwen.

Sanity check

Inside a Claude Code session, ask Claude:

Call codebrain_status and tell me what's installed.

If Ollama is running and the model is pulled, you'll get back qwen2.5-coder:14b in the list.

Configuration

Environment variables read by the backend:

Variable

Default

What it does

CODEBRAIN_OLLAMA_URL

http://localhost:11434

Point at a remote Ollama (e.g., an inference box on your LAN)

CODEBRAIN_MODEL

qwen2.5-coder:14b

Switch to any model you've pulled

CODEBRAIN_TIMEOUT

300

Seconds to wait for a single generation

Project structure

CodeBrain/
├── codebrain/
│   ├── __init__.py
│   ├── __main__.py            # `python -m codebrain` entry
│   ├── backend.py             # Ollama HTTP client
│   ├── server.py              # FastMCP server + tool definitions
│   ├── brain_scanner.py       # scan_file / scan_repo + hash gate
│   ├── brain_init.py          # one-shot .brain/context.md seeding
│   ├── verifier.py            # deterministic output checks
│   └── prompts/
│       └── brain_few_shot.md  # few-shot for brain-file generation
├── tests/                     # 96 unit + integration tests
├── .spec/
│   ├── CURRENT.md             # phase state
│   └── brain-file-format.md   # brain-file format v1
├── pyproject.toml
├── LICENSE
└── README.md

Roadmap

Phase 1 — scaffold ✓

  • Ollama HTTP client with error handling

  • FastMCP server with stdio transport

  • Three core tools: generate, explain, status

  • Documented setup + Claude Code config

  • Verified in a real Claude Code session

Phase 2 — batch & context ✓

  • codebrain_batch_generate for mass content with one shared system prompt, index-stable errors

  • codebrain_polish for targeted transforms (shorten / rephrase / translate) instead of regeneration

  • .brain/context.md passthrough — cwd project context auto-prepended to every generation call

  • Dogfood: coding tasks solid, text-transform tasks revealed real limits (informs Phase 3)

Phase 2.5 — brain system ✓

Per-file <source>.brain summaries sit next to each source file. Claude reads the brain first and only opens the source when the brain is insufficient.

  • codebrain_scan_file(path, force) — generate or refresh one brain file

  • codebrain_scan_repo(root, force, extensions, exclude_dirs) — bulk walk + scan

  • codebrain_init(root, force) — seed .brain/context.md with stack detection

  • Hash-gated regeneration (SHA256) — idempotent reruns

  • Programmatic frontmatter — deterministic source, source_hash, model; Qwen only writes the five sections

  • Defense-in-depth validation: fence-strip, skip-empty-sources (<10 chars), section-presence/order, retry-on-invalid

  • CLAUDE.md convention + PostToolUse hook snippet in this README

Phase 3 — VERIFIER loop ✓

Dogfood showed the local model drifts on text transforms. The verifier catches no-ops, length violations, and schema misses deterministically before they reach Claude.

  • detect_noop — whitespace-normalised equality check (auto-retries inside codebrain_polish)

  • check_word_count(min_words, max_words) — bounded-window gate

  • check_regex_schema(pattern) — structured-output check

  • codebrain_generate_verified(prompt, min_words, max_words, must_match, max_retries) — loop with tightened retry instructions, returns [codebrain warning] ... if verification fails after retries

Phase 4 — consensus decoding ✓

  • codebrain_consensus_generate(prompt, n) — generate N candidates (clamped to [2,5]), Qwen picks the best verbatim. N+1 inference calls, tightens quality on high-variance tasks.

  • Multi-pass skeleton→logic→edges→polish: deferred (low measured value; individual tools already compose).

Phase 5 — RAG (deferred — not a bottleneck)

Brain files already act as an index; cross-file RAG only makes sense if future use actually shows that indexing is the blocker. No current signal for it, so not built.

License

MIT — see LICENSE.

Available Tools

10 tools
codebrain_batch_generateA

Run several generation prompts in sequence and return all results.

One shared system prompt applies to every item. Prompts are processed serially (Ollama serialises on a single GPU anyway). A failure on one prompt is captured inline as [codebrain error] ... at that index, so the whole batch never aborts.

Returns a single string with per-item delimiters:

--- [0] ---
<result for prompts[0]>

--- [1] ---
<result for prompts[1]>

Args: prompts: List of prompts to run with the same system message. system: Optional shared system message. use_brain: If true, prepend .brain/context.md from cwd to the system prompt.

Inputs: prompts (required), system, use_brain. Returns: result

codebrain_consensus_generateA

Generate N candidates, let Qwen pick the best, return the winner.

Runs prompt N times (serial — Ollama serialises on single GPU anyway), then does one additional call where Qwen is shown all candidates and asked to return the best one verbatim. Useful for high-variance tasks where a single shot drifts but majority-vote style sampling tightens quality at the cost of N+1 inference calls.

Args: prompt: The task description or content request. system: Optional system message to steer tone / format / constraints. n: Number of candidates to generate (default 3, clamped to [2, 5]). use_brain: If true, prepend .brain/context.md to the system prompt.

Inputs: prompt (required), system, n, use_brain. Returns: result

codebrain_explainA

Ask the local model to explain a snippet of code (read-only, no generation).

Useful for getting quick, token-free explanations without consuming Claude's context budget on understanding-only tasks.

Args: code: The code snippet to explain. question: The specific question to answer about the code.

Inputs: code (required), question. Returns: result

codebrain_generateA

Delegate a generation task to the local Qwen-Coder model via Ollama.

Use this for bulk or routine work where a 14B local model is good enough: generating event templates, headlines, company descriptions, UI polish drafts, boilerplate, or repetitive transformations. The response is returned as raw text — review before applying.

Args: prompt: The task description or content request. system: Optional system message to steer tone / format / constraints. use_brain: If true, prepend .brain/context.md from cwd to the system prompt.

Inputs: prompt (required), system, use_brain. Returns: result

codebrain_generate_verifiedA

Generate with verifier loop — enforces word limits and regex schemas.

Runs codebrain_generate, then checks the output against the requested constraints. On failure, retries with a tightened instruction that names the specific problem. Gives up after max_retries attempts and returns the last output with a [codebrain warning] ... prefix.

Args: prompt: The task description or content request. system: Optional system message to steer tone / format / constraints. min_words: Minimum output word count (None = unbounded). max_words: Maximum output word count (None = unbounded). must_match: Regex pattern the output must match (re.search semantics). max_retries: Max retry attempts on verification failure (default 2). use_brain: If true, prepend .brain/context.md to the system prompt.

Inputs: prompt (required), system, min_words, max_words, +3 more. Returns: result

codebrain_initA

Seed .brain/context.md for a repo — one-time setup before scanning.

Detects the stack (python / js / ts / rust / go / java) from marker files, counts source-file extensions, asks Qwen for a short overview, and writes .brain/context.md with a pre-populated template. The user is expected to edit the ## Notes for Claude section afterwards. Idempotent: existing context.md is not overwritten unless force=True.

Args: root: Directory to initialise. force: If true, overwrite an existing .brain/context.md.

Inputs: root (required), force. Returns: result

codebrain_polishA

Apply a targeted transform to existing text — do not regenerate from scratch.

Use this when you have a draft and want it tightened, shortened, rephrased, made more formal, translated, or similar. The system prompt forces the model into transform-mode: it must preserve meaning and structure and only apply the requested change.

Args: text: The existing text to polish. instructions: What transformation to apply (e.g. "shorten to 2 lines", "make tone more formal", "translate to German"). use_brain: If true, prepend .brain/context.md from cwd to the system prompt.

Inputs: text (required), instructions (required), use_brain. Returns: result

codebrain_scan_fileA

Generate or refresh the <path>.brain summary file for a source file.

Reads the source at path, computes its SHA256, and compares to the existing .brain file's source_hash frontmatter. If they match and force is false, generation is skipped. Otherwise Qwen produces a new brain file (Purpose / Key exports / Collaborators / Gotchas / Conventions), the output is validated against the format spec, and on validation failure one retry with a sharper instruction is attempted before giving up. No partial or broken brain files are ever written.

Format spec: .spec/brain-file-format.md.

Args: path: Path to the source file to summarise. force: If true, regenerate even when the hash matches.

Inputs: path (required), force. Returns: result

codebrain_scan_repoA

Scan every source file under root and generate/refresh its .brain file.

Walks the directory tree, filters by file extension, prunes excluded directories, and runs codebrain_scan_file on each match. Hash-gated: unchanged files skip the model call. Per-file failures do not abort the batch — they are reported at the end.

Defaults:

  • extensions: .py .js .ts .tsx .jsx .java .go .rs

  • exclude_dirs: .git .venv venv node_modules pycache dist build target

Args: root: Directory to scan recursively. force: If true, regenerate every brain file even when source hash matches. extensions: Override default source extensions (e.g. [".py", ".rb"]). exclude_dirs: Override default directory-name exclusion list.

Inputs: root (required), force, extensions, exclude_dirs. Returns: result

codebrain_statusA

Report which Ollama models are available locally.

Call this to verify the local backend is reachable and discover which models the user has pulled.

Inputs: no named parameters. Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A4.4/5.0

    Scored across 10 tools

    Disambiguation5/5

    Each tool has a clearly distinct purpose: batch generation, consensus generation, explanation, single generation, verified generation, initialization, polishing, file scanning, repo scanning, and status. While some involve generation, they differ in process (e.g., batch vs consensus) or constraints, and descriptions make them easy to differentiate.

    Naming Consistency5/5

    All tool names follow the consistent pattern 'codebrain_verb_noun' in snake_case (e.g., codebrain_batch_generate, codebrain_scan_file). The verb is always present and descriptive, with no mixing of conventions.

    Tool Count5/5

    With 10 tools, the server is well-scoped for its purpose of local AI code assistance. Each tool earns its place, covering core operations like generation, verification, file analysis, and setup without unnecessary bloat.

    Completeness4/5

    The tool set covers the full lifecycle for the domain: setup (init), generation (generate, batch, consensus, verified), analysis (explain, scan_file, scan_repo), and polishing. A minor gap is the lack of a tool to delete or clear generated brain files, but this is not essential for the core workflow.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • F
      license
      Not graded
      quality
      D
      maintenance
      MCP server that lets Claude Code offload simple tasks like code explanation, writing tests, and adding comments to a local Ollama model, saving Claude API tokens.
      -
    • A
      license
      Not graded
      quality
      C
      maintenance
      A local MCP server that delegates coding tasks to local Qwen and cloud Gemini models, enabling orchestrators like Claude Code to offload routine code generation and receive verified results with automatic correction logging.
      1
      MIT
    • A
      license
      A
      quality
      C
      maintenance
      An MCP server that allows Claude Code to offload mechanical tasks such as summarization, classification, and drafting to a local LLM, reducing API costs while keeping Claude in control of complex reasoning and quality review.
      7
      MIT