这个 Skill 解决什么具体问题

前端开发日常有一半时间花在手动操作 DevTools 上:打开控制台看报错、点 Network 查请求、截屏对比样式偏差。这些重复动作完全可以交给 AI 来做——但前提是 AI 能“看见”你的浏览器。

chrome-devtools-mcp 把 DevTools 的能力封装成 MCP(Model Context Protocol)服务,你的 AI 编码助手(Cursor、Windsurf、Claude Desktop)通过 MCP 协议直接调用 Chrome 的调试接口。换句话说,AI 现在可以:

  • 打开任意页面的控制台,读取实时日志
  • 获取当前页面的截图(带视口、全页两种)
  • 执行 JavaScript 代码并取回结果
  • 读取网络请求的请求头、响应体、状态码
  • 检查 DOM 元素的内容与属性

这篇文章的目标是:让你在 10 分钟内跑通这个 MCP 服务,并给出一个可以直接复用的 Skill 模板,下次 AI 帮你调试时不再是“盲猜”,而是直接抓现场数据。

Skill 的触发条件和适用场景

触发条件 典型场景
控制台出现红字错误 AI 主动打开开发者工具的 Console 并读取错误堆栈
页面样式不符合预期 AI 截一张全页截图,再对比设计稿
接口返回 500 AI 打开 Network 面板,找到失败请求并看响应体
需要用户在页面上操作后观察变化 AI 通过 MCP 调用 page.evaluate 执行脚本,然后重新截图
性能分析 AI 打开 Performance 面板录制,但当前 MCP 只支持基础 timeline,所以更适合轻量检查

适用前提:

  • 你正在用支持 MCP 的 AI 编码工具(Cursor 0.42+、Windsurf、Claude Desktop、继续开源版 Continue)
  • 本地已经安装了 Node.js 18+(因为 MCP 服务本身是 TypeScript 写的)
  • 目标页面已经通过 chrome-devtools-mcp inspect 连接到你的调试端口

一个反直觉的点:这个 Skill 并不是用来代替人工调试的,而是让 AI 在你不在场时也能自动收集诊断信息。比如 CI 中运行 E2E 测试后,AI 可以直接抓取失败页面的控制台错误和截图,而不需要你回头手动复现。

完整 Skill 结构(SKILL.md 示例)

为了让 AI 知道它拥有 DevTools 能力,你需要编写一个明确的 Skill 定义文件。以下是我在实战中使用的模板,可以直接复制到项目根目录的 .cursor/skills/debug-chrome.skill.md 或类似位置(不同工具路径可能不同):

markdown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
# Debug with Chrome DevTools (via MCP)

## Description
Automatically capture browser console errors, network failures, and page screenshots using the chrome-devtools-mcp MCP server.

## Trigger
- User says "debug this page" or "check console" or "why is this page broken"
- User attaches a screenshot of a browser error and asks for analysis
- After running a test, if the test fails, run this skill automatically

## Steps

1. **Ensure MCP server is running**
   - The project has a `.cursor/mcp.json` file that starts the MCP server with:
     ```json
     {
       "mcpServers": {
         "chrome-devtools": {
           "command": "npx",
           "args": ["@anthropic-ai/chrome-devtools-mcp", "--port", "9222"]
         }
       }
     }
     ```
   - If not running, ask the user to start it manually: `npx @anthropic-ai/chrome-devtools-mcp --port 9222`

2. **Connect to a browser tab**
   - First check available debug targets: `DevTools.listTabs()`
   - Select the current active tab or the one matching the user's description
   - If no tab matches, ask the user to open the page first

3. **Collect diagnostics**
   - Run `DevTools.enableConsole()` to subscribe to logs
   - Wait 2 seconds for buffered logs, then `DevTools.getConsoleMessages()`
   - Run `DevTools.captureScreenshot()` and describe what you see
   - If any network request failed (status >= 400), run `DevTools.getNetworkRequests()` and read the response body

4. **Synthesize findings**
   - Output a structured report with:
     - Console errors (count, severity, message)
     - A brief description of the screenshot (layout, visible elements, any errors)
     - Failed network requests (URL, status, response preview)
     - Recommended next steps (e.g., “Fix the missing API key in config”)

## Example

When the user says "The signup page shows an error", I will:
1. Connect to the browser tab where the signup page is open
2. Capture console messages → see `TypeError: Cannot read properties of null`
3. Capture screenshot → see the red error banner below the form
4. Capture network requests → see a 401 on `/api/user/register`
5. Report: "The API call is returning 401 because the auth token is missing from the request header. Check your auth middleware configuration."

## Requirements
- Node.js 18+
- Chrome with remote debugging enabled (`--remote-debugging-port=9222`)
- Network access to localhost:9222

差 Prompt vs 好 Prompt 对比

差 Prompt 好 Prompt
“帮我看看这个页面为什么白屏” “使用 Chrome DevTools 调试 Skills:连接到当前浏览器标签页,先获取控制台日志,如果看到任何错误就读取完整堆栈;再截一张全页截图描述布局;最后检查网络请求中是否有失败的资源。给我一份诊断报告。”
“检查一下控制台” “DevTools.enableConsole() 之后等待 2 秒,然后 GetConsoleMessages,只返回 error 级别以上的消息,并按时间排序输出。如果有超过 5 条相同错误,只输出第一条的堆栈和重复次数。”
“这个接口返回 500” “DevTools.getNetworkRequests() 过滤出 status >= 500 的请求,读取 response body 前 2000 字符,同时检查 request headers 里的 Content-Type 和 Authorization。给我三次请求的对比。”

