Skip to main content
Glama

Coverity Semantic Graph (CSG) — Server

基于 Coverity 真实编译捕获的编译器级 C/C++ 代码知识图谱服务端。 为 AI Agent(MCP 协议)与编辑器(LSP 协议)提供"编译器验证过"的代码语义查询, 解决 tree-sitter 系图谱工具拿不到真实编译文件、不理解编译器内置语义的问题。

配套 VSCode 插件见独立仓库:csg-vscode-extension。

架构

cov-build/--dir idir(任意编译器,含 GHS/IAR 模板)
   → csg-index:cov-manage-emit / cov-format-errors 出口解析 → SQLite 图库
   → csg-mcp:FastMCP stdio server,11 个只读查询工具(AI Agent 用)
   → csg-ls:  LSP server(编辑器用,复用同一图库)
  • 查询与索引分离:MCP/LSP 只查预构建库,不起 cov 进程,毫秒级响应

  • CLI 为唯一数据出口:不解析 idir 内部未公开格式

  • 一次建库、多端消费:AI Agent 与 VSCode 插件共享同一份编译器级事实

Related MCP server: Kivgraph

安装

pip install .          # Python ≥ 3.10, mcp>=1,<2

安装后得到 4 个命令:csg-index / csg-mcp / csg-ls / csg-agent-setup。

构建索引

# 真实模式(需 Coverity 许可)
csg-index --idir /path/to/idir --db ./graph.db \
          --coverity-home /opt/coverity --ast-regex '^(main|parse_packet)$'

# 模拟模式(无 Coverity 环境,开发/测试)
python3 tests/mock_data.py mock_idir
csg-index --db ./graph.db --mock mock_idir

接入 AI Agent(MCP)

csg-agent-setup --db ./graph.db --scope user --only claude,codex,opencode
  • Claude Code:写入 ~/.claude.json(user)或 .mcp.json(project)

  • Codex CLI:写入 ~/.codex/config.toml 的 [mcp_servers.csg]

  • OpenCode:写入 ~/.config/opencode/opencode.json 的 mcp 段

也可手动配置:

// Claude Code / .mcp.json
{ "mcpServers": { "csg": { "command": "csg-mcp", "env": { "CSG_DB": "/abs/graph.db" } } } }
# Codex CLI / ~/.codex/config.toml
[mcp_servers.csg]
command = "csg-mcp"
[mcp_servers.csg.env]
CSG_DB = "/abs/graph.db"

接入编辑器(LSP)

CSG_DB=/abs/graph.db csg-ls   # 任何 LSP 客户端均可接入
  • VSCode:直接使用 csg-vscode-extension(已打包 vsix,设置 csg.dbPath 即可)

  • Neovim / 其他编辑器:将 csg-ls 配置为 C/C++ 的 language server

LSP 提供(目标取代内置 C/C++ 插件的语义能力集):

  • 定义/声明跳转、查找引用、悬停(实体+编译器+缺陷数)、调用层级、Coverity 缺陷诊断

  • 符号补全(项目级 FTS 前缀匹配,resolve 显示位置与编译器)

  • 大纲视图(documentSymbol)/ 全局符号搜索(Ctrl+T,workspaceSymbol)

  • 签名帮助(基于已索引的 CodeXM AST 参数表)

MCP 工具(11 个,只读)

工具

功能

list_tus()

真实参与编译的 TU 及完整编译参数

list_entities(file)

文件内全部实体

search_entities(query)

FTS5 符号模糊搜索

get_definition(name)

定义位置

who_calls(fn) / what_calls(fn)

编译器验证的调用边(反向/正向)

blast_radius(fn)

变更影响面(按传播深度分层)

call_path(a, b)

最短调用路径

file_issues(file)

Coverity 缺陷(CID/checker/严重级)

get_ast(fn)

函数级 AST(CodeXM 模型)

status()

索引状态

测试

python3 tests/test_pipeline.py   # 15 项检查:mock 数据→索引→建库→查询→MCP
python3 tests/test_lsp.py        # 12 项检查:definition/references/hover/callHierarchy/诊断

格式校准(接入真实 idir 时必读)

src/csg/coverity.py 中的解析层按 Coverity 官方文档输出格式实现: tus.json(cov-manage-emit list)、callees.json(find '.*' --print-callees)、 issues.json(cov-format-errors --json-output-v10,官方格式)。

