Cgraphy

io.github.pmgarg由 GitHub 账号 pmgarg 发布v0.1.1更新于 Oct 25, 2026

Code knowledge graph over MCP: token-budgeted context for AI coding agents.

已验证STDIO仅桌面知识与记忆开发者工具

概览

AI 生成的概览

一个本地代码知识图谱服务器,为 AI 编程助手索引代码库,并按 token 预算提供符号、依赖与变更上下文。

可以做什么
通过仓库总览图了解子系统、关键符号和文件,并按排序的词法或可选语义匹配搜索符号,返回文件与行号位置及摘要。按 token 预算获取某个符号周围的子图,包括调用方、被调用方、导入和共同变更,或读取单个符号带行号的源码。评估改动的波及范围,包括传递依赖、受影响的测试和历史共同变更的文件,并把工作区 git diff 映射到涉及的符号和覆盖测试。通过获取需要一句话摘要的符号批次并保存助手撰写的摘要来丰富图谱。
适用场景
适合助手在较大代码库中工作、需要快速定位、查找符号并判断修改影响,而不必每次提示都读取大量文件的场景。最适合能够被引导在批量读文件前先查询图谱的助手。
运行要求
作为本地进程从 PyPI 安装(pip 或 uv)运行,需要 Python,并在 serve 命令中传入仓库路径。索引在本地写入 .cgraphy 目录下的 SQLite 文件。可选语义搜索需要额外安装包;可选的 CI 摘要生成需要 Anthropic API 密钥。
安装前请注意
init 命令会写入项目级 MCP 配置,并向 CLAUDE.md 和 AGENTS.md 追加引导内容,从而修改仓库中的文件。图谱保存在本地,检索遥测数据留在本地 SQLite 文件中。可选的 CI 摘要生成会使用 ANTHROPIC_API_KEY 将代码发送给 Anthropic。

安装

在 SourceWeft 中

  1. 打开 控制台中的 Cgraphy,将其添加到工作区。
  2. 为需要使用其工具的对话启用该服务。

Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。

其他 MCP 客户端

参照 仓库 中的启动说明。

README

cgraphy – Code Knowledge Graph MCP Server

[PyPI version] [Python versions] [License] [DOI]

Paper (DOI): https://doi.org/10.5281/zenodo.21422935
PyPI: https://pypi.org/project/cgraphy/
GitHub: https://github.com/pmgarg/cgraphy

cgraphy is a Python code knowledge graph and Model Context Protocol (MCP) server for AI coding agents such as Claude Code, Codex CLI, Cursor, and Gemini CLI.

cgraphy indexes any codebase into a knowledge graph — functions, classes and files as nodes; calls, imports, inheritance and git co-change history as edges — and serves compact, token-budgeted slices of it to AI assistants through the Model Context Protocol. Instead of re-reading dozens of files to orient itself on every prompt, an agent asks the graph and gets the relevant subgraph in a couple of thousand tokens.

  • Any language. Full-fidelity extraction (calls, imports, inheritance) for Python, TypeScript/JavaScript, Java, Go, C, C++ and Rust; generic definition-level extraction for 20+ more via tree-sitter; config and docs files participate through summaries.
  • Importance-ranked. PageRank over the code graph puts load-bearing symbols first in every answer.
  • Token-budgeted. cgraphy_context expands the graph greedily around a symbol and stops exactly at your token budget — cost scales with the question, not the repo.
  • Git-aware. --git-history mines commit history for files that change together (logical coupling), an edge type static analysis can't see.
  • No API key. Semantic summaries are written by the host agent itself through the enrich loop; summaries survive re-indexing via content hashing.
  • Zero infrastructure. One SQLite file in .cgraphy/. No services, no daemons, no vector database.
  • Proven end-to-end. In 400+ controlled agent runs on SWE-bench Lite, the deployed configuration resolved 14 vs 8 of 57 real GitHub issues (official Docker harness). Indexes kubernetes (26K files, 219K nodes) in 49s, keeps it fresh in 1.6s cycles, answers queries in 1–152ms. All benchmarks and predictions are in this repo (Research paper and benchmarks).

Install

bash
pip install cgraphy        # or: uv tool install cgraphy

Quick start

bash
cd your-repocgraphy init          # one command: MCP config + agent steering + index

