DEV Community

VANSH ARORA
VANSH ARORA

Posted on

What the TokenCap MCP Server Gives Your Agent

TokenCap runs as a local MCP server over stdio. Your editor or agent host starts tokencap serve --mcp, and the agent can ask for ranked files, dependencies, impact and repository rules instead of rediscovering the codebase every session. Nothing leaves the machine: no upload, no LLM call, no API key.

This post covers how that flow behaves on first contact and in daily use, including the parts that used to break.

1. initialize is answered at once

The server used to build repository intelligence before it answered the host. On a large repository that took longer than the host was willing to wait, and the first run looked like a dead server.

Now the server answers initialize immediately and runs the build in the background, then starts its file watcher. Until the build finishes:

  • tools that need intelligence return status: "building"
  • results served from earlier intelligence carry stale: true

The build runs detached and logs to .tokencap/mcp/initial-build.log, so it completes even if the host stops the server. A failed initial build is not fatal: the server keeps serving whatever intelligence exists, and the file watcher still starts.

No separate tokencap make step is needed before using MCP.

2. Responses say how fresh the index is

tokencap make writes a build record (.tokencap/build.json: commit, time, layers built or failed, files indexed and skipped). One classifier reads it and reports the index as fresh, behind, dirty, incomplete or missing.

When the index is anything other than fresh, the MCP response carries an index field with the state, a short detail and the command that fixes it. tokencap_mcp_status and the overview report the same state. The server picks up new builds on its own, so there is no "restart the server" step after a make.

3. Two servers, one repository

Open the same repository in two windows and you get two MCP servers. The second one used to exit when it found the build lock taken.

Now it waits for the running build and keeps serving. The same applies when you run tokencap make by hand while the server is up. A lock held for more than 10 minutes fails with the holder's pid and the lock path, so a stuck build is something you can find and kill.

4. tokencap_files ranks like tokencap ask

tokencap_files takes a query and returns ranked task-relevant files:

{ "query": "login redirect bug" }
Enter fullscreen mode Exit fullscreen mode

It uses the same retrieval as task packs. tokencap ask "login redirect bug" in a terminal and tokencap_files from the agent agree on file order and scores. The first call in a session builds the graph from the working tree.

Tool responses are capped at about 15k tokens. A larger one is shortened, and the full copy is saved under .tokencap/mcp/cut/ and named in restore.path, so the agent can read the rest if it needs it.

5. --init writes where the host reads

tokencap serve --mcp --init                       # detect the host
tokencap serve --mcp --init --client claude-code  # or pick one
Enter fullscreen mode Exit fullscreen mode
✓ claude-code config written to .mcp.json
  Start the server: tokencap serve --mcp
Enter fullscreen mode Exit fullscreen mode
Host File written
Claude Code .mcp.json (project root)
Cursor .cursor/mcp.json
VS Code .vscode/mcp.json
Codex .codex/config.toml
Claude Desktop per-user claude_desktop_config.json
Windsurf per-user ~/.codeium/windsurf/mcp_config.json

The Claude Code row was a fix. An older build wrote .claude/mcp.json, which Claude Code does not read. Claude Desktop and Windsurf only read per-user files shared by every project, so their entry adds --root <repo>.

Running --init again does not clobber your setup:

✓ claude-code config .mcp.json already has a tokencap server; kept it (--force replaces it)
Enter fullscreen mode Exit fullscreen mode

An existing tokencap entry is kept as it is, including a custom command, --root or env. Other servers in the file are kept too.

It also reads real-world config files. A VS Code mcp.json that starts with a byte-order mark and has // comments and trailing commas gets the tokencap entry added, and the comments stay. Invalid JSON is refused rather than replaced. The command exits 1 when no host is detected, --client is unknown, or the write fails, so scripts can tell.

Codex only loads workspace MCP configuration for a trusted workspace. Trust it and reload Codex after --init.

Smaller fixes

  • tokencap_review refuses base refs that look like git options and reports an unknown base ref as an error instead of "No changes detected".
  • The final redaction pass keeps the JSON envelope intact and catches escaped secrets.
  • Error hints name commands that exist.

Check it

tokencap serve --mcp --tools    # list endpoints
tokencap serve --mcp --test     # exercise every endpoint
tokencap serve --mcp --health   # one-shot health check
Enter fullscreen mode Exit fullscreen mode

From inside the host, tokencap_mcp_status reports uptime, cache hit rate, per-endpoint call counts, the log location and the index state.

The MCP guide and tool contract are at tokencap.vansharora.app

Top comments (0)