MCP调试演示
此存储库演示了如何使用 mcp调试程序 通过模型上下文协议(MCP)使用AI代理调试带有故意错误的Python应用程序。
Bug是什么?
这 shopping_cart.py 应用程序中有一个微妙的错误 calculate_total() 方法。申请折扣时:
- 预期行为:100美元打九折,90美元
- 实际行为:计算结果相乘不正确,结果为10美元!
错误位于第45行 shopping_cart.py:
return subtotal * discount_amount # BUG: Should subtract discount, not multiply!先决条件
- Python 3.8或更高版本
- 带有MCP扩展的VS代码(或支持MCP的Claude Desktop)
- Git
快速开始
1.克隆和设置
git clone https://github.com/YOUR_USERNAME/mcp-debugpy-demo.git
cd mcp-debugpy-demo
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install pytest
# Install mcp-debugpy from source (until published to PyPI)
pip install git+https://github.com/markomanninen/mcp-debugpy.git2.运行测试(见Bug!)
pytest test_shopping_cart.py -v您将看到测试失败,显示折扣计算错误:
FAILED test_shopping_cart.py::test_calculate_total_with_discount
FAILED test_shopping_cart.py::test_complex_scenario3.运行应用程序
python shopping_cart.py输出将显示:
Items in cart: 4
Subtotal: $1139.96
Total with 10% discount: $11.40
Expected total: $1025.96
WARNING: Total doesn't match expected value!请注意,折扣后的总额(11.40美元)与预期值(1025.96美元)相去甚远!
使用MCP调试程序进行调试
重要:安装后,您需要配置MCP客户端(VS Code或Claude Desktop)以使用MCP-debugpy服务器。
选项A:带MCP扩展的VS代码
步骤1:在VS代码中打开
cd mcp-debugpy-demo
code .步骤2:配置MCP服务器
存储库包括 .vscode/settings.json 预配置。它使用 mcp-debug-server pip安装的命令。
它是什么样子的:
{
"mcp.servers.agentDebug": {
"command": "${workspaceFolder}/.venv/bin/mcp-debug-server",
"cwd": "${workspaceFolder}"
}
}步骤3:重新加载VS代码
- 按
Cmd+Shift+P(Mac)或Ctrl+Shift+P(Windows/Linux) - 键入“开发人员:重新加载窗口”
- 按Enter键
步骤4:验证MCP服务器
- 打开AI聊天面板
- 您应该看到“agentDebug”服务器可用
- 试着问:“有哪些MCP工具可用?”
步骤5:开始调试
问AI: “使用断点调试shopping_cart.py错误”
AI代理可以使用这些MCP工具:
dap_launch-使用断点开始调试dap_locals-检查变量值dap_step_over/dap_step_in-单步执行代码dap_continue-继续执行run_tests_json-运行测试并分析故障
选项B:克劳德桌面
步骤1:查找配置文件
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%\Claude\claude_desktop_config.json Linux: ~/.config/Claude/claude_desktop_config.json
步骤2:添加MCP服务器配置
编辑配置文件并添加(替换 /absolute/path/to/ 根据您的实际路径):
{
"mcpServers": {
"agentDebug": {
"command": "/absolute/path/to/mcp-debugpy-demo/.venv/bin/mcp-debug-server",
"cwd": "/absolute/path/to/mcp-debugpy-demo"
}
}
}获得你的绝对路径:
cd mcp-debugpy-demo
pwd
# Copy this path and use it above步骤3:重新启动克劳德桌面
完全退出并重新启动Claude Desktop。
步骤4:验证MCP服务器
在新的对话中,问: “你们有什么MCP工具?”
你应该看到这样的工具 dap_launch, dap_locals, run_tests_json等等。
步骤5:开始调试
问: “帮我调试购物车折扣漏洞”
选项C:克劳德代码(VS代码扩展)
Claude Code会自动检测 .vscode/settings.json 配置。只是:
- 在VS Code中打开文件夹
- 确保已安装Claude Code扩展
- 打开克劳德代码聊天
- 问: “使用断点调试shopping_cart.py”
MCP设置故障排除
“mcp调试服务器:找不到命令”
虚拟环境未激活或未安装mcp-debugpy:
source .venv/bin/activate
pip install git+https://github.com/markomanninen/mcp-debugpy.git
which mcp-debug-server # Should show path in .venv/bin/“MCP服务器未连接”
- 检查设置中的路径是否正确(绝对路径效果最佳)
- 确保虚拟环境存在
- 尝试手动运行服务器以查看错误:
.venv/bin/mcp-debug-server --help“没有可用的MCP工具”
- 重新加载VS代码窗口(Cmd/Ctrl+Shift+P→ “开发人员:重新加载窗口”)
- 检查VS代码输出面板中的MCP服务器错误
- 验证MCP扩展是否已安装并启用
手动配置(备选)
如果您不想使用预配置的 .vscode/settings.json,您可以手动配置:
- 打开VS代码设置(JSON)
- 手动添加MCP服务器配置
- 使用绝对路径保证可靠性
修复
一旦你发现了第45行的错误,修复就很简单了:
# WRONG (current):
return subtotal * discount_amount
# CORRECT:
return subtotal - discount_amount
# OR equivalently:
return subtotal * (1 - self.discount_rate)修复后,再次运行测试:
pytest test_shopping_cart.py -v现在所有测试都应该通过!
项目结构
mcp-debugpy-demo/
├── README.md # This file
├── .gitignore # Git ignore patterns
├── requirements.txt # Python dependencies
├── shopping_cart.py # Main application (with bug)
├── test_shopping_cart.py # Test suite (exposes the bug)
└── .vscode/
└── settings.json # VS Code MCP configuration学习资源
为什么这很重要
此演示展示了AI代理如何通过以下方式帮助调试:
- 自动运行测试 识别故障
- 以编程方式设置断点 在相关代码位置
- 检查变量 了解程序状态
- 遍历代码 追踪执行流程
- 解释bug 自然语言
传统的调试需要手动设置,但使用MCP工具,AI代理可以编排整个调试会话!
贡献
发现了一个bug(一个真实的bug,不是故意的演示bug!)?有什么建议吗?打开问题或拉取请求!
许可证
MIT许可证-有关详细信息,请参阅许可证文件。
致谢
- 内置 mcp调试程序 通过markomanninen
- 使用微软的 调试 调试适配器
- 实现 模型上下文协议(MCP) 通过Anthropic