--print-callees 原生为文本输出,本项目约定适配为 JSON。接入真实环境时:

  1. 在真实 idir 上运行 cov-manage-emit --dir idir find '.*' --print-callees > sample.txt;

  2. 在 parse_entities 中新增文本解析分支(或写脚本转成适配 JSON);

  3. 对照 build-log.txt 与官方文档校验 --json-output-v10 schema 差异;

  4. CI 中每次 Coverity 升级后重跑 tests/test_pipeline.py + 真实抽样比对。

已知限制

  • --print-codexm 为按函数导出,全程序图需基于 callees/definitions 拼装

  • 深层语义(数据流路径)建议用 CodeXM 自定义检查器补充为额外节点

  • 解析层输出格式依赖 Coverity 版本,升级需回归测试

许可

本项目代码随附于仓库,Coverity 本身为商业软件,使用需自备许可。

Available Tools

11 tools
blast_radiusC

影响面分析:fn 变更会传播影响到哪些函数(按传播深度分层)。

Inputs: fn (required), max_depth. Returns: result

call_pathC

搜索 a 到 b 的最短调用路径。返回调用链数组,无路径返回 null。

Inputs: a (required), b (required), max_depth. Returns: result

file_issuesB

查询指定文件的 Coverity 静态分析缺陷(CID/checker/严重级/位置/描述)。

Inputs: file (required). Returns: result

get_astB

获取函数级 AST(CodeXM 模型 JSON)。需要索引阶段已导出该函数 AST。

Inputs: fn (required). Returns: result

get_definitionC

获取符号的定义位置(文件:行号)及实体信息。

Inputs: name (required). Returns: result

list_entitiesA

列出指定源文件中定义的全部实体(函数/变量/类型),按行号排序。

Inputs: file (required). Returns: result

list_tusA

列出真实参与编译的翻译单元(TU)及其完整编译参数。回答"哪些文件真正被编译了"。

Inputs: no named parameters. Returns: result

search_entitiesB

按名称模糊搜索符号(FTS5 全文检索),如 search_entities('parse_*')。

Inputs: query (required), limit. Returns: result

statusB

索引状态:TU/实体/调用边/缺陷数量及构建时间。

Inputs: no named parameters. Returns: result

what_callsC

查询 fn 调用了哪些函数(正向调用边)。

Inputs: fn (required). Returns: result

who_callsA

查询哪些函数调用了 fn(编译器验证的反向调用边)。

Inputs: fn (required). Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    B3.4/5.0

    Scored across 11 tools

    Disambiguation5/5

    Each tool targets a clearly distinct query type: entity listing/searching, definitions, call-graph directions, impact analysis, call paths, static-analysis defects, ASTs, index status, and translation units. who_calls vs. what_calls and blast_radius vs. call_path are explicitly differentiated by direction and analysis purpose.

    Naming Consistency4/5

    All names use snake_case consistently, and most follow a readable verb_noun or query pattern. Minor deviations exist in noun-only names like blast_radius, call_path, file_issues, and status, but they remain clear and predictable within the set.

    Tool Count5/5

    Eleven tools are well-scoped for a code-intelligence and static-analysis server, covering entities, call graphs, defects, ASTs, and build artifacts without obvious redundancy. Each tool appears to earn its place.

    Completeness4/5

    The surface covers core code-analysis workflows: symbol lookup, definitions, callers/callees, impact paths, Coverity defects, ASTs, status, and translation units. Minor gaps exist, such as global defect search/listing or general file/source retrieval, but agents can largely work around them.

    Maintenance

    ActivityMaintained
    ResponsivenessOutdated data

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI agents to query and analyze code across multiple repositories through a unified knowledge graph, with tools for symbol search, impact analysis, and graph algorithms.
      MIT
    • A
      license
      A
      quality
      B
      maintenance
      Enables AI coding agents to query a semantic cross-repository code graph for symbols, references, callers, dependencies, and change impact across registered repositories.
      12
      24
      Apache 2.0
    • A
      license
      Not graded
      quality
      A
      maintenance
      Enables AI coding agents and IDEs to query a local structural code graph and retrieve focused, token-bounded context on code entities, Git relationships, callers/callees, impact, paths, and recorded mistakes for more efficient sessions.
      24 npm
      MIT