cppgraph
Analyzes C++ source code with libclang to build a call graph, enabling symbol search, node details, callers/callees, call paths, impact analysis, and context packages.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
cppgraph
在 python.exe 进程内用 libclang 构建 C++ 调用图,并通过标准 MCP(stdio)提供给 Claude Code、Codex、DeepSeek Harness。为“只有 Python 等进程被 DLP 透明解密”的 环境设计:不启动 node/clang/cmake/git/rg 等外部程序,不生成明文源码副本。
为什么不用 CodeGraph / graphify
CodeGraph 是 Node 程序,在只放行
python.exe的加密环境里读到的是密文。graphify 是 Python 程序,能读加密代码,证明「Python 进程内解析 + MCP」可行;但它的 C++ 解析是词法级的(
cpp_resolution.py自述:dependent types、alias templates、 transitive using、template specializations 都不解析),调用链精度不够。本项目用
compile_commands.json+ libclang:符号用 USR 跨 TU 对齐,重载、命名空间、 虚函数、跨文件调用都由 Clang 语义分析给出。
Related MCP server: code-auditor-mcp
文档
从安装到卸载的使用教程(docs/TUTORIAL.md) — 单文件 wheel / 源码目录安装、 初始化、查询、MCP 接入、交互式向导、完整卸载流程、常见问题。
完整使用教程(docs/USAGE.md) — 安装、compile_commands、索引、 查询命令详解、三客户端接入、加密机部署清单、排错、性能与安全。
能力
工具 | 作用 |
| 按名字搜索符号 |
| 符号定义、签名、直接上下游; |
| 谁调用了它(上游,含虚函数族) |
| 它调用了谁(下游) |
| 紧凑调用树(上游或下游) |
| 两点间逐跳调用路径 |
| 改动影响面:调用者、虚函数改写者、头文件重编 TU |
| 给 AI 的 token 预算上下文包(源码片段 + 调用关系) |
| 派生/实现某类的类型,或虚函数改写者 |
| 改文件影响的测试/依赖/TU(include 图 + 测试路径启发式) |
| 头文件循环包含 |
| 列出无法解析目标的调用点(file:line + TU),审计用 |
| 索引状态与待同步文件 |
CLI 同名命令均可直接使用。
单个函数的上下游用
cppgraph_callers/cppgraph_callees/cppgraph_call_tree;cppgraph_path需要 from + to,用于 A→B 链路。叶子函数的下游为空是正常结果。显示层说明:回调/RTOS 任务入口显示为
[registered];只有声明没有定义的符号标[decl]并提示检查 compile DB;call_tree受--max-nodes/--max-children限制,截断会明确写出“not exhaustive”,完整下游请用cppgraph_callees --depth N。 无法解析的调用用cppgraph unresolved审计。cppgraph_path也会遍历[registered]边(与 callers/call_tree/impact 一致), 因此“任务入口 → 业务函数”的链路可以直接查;输出会标[registered]。
安装
python -m pip install libclang
# 可选:把本项目安装成命令
python -m pip install -e .单文件 wheel(内置 libclang,Windows x64)
如果不想在新电脑上单独装 libclang,可以构建一个内置 clang 绑定 + libclang.dll 的 wheel:
:: 开发机需要先装 libclang(用来复制原生库)
python -m pip install libclang
python scriptsuild_bundled_wheel.py
:: 产物: dist\cppgraph-0.1.4-py3-none-win_amd64.whl(约 26 MB)新电脑只需要:
python -m pip install cppgraph-0.1.4-py3-none-win_amd64.whl
cppgraph -h不需要再装 libclang;wheel 内含
clang绑定和libclang 18.1.1DLL;仅 Windows x64 + Python 3.11+;Linux/macOS 需在对应平台重新构建;
如果机器上已装
libclangPyPI 包,建议先pip uninstall -y libclang,避免顶层clang包冲突;wheel 只含库/CLI/MCP,不含
scripts/、一键配置.cmd、docs/;需要向导就复制整个目录;LICENSE+LICENSE-libclang(LLVM)随 wheel 一起分发。
MCP 服务是零依赖的标准 stdio JSON-RPC 实现,不需要 mcp 包。
(mcp 2.x 改了 FastMCP API;内置实现更可控,也方便离线部署。)
加密机上请使用被 DLP 放行的那个 python.exe(与运行 graphify 的解释器相同最稳)。
项目内命令(graphify 风格)
安装后(pip install -e .)在项目根目录直接用。命令格式:
cppgraph <子命令> [选项] [位置参数]cd /d D:\proj
cppgraph init --compile-db D:\proj\build :: 或 --keil / --auto
cppgraph check
cppgraph index
cppgraph status
cppgraph agent --local :: 当前项目
cppgraph agent --global :: 全局(固定 DB)init 会写 <project>\.cppgraph\config.json(compile DB、索引路径、exclude、jobs、sample);
check / index / status / agent / install 会自动读取它,不需要每次再带参数。
编译库在项目外:
cppgraph init --compile-db "D:\trunk\cmake\build"(目录或 JSON 均可);Keil 工程:
cppgraph init --keil;不知道编译库在哪:
cppgraph init --auto --index,自动搜索compile_commands.json/.uvprojx,多个会提示选择;重新初始化:
cppgraph init --force;找不到
cppgraph命令:把<Python>\Scripts加进 PATH,或改用python -m cppgraph ...。
MCP 配置 scope:agent --local / --global
cd /d D:\proj
cppgraph agent --local --print :: 预览
cppgraph agent --local :: 交互式选择客户端
cppgraph agent --global :: 全局(固定 DB)
cppgraph agent --local --client claude
cppgraph agent --local --client allcppgraph agent 不再默认注册所有客户端,而是显示二级菜单:
1. Claude Code
2. Codex
3. DeepSeek Harness
4. 全部
0. 取消可多选,例如 1,3。非交互环境必须显式传 --client claude|codex|dsh|all。
--local:Claude 使用 local scope(项目私有、不进 Git);Codex/DSH 没有项目级 MCP 配置,会写一个全局条目但 不带--db,MCP 服务从客户端当前目录向上找.cppgraph/config.json。要求 Codex/DSH 在项目目录启动;否则用--global。--global:Claude 使用 user scope;Codex/DSH 写显式固定 DB。适合一台机器只分析 一个项目。cppgraph install仍然保留:Claude 写项目.mcp.json(project scope),Codex/DSH 写固定 DB。两种方式选一种,别同时配。
兼容 CodeGraph 的常用命令名
CodeGraph | cppgraph |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 同名 |
|
|
|
|
|
|
|
|
— |
|
|
|
|
|
| `cppgraph uninstall --client claude |
|
|
还没做的:daemon、unlock、telemetry、upgrade,以及更多客户端的 install。
快速开始
# 0) 环境/解密/解析自检(sample 指向加密区里的一个真实源文件)
python -m cppgraph doctor --project D:\proj ^
--sample D:\proj\src\a.cpp --compile-db D:\proj\build
# 1) 建索引(compile-db 可以是目录或 compile_commands.json)
python -m cppgraph index --project D:\proj --compile-db D:\proj\build --jobs 4
# 2) 查询
python -m cppgraph status --project D:\proj
python -m cppgraph search Scope_Init --project D:\proj
python -m cppgraph callers Scope_Init --project D:\proj
python -m cppgraph callees Derived::f --project D:\proj
python -m cppgraph path main HAL_NVIC_EnableIRQ --project D:\proj
python -m cppgraph impact Scope_TimerTick --project D:\proj
python -m cppgraph context "scope initialization" --project D:\proj索引默认写到 <project>/.cppgraph/graph.db,目录内自带 .gitignore。
增量:.cpp 变更只重解析该 TU;头文件变更会重解析所有(递归)包含它的 TU。
Keil 工程(.uvprojx)
没有 CMake 的 Keil 工程可以先从 .uvprojx 生成编译库:
python -m cppgraph keil --project D:\proj
python -m cppgraph index --project D:\proj ^
--compile-db D:\proj\.cppgraph\compile_commands.json
:: 或一键(生成编译库 + doctor + 索引 + MCP 自测)
scripts\setup.cmd -Project D:\proj -Keil -Clients print生成器会读取 target 的 include/define/源文件列表,自动处理:
__CC_ARM 宏剥离、--target=arm-none-eabi、ARM GCC sysroot 头注入、
以及 Keil include 列表遗漏的头文件目录兜底。UTF-16 BOM 源码会被自动转成 UTF-8
交给 libclang(文件路径/行号保持不变)。
GBK / GB18030 工程:
cppgraph init --source-encoding gbk --index --jobs 4
:: 或已有项目,写入配置后后续 index 自动沿用
cppgraph index --source-encoding gbk --jobs 4UTF-8 文件会优先按 UTF-8 解析,不会被 GBK 误转码;只有非法 UTF-8 的旧编码文件才转码。
交互式向导(推荐给非技术同事)
双击根目录的 一键配置.cmd,或把项目文件夹拖到它上面,按提示回答 6 个问题:
项目目录
Python(自动找,直接回车即可)
编译信息(自动识别 Keil
.uvprojx/ CMake / 已有compile_commands.json)建索引(快速更新 / 全部重建 / 跳过)
客户端(默认只打印;选择 2/3/4/5 才写入 Claude/Codex/DSH)
确认后执行
默认不修改任何客户端;日志写在工具目录的 wizard-log-*.txt。
高级/自动化:powershell -File scripts\wizard.ps1 -Project D:\proj -Defaults
(-Defaults 用默认答案:已有编译信息、快速更新、只打印、不暂停)。
一键脚本(Windows)
把「装依赖 → 找 compile_commands → doctor 自检 → 建索引 → MCP 自测 → 写客户端配置」 收敛成一个脚本:
:: 只打印三客户端配置(默认,不写任何配置)
scripts\setup.cmd -Project D:\proj -Clients print
:: 真正写入 Claude/Codex/DSH 配置(-Yes 跳过确认)
scripts\setup.cmd -Project D:\proj -Clients all -Yes
:: 先看会执行哪些命令,不做任何修改
scripts\setup.cmd -Project D:\proj -DryRun
:: 指定被 DLP 放行的 Python / 离线 wheels / 没有 compile DB 时用 CMake 生成
scripts\setup.cmd -Project D:\proj -Python "C:\path\python.exe" ^
-Wheels .\wheels -GenerateCompileDb
:: Keil 工程:从 .uvprojx 生成编译库后继续
scripts\setup.cmd -Project D:\proj -Keil -Clients print默认 -Clients print;写入配置需显式指定 claude/codex/dsh/all(加 -Yes 跳过确认)。
索引路径默认 <project>\.cppgraph\graph.db,自定义用 -IndexDb。完整参数见
使用教程 14 节。
接入三个客户端
统一入口:
# 只打印配置,不写文件
python -m cppgraph install --client print --project D:\proj
# 自动写入已检测到的客户端
python -m cppgraph install --client all --project D:\proj
# 单个客户端
python -m cppgraph install --client claude --project D:\proj
python -m cppgraph install --client codex --project D:\proj
python -m cppgraph install --client dsh --project D:\projClaude Code
写入 <project>/.mcp.json:
{
"mcpServers": {
"cppgraph": {
"type": "stdio",
"command": "<被放行的 python.exe>",
"args": ["-m", "cppgraph.mcp_server", "--db", "<project>/.cppgraph/graph.db"]
}
}
}或:claude mcp add --transport stdio cppgraph -- <python> -m cppgraph.mcp_server --db <db>。
Codex
codex mcp add cppgraph -- <python> -m cppgraph.mcp_server --db <db>等价于 ~/.codex/config.toml:
[mcp_servers.cppgraph]
command = "<python>"
args = ["-m", "cppgraph.mcp_server", "--db", "<db>"]DeepSeek Harness (dsh)
写入 $DSH_HOME/cordis.patch.yml(默认 ~/.dsh),并可选在 $DSH_HOME/AGENTS.md
追加使用说明:
- insert:
- id: mcp-cppgraph
name: '@deepseek-ai/dsh-mcp-client'
config:
serverName: cppgraph
transport: stdio
command: "<python>"
args: ["-m", "cppgraph.mcp_server", "--db", "<db>"]前置条件:该 dsh profile 需已安装插件
dsh plugin --profile <name> add @deepseek-ai/dsh-mcp-client。
客户端本身不需要解密权限:它只通过 stdio 收发 JSON,真正读文件的是 MCP 服务进程 (
python.exe)。
加密机部署
把整个
CodeReview目录复制到加密机(文件夹被 DLP 加密不影响 Python 读取)。确认 Python 能读到明文:
python -m cppgraph doctor --project <repo> --sample <repo>/src/a.cpp --compile-db <repo>/build检查:
NUL bytes: 0、parse: OK、libclang load: OK。依赖离线安装(如不能联网):
# 开发机 python -m pip download -d wheels libclang # 加密机 python -m pip install --no-index --find-links wheels libclang建索引 →
install --client all→ 重启客户端。索引库放在项目内受保护目录,不要写
%TEMP%。
实现要点
compile_commands.json 自己解析:支持
command字符串与arguments数组;Windows 引号/反斜杠;MSVC(/I、/D、/std:、/EHsc、/GR)与 GCC/Clang 参数归一化。Keil 工程:
cppgraph keil从.uvprojx生成编译库(include/define/源文件), 剥离__CC_ARM、注入 ARM GCC sysroot、对 Keil include 列表遗漏的头目录做兜底。源码编码:UTF-8 默认;GBK / GB18030 用
--source-encoding gbk或项目配置启用; UTF-16 BOM 源码(Keil 常见)自动转 UTF-8 后通过unsaved_files交给 libclang, 路径与行号保持不变。大项目稳定性:配置旧编码时仍优先按 UTF-8 解析;只有非法 UTF-8 文件才转码; 单文件继续受
--max-file-mb限制;解码失败只影响该 TU,不中断全局索引。交叉编译:从编译器名或
--target=推导 triple(如arm-none-eabi-g++→arm-none-eabi),并注入 GCC sysroot 头目录(CMake 的 compile DB 通常不包含它们)。USR 主键:
get_usr()在声明/定义、跨 TU、重载之间稳定;无需自己写名字解析。边:
calls(referenced)、inherits(CXX_BASE_SPECIFIER)、includes(tu.get_includes());overrides在查询层按“类层次 + 同名虚函数”推断并标注。过滤:只索引项目内文件;不递归系统/第三方头;单文件大小上限;跳过含 NUL 的密文 (UTF-16 BOM 源码除外,会先转 UTF-8)。
并行/增量:
ProcessPoolExecutor(worker 也是python.exe),主进程单写 SQLite;node_tu/edges.tu支持按 TU 替换;includes递归 CTE 找头文件依赖。MCP:内置 stdio JSON-RPC(newline-delimited),实现
initialize、tools/list、tools/call、ping;无第三方依赖。
已知限制
宏展开产生的调用、函数指针/
std::function的间接调用:referenced为空,记入unresolved_calls,不猜测。UCOS/FreeRTOS 的
OSTaskCreate/xTaskCreate通过函数指针传任务入口,静态图不会自动把main连到任务函数;请从任务函数本身查询(如path MainTask GUI_Init)。模板特化/实例:按 USR 去重,准确性取决于 Clang;复杂模板可能多记/少记。
虚函数:PyPI 的 libclang 绑定未暴露
clang_getOverriddenCursors,本项目用 “显式继承关系 + 同名虚函数”推断,输出标[virtual];同名非虚重载可能被过度包含。ARM GCC 的 CMSIS 头会用到 Clang 不认识的 GCC 内建函数,产生诊断;
PARSE_INCOMPLETE仍能生成有效 AST。真实工程请以实际诊断数为准。不是编译器:不做代码生成/完整类型检查;超大仓库建议用
--exclude排除第三方。
安全模型
所有源码读取发生在本进程(
python.exe);libclang.dll同进程加载,无子进程。不调用外部 exe,不联网,无遥测。
数据库只存签名与行号区间,不存函数体;源码片段在查询时按需读取。
读到 NUL 字节视为密文/二进制并跳过;解析失败不会覆盖旧索引。
测试
python -m pytest -q --basetemp .pytest-tmp覆盖:compile DB 归一化(MSVC/ARM GCC/arguments)、跨 TU 调用、虚函数族、调用路径、
增量(头文件/源文件)、GBK 源码片段、MCP 握手与 tools/call。
本机实测:STM32F407_LED 26 个 TU(ARM GCC 14.3.1、-std=gnu++17)索引耗时约
2.8 秒,1577 节点 / 897 条调用边;main → Scope_Init → TimerInit → HAL_NVIC_EnableIRQ
路径可完整还原。
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
Hosted code graph over MCP: exact callers, dependencies, and cross-repo blast radius for AI agents.
Ask a codebase what calls what: search, blast radius, paths between symbols, and diffs.
Shared context for your codebase: a hosted code knowledge graph over MCP. Free plan, OAuth sign-up.
The Cortex MCP server provides read-only access to real-time engineering context from the Cortex developer portal, allowing AI coding assistants to answer natural language questions about your organization's catalog (microservices, libraries, domains, teams, infrastructure), scorecards (engineering standards and best practices), initiatives (goals and deadlines), and Engineering Intelligence metrics. It includes tools for querying documentation, tracking personal entities, and accessing AI-assisted insights across the entire Cortex ecosystem.
Related MCP Servers
- FlicenseNot gradedqualityCmaintenanceBuilds a semantic knowledge graph of C++ code and exposes 9 MCP tools for AI assistants to search classes, functions, inheritance, callers, callees, overrides, and more.3-
- AlicenseAqualityBmaintenanceEnables AI assistants to search, analyze, and understand multi-language codebases by providing indexed code intelligence via MCP.161,013 npm8MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI coding agents to query a local, continuously updated symbol graph of a codebase, providing ranked search, caller/callee exploration, dependency paths, and git-diff impact analysis through MCP tools.9 npmApache 2.0
- AlicenseNot gradedqualityCmaintenanceEnables AI coding agents to navigate and understand entire codebases through symbol-aware semantic retrieval and relational call graph traversal, supporting structural impact analysis and code modification workflows over MCP.Apache 2.0