cgraphy init does three things:

  1. Writes a project-scoped .mcp.json — picked up automatically by Claude Code in all its forms: CLI, VSCode extension, and the desktop app.
  2. Appends a steering block to CLAUDE.md and AGENTS.md telling agents to consult the graph (cgraphy_overview → cgraphy_search → cgraphy_context) before reading files — this is what makes the graph actually replace bulk file reading. (Agents can't be forced, only steered: instruction files + persuasive tool descriptions + the tools being genuinely faster is the mechanism, and it works.)
  3. Builds the index with git co-change history.

Or register the MCP server manually with your assistant:

Claude Code

bash
claude mcp add cgraphy -- uvx cgraphy serve /path/to/repo

Codex CLI (~/.codex/config.toml)

toml
[mcp_servers.cgraphy]command = "uvx"args = ["cgraphy", "serve", "/path/to/repo"]

Gemini CLI (~/.gemini/settings.json) / Cursor (.cursor/mcp.json)

json
{"mcpServers": {"cgraphy": {"command": "uvx",                            "args": ["cgraphy", "serve", "/path/to/repo"]}}}

The eight tools

Reading / orientation:

ToolReturnsThe agent uses it…
cgraphy_overviewRepo map: subsystems, key symbols by importance, all filesfirst, instead of reading files to orient
cgraphy_searchRanked matches with file:line and summaries (hybrid lexical+semantic when the [semantic] extra is installed)before grep / directory listing
cgraphy_contextSubgraph around a symbol (callers, callees, imports, co-changes) within a token budgetinstead of reading whole files
cgraphy_readJust one symbol's source, line-numbered, budgetedinstead of reading the whole file

Editing / reviewing — the tools that make the graph part of the change loop:

ToolReturnsThe agent uses it…
cgraphy_impactBlast radius: direct + transitive dependents, affected tests, historically co-changed filesbefore modifying shared code
cgraphy_diff_contextThe working git diff mapped to touched symbols, their users, and covering testsbefore committing / when resuming work

Enrichment:

ToolReturnsThe agent uses it…
cgraphy_enrichBatch of symbols that still need one-line summarieswhen asked to "enrich the graph"
cgraphy_store_summariesConfirmation + remaining countto save the summaries it wrote

Retrieval is usage-aware: symbols an agent repeatedly asks about get a small, capped boost in future context expansion (telemetry stays in the local SQLite file; nothing leaves your machine).

Semantic search (optional)

bash
pip install "cgraphy[semantic]"

Adds tiny static embeddings (model2vec, CPU-only, no torch) fused with FTS5 by reciprocal-rank fusion — closes the vocabulary gap between issue-style prose ("login broken") and code identifiers (validate_jwt).

The graph self-heals: tools detect stale files and re-index incrementally (changed files only) before answering.

Enriching the graph

Structure is extracted automatically; meaning comes from summaries. Tell your agent once:

enrich the cgraphy graph

It will loop cgraphy_enrich → cgraphy_store_summaries until every symbol has a one-line semantic summary. Summaries are keyed to a hash of each symbol's source, so editing one function invalidates only that summary.

For CI, cgraphy index --summarize pre-bakes summaries with your own Anthropic API key (pip install cgraphy[summarize], ANTHROPIC_API_KEY set).

Viewer

bash
cgraphy view .        # http://localhost:8787

A dependency-free local page (bundled Cytoscape.js): search, color by kind, click for details, double-click to expand neighbors; co-change edges shown dashed.

Measuring the savings

bash
python scripts/benchmark.py /path/to/repo "your question"

Prints the tokens an agent spends orienting via cgraphy (overview + search + context) versus reading every code file, and the reduction factor.

Localization benchmark (research harness)

bash
python scripts/eval_localization.py /path/to/repo 50

Mines fix-like commits from the repo's history (subject = query, touched files = ground truth, co-change mining excludes evaluated commits), then scores an ablation ladder — FTS-only, +PageRank, +graph expansion, ±co-change edges — on hit@5/hit@10/MRR and token cost. No LLM calls, no human grading, fully reproducible. Results and a paper draft live in paper/.

How it works

  1. cgraphy index walks the repo (respecting .gitignore + .cgraphyignore), parses each file with tree-sitter, and stores nodes and edges in .cgraphy/graph.db (SQLite + FTS5). Re-indexing is incremental by content hash.
  2. A resolver links cross-file references (calls, imports, inheritance) by qualified name, best-effort; unresolved names are kept, never dropped.
  3. PageRank runs over the edge graph; every query surfaces important symbols first. Search blends FTS5 relevance with rank.
  4. cgraphy serve exposes the five MCP tools over stdio.
  5. Optional: --git-history adds weighted co-change edges mined from git log.

Design details: docs/superpowers/specs/2026-07-08-cgraphy-design.md

Citing cgraphy

If you use cgraphy in research or tooling, please cite the paper:

bibtex
@misc{garg2026cgraphy,  author = {Garg, Prateek Mohan},  title  = {Which Graph Signals Pay for Their Tokens? cgraphy: A Token-Budgeted            Code Knowledge Graph as a Portable Context Layer for AI Coding Agents},  year   = {2026},  doi    = {10.5281/zenodo.21422935},  url    = {https://doi.org/10.5281/zenodo.21422935},  note   = {Preprint}}

License

MIT

mcp-name: io.github.pmgarg/cgraphy

来源:README.md,提交 4541e4f

工具

0
SourceWeft 尚未在沙箱中启动过该软件包,因此其工具未知。

版本历史

1
  1. v0.1.1最新Sep 16, 2026

评分

已满足适用检查项的 50%

该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。

  • 近 30 天内可连通25 分

    请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。

  • 已声明认证方式10 分

    请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。

  • 已列出工具15 分

    请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。

  • 描述必需15 分
  • 图标10 分
  • 文档15 分
  • 许可证10 分