好 Prompt 做对了什么:

  • 指定了工具调用的顺序(先 enable 再 get)
  • 限制了返回数据量(避免 AI 被数千条控制台日志撑爆上下文)
  • 明确了输出格式(结构化的报告)
  • 引导 AI 根据截图做定性判断,而不只是堆数据

实际案例演示

假设你有一个 React 项目,登录后页面闪白然后报错。你打开 Cursor,让 AI 帮你调试。

Step 1:启动 MCP 服务

bash
1 2 3 4 5
# 打开 Chrome 时需要开启远程调试
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222

# 在项目根目录启动 MCP 服务(Cursor 会自动读取 mcp.json)
npx @anthropic-ai/chrome-devtools-mcp --port 9222

Step 2:在 Cursor 中发起调试请求

text
1
@Skills debug-chrome 请帮我诊断当前打开的登录页面为什么登录后闪白

Step 3:AI 会依次执行:

  1. DevTools.listTabs() → 找到标题包含“登录”的标签页
  2. DevTools.navigateTo('http://localhost:3000/login')(如果还没打开,先导航)
  3. DevTools.enableConsole() + DevTools.getConsoleMessages() → 拿到控制台日志
  4. DevTools.captureScreenshot() → 截取登录后闪白的瞬间
  5. DevTools.getNetworkRequests() → 看到 /api/auth/login 返回 200,但之后的 /api/user/profile 返回 500
  6. 读取 /api/user/profile 的 response body → 发现“Cannot read property 'id' of undefined”
  7. 综合输出报告

实际输出可能如下:

text
1 2 3 4 5 6 7
诊断报告
━━━━━━━━━━━━━━━━━━━━━━
1. 控制台:无 JS 错误,但有一条 Warning: React hydration mismatch
2. 截图:页面显示空白,只有顶部导航栏(见附件 screenshot.png)
3. 网络请求:/api/user/profile 返回 500(响应体:"Cannot read property 'id' of undefined")
4. 原因:后端在用户登录后没有正确返回 user.id,导致前端试图渲染用户信息时报错,引发 React 卸载组件,页面变空白
5. 修复建议:检查 /api/auth/login 的返回字段是否包含 user.id,或在前端加 ?. 可选链

这个案例中,AI 真正加速的是“从一个白屏现象定位到后端接口字段缺失”的过程,原本你需要手动打开 DevTools、切到 Network、找到请求、看响应体、再翻代码;现在 AI 一步到位。

screenshot comparison before after AI debugging with Chrome DevTools MCP

复用和组合技巧

1. 与 Playwright 结合做智能 E2E 测试

Playwright 跑测试时会自动打开浏览器,但默认不暴露调试端口。你可以让 Playwright 在启动时添加 --remote-debugging-port=9222,然后 MCP 服务连上去。这样测试失败后,AI 可以自动用 DevTools 查 Control 日志并截图,生成更丰富的失败报告。

Playwright 配置片段:

ts
1 2 3 4 5 6 7 8 9
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
  use: {
    launchOptions: {
      args: ['--remote-debugging-port=9222'],
    },
  },
});

然后在 MCP 服务器中,你可以直接用 DevTools.connectToTarget() 指向 Playwright 打开的页面。

2. 与 Lighthouse 集成做性能分析

Lighthouse 本身会生成报告,但结合 DevTools MCP 可以做到:

  • AI 先调用 Lighthouse CLI 生成报告
  • 然后通过 DevTools MCP 打开报告页,截图并读取关键指标
  • 最终 AI 输出一份“性能报告 + 截图 + 优化建议”的组合文档

3. 自定义 Skill:持续监控错误

创建一个 monitor-errors.skill.md,触发条件是“帮我监控这个页面的实时错误”。AI 会:

  1. 启动一个循环(最多 30 秒或收集到 10 个错误),每次间隔 5 秒刷新控制台
  2. 记录新增错误,去除重复
  3. 结束时汇总输出错误列表和首次出现时间

这样的 Skill 可以用来重现偶发 bug,而不需要人工盯着屏幕。

4. 跨工具使用:在 Claude Desktop 中调试

如果你习惯用 Claude Desktop,配置同样简单:在 claude_desktop_config.jsonmcpServers 中加入相同 JSON。唯一区别是 Claude Desktop 需要你手动启动 Chrome 并指定端口。

json
1 2 3 4 5 6 7 8
{
  "mcpServers": {
    "chrome-devtools": {
      "command": "npx",
      "args": ["@anthropic-ai/chrome-devtools-mcp", "--port", "9222"]
    }
  }
}

注意事项与局限

  1. MCP 服务只能连接一个浏览器实例,不支持多 tab 同时调试(除非你启动多个服务实例绑定不同端口)。
  2. 默认只支持 HTTP 协议,如果你用 HTTPS 的 localhost(例如 CRA 默认使用 HTTPS),需要额外配置证书,否则 DevTools 连接会被浏览器拒绝。
  3. Console 日志有缓存上限:如果页面已经弹出 1000 条日志,MCP 只能拿到最新的 200 条左右(具体取决于 Chrome 内部限制)。建议在页面加载初期就 enable Console。
  4. 截图质量有限captureScreenshot() 返回的是 PNG base64,视口外内容会被截掉,全页截图需要额外参数 {fullPage: true}

总结

chrome-devtools-mcp 最大的价值不是让你更牛,而是让 AI 能成为一个真正有“眼睛”的调试助手。从配置到上手,你只需要 3 步:

  1. 打开 Chrome 的远程调试端口
  2. 在你的 AI 工具中配置 MCP 服务器
  3. 编写(或复用本文的)Skill 文件,告诉 AI 怎么使用

下次遇到页面白屏、接口报错、样式错乱时,别再自己动手打开 DevTools 了——让 AI 替你干这些脏活,你只需要看最终报告和修复代码。