MCP服务器调试工具与日志配置要点
面向MCP服务器开发与集成场景,介绍Inspector交互测试、stdio传输下的日志输出约束、工作目录与环境变量配置,以及客户端日志和DevTools排查方式,帮助定位启动与连接问题。
开发 MCP 服务器或集成时,有效的调试至关重要 本指南涵盖调试工具和办法 可在MCP生态系统中找到。 ## 调试工具概览 MCP提供了不同级别调试的几种工具: 1. **[MCP 检查员](<https://github.com/docs/2026-07-28/tools/inspector>)**: 互动、交通不可知 测试 UI 。 连接到 stdio 或 可流的 HTTP 服务器, 引用 [工具](<https://github.com/specification/2026-07-28/server/tools>), [提示](<https://github.com/specification/2026-07-28/server/prompts>),以及 [资源](<https://github.com/specification/2026-07-28/server/resources>),并观看 通知流,这应该是你的第一站 2. **服务器日志**: 结构化日志至 stderr( stdio transport)或通过 [OpenTelemetry](https://opentelemetry.io/) (所有运输). 记录协议( D)`notifications/message`)为 [已贬值](<https://github.com/specification/2026-07-28/deprecated>) 作为协议版本 `2026-07-28`. 3. **客户端开发工具**: 大多数MCP客户端曝光日志和连接 状态。 [Claude 桌面调试](#debugging-in-claude-desktop) 以下文为例,或查阅客户的文件。 ## 实施伐木 ### 服务器侧日志 创建使用本地服务器时 [钢铁运输](<https://github.com/specification/2026-07-28/basic/transports/stdio>),所有信件 登录到 stderr( 标准错误) 将被主机应用程序捕获 自动。 本地的 MCP 服务器不应将信件日志为 stdout( 标准退出) , 因此 将干扰协议操作。 用于使用该软件的服务器 [可流式 HTTP 运输](<https://github.com/specification/2026-07-28/basic/transports/streamable-http>), Stderr 不被客户端抓取。 使用您自己的服务器侧日志汇总 或 时 间 [OpenTelemetry](https://opentelemetry.io/) 用于日志,以及标准的 HTTP 工具化(curl,浏览器 DevTools 网络面板)以检查请求和SSE 溪流。 溪流。 为所有人 [运输](<https://github.com/specification/2026-07-28/basic/transports>),记录 服务器正在运行中。例如,要写入 stderr : ```python Python import logging from mcp.server import MCPServer logger = logging.getLogger(__name__) mcp = MCPServer("reports") @mcp.tool() async def fetch_report(report_id: str) -> str: """Fetch a report by id.""" logger.info("Fetching report %s", report_id) return f"Report {report_id} is ready." ``` ```typescript TypeScript import { McpServer } from "@modelcontextprotocol/server"; import { z } from "zod"; const server = new McpServer({ name: "reports", version: "1.0.0" }); server.registerTool( "fetch_report", { description: "Fetch a report by id.", inputSchema: z.object({ reportId: z.string() }), }, async ({ reportId }) => { console.error(`Fetching report ${reportId}`); return { content: [{ type: "text", text: `Report ${reportId} is ready.` }], }; }, ); ``` 要日志的重要事件 : - 启动步骤 - 资源获取 - 工具执行 - 错误条件 - 业绩计量 ## 共同问题 下面的例子使用 Claude 桌面 [`claude_desktop_config.json`](<https://github.com/docs/2026-07-28/develop/connect-local-servers>); 不变 原则适用于任何基于 stdio 的 MCP 客户端。 ### 工作目录 当一个MCP客户端发射 stdio 服务器时: - 通过客户端配置启动的服务器工作目录可能是 未定义( 如 `/` 打开macOS),因为客户端可以从 任何地方 - 在配置中总是使用绝对路径,并且 `.env` 要保证的文件 可靠操作 - 对于直接通过命令行测试服务器,工作目录将是 运行命令的地方 例如,在 `claude_desktop_config.json`,使用: ```json { "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem@2.4.1", "/Users/username/data" ] } } } ``` 而不是相对路径像 `./data` ### 环境变量 在 stdio 上启动的 MCP 服务器只继承有限的环境子集 变量自动(精确的集依赖平台)。 要覆盖默认变量或提供自己的变量,您可以指定 `env` 键入 `claude_desktop_config.json`: ```json { "mcpServers": { "myserver": { "command": "mcp-server-myapp", "env": { "MYAPP_API_KEY": "some_key" } } } } ``` ### 服务器启动 常见的启动问题 : 1. **路径问题** - 不正确的服务器可执行路径 - 缺少所需文件 - 权限问题 - 尝试使用绝对路径 `command` 2. **配置错误** - 无效的 JSON 语法 - 缺少所需字段 - 类型不匹配 3. **环境问题** - 缺少环境变量 - 不正确的变量值 - 许可限制 ### 连接问题 当服务器无法连接时 : 1. 检查客户端日志 2. 正在验证服务器进程 3. 独立测试 [检查员](<https://github.com/docs/2026-07-28/tools/inspector>) 4. 校验 [协议兼容性](<https://github.com/docs/2026-07-28/learn/versioning#negotiation>): 调用 [`server/discover`](<https://github.com/specification/2026-07-28/server/discover>) 看看哪个 服务器支持的协议版本。 A `UnsupportedProtocolVersionError` (`-32022`) 列出支持的服务器 报告案文 `data` 字段 5. 检查一下 [per-request `_meta` 字段](<https://github.com/specification/2026-07-28/basic/index#_meta>): 每一个请求必须携带 `io.modelcontextprotocol/protocolVersion` 财务报告和财务报告 `io.modelcontextprotocol/clientCapabilities`,客户也应 包含 `io.modelcontextprotocol/clientInfo`。请求中缺少 以错误拒绝需要的字段 `-32602` (无效参数),相同 代码返回到其它许多错误的输入。如果服务器需要 请求的能力 `clientCapabilities` 未宣布,例如: [诱导](<https://github.com/specification/2026-07-28/client/elicitation>),它返回一个 `MissingRequiredClientCapabilityError` (`-32021`) 命名缺失者 检查请求中 `_meta` 页:1 [`server/discover`](<https://github.com/specification/2026-07-28/server/discover>) 答复 核实双方声明你所期望的 ## Claude 桌面调试 Claude Desktop 是许多MCP客户端之一。它可以在 macOS,Windows,和Linux. 星洲网. ### 正在检查服务器状态 在聊天输入中单击“ 添加文件、 连接器和更多” 外加图标, 然后 徘徊在飞船上 **连接器** 菜单以查看连接的服务器和可用性 工具。  ### 查看日志 日志文件写入 : - macOS: `~/Library/Logs/Claude` - 视窗 : `%APPDATA%\Claude\logs` - Linux : (美国英语). `~/.config/Claude/logs` ```bash macOS tail -n 20 -F ~/Library/Logs/Claude/mcp*.log ``` ```bash Linux tail -n 20 -F ~/.config/Claude/logs/mcp*.log ``` ```powershell Windows type "$env:AppData\Claude\logs\mcp*.log" ``` 日志捕获 : - 服务器连接事件 - 配置问题 - 运行时间错误 - 信件交换 ### 使用 Chrome DevTools 软件 访问 Claude 桌面内部的 Chrome 开发者工具以调查 客户端错误 : 1. 创建一个 `developer_settings.json` 文件为 `allowDevTools` 设置为真 : ```bash macOS echo '{"allowDevTools": true}' > ~/Library/Application\ Support/Claude/developer_settings.json ``` ```powershell Windows '{"allowDevTools": true}' | Set-Content "$env:AppData\Claude\developer_settings.json" ``` 2. 打开 DevTools : `Command-Option-I` (macOS) 或 (中文(简体) ). `Ctrl+Alt+I` (风窗. 注:你会看到两个DevTools窗口: - 主要内容窗口 - 应用标题栏窗口 使用控制台面板检查客户端错误 。 使用网络面板检查 : - 信件有效载荷 - 连接时间 ## 调试工作流程 ### 发展周期 1. 初步发展 - 使用( U) [检查员](<https://github.com/docs/2026-07-28/tools/inspector>) 基本测试 - 执行核心功能 - 添加伐木点 2. 集成测试 - 在目标MCP客户端中测试 - 监视日志 - 检查错误处理 ### 测试变化 为有效测试变化: - **配置变化**: 重新启动MCP客户端 - **服务器代码更改**: 重新启动客户端( 对于 Claude Desktop, 完全退出) 并重新打开; 关闭窗口还不够) - **快速重排( E)**编号: 使用 [检查员](<https://github.com/docs/2026-07-28/tools/inspector>) 期间 发展 ## 最佳做法 ### 伐木战略 1. **结构化日志** - 使用一致的格式 - 包含上下文 - 添加时间戳 - 音轨请求ID 2. **处理错误** - 日志堆栈跟踪 - 包含错误上下文 - 音轨错误模式 - 监测复原情况 3. **业绩跟踪** - 日志运行时间 - 监测资源使用情况 - 音轨消息大小 - 计量延迟性 ### 安全考虑 调试时 : 1. **敏感数据** - 对日志进行无害化 - 保护全权证书 - 遮盖个人信息 2. **访问控制** - 校验权限 - 检查认证 - 监测访问模式 关于MCP攻击载体和减缓的全面治疗,见: [安保最佳做法](<https://github.com/docs/2026-07-28/tutorials/security/security_best_practices>). ## 获得帮助 遇到问题时: 1. **第一步** - 检查服务器日志 - 测试用 [检查员](<https://github.com/docs/2026-07-28/tools/inspector>) - 审查配置 - 校验环境 2. **支持频道** - [GitHub问题](https://github.com/modelcontextprotocol/modelcontextprotocol/issues) - [GitHub讨论](https://github.com/modelcontextprotocol/modelcontextprotocol/discussions) 3. **提供信息** - 日志节录 - 配置文件 - 复制步骤 - 环境细节 ## 今后的步骤 < 证书 标题="MCP 探长" 图标=“放大镜” href="/docs/2026 - 07 - 28 /工具/检查员". > 学会使用 MCP 检查器 < 证书 标题="建造了 MCP 服务器" 图标=“代码” href="/docs/2026 - 07 - 28 - 开发/建设-服务器". > 从零开始走过一个服务器的构建 < 证书 标题=“连接本地服务器” 图标=“插件” href="/docs/2026 - 07 - 28 - 开发/connect-local-servers". > 完全 claude_desktop_config .json 参考和排除故障
查看原始来源