chrome-debugger-mcp 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +567 -0
- package/dist/chrome-manager.d.ts +167 -0
- package/dist/chrome-manager.d.ts.map +1 -0
- package/dist/chrome-manager.js +719 -0
- package/dist/chrome-manager.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +605 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 chrome-debugger-mcp contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
# chrome-debugger-mcp
|
|
2
|
+
|
|
3
|
+
[English](#english) | [中文](#chinese)
|
|
4
|
+
|
|
5
|
+
<a id="english"></a >
|
|
6
|
+
|
|
7
|
+
## English
|
|
8
|
+
|
|
9
|
+
An MCP server for breakpoint-driven Chrome debugging.
|
|
10
|
+
|
|
11
|
+
`chrome-debugger-mcp` exposes Chrome DevTools Protocol primitives as MCP tools so an AI agent can attach to a real Chrome tab, pause execution, inspect runtime values, evaluate expressions inside the current call frame, and step through code instead of guessing from static source.
|
|
12
|
+
|
|
13
|
+
This server is built for questions like:
|
|
14
|
+
|
|
15
|
+
- What is this variable at runtime?
|
|
16
|
+
- Why did this branch execute?
|
|
17
|
+
- What does the real API payload look like in the browser?
|
|
18
|
+
- Which function changed this state?
|
|
19
|
+
- Why does the UI fail only after a specific click or reload?
|
|
20
|
+
|
|
21
|
+
It is not a general browser automation server. The focus is runtime debugging.
|
|
22
|
+
|
|
23
|
+
### Features
|
|
24
|
+
|
|
25
|
+
- Launch a dedicated Chrome instance with remote debugging enabled
|
|
26
|
+
- List open tabs and require explicit user confirmation before connecting
|
|
27
|
+
- Connect to a specific Chrome page over CDP
|
|
28
|
+
- Set and remove DevTools breakpoints without editing source code
|
|
29
|
+
- Reload the page through CDP so breakpoints reliably bind after navigation
|
|
30
|
+
- Wait for the next pause, or wait for a specific file and line target
|
|
31
|
+
- Read scope variables from the paused frame
|
|
32
|
+
- Evaluate arbitrary JavaScript in the current call frame
|
|
33
|
+
- Step into, step over, step out, and resume execution
|
|
34
|
+
- Poll debugger state when the MCP client has short request timeouts
|
|
35
|
+
- Emit `_ui` payloads and logging messages that clients can surface to the user
|
|
36
|
+
|
|
37
|
+
### Why This Exists
|
|
38
|
+
|
|
39
|
+
Browser-focused MCP tools are usually good at DOM interaction and network inspection, but weak at answering runtime debugging questions. This project gives an MCP client access to the debugging workflow you would normally use in Chrome DevTools:
|
|
40
|
+
|
|
41
|
+
1. Attach to the right tab.
|
|
42
|
+
2. Pause execution at the right moment.
|
|
43
|
+
3. Read actual runtime values.
|
|
44
|
+
4. Step through code if needed.
|
|
45
|
+
5. Resume and clean up.
|
|
46
|
+
|
|
47
|
+
The server is intentionally opinionated. It encodes guardrails that prevent common agent mistakes such as:
|
|
48
|
+
|
|
49
|
+
- guessing which tab to attach to
|
|
50
|
+
- concluding behavior without inspecting runtime values
|
|
51
|
+
- ending the turn between `reloadPage()` and `waitForSpecificPause()`
|
|
52
|
+
|
|
53
|
+
### Requirements
|
|
54
|
+
|
|
55
|
+
- Google Chrome installed locally
|
|
56
|
+
- An MCP client that supports stdio servers and tool calling
|
|
57
|
+
- Access to the application you want to debug
|
|
58
|
+
- Local source access if you plan to insert temporary `debugger;` statements
|
|
59
|
+
|
|
60
|
+
### Installation
|
|
61
|
+
|
|
62
|
+
#### From npm
|
|
63
|
+
|
|
64
|
+
After this package is published, users can run it directly with `npx`:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npx -y chrome-debugger-mcp
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Or install it globally:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npm install -g chrome-debugger-mcp
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### From source
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pnpm install
|
|
80
|
+
pnpm build
|
|
81
|
+
node dist/index.js
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### MCP Client Configuration
|
|
85
|
+
|
|
86
|
+
#### Use the published package
|
|
87
|
+
|
|
88
|
+
```json
|
|
89
|
+
{
|
|
90
|
+
"mcpServers": {
|
|
91
|
+
"chrome-debugger": {
|
|
92
|
+
"command": "npx",
|
|
93
|
+
"args": ["-y", "chrome-debugger-mcp"]
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Use a local checkout
|
|
100
|
+
|
|
101
|
+
```json
|
|
102
|
+
{
|
|
103
|
+
"mcpServers": {
|
|
104
|
+
"chrome-debugger": {
|
|
105
|
+
"command": "node",
|
|
106
|
+
"args": ["/absolute/path/to/chrome-debugger-mcp/dist/index.js"]
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Tooling Model
|
|
113
|
+
|
|
114
|
+
The server runs over stdio and exposes MCP tools. The most important tools are:
|
|
115
|
+
|
|
116
|
+
- `startDebuggingSession`: returns the recommended debugging workflow and critical rules for agent behavior
|
|
117
|
+
- `launchChrome`: launches a dedicated Chrome instance with remote debugging enabled
|
|
118
|
+
- `listTargets`: lists available Chrome tabs and requires the user to pick one
|
|
119
|
+
- `connect`: attaches to the confirmed tab
|
|
120
|
+
- `setBreakpoint`: creates a CDP breakpoint without modifying source files
|
|
121
|
+
- `removeBreakpoint`: removes a breakpoint created by `setBreakpoint`
|
|
122
|
+
- `reloadPage`: reloads the current page through CDP
|
|
123
|
+
- `waitForSpecificPause`: waits for the next pause and checks whether it matches a target file and line
|
|
124
|
+
- `waitForPause`: waits for any pause without location matching
|
|
125
|
+
- `getScopeVariables`: reads local, closure, and module scope values from the paused frame
|
|
126
|
+
- `evaluate`: executes JavaScript in the paused call frame
|
|
127
|
+
- `stepInto`, `stepOver`, `stepOut`: standard execution control
|
|
128
|
+
- `resume`: resumes execution after inspection
|
|
129
|
+
- `getStatus`: non-blocking polling for connected or paused state
|
|
130
|
+
- `forcePause`: requests a pause at the next JavaScript statement
|
|
131
|
+
|
|
132
|
+
### Recommended Workflow
|
|
133
|
+
|
|
134
|
+
For AI clients, the intended flow is:
|
|
135
|
+
|
|
136
|
+
1. Call `startDebuggingSession()`.
|
|
137
|
+
2. Call `launchChrome()` or use an already-running Chrome instance with a CDP port.
|
|
138
|
+
3. Call `listTargets()` and show the full tab list to the user.
|
|
139
|
+
4. Wait for the user to confirm the exact page URL.
|
|
140
|
+
5. Call `connect({ targetUrl })`.
|
|
141
|
+
6. Insert a temporary `debugger;` statement in local source code, or call `setBreakpoint()`.
|
|
142
|
+
7. Call `reloadPage()`.
|
|
143
|
+
8. Immediately call `waitForSpecificPause()` in the same turn.
|
|
144
|
+
9. Call `getScopeVariables()` and `evaluate()` to inspect runtime values.
|
|
145
|
+
10. Step if necessary with `stepInto()`, `stepOver()`, or `stepOut()`.
|
|
146
|
+
11. Call `resume()`.
|
|
147
|
+
12. Remove any temporary `debugger;` statements from source code.
|
|
148
|
+
|
|
149
|
+
### Important Rules For Agent Authors
|
|
150
|
+
|
|
151
|
+
This server is designed for tool-using agents, not only for humans. If you are integrating it into an MCP client, keep these rules:
|
|
152
|
+
|
|
153
|
+
- Never skip `listTargets()`.
|
|
154
|
+
- Never guess the target URL, even if only one tab is open.
|
|
155
|
+
- Always wait for explicit user confirmation before `connect()`.
|
|
156
|
+
- After `reloadPage()`, immediately call `waitForSpecificPause()` or `waitForPause()` in the same turn.
|
|
157
|
+
- Do not explain behavior from static code when runtime values can be inspected directly.
|
|
158
|
+
- Always `resume()` after inspection.
|
|
159
|
+
- If you added temporary `debugger;` statements to source code, remove them before finishing.
|
|
160
|
+
|
|
161
|
+
### How `waitForSpecificPause` Matches
|
|
162
|
+
|
|
163
|
+
`waitForSpecificPause` is the preferred waiting primitive because it is more reliable than waiting for an arbitrary pause.
|
|
164
|
+
|
|
165
|
+
It matches a pause using two strategies:
|
|
166
|
+
|
|
167
|
+
1. URL fragment plus line tolerance
|
|
168
|
+
2. URL fragment plus `debugger-statement` pause reason
|
|
169
|
+
|
|
170
|
+
The second path matters when source maps, transpilation, or bundling shift compiled line numbers away from editor line numbers.
|
|
171
|
+
|
|
172
|
+
### Example Tool Sequence
|
|
173
|
+
|
|
174
|
+
An agent debugging a local Vite app might do something like this:
|
|
175
|
+
|
|
176
|
+
1. `launchChrome({ dryRun: true })`
|
|
177
|
+
2. `launchChrome()`
|
|
178
|
+
3. `listTargets()`
|
|
179
|
+
4. Wait for the user to confirm `http://127.0.0.1:5173`
|
|
180
|
+
5. `connect({ targetUrl: "127.0.0.1:5173" })`
|
|
181
|
+
6. Insert `debugger;` in `App.jsx`
|
|
182
|
+
7. `reloadPage()`
|
|
183
|
+
8. `waitForSpecificPause({ urlFragment: "App.jsx", line: 62, actionHint: "click the Refetch payloads button" })`
|
|
184
|
+
9. `getScopeVariables()`
|
|
185
|
+
10. `evaluate({ expression: "payload.modules" })`
|
|
186
|
+
11. `resume()`
|
|
187
|
+
|
|
188
|
+
### Chrome Launch Behavior
|
|
189
|
+
|
|
190
|
+
`launchChrome()` uses a dedicated profile so it does not interfere with the user's normal browser session.
|
|
191
|
+
|
|
192
|
+
Defaults:
|
|
193
|
+
|
|
194
|
+
- remote debugging port: `9222`
|
|
195
|
+
- profile directory: `~/.chrome-debug-profile`
|
|
196
|
+
|
|
197
|
+
Expected Chrome binary locations:
|
|
198
|
+
|
|
199
|
+
- macOS: `/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`
|
|
200
|
+
- Linux: `google-chrome`
|
|
201
|
+
- Windows: `C:\Program Files\Google\Chrome\Application\chrome.exe`
|
|
202
|
+
|
|
203
|
+
If automatic launch fails, the tool returns a command the user can run manually.
|
|
204
|
+
|
|
205
|
+
### Local Playground
|
|
206
|
+
|
|
207
|
+
This repository includes a disposable test app under [`test/`](./test) so you can exercise the debugger server against a realistic browser workflow.
|
|
208
|
+
|
|
209
|
+
#### Start the mock service
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
cd test/service
|
|
213
|
+
node src/server.js
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
The service listens on `http://127.0.0.1:3030`.
|
|
217
|
+
|
|
218
|
+
#### Start the web app
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
cd test/web
|
|
222
|
+
pnpm install
|
|
223
|
+
pnpm dev
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The web app runs on `http://127.0.0.1:5173`.
|
|
227
|
+
|
|
228
|
+
Useful places to pause:
|
|
229
|
+
|
|
230
|
+
- `test/web/src/App.jsx` inside `loadWorkbench`
|
|
231
|
+
- `test/web/src/App.jsx` inside `loadModuleDetail`
|
|
232
|
+
- `test/web/src/App.jsx` around the unfinished detail sections
|
|
233
|
+
|
|
234
|
+
Runtime payload areas worth inspecting:
|
|
235
|
+
|
|
236
|
+
- `summaryCards`
|
|
237
|
+
- `modules`
|
|
238
|
+
- `apiContracts`
|
|
239
|
+
- `nextActions`
|
|
240
|
+
- `responseShape`
|
|
241
|
+
|
|
242
|
+
### Troubleshooting
|
|
243
|
+
|
|
244
|
+
#### No targets found
|
|
245
|
+
|
|
246
|
+
Make sure Chrome is running with `--remote-debugging-port=9222` and the target page is open.
|
|
247
|
+
|
|
248
|
+
#### More than one tab matches `targetUrl`
|
|
249
|
+
|
|
250
|
+
Pass a more specific substring so the match becomes unique.
|
|
251
|
+
|
|
252
|
+
#### `waitForPause` or `waitForSpecificPause` times out
|
|
253
|
+
|
|
254
|
+
This can happen when:
|
|
255
|
+
|
|
256
|
+
- the page action was never triggered
|
|
257
|
+
- the wrong breakpoint was set
|
|
258
|
+
- the MCP client itself has a shorter request timeout than the tool call
|
|
259
|
+
|
|
260
|
+
If your client times out quickly, use `getStatus()` to poll or increase the client timeout.
|
|
261
|
+
|
|
262
|
+
#### The paused line number does not match the editor line
|
|
263
|
+
|
|
264
|
+
Bundlers and transpilers can shift compiled line numbers. Use `waitForSpecificPause()` and rely on URL fragment matching plus `debugger-statement` semantics.
|
|
265
|
+
|
|
266
|
+
#### Chrome does not launch automatically
|
|
267
|
+
|
|
268
|
+
The machine may use a non-default Chrome install path. Run the returned launch command manually or adjust the implementation to match your environment.
|
|
269
|
+
|
|
270
|
+
### Development
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
pnpm install
|
|
274
|
+
pnpm build
|
|
275
|
+
node dist/index.js
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
The implementation lives in:
|
|
279
|
+
|
|
280
|
+
- [`src/index.ts`](./src/index.ts): MCP tool definitions and user-facing workflow hints
|
|
281
|
+
- [`src/chrome-manager.ts`](./src/chrome-manager.ts): Chrome DevTools Protocol integration and debugger state management
|
|
282
|
+
|
|
283
|
+
### License
|
|
284
|
+
|
|
285
|
+
MIT
|
|
286
|
+
|
|
287
|
+
<a id="chinese"></a >
|
|
288
|
+
|
|
289
|
+
## 中文
|
|
290
|
+
|
|
291
|
+
一个面向 Chrome 断点调试的 MCP Server。
|
|
292
|
+
|
|
293
|
+
`chrome-debugger-mcp` 把 Chrome DevTools Protocol 的核心调试能力暴露为 MCP 工具,让 AI agent 可以连接真实的 Chrome 标签页,在运行时暂停执行、读取变量、在当前调用帧中执行表达式、单步跟踪代码,而不是只靠静态源码猜测行为。
|
|
294
|
+
|
|
295
|
+
这个服务适合处理这类问题:
|
|
296
|
+
|
|
297
|
+
- 这个变量在运行时到底是什么值?
|
|
298
|
+
- 为什么会走到这个分支?
|
|
299
|
+
- 浏览器里真实拿到的 API 返回结构是什么?
|
|
300
|
+
- 是哪一个函数改掉了这个状态?
|
|
301
|
+
- 为什么这个 UI 只有在某次点击或刷新后才出错?
|
|
302
|
+
|
|
303
|
+
它不是通用浏览器自动化工具。它的重点是运行时调试。
|
|
304
|
+
|
|
305
|
+
### 功能特性
|
|
306
|
+
|
|
307
|
+
- 启动带远程调试端口的独立 Chrome 实例
|
|
308
|
+
- 列出所有标签页,并强制要求用户明确确认目标页
|
|
309
|
+
- 通过 CDP 连接指定 Chrome 页面
|
|
310
|
+
- 无需修改源码即可设置和移除断点
|
|
311
|
+
- 通过 CDP 重载页面,确保跳转后断点可靠绑定
|
|
312
|
+
- 支持等待任意 pause,也支持等待指定文件和行附近的 pause
|
|
313
|
+
- 读取当前暂停帧里的作用域变量
|
|
314
|
+
- 在当前调用帧里执行任意 JavaScript 表达式
|
|
315
|
+
- 支持 `stepInto`、`stepOver`、`stepOut` 和 `resume`
|
|
316
|
+
- 当 MCP 客户端请求超时较短时,可轮询调试器状态
|
|
317
|
+
- 输出 `_ui` 结果和 logging 消息,方便客户端展示给用户
|
|
318
|
+
|
|
319
|
+
### 为什么做这个项目
|
|
320
|
+
|
|
321
|
+
很多浏览器方向的 MCP 工具更擅长 DOM 操作和网络请求观察,但不擅长回答运行时调试问题。这个项目把 Chrome DevTools 中常用的调试流程带进了 MCP:
|
|
322
|
+
|
|
323
|
+
1. 连接正确的标签页。
|
|
324
|
+
2. 在正确的时机暂停执行。
|
|
325
|
+
3. 读取真实运行时值。
|
|
326
|
+
4. 必要时单步跟踪。
|
|
327
|
+
5. 恢复执行并清理现场。
|
|
328
|
+
|
|
329
|
+
这个服务是有明确约束的。它内置了一些 guardrails,专门避免 agent 出现这些常见错误:
|
|
330
|
+
|
|
331
|
+
- 猜测应该连接哪个标签页
|
|
332
|
+
- 没看运行时值就直接下结论
|
|
333
|
+
- 在 `reloadPage()` 和 `waitForSpecificPause()` 之间错误地结束当前轮次
|
|
334
|
+
|
|
335
|
+
### 运行要求
|
|
336
|
+
|
|
337
|
+
- 本机安装了 Google Chrome
|
|
338
|
+
- 使用支持 stdio MCP server 和工具调用的 MCP 客户端
|
|
339
|
+
- 可以访问你要调试的应用
|
|
340
|
+
- 如果要插入临时 `debugger;`,需要能访问本地源码
|
|
341
|
+
|
|
342
|
+
### 安装方式
|
|
343
|
+
|
|
344
|
+
#### 从 npm 使用
|
|
345
|
+
|
|
346
|
+
这个包发布后,用户可以直接用 `npx` 运行:
|
|
347
|
+
|
|
348
|
+
```bash
|
|
349
|
+
npx -y chrome-debugger-mcp
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
也可以全局安装:
|
|
353
|
+
|
|
354
|
+
```bash
|
|
355
|
+
npm install -g chrome-debugger-mcp
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
#### 从源码运行
|
|
359
|
+
|
|
360
|
+
```bash
|
|
361
|
+
pnpm install
|
|
362
|
+
pnpm build
|
|
363
|
+
node dist/index.js
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### MCP 客户端配置
|
|
367
|
+
|
|
368
|
+
#### 使用已发布包
|
|
369
|
+
|
|
370
|
+
```json
|
|
371
|
+
{
|
|
372
|
+
"mcpServers": {
|
|
373
|
+
"chrome-debugger": {
|
|
374
|
+
"command": "npx",
|
|
375
|
+
"args": ["-y", "chrome-debugger-mcp"]
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
#### 使用本地源码
|
|
382
|
+
|
|
383
|
+
```json
|
|
384
|
+
{
|
|
385
|
+
"mcpServers": {
|
|
386
|
+
"chrome-debugger": {
|
|
387
|
+
"command": "node",
|
|
388
|
+
"args": ["/absolute/path/to/chrome-debugger-mcp/dist/index.js"]
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### 工具模型
|
|
395
|
+
|
|
396
|
+
这个服务通过 stdio 运行,并暴露一组 MCP tools。最核心的工具有:
|
|
397
|
+
|
|
398
|
+
- `startDebuggingSession`:返回推荐调试流程和 agent 行为约束
|
|
399
|
+
- `launchChrome`:启动带远程调试能力的独立 Chrome 实例
|
|
400
|
+
- `listTargets`:列出可调试标签页,并要求用户做选择
|
|
401
|
+
- `connect`:连接到已确认的目标标签页
|
|
402
|
+
- `setBreakpoint`:在不改源码的情况下通过 CDP 设置断点
|
|
403
|
+
- `removeBreakpoint`:移除通过 `setBreakpoint` 创建的断点
|
|
404
|
+
- `reloadPage`:通过 CDP 重载当前页面
|
|
405
|
+
- `waitForSpecificPause`:等待下一次暂停,并判断是否命中目标文件和行
|
|
406
|
+
- `waitForPause`:不做位置匹配,等待任意暂停
|
|
407
|
+
- `getScopeVariables`:读取当前暂停帧中的局部、闭包、模块作用域变量
|
|
408
|
+
- `evaluate`:在暂停调用帧中执行 JavaScript
|
|
409
|
+
- `stepInto`、`stepOver`、`stepOut`:标准单步控制
|
|
410
|
+
- `resume`:检查完毕后恢复执行
|
|
411
|
+
- `getStatus`:非阻塞方式查询是否已连接、是否已暂停
|
|
412
|
+
- `forcePause`:请求在下一条 JavaScript 语句处暂停
|
|
413
|
+
|
|
414
|
+
### 推荐工作流
|
|
415
|
+
|
|
416
|
+
对于 AI 客户端,建议流程是:
|
|
417
|
+
|
|
418
|
+
1. 调用 `startDebuggingSession()`。
|
|
419
|
+
2. 调用 `launchChrome()`,或直接复用已经开启 CDP 端口的 Chrome。
|
|
420
|
+
3. 调用 `listTargets()`,并把完整标签页列表展示给用户。
|
|
421
|
+
4. 等待用户明确确认要调试的页面 URL。
|
|
422
|
+
5. 调用 `connect({ targetUrl })`。
|
|
423
|
+
6. 在本地源码插入临时 `debugger;`,或者调用 `setBreakpoint()`。
|
|
424
|
+
7. 调用 `reloadPage()`。
|
|
425
|
+
8. 在同一轮里立刻调用 `waitForSpecificPause()`。
|
|
426
|
+
9. 调用 `getScopeVariables()` 和 `evaluate()` 检查运行时值。
|
|
427
|
+
10. 必要时使用 `stepInto()`、`stepOver()`、`stepOut()` 继续跟踪。
|
|
428
|
+
11. 调用 `resume()`。
|
|
429
|
+
12. 删除源码里临时加入的 `debugger;`。
|
|
430
|
+
|
|
431
|
+
### 给 Agent 作者的重要规则
|
|
432
|
+
|
|
433
|
+
这个服务首先是为会调用工具的 agent 设计的,而不仅仅是给人手动点工具用。如果你要把它接入自己的 MCP 客户端,建议遵守这些规则:
|
|
434
|
+
|
|
435
|
+
- 不要跳过 `listTargets()`。
|
|
436
|
+
- 即使只看到一个标签页,也不要猜测目标 URL。
|
|
437
|
+
- 一定要等用户明确确认后再调用 `connect()`。
|
|
438
|
+
- 调用 `reloadPage()` 后,必须在同一轮里立刻调用 `waitForSpecificPause()` 或 `waitForPause()`。
|
|
439
|
+
- 能读取运行时值时,不要只根据静态代码解释行为。
|
|
440
|
+
- 检查完之后一定要 `resume()`。
|
|
441
|
+
- 如果向源码里插入了临时 `debugger;`,结束前要清理掉。
|
|
442
|
+
|
|
443
|
+
### `waitForSpecificPause` 如何匹配
|
|
444
|
+
|
|
445
|
+
`waitForSpecificPause` 是首选的等待工具,因为它比“等待任意暂停”更可靠。
|
|
446
|
+
|
|
447
|
+
它有两层匹配策略:
|
|
448
|
+
|
|
449
|
+
1. URL 片段加行号容差
|
|
450
|
+
2. URL 片段加 `debugger-statement` 暂停原因
|
|
451
|
+
|
|
452
|
+
第二层匹配对经过 source map、转译、打包后的代码尤其重要,因为编译后的行号可能和编辑器行号不完全一致。
|
|
453
|
+
|
|
454
|
+
### 调用序列示例
|
|
455
|
+
|
|
456
|
+
一个 agent 调试本地 Vite 应用时,调用顺序大致会像这样:
|
|
457
|
+
|
|
458
|
+
1. `launchChrome({ dryRun: true })`
|
|
459
|
+
2. `launchChrome()`
|
|
460
|
+
3. `listTargets()`
|
|
461
|
+
4. 等用户确认 `http://127.0.0.1:5173`
|
|
462
|
+
5. `connect({ targetUrl: "127.0.0.1:5173" })`
|
|
463
|
+
6. 在 `App.jsx` 插入 `debugger;`
|
|
464
|
+
7. `reloadPage()`
|
|
465
|
+
8. `waitForSpecificPause({ urlFragment: "App.jsx", line: 62, actionHint: "click the Refetch payloads button" })`
|
|
466
|
+
9. `getScopeVariables()`
|
|
467
|
+
10. `evaluate({ expression: "payload.modules" })`
|
|
468
|
+
11. `resume()`
|
|
469
|
+
|
|
470
|
+
### Chrome 启动行为
|
|
471
|
+
|
|
472
|
+
`launchChrome()` 会使用独立 profile,不会影响用户平时正在用的浏览器会话。
|
|
473
|
+
|
|
474
|
+
默认值:
|
|
475
|
+
|
|
476
|
+
- 远程调试端口:`9222`
|
|
477
|
+
- profile 目录:`~/.chrome-debug-profile`
|
|
478
|
+
|
|
479
|
+
默认 Chrome 可执行文件路径:
|
|
480
|
+
|
|
481
|
+
- macOS:`/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`
|
|
482
|
+
- Linux:`google-chrome`
|
|
483
|
+
- Windows:`C:\Program Files\Google\Chrome\Application\chrome.exe`
|
|
484
|
+
|
|
485
|
+
如果自动启动失败,工具会返回一条可供用户手动执行的启动命令。
|
|
486
|
+
|
|
487
|
+
### 本地 Playground
|
|
488
|
+
|
|
489
|
+
仓库里带了一个可丢弃的测试应用,目录在 [`test/`](./test)。你可以直接用它验证这个调试 MCP 的完整链路。
|
|
490
|
+
|
|
491
|
+
#### 启动 mock service
|
|
492
|
+
|
|
493
|
+
```bash
|
|
494
|
+
cd test/service
|
|
495
|
+
node src/server.js
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
服务监听在 `http://127.0.0.1:3030`。
|
|
499
|
+
|
|
500
|
+
#### 启动 web app
|
|
501
|
+
|
|
502
|
+
```bash
|
|
503
|
+
cd test/web
|
|
504
|
+
pnpm install
|
|
505
|
+
pnpm dev
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
Web 应用运行在 `http://127.0.0.1:5173`。
|
|
509
|
+
|
|
510
|
+
建议下断点的位置:
|
|
511
|
+
|
|
512
|
+
- `test/web/src/App.jsx` 里的 `loadWorkbench`
|
|
513
|
+
- `test/web/src/App.jsx` 里的 `loadModuleDetail`
|
|
514
|
+
- `test/web/src/App.jsx` 里尚未完成的 detail 区域附近
|
|
515
|
+
|
|
516
|
+
值得在运行时查看的 payload 字段:
|
|
517
|
+
|
|
518
|
+
- `summaryCards`
|
|
519
|
+
- `modules`
|
|
520
|
+
- `apiContracts`
|
|
521
|
+
- `nextActions`
|
|
522
|
+
- `responseShape`
|
|
523
|
+
|
|
524
|
+
### 故障排查
|
|
525
|
+
|
|
526
|
+
#### 找不到 targets
|
|
527
|
+
|
|
528
|
+
确认 Chrome 是用 `--remote-debugging-port=9222` 启动的,并且目标页面已经打开。
|
|
529
|
+
|
|
530
|
+
#### `targetUrl` 匹配到多个标签页
|
|
531
|
+
|
|
532
|
+
传入更具体的 URL 子串,保证匹配结果唯一。
|
|
533
|
+
|
|
534
|
+
#### `waitForPause` 或 `waitForSpecificPause` 超时
|
|
535
|
+
|
|
536
|
+
常见原因包括:
|
|
537
|
+
|
|
538
|
+
- 页面操作没有真正触发
|
|
539
|
+
- 断点位置不对
|
|
540
|
+
- MCP 客户端自身的请求超时时间比工具调用更短
|
|
541
|
+
|
|
542
|
+
如果客户端超时比较短,可以改用 `getStatus()` 轮询,或者调大客户端超时。
|
|
543
|
+
|
|
544
|
+
#### 暂停时的行号和编辑器对不上
|
|
545
|
+
|
|
546
|
+
打包和转译会导致编译后的行号偏移。优先使用 `waitForSpecificPause()`,并依赖 URL 片段匹配加 `debugger-statement` 语义匹配。
|
|
547
|
+
|
|
548
|
+
#### Chrome 无法自动启动
|
|
549
|
+
|
|
550
|
+
机器上的 Chrome 安装路径可能不是默认值。可以直接运行工具返回的启动命令,或者按你的环境调整实现。
|
|
551
|
+
|
|
552
|
+
### 开发
|
|
553
|
+
|
|
554
|
+
```bash
|
|
555
|
+
pnpm install
|
|
556
|
+
pnpm build
|
|
557
|
+
node dist/index.js
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
主要实现文件:
|
|
561
|
+
|
|
562
|
+
- [`src/index.ts`](./src/index.ts):MCP 工具定义和面向用户的工作流提示
|
|
563
|
+
- [`src/chrome-manager.ts`](./src/chrome-manager.ts):Chrome DevTools Protocol 集成和调试状态管理
|
|
564
|
+
|
|
565
|
+
### 许可证
|
|
566
|
+
|
|
567
|
+
MIT
|