MCP server that retrieves the most relevant source code for a query, within a token budget
Project description
fittok
Retrieve only the relevant source code for a question โ instead of the model reading whole files โ so an LLM answers codebase questions on a small, focused slice of context. Less input = fewer tokens, lower cost, faster answers.
Works three ways from one install: an MCP server, a CLI, and a Python library โ plus a Claude Code plugin that injects context automatically.
๐ Full command reference โ docs/HANDBOOK.md
mcp-name: io.github.likhithreddy/fittok
How it works
codebase โโโถ graphify โโโถ slurp โโโถ readable slice โโโถ LLM answers
(parse) (select) (trim to budget)
-
graphify โ parses the repo with tree-sitter into a knowledge graph of functions / classes / methods (Python, JS, JSX, TS, TSX, Java, Go, Rust). Supports multi-language call/import/reference edges.
-
slurp โ scores every node against the question using a 4-signal hybrid:
- Semantic embeddings (all-MiniLM-L6-v2) โ meaning-based matching
- Content-BM25 (with camelCase/snake_case splitting) โ keyword matching
- Summary-BM25 (node name + file + callers + callees) โ structural matching
- PageRank โ graph centrality / hub detection
Signals are fused via Reciprocal Rank Fusion (RRF) โ rank-based, no score calibration issues. Nodes are selected via round-robin directory interleaving (guarantees facet coverage on multi-aspect queries โ one node from each code area before any gets a second) with a per-node token cap (25% of budget, so large components don't crowd out smaller functions). A relevance cliff (semantic OR BM25 OR summary-BM25 threshold) excludes noise.
-
readable output โ returns the actual source code of selected nodes, plus a codebase map (table of contents with docstrings, inspired by Karpathy's LLM Wiki / Google's OKF) so the model can route follow-up calls precisely. The model answers directly from it โ no file reads needed.
As you edit, a file watcher (auto-started on first query) updates the graph
incrementally โ only changed files are re-parsed and merged, and only
changed functions re-embed. Graphs and embeddings are cached on disk
(~/.cache/fittok). Set FITTOK_AUTOWATCH=false to disable the watcher, in
which case an edit triggers a full re-parse on the next query.
Getting the best results (and known limitations)
Ask focused questions
fittok ranks code against your question using a 4-signal hybrid (semantic + BM25 + structural + PageRank, fused via RRF) with round-robin directory diversity โ so multi-facet questions surface code from multiple areas (UI, server, database) instead of clustering in one dominant area. It's most accurate with focused, specific questions โ ideally one concern each, and naming the function/component/route when you can. Multi-facet questions are supported via decomposition (the tool description tells the model to call once per aspect) and the codebase map (a table of contents prepended to every response).
- โ
"How does
runSandboxQueryexecute and isolate a SQL query?" โ surfaces the exact function + its isolation code. - โ "How does the querydle client submit a query and render results?" โ surfaces the UI component.
- โ "Trace the full lifecycle: UI submission, sandbox execution, data isolation" โ decomposition + round-robin diversity covers multiple facets; the codebase map routes the model to any missed files.
Rule of thumb: one concern per question (or 2โ3 facets max). For "explain the whole feature," split it into a few focused questions instead of one mega-query.
Known limitations
- Vocabulary gap on abstract queries. When the query uses words that don't appear in the code (e.g. "isolation" โ
REVOKE/DENY), neither semantic nor BM25 can bridge it. The codebase map (which lists file names + docstrings) and round-robin diversity help, but the model may still read 1โ2 files for these facets. Mitigation: name the function/file, or the codebase map routes the model to it. - Copilot's "verify by reading" habit. Even when fittok returns the full code, Copilot's model sometimes reads the file to confirm. This is model behavior, not a fittok limitation โ a strong
.github/copilot-instructions.mdsaying "do not Read files that appear in fittok's output" reduces it. - Incremental edge-loss: editing a file can drop call/import edges into it from unchanged files until a full re-parse. fittok auto-recovers on restart or
reset_graph. - Token budgets are approximate: counts use
cl100k_base, so real usage drifts ~10โ20% vs Claude's tokenizer (headroom is built into the budget constants). - Very large repos: PageRank is not yet vectorized โ fine through low-thousands of nodes, slower beyond that.
Installation
fittok ships as an MCP server, a CLI, and a Python library. It uses
torch for embeddings, so a Python runtime must be present. Pick one runtime
below, then follow the section for your client.
Every config below launches fittok as
uvx fittok. If you chose Python orpipx, swap that forpython -m fittokorpipx run fittokrespectively.
Prerequisites โ choose a runtime (one of)
A. uv โ recommended (no Python needed on the machine)
curl -LsSf https://astral.sh/uv/install.sh | sh # Linux / macOS
winget install astral-sh.uv # Windows
brew install uv # macOS (Homebrew)
Launch command: uvx fittok โ uv provisions its own Python + all deps in
isolation. One static binary, so it's deployable org-wide via MDM/Intune/winget.
B. Python 3.10+ (already on the machine)
python -m pip install fittok # Linux / macOS
py -m pip install fittok # Windows
Launch command: python -m fittok (Windows: py -m fittok).
Managed Linux may reject
pip installwith PEP 668 ("externally-managed-environment") โ use option A to avoid it.
C. pipx โ isolated, no global install
brew install pipx # macOS
pip install --user pipx && pipx ensurepath # Linux / Windows
Launch command: pipx run fittok.
MCP server โ Claude Code
claude mcp add fittok -s user -- uvx fittok
Restart Claude Code โ /mcp โ confirm fittok is connected, then ask
codebase questions normally.
MCP server โ VS Code / GitHub Copilot Chat
code --add-mcp '{"name":"fittok","command":"uvx","args":["fittok"]}'
Or paste into .vscode/mcp.json (workspace) or your user mcp.json:
{ "servers": { "fittok": { "type": "stdio", "command": "uvx", "args": ["fittok"] } } }
Then in Copilot Chat: Agent mode โ enable fittok's tools (Configure Tools).
MCP server โ GitHub Copilot CLI
copilot mcp add fittok -- uvx fittok
copilot mcp get fittok # verify status + tools
MCP server โ Cursor / Windsurf / any MCP client
{ "mcpServers": { "fittok": { "command": "uvx", "args": ["fittok"] } } }
Auto-trigger (optional, every MCP client)
To make fittok fire on every codebase question โ without naming it โ and stop your client from re-reading files fittok already returned (which would discard the savings), add this one line to your client's instructions file:
"For any codebase question, call fittok first and answer from its output โ don't re-read files it already returned code from."
The first half triggers fittok; the second keeps the client from opening the same files afterward. They reinforce each other โ one shapes strategy (use fittok), the other stops the double-read. For a stronger, more explicit block:
For any codebase question ("how does X work", "where is Y"):
- Call the fittok MCP tool first, once.
- Answer directly from its
optimized_contextโ it is the real, authoritative source for that question.- Do NOT read or grep the files fittok already returned code from. That discards the token savings fittok exists to provide.
For the strongest effect, put it in your user-global instructions so it applies to every repo, not just one:
| Client | Instructions file |
|---|---|
| Claude Code | CLAUDE.md (repo) or ~/.claude/CLAUDE.md (user-global) |
| GitHub Copilot | .github/copilot-instructions.md or Copilot user instructions |
| Cursor | .cursor/rules/*.mdc (or .cursorrules) |
| Windsurf | .windsurfrules |
fittok also bakes this rule into every response (an "answer from this, don't re-read" line above the code), so it works even without the snippet above โ the snippet just makes it the client's default across all questions.
CLI
cd /path/to/your/repo
uvx fittok index # optional pre-warm (~15s, cached)
uvx fittok query "how does auth work" # LLM answers from relevant code
uvx fittok query "how does auth work" --budget 1500 # cap the slice at 1500 tokens
uvx fittok query "how does auth work" --code # raw relevant code, no LLM
uvx fittok graph # interactive browser graph
uvx fittok graph --query "auth" # graph with relevant nodes highlighted
query sends the relevant code slice to an LLM and streams the answer.
Set one key in your shell and it just works:
export ANTHROPIC_API_KEY="sk-ant-..." # โ claude-haiku-4-5 (recommended)
export OPENAI_API_KEY="sk-..." # โ gpt-4o-mini (fallback)
Users of Claude Code already have ANTHROPIC_API_KEY set โ no extra step needed.
If neither key is set, fittok falls back to --code and prints a setup hint.
graph requires pyvis: uv pip install "fittok[ui]".
Python library
uv add fittok # in a uv project (or: uv pip install fittok in a venv)
from fittok import optimize
result = optimize("/path/to/repo", "how does authentication work", token_budget=1500)
print(result["optimized_context"]) # the relevant code slice
print(result["savings"]) # token reduction stats
Upgrading
uvx caches the environment, so a new fittok release isn't picked up
automatically โ server restarts reuse the cached version. Upgrade with one
command (no need to re-register the MCP server):
uvx --refresh fittok # re-resolve from PyPI โ latest version
Then restart the MCP server (reload the window in VS Code, or restart the Copilot CLI) so it boots the new version. For other runtimes:
- pip:
python -m pip install --upgrade fittok - pipx:
pipx upgrade fittok
Why tree-sitter, not LSP?
fittok uses tree-sitter (fast, syntactic AST parsing) instead of LSP (Language Server Protocol โ semantic analysis with types, cross-file references, go-to-definition). This is a deliberate tradeoff:
| tree-sitter (fittok) | LSP (e.g. Serena MCP) | |
|---|---|---|
| What it returns | Actual source code โ the model answers directly | Symbol metadata (names, refs, types) โ the model must still Read files |
| Setup | Zero config, works on any directory | Needs language servers installed + project config (tsconfig, pyproject, etc.) |
| Languages | 8 out of the box (Python, JS/TS/TSX, Java, Go, Rust) | Only as many as LSP servers you've installed |
| Startup | ~15s (parse + embed) | Minutes (full project indexing per language) |
| Memory | ~100 MB (graph + embeddings) | 500 MB+ per language server |
| Model calls per question | 1โ5 (one-shot retrieval) | 5โ20+ (iterative symbol navigation) |
| Token cost | ~2,500 tokens (code delivered directly) | ~15,000+ tokens (metadata + file reads) |
fittok's USP is token savings. It returns the actual code in one call so the model doesn't need to read files. LSP-based tools return metadata (symbol names, reference lists) โ precise, but the model still has to open the files to see the implementation. More round-trips, more tokens.
The tradeoff: tree-sitter can't resolve cross-file references as accurately as LSP (a fetch("/api/run") call in a .tsx file won't perfectly link to the route handler). fittok compensates with 4-signal retrieval (semantic + content-BM25 + structural summary-BM25 + PageRank, fused via RRF) and round-robin directory diversity โ which cover the gap in practice.
Complementary, not competitive: LSP-based tools like Serena excel at symbol-level navigation ("find all callers of runSandboxQuery"). fittok excels at semantic retrieval ("how does SQL execution work?"). Install both โ the model picks the right tool per task.
Token savings โ honest numbers
On a real Next.js/TS repo (~5k functions), fittok returns a ~1.5โ3.5k-token
slice instead of the model reading 15โ20k+ tokens of files โ an ~80โ90%
reduction on input, deterministic and reported in the savings footer.
On Opus 4.8, a broad question cost ~84k total tokens without fittok vs ~27k with it โ because fittok replaced a 58k-token Explore subagent with one tool call.
How to measure it honestly:
- Use the
๐ช saved X%footer or your API bill (total tokens). - Do not judge by Claude Code's
/contextMessages number โ it excludes subagent tokens and is dominated by model reasoning, which fittok doesn't touch.
Configuration
| Variable | Default | Description |
|---|---|---|
ANTHROPIC_API_KEY |
โ | Enables LLM answers via claude-haiku-4-5 |
OPENAI_API_KEY |
โ | Fallback LLM via gpt-4o-mini |
FITTOK_SHOW_SAVINGS |
true |
๐ช saved X% footer on MCP answers; set false to disable |
FITTOK_AUTOWATCH |
true |
Auto-start the file watcher so graph updates are incremental (only changed files re-parse); set false to fall back to full re-parse on edits |
FITTOK_EMBED_MODEL |
all-MiniLM-L6-v2 |
Embedding model |
FITTOK_DEVICE |
auto |
auto / cuda / mps / cpu |
FITTOK_CACHE_DIR |
~/.cache/fittok |
Cache location |
Full reference: docs/HANDBOOK.md
Requirements
Python โฅ 3.10. First run downloads a ~90 MB embedding model. Graph visualization
(fittok graph) is included by default. Optional extras:
uv pip install "fittok[ui]"โ Gradio web dashboard (launch_uitool)uv pip install "fittok[gpu]"โ torch/CUDA for GPU-accelerated embeddings
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fittok-0.9.3.tar.gz.
File metadata
- Download URL: fittok-0.9.3.tar.gz
- Upload date:
- Size: 254.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d0b610c2464b3ebd1e74d053173c22abd1ee662b2ff6b07d180c27d1b3b9db6
|
|
| MD5 |
e80c2dc1750a4da3eb0c2c7beec0865b
|
|
| BLAKE2b-256 |
813254245c7b48f2b0959bf11d008f9dada36d68b5f3e67f743f77dbc12aacc2
|
File details
Details for the file fittok-0.9.3-py3-none-any.whl.
File metadata
- Download URL: fittok-0.9.3-py3-none-any.whl
- Upload date:
- Size: 69.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
becdfcc8824e06dd9483817494c257f3ed202c549ac476a096b061aa64876007
|
|
| MD5 |
11f5c686b824f6c51a053a19410c62cd
|
|
| BLAKE2b-256 |
b1c7b39b4dbba5b43317a985f351caca638acdc1b1b4cb3b2658bc0a648df97e
|