MCP服务器调试与协议验证方法

面向使用GitHub Copilot SDK接入MCP服务器的开发者,介绍先脱离SDK独立验证服务器、再排查启动故障的调试思路,并说明通过包装脚本记录通信流量以定位问题。

## [目录](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#table-of-contents>) - [快速诊断](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#quick-diagnostics>) - [独立测试 MCP 服务器](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#testing-mcp-servers-independently>) - [共同问题](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#common-issues>) - [平台具体问题](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#platform-specific-issues>) - [高级调试](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#advanced-debugging>) ## [快速诊断](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#quick-diagnostics>) ### [核对清单](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#checklist>) 在潜入深处之前,验证这些基本情况: - MCP 服务器可执行文件存在且可运行 - 命令路径正确( 当有疑问时使用绝对路径) - 启用了工具( E)`tools: ["*"]` 或特定工具名称) - 服务器正确执行 MCP 协议(对应 `initialize`) - 没有防火墙/抗病毒屏蔽进程(Windows) ### [启用 MCP 调试记录](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#enable-mcp-debug-logging>) 添加环境变量到您的 MCP 服务器配置 : ```typescript mcpServers: { "my-server": { type: "local", command: "/path/to/server", args: [], env: { MCP_DEBUG: "1", DEBUG: "*", NODE_DEBUG: "mcp", // For Node.js MCP servers }, }, } ``` ## [独立测试 MCP 服务器](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#testing-mcp-servers-independently>) 总是先在SDK外测试你的MCP服务器. ### [手动协议测试](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#manual-protocol-test>) 发送一个 `initialize` 通过 stdin 请求 : ```bash # Unix/macOS echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' | /path/to/your/mcp-server # Windows (PowerShell) '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' | C:\path\to\your\mcp-server.exe ``` **预期反应:** ```json {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{}},"serverInfo":{"name":"your-server","version":"1.0"}}} ``` ### [测试工具列表](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#test-tool-listing>) 初始化后, 请求工具列表 : ```bash echo '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' | /path/to/your/mcp-server ``` **预期反应:** ```json {"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"my_tool","description":"Does something","inputSchema":{...}}]}} ``` ### [交互式测试脚本](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#interactive-testing-script>) 创建一个测试脚本以交互调试您的 MCP 服务器 : ```bash #!/bin/bash # test-mcp.sh SERVER="$1" # Initialize echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' # Send initialized notification echo '{"jsonrpc":"2.0","method":"notifications/initialized"}' # List tools echo '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' # Keep stdin open cat ``` 用法 : ```bash ./test-mcp.sh | /path/to/mcp-server ``` ## [共同问题](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#common-issues>) ### [服务器未启动](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#server-not-starting>) **症状 :** 没有工具出现, 没有日志错误 。 **原因解决方案( S) :** | 原因 | 解决方案 | | --- | --- | | 错误的命令路径 | 使用绝对路径 : `/usr/local/bin/server` | | 缺少可执行权限 | 运行 `chmod +x /path/to/server` | | 缺少依赖关系 | 检查 `ldd` (Linux) 或手动运行 | | 工作目录问题 | 设定 `cwd` 在配置中 | **手动运行调试 :** ```bash # Run exactly what the SDK would run cd /expected/working/dir /path/to/command arg1 arg2 ``` ### [服务器启动但工具不出现](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#server-starts-but-tools-dont-appear>) **症状 :** 服务器进程运行,但没有可用的工具 。 **原因解决方案( S) :** 1. **配置中未启用的工具 :** ```typescript mcpServers: { "server": { // ... tools: ["*"], // Must be "*" or list of tool names }, } ``` 2. **服务器不曝光工具 :** - 测试用 `tools/list` 手动请求 - 检查服务器工具 `tools/list` 方法 3. **初始化握手失败 :** - 服务器必须响应 `initialize` 正确的 - 服务器必须处理 `notifications/initialized` ### [列出但从未调用的工具](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#tools-listed-but-never-called>) **症状 :** 工具出现在调试日志中,但模型并不使用. **原因解决方案( S) :** 1. **提示显然不需要工具:** ```typescript // Too vague await session.sendAndWait({ prompt: "What's the weather?" }); // Better - explicitly mentions capability await session.sendAndWait({ prompt: "Use the weather tool to get the current temperature in Seattle" }); ``` 2. **工具描述不明 :** ```typescript // Bad - model doesn't know when to use it { name: "do_thing", description: "Does a thing" } // Good - clear purpose { name: "get_weather", description: "Get current weather conditions for a city. Returns temperature, humidity, and conditions." } ``` 3. **工具计划问题:** - 保证 `inputSchema` 是有效的 JSON 舍马 - 需要的字段必须包含在内 `required` 数组 ### [超时错误](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#timeout-errors>) **症状 :** `MCP tool call timed out` 错误。 **解决方案 :** 1. **增加超时 :** ```typescript mcpServers: { "slow-server": { // ... timeout: 300000, // 5 minutes }, } ``` 2. **优化服务器性能 :** - 添加进度记录以识别瓶颈 - 考虑同步操作 - 检查是否屏蔽 I/ O 3. **用于长期工具**,如果支持,则考虑流式响应。 ### [JSON - RPC 错误](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#json-rpc-errors>) **症状 :** 解析错误, 请求错误无效 。 **常见原因:** 1. **服务器写入错误的退出 :** - 调试输出到 stdout 而不是 stderr - 额外的新线或空白 ```typescript // Wrong - pollutes stdout console.log("Debug info"); // Correct - use stderr for debug console.error("Debug info"); ``` 2. **编码问题 :** - 确保 UTF-8 编码 - 没有 BOM (Byte 命令 Mark) 3. **信件框架 :** - 每个信件必须是完整的 JSON 对象 - 新线限制( 每行一条消息) ## [平台具体问题](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#platform-specific-issues>) ### [窗口](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#windows>) #### [.NET控制台应用程序/工具](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#net-console-apps--tools>) ```csharp // Correct configuration for .NET exe ["my-dotnet-server"] = new McpStdioServerConfig { Command = @"C:\Tools\MyServer\MyServer.exe", // Full path with .exe Args = new List<string>(), WorkingDirectory = @"C:\Tools\MyServer", // Set working directory Tools = new List<string> { "*" }, } // For dotnet tool (DLL) ["my-dotnet-tool"] = new McpStdioServerConfig { Command = "dotnet", Args = new List<string> { @"C:\Tools\MyTool\MyTool.dll" }, WorkingDirectory = @"C:\Tools\MyTool", Tools = new List<string> { "*" }, } ``` #### [npx 命令](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#npx-commands>) ```csharp // Windows needs cmd /c for npx ["filesystem"] = new McpStdioServerConfig { Command = "cmd", Args = new List<string> { "/c", "npx", "-y", "@modelcontextprotocol/server-filesystem", "C:\\allowed\\path" }, Tools = new List<string> { "*" }, } ``` #### [路径问题](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#path-issues>) - 使用原始字符串( R)`@"C:\path"`)或前斜线(`"C:/path"`) - 尽可能避免路径中的空格 - 如果需要空格,请确保正确引用 #### [Antivirus/firewall](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#antivirusfirewall>) Windows Defender 或其他 AV 可能屏蔽: - 新建可执行文件 - 通过 stdin/ stdout 进行通信的过程 **解决方案 :** 为您的 MCP 服务器执行文件添加排除 。 ### [macOS](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#macos>) #### [门卫屏蔽](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#gatekeeper-blocking>) ```bash # If the server is blocked xattr -d com.apple.quarantine /path/to/mcp-server ``` #### [土生土长路径](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#homebrew-paths>) ```typescript // GUI apps may not have /opt/homebrew in PATH mcpServers: { "my-server": { command: "/opt/homebrew/bin/node", // Full path args: ["/path/to/server.js"], }, } ``` ### [链接( L)](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#linux>) #### [许可问题](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#permission-issues>) ```bash chmod +x /path/to/mcp-server ``` #### [缺少共享库](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#missing-shared-libraries>) ```bash # Check dependencies ldd /path/to/mcp-server # Install missing libraries apt install libfoo # Debian/Ubuntu yum install libfoo # RHEL/CentOS ``` ## [高级调试](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#advanced-debugging>) ### [抓取全部 MCP 流量](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#capture-all-mcp-traffic>) 创建一个折叠脚本来记录所有通讯 : ```bash #!/bin/bash # mcp-debug-wrapper.sh LOG="./mcp-debug-$(date +%s).log" ACTUAL_SERVER="$1" shift echo "=== MCP Debug Session ===" >> "$LOG" echo "Server: $ACTUAL_SERVER" >> "$LOG" echo "Args: $@" >> "$LOG" echo "=========================" >> "$LOG" # Tee stdin/stdout to log file tee -a "$LOG" | "$ACTUAL_SERVER" "$@" 2>> "$LOG" | tee -a "$LOG" ``` 用它来: ```typescript mcpServers: { "debug-server": { command: "/path/to/mcp-debug-wrapper.sh", args: ["/actual/server/path", "arg1", "arg2"], }, } ``` ### [与MCP检查员一起检查](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#inspect-with-mcp-inspector>) 使用官方的 MCP 检查工具 : ```bash npx @modelcontextprotocol/inspector /path/to/your/mcp-server ``` 这提供了一个网络UI,用于: - 发送测试请求 - 查看回复 - 检查工具计划 ### [协议版本不匹配](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#protocol-version-mismatches>) 检查您的服务器支持SDK使用的协议版本: ```json // In initialize response, check protocolVersion {"result":{"protocolVersion":"2024-11-05",...}} ``` 如果版本不匹配,则更新您的 MCP 服务器库. ## [调试核对表](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#debugging-checklist>) 在开设一个问题或寻求帮助时,收集: - SDK 语言和版本 - CLI版本 (中文(简体) ).`copilot --version`) - MCP服务器类型(Node.js,Python,.NET,Go,Rust,以及更多) - 完全的 MCP 服务器配置( 编辑机密) - 手册的结果 `initialize` 测试 - 手册的结果 `tools/list` 测试 - 来自 SDK 的调试日志 - 任何错误消息 ## [另见](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/mcp-debugging#see-also>) - [使用 MCP 服务器使用 GitHub 副驾驶 SDK](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/mcp>) - 配置和设置 - [调试指南](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/troubleshooting/debugging>) - SDK 全局调试 - [MCP 规格](<https://modelcontextprotocol.io/>) - 正式协议文件

查看原始来源