Codebase-memory-mcp: Reducing Agent Token Usage via Knowledge Graphs

Indie Hacker Newsgo watch the original →

Codebase-memory-mcp uses a C-based knowledge graph to index repositories, allowing AI agents to perform structural queries without brute-force file reading, resulting in significant token savings.

The Breakthrough

Codebase-memory-mcp replaces standard file-by-file grep operations in AI coding agents with a persistent knowledge graph, allowing agents to resolve structural code relationships in under a millisecond while reducing token consumption by up to 120x on structural queries.

What Actually Worked

  • The tool utilizes Tree-sitter to parse code into nodes and edges, mapping functions, classes, call chains, and HTTP routes across 158 supported languages.
  • It implements a hybrid LSP written in C to resolve function calls by type rather than simple name matching, ensuring higher accuracy than standard vector-based embeddings.
  • The system exposes 14 specific tools to MCP-compatible agents, including a trace-path tool for navigating call graphs and a detect-changes tool for calculating the blast radius of git diffs.
  • It ships with a local, offline code-embedding model compiled directly into the static binary, enabling semantic search without external API keys or cloud dependencies.
  • The architecture supports cross-repository indexing and links HTTP routes to backend code, allowing agents to trace execution flows that span multiple services.

Before / After

  • Token usage: 3,400 tokens (graph-based) vs. 412,000 tokens (file-by-file grep) on a batch of structural queries.
  • Indexing speed: 28 million lines of code (Linux kernel) indexed in 3 minutes.
  • Answer quality: 83% accuracy (graph-based) vs. 92% accuracy (brute-force reading) as reported in the project's academic paper.

Context

AI coding agents typically rely on repeated file reading or vector search to understand a codebase, which is both token-intensive and structurally imprecise. Codebase-memory-mcp addresses this by creating a persistent, local structural map of the repository. By hooking into the agent's existing workflow as an MCP server, it intercepts queries and provides structured data, effectively acting as a memory layer that prevents the agent from needing to rediscover the codebase for every request.

Notable Quotes

  • "The most expensive thing your coding agent does is rediscover your codebase from scratch on every question, and somebody just built the memory layer that fixes it in a single file of C."

Content References

  • #ai
  • #dev-tooling
  • #mcp

summary by google/gemini-3.1-flash-lite. probably wrong about something. check the source.