C++调试插件

一个用于Claude Code的C++调试插件,集成GDB和clangd,提供代码步进、变量检查和运行时状态分析功能。

  • by William-An
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 William-An/cpp-debug-mcp

使用说明

cpp调试mcp

A. 克劳德代码 用于C++调试的MCP服务器插件。合并 GDB (通过GDB/MI)和 clangd 的 (通过LSP)为Claude Code提供工具,用于遍历代码、检查变量、读取诊断以及将运行时状态与静态分析相关联——所有这些都来自对话。

先决条件

  • Python 3.10+
  • GDB (用于调试工具)
  • clangd 的 (用于静态分析工具)
  • 紫外线 (推荐)或pip

快速开始

全局注册MCP服务器(所有项目,自动更新):

claude mcp add --scope user cpp-debug -- uvx cpp-debug-mcp

就是这样。请确认 /mcp 在Claude Code内部确认服务器已连接。

安装选项

通过uvx自动更新(推荐)

无需安装步骤-- uvx 每次调用时从PyPI获取最新版本。只需使用上面的命令注册。

来自PyPI

pip install cpp-debug-mcp

来自GitHub

pip install git+https://github.com/William-An/cpp-debug-mcp.git

来源(用于开发)

git clone https://github.com/William-An/cpp-debug-mcp.git
cd cpp-debug-mcp
uv venv .venv
source .venv/bin/activate
uv pip install -e ".[dev]"

使用克劳德代码注册

全球(所有项目)

claude mcp add --scope user cpp-debug -- uvx cpp-debug-mcp

每个项目(CLI)

claude mcp add cpp-debug -- uvx cpp-debug-mcp

每个项目(.mcp.json)

{
  "mcpServers": {
    "cpp-debug": {
      "type": "stdio",
      "command": "uvx",
      "args": ["cpp-debug-mcp"]
    }
  }
}
范围 标志 配置文件 可用性
用户 --scope user ~/.claude.json 所有项目,私人给你
项目 --scope project .mcp.json 这个项目,通过git共享
本地 (默认) .mcp.json 这个项目,对你来说是私人的

工具

GDB工具(16)

工具 说明
gdb_start_session 为已编译的可执行文件启动GDB会话
gdb_end_session 结束会话并清理
gdb_run 开始执行(可选择在以下位置停止 main)
gdb_set_breakpoint 按文件设置断点:行、函数或地址
gdb_delete_breakpoint 按ID删除断点
gdb_list_breakpoints 列出所有活动断点
gdb_continue 继续,直到下一个断点或退出
gdb_step 进入、结束或退出功能
gdb_backtrace 获取调用堆栈
gdb_list_variables 列出堆栈帧中的局部变量
gdb_evaluate 计算C++表达式(例如。 *ptr, arr[5])
gdb_read_memory 读取某个地址的原始内存
gdb_thread_info 列出所有线程及其状态
gdb_raw_command 执行原始GDB命令(有安全限制)
gdb_open_console 通过tmux打开交互式GDB控制台(需要tmux)
gdb_close_console 关闭交互式GDB控制台

LSP/cland工具(8)

工具 说明
lsp_start_session 为项目启动cland会话
lsp_end_session 结束叮当作响的会话
lsp_diagnostics 获取文件的编译错误和警告
lsp_hover 获取某个职位的类型/文档信息
lsp_goto_definition 查找符号的定义位置
lsp_find_references 查找对某个符号的所有引用
lsp_document_symbols 列出文件中的所有符号
lsp_signature_help 在调用站点获取函数签名帮助

组合工具(3)

工具 说明
inspect_variable_with_type GDB运行时值+变量的cland类型信息
diagnose_crash_site 回溯+局部变量+崩溃时的LSP诊断
analyze_function 函数的断点+签名+引用+局部变量

示例用法

用调试符号编译你的C++程序,然后让Claude Code进行调试:

> Compile main.cpp with debug symbols and find why it segfaults

Claude will:
1. Run g++ -g -O0 -o main main.cpp
2. Call gdb_start_session with the executable
3. Call gdb_run to execute until the crash
4. Call diagnose_crash_site to get the full crash report
5. Explain the root cause with backtrace, variable values, and type info

建筑

src/cpp_debug_mcp/
├── server.py              # FastMCP entry point with lifespan management
├── gdb/
│   ├── controller.py      # Async GDB/MI subprocess wrapper (pygdbmi)
│   └── session.py         # Session lifecycle (max 4, 30min timeout)
├── lsp/
│   ├── client.py          # Async JSON-RPC client for clangd over STDIO
│   ├── protocol.py        # LSP message helpers and response parsers
│   └── session.py         # Session lifecycle (max 2, 30min timeout)
├── analysis/
│   └── correlator.py      # Cross-references GDB runtime + LSP static info
└── tools/
    ├── fmt.py             # Human-readable output formatting
    ├── gdb_tools.py       # 16 GDB MCP tools (incl. interactive console)
    ├── lsp_tools.py       # 8 LSP MCP tools
    └── combined_tools.py  # 3 combined analysis tools

安全

  • 命令清理: gdb_raw_command 块 shell, !, python, pipe,以及 source 命令
  • 资源限制:最多4个GDB会话和2个LSP会话同时进行
  • 自动清理:闲置30分钟后,陈旧的会话会被清理干净
  • 流程生命周期:所有子进程在服务器关闭时终止

运行测试

source .venv/bin/activate
uv pip install -e ".[dev]"
python -m pytest tests/ -v

GDB测试需要 gdb 待安装。LSP测试需要 clangd。需要不可用工具的测试将自动跳过。

许可证

麻省理工学院