diffctx
This server provides three tools to intelligently extract code context for LLM-assisted review and understanding:
get_diff_context: Analyze git diffs to retrieve the most relevant code fragments (functions, classes, dependencies) ranked by relevance and within a token budget. Supports 30+ languages, optional raw diff inclusion, and clipboard output. Useful for PR review, change explanation, and refactor impact analysis.get_tree_map: Generate a structured directory tree map of a codebase with file contents in YAML or Markdown. Respects.gitignore, skips binaries, supports depth limiting, content suppression, and clipboard output.get_file_context: Read and format files matching glob patterns from any directory (no git required). Supports dry-run, file count and size limits, and clipboard output. Ideal for reading multiple specific files at once.
diffctx — smart diff context for LLM code review
diffctx selects the minimum code an LLM needs to review a git diff. Instead of pasting whole files, it walks the dependency graph outward from the changed lines and stops once more context stops paying for itself.
Formerly published as
treemapper— every command, flag, and API call works unchanged.
How it compares
Whole-repo packers (repomix and friends) seed on the repository and export everything; persistent code-graph servers answer structural queries against a maintained index. diffctx is diff-seeded: the input is a change, the output is the fragments needed to understand it, packed under a hard token budget — local, deterministic, no index, no model calls. Measured results and when the other two families fit better: COMPARISON.md.
Related MCP server: better-code-review-graph
Install
uvx diffctx . --diff HEAD~1 # zero-install, run once via uv
pipx install diffctx # recommended: isolated CLI, no venv needed
pip install diffctx # or: into an active environment
pipx install 'diffctx[mcp]' # + MCP server for AI assistantsWithout Python:
cargo install diffctx # native CLI from crates.io
npx diffctx . --diff HEAD~1 # npm wrapper over the native binary
docker run --rm -v "$PWD:/repo" ghcr.io/nikolay-e/diffctx . --diff HEAD~1On Windows, via Scoop (this repository is the bucket):
scoop bucket add diffctx https://github.com/nikolay-e/diffctx
scoop install diffctx/diffctxPrebuilt binaries for linux (x86_64/aarch64), macOS (arm64) and Windows (x64)
are attached to every release.
The native binary and Docker image cover diff mode with YAML/JSON output and
write to stdout (redirect to capture); tree mode, Markdown output, the graph
subcommand and the MCP server live in the Python package.
Quick start
diffctx . --diff HEAD~1 # smart context for last commit → paste into Claude/ChatGPT
diffctx . -f md -c # full codebase export → clipboard in Markdown
diffctx . --diff HEAD~1 selects only the fragments an LLM needs to review the
last commit, instead of dumping every changed file in full.
Diff context mode
Finds the minimal set of fragments needed to understand a change — imports,
callers, type definitions, config dependencies — across 50+ file types. It
builds a code graph (imports, co-changes, type refs), propagates relevance
outward from the changed lines, and stops when relevance drops below --tau or
the --budget token cap is hit.
Flag | Default | Description |
|
|
|
| auto | Hard cap in o200k_base tokens (see Token counting): |
| 0.60 | PPR damping; higher = context clusters tighter around changes ( |
| 0.12 | Relevance threshold for full fragment content; lower-scoring fragments are stubbed or dropped (lower = more context) |
| false | Only the changed files, every fragment, no related-code context |
| 300 | Wall-clock deadline in seconds; on expiry diffctx exits 124 instead of hanging |
| false | Also embed git's raw unified diff ahead of the selected fragments — additive (selection unchanged), not charged to |
|
|
|
graph subcommand
Explore the underlying dependency graph directly, without a diff:
diffctx graph . # Mermaid graph of directory deps (default)
diffctx graph . --summary # cycles, hotspots, coupling metrics
diffctx graph . --level fragment -f json # fragment-level graph as JSON
diffctx graph . --level file -f graphml -o g.xml # file-level graph as GraphMLUsage
# full codebase export:
diffctx . # Markdown to stdout + token count
diffctx . -f md -c # Markdown → clipboard
diffctx . -f json -o tree.json # JSON → file
diffctx . --no-content # structure only, no file contents
diffctx . --max-depth 3 # limit depth
diffctx . -i custom.ignore # custom ignore patterns
# diff context mode (requires git repo):
diffctx . --diff # uncommitted changes (working tree vs HEAD)
diffctx . --diff HEAD~1 # context for last commit
diffctx . --diff main..feature # context for feature branch
diffctx . --diff HEAD~1 --budget 30000 # limit to ~30k tokens
diffctx . --diff HEAD~1 -c # diff context to clipboard
diffctx . --diff HEAD~1 --with-raw-diff # raw patch + selected context
diffctx . --diff HEAD~1 --mode locate # ranked navigation JSON, no sourceEvery run reports token count and size on stderr — 12,847 tokens (o200k_base), 52.3 KB. Counts are exact only for the GPT-4o family; Claude,
Gemini and others tokenize differently, so treat --budget as an upper bound
and leave headroom (details). Unreadable files
become placeholders like <binary file: N bytes>.
Python API
from pathlib import Path
from diffctx import build_diff_context, map_directory, to_json, to_markdown, to_text, to_yaml
ctx = build_diff_context(
Path("."),
"HEAD~1..HEAD",
budget_tokens=None, # None = auto; 0 = strict-zero floor (empty); -1 = uncapped; N = hard cap
alpha=0.6,
tau=0.12,
full=False,
scoring_mode="ego",
timeout=300,
with_raw_diff=False, # True also embeds the raw unified diff (not charged to budget)
)
print(to_markdown(ctx))
tree = map_directory(
".",
max_depth=None,
no_content=False,
max_file_bytes=None,
ignore_file=None,
no_default_ignores=False,
whitelist_file=None,
)
print(to_yaml(tree))MCP server
diffctx includes an MCP server that lets AI
assistants (Claude Code, Cursor, Windsurf, etc.) call diff context analysis
automatically during code review. It is published in the official MCP registry
as io.github.nikolay-e/diffctx. One-line setup (zero-install via
uv):
# Claude Code
claude mcp add diffctx -- uvx --from 'diffctx[mcp]' diffctx-mcp
# Codex CLI
codex mcp add diffctx -- uvx --from 'diffctx[mcp]' diffctx-mcp
# Gemini CLI
gemini mcp add diffctx uvx -- --from 'diffctx[mcp]' diffctx-mcp
# VS Code
code --add-mcp '{"name":"diffctx","command":"uvx","args":["--from","diffctx[mcp]","diffctx-mcp"]}'With pip install 'diffctx[mcp]' already done, replace the
uvx --from 'diffctx[mcp]' diffctx-mcp tail with plain diffctx-mcp.
The server exposes three tools — get_diff_context, get_tree_map, and
get_file_context — that assistants call when reviewing PRs, explaining
changes, or investigating broken tests. Tool reference and JSON configs for
Claude Desktop, Cursor, Continue, Windsurf, and Zed:
src/diffctx/mcp/README.md.
Ignore patterns
Respects .gitignore and .diffctx/ignore automatically — hierarchically at
every directory level, with full gitignore semantics (negation !important.log,
anchored /root_only.txt). .diffctx/whitelist acts as an include-only filter,
and the output file is always auto-ignored. --no-default-ignores disables the
built-in patterns; --no-ignores disables all ignore rules (tree mode only).
An excluded path never appears in the output in any role: in diff mode it is
dropped both from changed_files and from the candidate universe, so it cannot
come back as a related-context fragment either (including under --full). The
same guarantee covers secret-like paths (id_rsa, *.pem, *.key, ...),
which are filtered even without an ignore entry.
Token cache
Diff mode caches per-blob tokenization in the OS cache directory (e.g.
~/Library/Caches/diffctx/token-cache) — a pure speedup, safe to delete.
DIFFCTX_TOKEN_CACHE_DIR relocates it; DIFFCTX_TOKEN_CACHE_MAX_BYTES caps
its size (default 512 MB, 0 disables eviction).
Exit codes
Code | Meaning |
| Success — output contains content |
| Runtime error (bad path, permission denied, etc.) |
| Usage error (invalid flags/arguments) |
| Environment error ( |
|
|
|
|
| Interrupted (Ctrl-C) |
| Broken pipe (e.g. piping into |
License
Apache 2.0
Documentation site — the pipeline end to end: diff → fragments → graph → relevance → selection
GitHub Action — diff context as a CI step for LLM review
Token counting — which encoder, and what
--budgetmeans for non-GPT modelsComparison — measured results, and when a whole-repo packer or a persistent code-graph server fits better
Paper — budgeted typed-graph retrieval for diff-aware context selection (Zenodo, 2026)
Security policy — threat model and vulnerability reporting
Parameter strategy — how
--alpha,--tau, and edge weights are calibrated
Maintenance
Related MCP Servers
- AlicenseBqualityDmaintenanceExtracts minimal, relevant code context from multiple programming languages while analyzing diffs and optimizing imports to reduce token usage for AI assistants. Supports TypeScript/JavaScript, Python, Go, and Rust with token-aware caching.Last updated7401MIT
- AlicenseBqualityAmaintenanceKnowledge graph for token-efficient code reviews. Builds a structural map of your codebase with Tree-sitter, tracks changes incrementally, and gives AI agents precise context via MCP tools. Features fixed multi-word search, qualified call resolution, dual-mode embedding (ONNX local + LiteLLM cloud), and output pagination.Last updated765MIT
- AlicenseAqualityCmaintenanceCode graph context engine that parses codebases with tree-sitter (170+ languages), builds structural dependency graphs, and provides 24 MCP tools for code intelligence. One prepare_context call gives your AI agent the right files for any task. Includes focus, blast radius, hotspots, dead code detection, and hybrid search.Last updated241AGPL 3.0
- Alicense-qualityCmaintenanceProvides a semantic understanding of your codebase by parsing with tree-sitter and building a graph of symbols and dependencies. Enables AI assistants to navigate code, analyze changes, and discover architecture using 18 tools with minimal context overhead.Last updated191MIT
Related MCP Connectors
Provide your AI coding tools with token-efficient access to up-to-date technical documentation for…
Enterprise code intelligence for M&A, security audits, and tech debt. Hosted server with 200k free.
A paid remote MCP for OpenAI Codex context compressor, built to return verdicts, receipts, usage log
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/nikolay-e/diffctx'
If you have feedback or need assistance with the MCP directory API, please join our Discord server