pagerig 0.0.0-stage → 0.1.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 +416 -2
- package/SECURITY.md +42 -0
- package/lib/bridge.mjs +330 -0
- package/mcp-server.mjs +416 -0
- package/package.json +51 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hxhy00
|
|
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
CHANGED
|
@@ -1,3 +1,417 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PageRig
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/hxhy00/pagerig/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/pagerig)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
让 agent 直接读取和操作你已经登录的 Chrome。
|
|
8
|
+
|
|
9
|
+
PageRig 由一个 Chrome 扩展和一个本地 MCP server 组成。MCP server 在
|
|
10
|
+
`127.0.0.1:17321` 提供回环 WebSocket,扩展连上后,agent 就能通过 MCP 工具
|
|
11
|
+
读取页面、执行 JavaScript、抓取网络请求、读取 console、点击、输入和截图。
|
|
12
|
+
|
|
13
|
+
## 快速开始
|
|
14
|
+
|
|
15
|
+
1. 从 [GitHub Releases](https://github.com/hxhy00/pagerig/releases) 下载
|
|
16
|
+
`pagerig-extension-vX.Y.Z.zip`,解压后在 `chrome://extensions` 里用
|
|
17
|
+
**加载已解压的扩展程序** 加载。
|
|
18
|
+
2. 在 MCP 客户端里配置 `npx -y pagerig`,或使用预编译二进制。
|
|
19
|
+
3. 对 agent 说:**“打开 example.com,读取页面标题。”**
|
|
20
|
+
|
|
21
|
+
## 为什么需要 Chrome 扩展
|
|
22
|
+
|
|
23
|
+
Chrome 扩展的 MV3 service worker 不能监听 TCP 端口,只能主动向外连接。
|
|
24
|
+
所以必须有一个常驻进程提供回环地址:
|
|
25
|
+
|
|
26
|
+
```text
|
|
27
|
+
Agent --MCP stdio--> pagerig(MCP server + bridge)
|
|
28
|
+
|
|
|
29
|
+
+-- WebSocket /ext --> Chrome 扩展
|
|
30
|
+
|
|
|
31
|
+
+-- DOM / JS / console / network / tabs / input
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
MCP server 本身就是长期进程,因此它直接兼任 bridge:没有独立 daemon,没有
|
|
35
|
+
HTTP 命令接口,也没有 CLI。浏览器操作全部在扩展里执行,MCP server 只负责
|
|
36
|
+
转发命令、等待扩展重连和维护单实例端口。
|
|
37
|
+
|
|
38
|
+
## 功能
|
|
39
|
+
|
|
40
|
+
- 读取页面快照、可见文本、DOM 和 iframe 信息
|
|
41
|
+
- 在页面 MAIN world 执行 JavaScript,CSP 拦截时回退到隔离世界
|
|
42
|
+
- 捕获网络请求头、请求体和响应体
|
|
43
|
+
- 捕获 console 输出、未捕获异常和资源加载错误
|
|
44
|
+
- CDP 真实鼠标点击、悬停、输入、按键、选择框和复选框操作
|
|
45
|
+
- 上传本地文件到 `<input type="file">`
|
|
46
|
+
- 视口或整页 PNG 截图
|
|
47
|
+
- 读取 cookie,包括 HttpOnly cookie
|
|
48
|
+
- 多标签页列出、打开、切换、关闭和绑定
|
|
49
|
+
- 对已知会检测 CDP 的站点自动回退到合成事件
|
|
50
|
+
|
|
51
|
+
## 环境要求
|
|
52
|
+
|
|
53
|
+
- Chrome 116 或更高版本
|
|
54
|
+
- Node.js 22+(使用 `npx` 或源码运行时)
|
|
55
|
+
- 或直接下载预编译二进制,不需要 Node.js
|
|
56
|
+
|
|
57
|
+
## 安装扩展
|
|
58
|
+
|
|
59
|
+
### 从 Release 安装(推荐)
|
|
60
|
+
|
|
61
|
+
1. 打开 [Releases](https://github.com/hxhy00/pagerig/releases);
|
|
62
|
+
2. 下载最新的 `pagerig-extension-vX.Y.Z.zip`;
|
|
63
|
+
3. 解压到一个固定目录;
|
|
64
|
+
4. 打开 `chrome://extensions`,开启右上角 **开发者模式**;
|
|
65
|
+
5. 点击 **加载已解压的扩展程序**,选择解压后的目录;
|
|
66
|
+
6. 建议把 PageRig 固定到工具栏,方便查看连接状态。
|
|
67
|
+
|
|
68
|
+
### 从源码安装
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
git clone https://github.com/hxhy00/pagerig.git
|
|
72
|
+
cd pagerig
|
|
73
|
+
npm install
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
然后在 `chrome://extensions` 里加载本仓库的 `extension/` 目录。
|
|
77
|
+
|
|
78
|
+
修改 `extension/` 下的文件后,需要在 `chrome://extensions` 里重新加载扩展。
|
|
79
|
+
|
|
80
|
+
## 配置 MCP 客户端
|
|
81
|
+
|
|
82
|
+
### 方式一:npx(推荐)
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"mcpServers": {
|
|
87
|
+
"pagerig": {
|
|
88
|
+
"command": "npx",
|
|
89
|
+
"args": ["-y", "pagerig"]
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Windows 上如果 `npx` 不能直接启动,使用:
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{
|
|
99
|
+
"mcpServers": {
|
|
100
|
+
"pagerig": {
|
|
101
|
+
"command": "cmd",
|
|
102
|
+
"args": ["/c", "npx", "-y", "pagerig"]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 方式二:预编译二进制
|
|
109
|
+
|
|
110
|
+
从 Releases 下载对应平台的二进制,例如 `pagerig-darwin-arm64`,然后:
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"mcpServers": {
|
|
115
|
+
"pagerig": {
|
|
116
|
+
"command": "/absolute/path/to/pagerig-darwin-arm64"
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
macOS 如果提示无法验证开发者,可以右键打开一次,或执行:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
xattr -dr com.apple.quarantine /path/to/pagerig-darwin-arm64
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### 方式三:从源码运行
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"mcpServers": {
|
|
133
|
+
"pagerig": {
|
|
134
|
+
"command": "node",
|
|
135
|
+
"args": ["/absolute/path/to/pagerig/mcp-server.mjs"]
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Claude Desktop
|
|
142
|
+
|
|
143
|
+
配置文件位置:
|
|
144
|
+
|
|
145
|
+
- macOS:`~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
146
|
+
- Windows:`%APPDATA%\Claude\claude_desktop_config.json`
|
|
147
|
+
- Linux:`~/.config/Claude/claude_desktop_config.json`
|
|
148
|
+
|
|
149
|
+
写入上面的 `mcpServers` 配置,完全退出并重启 Claude Desktop。
|
|
150
|
+
|
|
151
|
+
### Cursor
|
|
152
|
+
|
|
153
|
+
在项目里创建 `.cursor/mcp.json`:
|
|
154
|
+
|
|
155
|
+
```json
|
|
156
|
+
{
|
|
157
|
+
"mcpServers": {
|
|
158
|
+
"pagerig": {
|
|
159
|
+
"command": "npx",
|
|
160
|
+
"args": ["-y", "pagerig"]
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### VS Code
|
|
167
|
+
|
|
168
|
+
在项目里创建 `.vscode/mcp.json`:
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{
|
|
172
|
+
"servers": {
|
|
173
|
+
"pagerig": {
|
|
174
|
+
"command": "npx",
|
|
175
|
+
"args": ["-y", "pagerig"]
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### OpenCode
|
|
182
|
+
|
|
183
|
+
在 `opencode.json` 或 `opencode.jsonc` 中:
|
|
184
|
+
|
|
185
|
+
```json
|
|
186
|
+
{
|
|
187
|
+
"$schema": "https://opencode.ai/config.json",
|
|
188
|
+
"mcp": {
|
|
189
|
+
"pagerig": {
|
|
190
|
+
"type": "local",
|
|
191
|
+
"command": ["npx", "-y", "pagerig"],
|
|
192
|
+
"enabled": true
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Codex
|
|
199
|
+
|
|
200
|
+
在 `~/.codex/config.toml` 中:
|
|
201
|
+
|
|
202
|
+
```toml
|
|
203
|
+
[mcp_servers.pagerig]
|
|
204
|
+
command = "npx"
|
|
205
|
+
args = ["-y", "pagerig"]
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## 使用示例
|
|
209
|
+
|
|
210
|
+
配置好之后,可以直接用自然语言让 agent 操作浏览器:
|
|
211
|
+
|
|
212
|
+
```text
|
|
213
|
+
打开 https://example.com,读取页面标题和正文。
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
```text
|
|
217
|
+
在当前页面找到“登录”按钮并点击,然后等待页面出现“欢迎”。
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
```text
|
|
221
|
+
抓取最近 30 秒内所有包含 /api/ 的请求,列出 URL、状态码和响应体。
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
```text
|
|
225
|
+
读取当前页面的 console,找出所有报错。
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
```text
|
|
229
|
+
对当前页面截图,保存为 /tmp/page.png。
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## 工具列表
|
|
233
|
+
|
|
234
|
+
| 工具 | 作用 |
|
|
235
|
+
| --- | --- |
|
|
236
|
+
| `browser_snapshot` | 读取 URL、标题、文本、可交互元素和 frame 信息 |
|
|
237
|
+
| `browser_eval` | 在 MAIN world 执行 JavaScript,CSP 拦截时回退隔离世界 |
|
|
238
|
+
| `browser_network` | 读取或清空捕获的请求,可按 URL/方法过滤 |
|
|
239
|
+
| `browser_console` | 读取或清空 console 输出 |
|
|
240
|
+
| `browser_click` | CDP 鼠标点击,支持左右键和双击 |
|
|
241
|
+
| `browser_status` | 查看 bridge 端口、绑定标签页、捕获计数和连接状态 |
|
|
242
|
+
| `browser_hover` | CDP 鼠标悬停 |
|
|
243
|
+
| `browser_scroll` | 滚动到元素、顶部、底部或指定偏移 |
|
|
244
|
+
| `browser_select` | 按 value 或可见 label 选择 `<select>` |
|
|
245
|
+
| `browser_check` | 勾选或取消 checkbox / radio |
|
|
246
|
+
| `browser_text` | 读取页面或元素的可见文本 |
|
|
247
|
+
| `browser_dom` | 读取 outerHTML 或 innerHTML |
|
|
248
|
+
| `browser_history` | 后退、前进或刷新 |
|
|
249
|
+
| `browser_cookies` | 读取 cookie,包括 HttpOnly |
|
|
250
|
+
| `browser_upload` | 通过 CDP 给文件输入框附加本地文件 |
|
|
251
|
+
| `browser_screenshot` | 截取视口或整页 PNG |
|
|
252
|
+
| `browser_navigate` | 打开 HTTP(S) URL 并等待加载完成 |
|
|
253
|
+
| `browser_wait` | 等待选择器、文本、请求、DOM 空闲或网络空闲 |
|
|
254
|
+
| `browser_tabs` | 列出、打开、切换或关闭标签页 |
|
|
255
|
+
| `browser_bind` | 把后续命令绑定到指定标签页 |
|
|
256
|
+
| `browser_type` | 聚焦并输入文本,长文本使用 CDP 批量插入 |
|
|
257
|
+
| `browser_key` | 按下 Enter、Tab、Escape、方向键或单个字符 |
|
|
258
|
+
|
|
259
|
+
## 配置项
|
|
260
|
+
|
|
261
|
+
| 变量 | 含义 | 默认值 |
|
|
262
|
+
| --- | --- | --- |
|
|
263
|
+
| `BB_HOST` | bridge 绑定地址 | `127.0.0.1` |
|
|
264
|
+
| `BB_PORT` | bridge HTTP/WebSocket 端口 | `17321` |
|
|
265
|
+
| `BB_TIMEOUT` | 单条命令超时(毫秒) | `120000` |
|
|
266
|
+
| `BB_WAIT_EXT` | 等待已连接扩展唤醒的最长时间(毫秒) | `35000` |
|
|
267
|
+
| 扩展 popup 的“服务地址” | 扩展连接的 WebSocket 地址 | `ws://127.0.0.1:17321/ext` |
|
|
268
|
+
|
|
269
|
+
bridge 只绑定回环地址。扩展 popup 支持填写 `host:port` 或
|
|
270
|
+
`ws://host:port/ext`,保存后会立即重连。
|
|
271
|
+
|
|
272
|
+
## 单实例与端口冲突
|
|
273
|
+
|
|
274
|
+
同一时间只能有一个 PageRig MCP server 占用 `BB_PORT`。
|
|
275
|
+
|
|
276
|
+
- 第二个 MCP server 不会抢占端口,也不会代理到第一个;
|
|
277
|
+
- 它的 `browser_status` 会返回 `startupError.code = "PORT_IN_USE"`;
|
|
278
|
+
- 其它工具会返回同样的结构化错误。
|
|
279
|
+
|
|
280
|
+
解决办法:
|
|
281
|
+
|
|
282
|
+
1. 关闭已经占用端口的 agent 会话;或
|
|
283
|
+
2. 给新实例设置不同的 `BB_PORT`,并把扩展 popup 的服务地址改成同一个端口。
|
|
284
|
+
|
|
285
|
+
## 安全与权限
|
|
286
|
+
|
|
287
|
+
扩展申请了以下权限:
|
|
288
|
+
|
|
289
|
+
- `debugger`:通过 CDP 发送真实鼠标/键盘事件、整页截图、文件上传
|
|
290
|
+
- `webRequest`:读取请求和响应头
|
|
291
|
+
- `cookies`:读取 HttpOnly cookie
|
|
292
|
+
- `tabs`、`scripting`:切换标签页、在页面执行脚本
|
|
293
|
+
- `storage`、`alarms`:保存服务地址、维持 MV3 service worker 连接
|
|
294
|
+
- `<all_urls>`:在任意页面工作
|
|
295
|
+
|
|
296
|
+
这是一个本地个人工具,不是 Chrome Web Store 包。bridge 只监听
|
|
297
|
+
`127.0.0.1`,并且 WebSocket upgrade 只接受 `chrome-extension://` 来源。
|
|
298
|
+
但扩展权限本身很大,请只在你信任的机器和页面上使用。
|
|
299
|
+
|
|
300
|
+
漏洞报告方式见 [SECURITY.md](SECURITY.md)。
|
|
301
|
+
|
|
302
|
+
在会检测 CDP 的站点(如 zhipin、weizhipin、kanzhun)上,PageRig 不会附加
|
|
303
|
+
debugger,输入会回退到合成事件;整页截图和文件上传在该类站点不可用。
|
|
304
|
+
|
|
305
|
+
## 故障排查
|
|
306
|
+
|
|
307
|
+
### 扩展显示“未连接”
|
|
308
|
+
|
|
309
|
+
1. 确认 MCP server 正在运行(agent 会话已启动);
|
|
310
|
+
2. 检查 popup 里的服务地址是否和 `BB_PORT` 一致;
|
|
311
|
+
3. 在 `chrome://extensions` 里重新加载扩展;
|
|
312
|
+
4. 确认没有另一个 PageRig MCP server 占用了同一端口。
|
|
313
|
+
|
|
314
|
+
### `PORT_IN_USE`
|
|
315
|
+
|
|
316
|
+
说明另一个 PageRig MCP server 已经绑定 `17321`。关闭那个会话,或修改
|
|
317
|
+
`BB_PORT` 并同步修改扩展 popup 地址。
|
|
318
|
+
|
|
319
|
+
### 命令一直等待扩展
|
|
320
|
+
|
|
321
|
+
MV3 service worker 空闲会被 Chrome 终止。扩展的 alarm 和页面事件会自动唤醒
|
|
322
|
+
它,通常等待几十秒即可恢复。如果一直不恢复,重新加载扩展。
|
|
323
|
+
|
|
324
|
+
### `element_not_found`
|
|
325
|
+
|
|
326
|
+
snapshot 生成的 `rN` ref 会在下一次 snapshot 后失效。重新执行
|
|
327
|
+
`browser_snapshot`,或改用 CSS selector。
|
|
328
|
+
|
|
329
|
+
### npm 缓存权限
|
|
330
|
+
|
|
331
|
+
如果 `npm ci` 失败,检查:
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
npm config get cache
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
修复缓存目录权限,或把 `npm_config_cache` 指向可写目录。
|
|
338
|
+
|
|
339
|
+
## 开发
|
|
340
|
+
|
|
341
|
+
```bash
|
|
342
|
+
npm install
|
|
343
|
+
npm test
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
端到端测试会启动一个 fixture 页面、一个 MCP server 和 Chrome for Testing,
|
|
347
|
+
并加载未打包扩展。`CHROME_PATH` 可以指定 Chrome 路径。
|
|
348
|
+
|
|
349
|
+
对编译二进制跑同一套测试:
|
|
350
|
+
|
|
351
|
+
```bash
|
|
352
|
+
npm run build
|
|
353
|
+
BB_BINARY=dist/pagerig npm test
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
常用命令:
|
|
357
|
+
|
|
358
|
+
```bash
|
|
359
|
+
node --check mcp-server.mjs
|
|
360
|
+
node --check lib/bridge.mjs
|
|
361
|
+
node --check test/e2e.mjs
|
|
362
|
+
node scripts/version.mjs check
|
|
363
|
+
npm run build
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
## 目录结构
|
|
367
|
+
|
|
368
|
+
```text
|
|
369
|
+
extension/ Manifest V3 扩展、页面钩子和 popup
|
|
370
|
+
lib/bridge.mjs 回环 WebSocket bridge 和命令分发
|
|
371
|
+
mcp-server.mjs stdio MCP server,唯一入口
|
|
372
|
+
test/e2e.mjs MCP + 扩展 + Chrome 集成测试
|
|
373
|
+
scripts/version.mjs package / manifest / server.json 版本同步
|
|
374
|
+
server.json MCP Registry 元数据
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
## 构建与发布
|
|
378
|
+
|
|
379
|
+
构建单文件二进制(需要 Bun 1.3.14+):
|
|
380
|
+
|
|
381
|
+
```bash
|
|
382
|
+
npm run build
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
产物是 `dist/pagerig`,平台相关。GitHub Actions 会为以下目标分别构建:
|
|
386
|
+
|
|
387
|
+
- `pagerig-darwin-arm64`
|
|
388
|
+
- `pagerig-darwin-x64`
|
|
389
|
+
- `pagerig-linux-x64`
|
|
390
|
+
- `pagerig-linux-arm64`
|
|
391
|
+
- `pagerig-windows-x64.exe`
|
|
392
|
+
|
|
393
|
+
发布新版本:
|
|
394
|
+
|
|
395
|
+
```bash
|
|
396
|
+
npm run version:set -- 0.2.0
|
|
397
|
+
git add package.json package-lock.json extension/manifest.json server.json
|
|
398
|
+
git commit -m "chore: release v0.2.0"
|
|
399
|
+
git tag v0.2.0
|
|
400
|
+
git push origin main --tags
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
推送 `v*` tag 后,`.github/workflows/release.yml` 会:
|
|
404
|
+
|
|
405
|
+
1. 校验 tag、`package.json`、`extension/manifest.json` 和 `server.json` 版本一致;
|
|
406
|
+
2. 打包扩展 zip;
|
|
407
|
+
3. 构建 5 个平台的二进制;
|
|
408
|
+
4. 发布 npm 包;
|
|
409
|
+
5. 创建 GitHub Release 并上传全部资产;
|
|
410
|
+
6. 发布到 MCP Registry。
|
|
411
|
+
|
|
412
|
+
npm 发布使用仓库 secret `NPM_TOKEN`。MCP Registry 使用 GitHub OIDC,不需要额外
|
|
413
|
+
secret。
|
|
414
|
+
|
|
415
|
+
## License
|
|
416
|
+
|
|
417
|
+
[MIT](LICENSE)
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# 安全策略
|
|
2
|
+
|
|
3
|
+
## 报告漏洞
|
|
4
|
+
|
|
5
|
+
请不要在公开 issue 中披露安全问题。请通过 GitHub 的
|
|
6
|
+
[Security Advisories](https://github.com/hxhy00/pagerig/security/advisories/new)
|
|
7
|
+
私下报告,并附上:
|
|
8
|
+
|
|
9
|
+
- 受影响的版本;
|
|
10
|
+
- 复现步骤;
|
|
11
|
+
- 影响范围;
|
|
12
|
+
- 如果方便,附上最小修复建议。
|
|
13
|
+
|
|
14
|
+
## 支持版本
|
|
15
|
+
|
|
16
|
+
当前只维护最新的 `0.1.x` 版本。
|
|
17
|
+
|
|
18
|
+
## 威胁模型
|
|
19
|
+
|
|
20
|
+
PageRig 是一个本地个人工具,不是多租户服务:
|
|
21
|
+
|
|
22
|
+
- MCP server 只监听 `127.0.0.1`;
|
|
23
|
+
- 扩展通过回环 WebSocket 连接,upgrade 只接受 `chrome-extension://` 来源;
|
|
24
|
+
- 没有 HTTP 命令接口,也没有远程访问入口;
|
|
25
|
+
- 不要把 `BB_PORT` 通过反向代理、端口转发或容器映射暴露到公网。
|
|
26
|
+
|
|
27
|
+
## 扩展权限
|
|
28
|
+
|
|
29
|
+
扩展申请了 `debugger`、`webRequest`、`cookies`、`tabs`、`scripting`、
|
|
30
|
+
`storage`、`alarms` 和 `<all_urls>`。这些权限足以读取页面、cookie、网络请求
|
|
31
|
+
并模拟输入,因此:
|
|
32
|
+
|
|
33
|
+
- 只在你自己信任的机器上使用;
|
|
34
|
+
- 不要在不可信页面或共享浏览器配置里长期开启;
|
|
35
|
+
- 使用完毕后可以在 `chrome://extensions` 里关闭扩展;
|
|
36
|
+
- 不要把带有扩展权限的浏览器配置文件提交到仓库或分享给他人。
|
|
37
|
+
|
|
38
|
+
## 已知限制
|
|
39
|
+
|
|
40
|
+
- 在会检测 CDP 的站点上,PageRig 不会附加 debugger,部分功能会降级或不可用;
|
|
41
|
+
- MCP server 采用单实例端口模型,端口被占用时会返回 `PORT_IN_USE`;
|
|
42
|
+
- 扩展使用 MV3 service worker,空闲时可能被 Chrome 终止,需要等待自动重连。
|
package/lib/bridge.mjs
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PageRig bridge.
|
|
3
|
+
*
|
|
4
|
+
* The Chrome extension can only make outbound connections, so it connects to a
|
|
5
|
+
* loopback WebSocket served here. The MCP server embeds this bridge and calls
|
|
6
|
+
* into it directly; there is no separate daemon and no HTTP command endpoint.
|
|
7
|
+
*/
|
|
8
|
+
import { createServer } from 'node:http';
|
|
9
|
+
import { randomUUID } from 'node:crypto';
|
|
10
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
11
|
+
|
|
12
|
+
const DEFAULT_HOST = '127.0.0.1';
|
|
13
|
+
const DEFAULT_PORT = 17321;
|
|
14
|
+
const DEFAULT_COMMAND_TIMEOUT_MS = 120000;
|
|
15
|
+
const DEFAULT_WAIT_EXTENSION_MS = 35000;
|
|
16
|
+
const MAX_WAIT_HEARTBEAT_MS = 60000;
|
|
17
|
+
|
|
18
|
+
function positiveNumber(value, fallback) {
|
|
19
|
+
const number = Number(value);
|
|
20
|
+
return Number.isFinite(number) && number > 0 ? number : fallback;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function disconnectedError(message = 'extension_not_connected') {
|
|
24
|
+
const error = new Error(message);
|
|
25
|
+
error.code = 'EXTENSION_DISCONNECTED';
|
|
26
|
+
error.hint = 'Chrome 未加载 PageRig 扩展,或扩展 Service Worker 正在重连。';
|
|
27
|
+
return error;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function portInUseError(port, cause) {
|
|
31
|
+
const error = new Error(`port_in_use: ${port}`);
|
|
32
|
+
error.code = 'PORT_IN_USE';
|
|
33
|
+
error.hint = `另一个 PageRig MCP server 已占用 ${port}。关闭那个 agent 会话,或设置 BB_PORT 并同步修改扩展的服务地址。`;
|
|
34
|
+
error.cause = cause;
|
|
35
|
+
return error;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function closeWebSocket(ws) {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
if (ws.readyState === WebSocket.CLOSED) {
|
|
41
|
+
resolve();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const timer = setTimeout(() => {
|
|
45
|
+
try {
|
|
46
|
+
ws.terminate();
|
|
47
|
+
} catch {}
|
|
48
|
+
resolve();
|
|
49
|
+
}, 500);
|
|
50
|
+
ws.once('close', () => {
|
|
51
|
+
clearTimeout(timer);
|
|
52
|
+
resolve();
|
|
53
|
+
});
|
|
54
|
+
try {
|
|
55
|
+
ws.close();
|
|
56
|
+
} catch {
|
|
57
|
+
clearTimeout(timer);
|
|
58
|
+
resolve();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Start the loopback WebSocket bridge.
|
|
65
|
+
*
|
|
66
|
+
* @param {object} [options]
|
|
67
|
+
* @param {string} [options.host]
|
|
68
|
+
* @param {number} [options.port]
|
|
69
|
+
* @param {number} [options.commandTimeoutMs]
|
|
70
|
+
* @param {number} [options.waitForExtensionMs]
|
|
71
|
+
* @param {(message: string) => void} [options.log]
|
|
72
|
+
*/
|
|
73
|
+
export async function startBridge(options = {}) {
|
|
74
|
+
const host = options.host || process.env.BB_HOST || DEFAULT_HOST;
|
|
75
|
+
const port = positiveNumber(options.port ?? process.env.BB_PORT, DEFAULT_PORT);
|
|
76
|
+
const commandTimeoutMs = positiveNumber(
|
|
77
|
+
options.commandTimeoutMs ?? process.env.BB_TIMEOUT,
|
|
78
|
+
DEFAULT_COMMAND_TIMEOUT_MS,
|
|
79
|
+
);
|
|
80
|
+
const waitForExtensionMs = positiveNumber(
|
|
81
|
+
options.waitForExtensionMs ?? process.env.BB_WAIT_EXT,
|
|
82
|
+
DEFAULT_WAIT_EXTENSION_MS,
|
|
83
|
+
);
|
|
84
|
+
const log = typeof options.log === 'function' ? options.log : (message) => console.error(message);
|
|
85
|
+
const startedAt = Date.now();
|
|
86
|
+
|
|
87
|
+
/** @type {import('ws').WebSocket | null} */
|
|
88
|
+
let extSocket = null;
|
|
89
|
+
let extensionVersion = null;
|
|
90
|
+
let everConnected = false;
|
|
91
|
+
let closed = false;
|
|
92
|
+
const pending = new Map();
|
|
93
|
+
|
|
94
|
+
const server = createServer((req, res) => {
|
|
95
|
+
// The bridge only speaks WebSocket; plain HTTP requests are not an API.
|
|
96
|
+
const body = JSON.stringify({ ok: false, error: 'not_found' });
|
|
97
|
+
res.writeHead(404, {
|
|
98
|
+
'content-type': 'application/json; charset=utf-8',
|
|
99
|
+
'content-length': Buffer.byteLength(body),
|
|
100
|
+
'cache-control': 'no-store',
|
|
101
|
+
});
|
|
102
|
+
res.end(body);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
106
|
+
|
|
107
|
+
server.on('upgrade', (req, socket, head) => {
|
|
108
|
+
if (closed) {
|
|
109
|
+
socket.destroy();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const url = new URL(req.url || '/', `http://${host}:${port}`);
|
|
113
|
+
if (url.pathname !== '/ext') {
|
|
114
|
+
socket.destroy();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const origin = req.headers.origin || '';
|
|
118
|
+
if (origin && !origin.startsWith('chrome-extension://')) {
|
|
119
|
+
socket.destroy();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
wss.handleUpgrade(req, socket, head, (ws) => wss.emit('connection', ws, req));
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
wss.on('connection', (ws) => {
|
|
126
|
+
// The MV3 worker may start more than one socket during startup. Keep the
|
|
127
|
+
// newest one for commands but do not close the older ones, otherwise the
|
|
128
|
+
// extension's reconnect handler can enter a connect/close loop.
|
|
129
|
+
extSocket = ws;
|
|
130
|
+
everConnected = true;
|
|
131
|
+
ws.lastSeen = Date.now();
|
|
132
|
+
log('[bridge] extension connected');
|
|
133
|
+
|
|
134
|
+
ws.on('message', (raw) => {
|
|
135
|
+
let message;
|
|
136
|
+
try {
|
|
137
|
+
message = JSON.parse(raw.toString());
|
|
138
|
+
} catch {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
ws.lastSeen = Date.now();
|
|
142
|
+
if (message.type === 'hello') {
|
|
143
|
+
extensionVersion = message.version || null;
|
|
144
|
+
log(`[bridge] extension hello v${extensionVersion}`);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (message.type === 'pong') return;
|
|
148
|
+
if (message.type === 'log') {
|
|
149
|
+
log(`[ext] ${message.message}`);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!message.id) return;
|
|
153
|
+
const entry = pending.get(message.id);
|
|
154
|
+
if (!entry) return;
|
|
155
|
+
pending.delete(message.id);
|
|
156
|
+
if (message.ok) {
|
|
157
|
+
entry.resolve(message.data);
|
|
158
|
+
} else {
|
|
159
|
+
const error = new Error(message.error || 'command_failed');
|
|
160
|
+
error.code = message.errorCode || 'COMMAND_FAILED';
|
|
161
|
+
error.hint = message.errorHint;
|
|
162
|
+
entry.reject(error);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
ws.on('close', () => {
|
|
167
|
+
if (extSocket !== ws) return;
|
|
168
|
+
extSocket = null;
|
|
169
|
+
log('[bridge] extension disconnected');
|
|
170
|
+
for (const [id, entry] of pending) {
|
|
171
|
+
pending.delete(id);
|
|
172
|
+
entry.reject(disconnectedError('extension_disconnected'));
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
ws.on('error', () => {});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// MV3 service workers are terminated when idle, and WebSocket messages do
|
|
180
|
+
// not reset that timer (Chrome 116+). A message every 20s keeps the worker
|
|
181
|
+
// warm while a session is in use; a browser that died without closing its
|
|
182
|
+
// socket is reaped here.
|
|
183
|
+
const keepalive = setInterval(() => {
|
|
184
|
+
if (!extSocket || extSocket.readyState !== WebSocket.OPEN) return;
|
|
185
|
+
if (Date.now() - (extSocket.lastSeen || 0) > MAX_WAIT_HEARTBEAT_MS) {
|
|
186
|
+
log('[bridge] extension heartbeat lost, dropping stale socket');
|
|
187
|
+
try {
|
|
188
|
+
extSocket.terminate();
|
|
189
|
+
} catch {}
|
|
190
|
+
extSocket = null;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
extSocket.send(JSON.stringify({ type: 'ping', ts: Date.now() }));
|
|
195
|
+
} catch {}
|
|
196
|
+
}, 20000);
|
|
197
|
+
keepalive.unref?.();
|
|
198
|
+
|
|
199
|
+
function waitForExtension(timeoutMs = waitForExtensionMs) {
|
|
200
|
+
if (extSocket && extSocket.readyState === WebSocket.OPEN) return Promise.resolve();
|
|
201
|
+
// A fresh process with no extension yet should fail fast. Once we know the
|
|
202
|
+
// extension exists, wait for its MV3 worker to wake up and reconnect.
|
|
203
|
+
let budget = timeoutMs;
|
|
204
|
+
if (!everConnected && budget > 1500) budget = 1500;
|
|
205
|
+
return new Promise((resolve, reject) => {
|
|
206
|
+
const started = Date.now();
|
|
207
|
+
const timer = setInterval(() => {
|
|
208
|
+
if (extSocket && extSocket.readyState === WebSocket.OPEN) {
|
|
209
|
+
clearInterval(timer);
|
|
210
|
+
resolve();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (closed) {
|
|
214
|
+
clearInterval(timer);
|
|
215
|
+
const error = disconnectedError('bridge_closed');
|
|
216
|
+
error.hint = 'PageRig MCP server 正在关闭。';
|
|
217
|
+
reject(error);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (Date.now() - started > budget) {
|
|
221
|
+
clearInterval(timer);
|
|
222
|
+
const error = disconnectedError();
|
|
223
|
+
error.hint = everConnected
|
|
224
|
+
? '扩展 Service Worker 正在重连。'
|
|
225
|
+
: 'Chrome 未加载 PageRig 扩展。';
|
|
226
|
+
reject(error);
|
|
227
|
+
}
|
|
228
|
+
}, 300);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function dispatch(action, params, timeoutMs = commandTimeoutMs) {
|
|
233
|
+
return new Promise((resolve, reject) => {
|
|
234
|
+
if (!extSocket || extSocket.readyState !== WebSocket.OPEN) {
|
|
235
|
+
reject(disconnectedError());
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const id = randomUUID();
|
|
239
|
+
const timer = setTimeout(() => {
|
|
240
|
+
pending.delete(id);
|
|
241
|
+
const error = new Error(`command_timeout after ${timeoutMs}ms`);
|
|
242
|
+
error.code = 'TIMEOUT';
|
|
243
|
+
reject(error);
|
|
244
|
+
}, timeoutMs);
|
|
245
|
+
pending.set(id, {
|
|
246
|
+
resolve: (value) => {
|
|
247
|
+
clearTimeout(timer);
|
|
248
|
+
resolve(value);
|
|
249
|
+
},
|
|
250
|
+
reject: (error) => {
|
|
251
|
+
clearTimeout(timer);
|
|
252
|
+
reject(error);
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
try {
|
|
256
|
+
extSocket.send(JSON.stringify({ action, ...params, id }));
|
|
257
|
+
} catch (error) {
|
|
258
|
+
pending.delete(id);
|
|
259
|
+
clearTimeout(timer);
|
|
260
|
+
reject(error);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function call(action, params = {}, timeoutMs = commandTimeoutMs) {
|
|
266
|
+
if (closed) {
|
|
267
|
+
const error = disconnectedError('bridge_closed');
|
|
268
|
+
error.hint = 'PageRig MCP server 正在关闭。';
|
|
269
|
+
throw error;
|
|
270
|
+
}
|
|
271
|
+
await waitForExtension();
|
|
272
|
+
return dispatch(action, params, timeoutMs);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function status() {
|
|
276
|
+
return {
|
|
277
|
+
connected: Boolean(extSocket && extSocket.readyState === WebSocket.OPEN),
|
|
278
|
+
everConnected,
|
|
279
|
+
extensionVersion,
|
|
280
|
+
host,
|
|
281
|
+
port,
|
|
282
|
+
uptimeMs: Date.now() - startedAt,
|
|
283
|
+
pending: pending.size,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
async function close() {
|
|
288
|
+
if (closed) return;
|
|
289
|
+
closed = true;
|
|
290
|
+
clearInterval(keepalive);
|
|
291
|
+
for (const [id, entry] of pending) {
|
|
292
|
+
pending.delete(id);
|
|
293
|
+
entry.reject(disconnectedError('bridge_closed'));
|
|
294
|
+
}
|
|
295
|
+
await Promise.all([...wss.clients].map(closeWebSocket));
|
|
296
|
+
await new Promise((resolve) => wss.close(() => resolve()));
|
|
297
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
await new Promise((resolve, reject) => {
|
|
301
|
+
const onError = (error) => {
|
|
302
|
+
server.off('listening', onListening);
|
|
303
|
+
try {
|
|
304
|
+
wss.close();
|
|
305
|
+
} catch {}
|
|
306
|
+
try {
|
|
307
|
+
server.close();
|
|
308
|
+
} catch {}
|
|
309
|
+
if (error.code === 'EADDRINUSE') {
|
|
310
|
+
reject(portInUseError(port, error));
|
|
311
|
+
} else {
|
|
312
|
+
const wrapped = new Error(`bridge_start_failed: ${error.message || error}`);
|
|
313
|
+
wrapped.code = 'BRIDGE_START_FAILED';
|
|
314
|
+
wrapped.cause = error;
|
|
315
|
+
reject(wrapped);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
const onListening = () => {
|
|
319
|
+
server.off('error', onError);
|
|
320
|
+
server.on('error', (error) => log(`[bridge] server error: ${error.message || error}`));
|
|
321
|
+
log(`[bridge] listening on ws://${host}:${port}/ext`);
|
|
322
|
+
resolve();
|
|
323
|
+
};
|
|
324
|
+
server.once('error', onError);
|
|
325
|
+
server.once('listening', onListening);
|
|
326
|
+
server.listen(port, host);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
return { call, status, close, host, port };
|
|
330
|
+
}
|
package/mcp-server.mjs
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* PageRig MCP server.
|
|
4
|
+
*
|
|
5
|
+
* Transport: stdio. This process also owns the loopback WebSocket bridge that
|
|
6
|
+
* the Chrome extension connects to, so browser commands are dispatched
|
|
7
|
+
* in-process. There is no separate daemon and no HTTP command endpoint.
|
|
8
|
+
*/
|
|
9
|
+
import { writeFile } from 'node:fs/promises';
|
|
10
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
11
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
12
|
+
import { z } from 'zod';
|
|
13
|
+
import { startBridge } from './lib/bridge.mjs';
|
|
14
|
+
|
|
15
|
+
const server = new McpServer({ name: 'pagerig', version: '0.1.0' });
|
|
16
|
+
const port = Number(process.env.BB_PORT || 17321);
|
|
17
|
+
|
|
18
|
+
let bridge = null;
|
|
19
|
+
let startupError = null;
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
bridge = await startBridge({ port });
|
|
23
|
+
} catch (error) {
|
|
24
|
+
startupError = error;
|
|
25
|
+
console.error(`[pagerig] bridge failed to start (${error.code || 'BRIDGE_START_FAILED'}): ${error.message}`);
|
|
26
|
+
if (error.hint) console.error(`[pagerig] ${error.hint}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function call(action, params = {}, timeoutMs) {
|
|
30
|
+
if (startupError) throw startupError;
|
|
31
|
+
if (!bridge) {
|
|
32
|
+
const error = new Error('bridge_not_started');
|
|
33
|
+
error.code = 'BRIDGE_NOT_STARTED';
|
|
34
|
+
error.hint = 'PageRig bridge 未启动。';
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
return bridge.call(action, params, timeoutMs);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function ok(data) {
|
|
41
|
+
return { content: [{ type: 'text', text: JSON.stringify(data, null, 2) }] };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function fail(error) {
|
|
45
|
+
const payload = { ok: false, error: String(error?.message || error) };
|
|
46
|
+
if (error?.code) payload.errorCode = error.code;
|
|
47
|
+
if (error?.hint) payload.hint = error.hint;
|
|
48
|
+
return { isError: true, content: [{ type: 'text', text: JSON.stringify(payload, null, 2) }] };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function tool(name, config, handler) {
|
|
52
|
+
server.registerTool(name, config, async (args) => {
|
|
53
|
+
try {
|
|
54
|
+
return await handler(args || {});
|
|
55
|
+
} catch (error) {
|
|
56
|
+
return fail(error);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const tabId = z.number().int().optional().describe('Chrome tab id; defaults to the bound tab or the focused tab.');
|
|
62
|
+
|
|
63
|
+
tool(
|
|
64
|
+
'browser_snapshot',
|
|
65
|
+
{
|
|
66
|
+
description:
|
|
67
|
+
'Read the current page: url, title, visible text and interactive elements with stable refs (r1, r2...). ' +
|
|
68
|
+
'Refs are valid until the next snapshot.',
|
|
69
|
+
inputSchema: { maxElements: z.number().int().min(10).max(500).optional(), tabId },
|
|
70
|
+
},
|
|
71
|
+
(args) => call('snapshot', args).then(ok),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
tool(
|
|
75
|
+
'browser_eval',
|
|
76
|
+
{
|
|
77
|
+
description:
|
|
78
|
+
'Execute JavaScript in the page MAIN world (can read window.* globals such as app state) and return the JSON result. ' +
|
|
79
|
+
'Wrap expressions in parentheses when needed.',
|
|
80
|
+
inputSchema: { code: z.string(), tabId },
|
|
81
|
+
},
|
|
82
|
+
(args) => call('eval', args).then(ok),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
tool(
|
|
86
|
+
'browser_network',
|
|
87
|
+
{
|
|
88
|
+
description:
|
|
89
|
+
'Read captured requests incrementally. Pass the returned cursor back to get only new or updated entries. ' +
|
|
90
|
+
'Headers come from webRequest, response bodies from a page hook. Response bodies are previewed to 4KB by default; ' +
|
|
91
|
+
'pass raw=true for the full body (can be hundreds of KB).',
|
|
92
|
+
inputSchema: {
|
|
93
|
+
cursor: z.number().int().optional(),
|
|
94
|
+
filter: z.string().optional().describe('Substring matched against the URL.'),
|
|
95
|
+
method: z.string().optional().describe('Case-insensitive HTTP method filter.'),
|
|
96
|
+
limit: z.number().int().min(1).max(500).optional(),
|
|
97
|
+
clear: z.boolean().optional(),
|
|
98
|
+
tabId,
|
|
99
|
+
raw: z.boolean().optional(),
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
(args) => call('network', { ...args, ...(args.raw ? { bodyLimit: 2000000 } : {}) }).then(ok),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
tool(
|
|
106
|
+
'browser_console',
|
|
107
|
+
{
|
|
108
|
+
description: 'Read captured console output and uncaught errors incrementally (cursor-based).',
|
|
109
|
+
inputSchema: {
|
|
110
|
+
cursor: z.number().int().optional(),
|
|
111
|
+
filter: z.string().optional(),
|
|
112
|
+
clear: z.boolean().optional(),
|
|
113
|
+
limit: z.number().int().min(1).max(500).optional(),
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
(args) => call('console', args).then(ok),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
tool(
|
|
120
|
+
'browser_click',
|
|
121
|
+
{
|
|
122
|
+
description:
|
|
123
|
+
'Click an element by ref from browser_snapshot or by CSS selector. Uses a real CDP mouse event, with a synthetic fallback.',
|
|
124
|
+
inputSchema: {
|
|
125
|
+
ref: z.string().optional(),
|
|
126
|
+
selector: z.string().optional(),
|
|
127
|
+
double: z.boolean().optional().describe('Double click (two clicks with clickCount 1 then 2).'),
|
|
128
|
+
button: z.enum(['left', 'right', 'middle']).optional(),
|
|
129
|
+
tabId,
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
(args) => call('click', args).then(ok),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
tool(
|
|
136
|
+
'browser_status',
|
|
137
|
+
{
|
|
138
|
+
description:
|
|
139
|
+
'Report the bridge state, bound tab, capture counters and extension connectivity. ' +
|
|
140
|
+
'Works even before the extension connects, and reports PORT_IN_USE when another MCP server owns the port.',
|
|
141
|
+
inputSchema: {},
|
|
142
|
+
},
|
|
143
|
+
async () => {
|
|
144
|
+
const local = bridge
|
|
145
|
+
? bridge.status()
|
|
146
|
+
: {
|
|
147
|
+
connected: false,
|
|
148
|
+
everConnected: false,
|
|
149
|
+
extensionVersion: null,
|
|
150
|
+
host: null,
|
|
151
|
+
port,
|
|
152
|
+
pending: 0,
|
|
153
|
+
uptimeMs: 0,
|
|
154
|
+
};
|
|
155
|
+
let extension = null;
|
|
156
|
+
if (bridge && local.connected) {
|
|
157
|
+
extension = await bridge.call('status').catch(() => null);
|
|
158
|
+
}
|
|
159
|
+
return ok({
|
|
160
|
+
...local,
|
|
161
|
+
...(extension || {}),
|
|
162
|
+
startupError: startupError
|
|
163
|
+
? {
|
|
164
|
+
code: startupError.code || 'BRIDGE_START_FAILED',
|
|
165
|
+
message: String(startupError.message || startupError),
|
|
166
|
+
hint: startupError.hint,
|
|
167
|
+
}
|
|
168
|
+
: null,
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
tool(
|
|
174
|
+
'browser_type',
|
|
175
|
+
{
|
|
176
|
+
description:
|
|
177
|
+
'Type text into the focused element, or focus ref/selector first. Uses real CDP input; clears the field by default.',
|
|
178
|
+
inputSchema: {
|
|
179
|
+
text: z.string(),
|
|
180
|
+
ref: z.string().optional(),
|
|
181
|
+
selector: z.string().optional(),
|
|
182
|
+
clear: z.boolean().optional(),
|
|
183
|
+
tabId,
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
(args) => call('type', args).then(ok),
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
tool(
|
|
190
|
+
'browser_key',
|
|
191
|
+
{
|
|
192
|
+
description: 'Press one key: Enter, Tab, Escape, Backspace, Delete, ArrowUp/Down/Left/Right, or a single character.',
|
|
193
|
+
inputSchema: { key: z.string(), tabId },
|
|
194
|
+
},
|
|
195
|
+
(args) => call('key', args).then(ok),
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
tool(
|
|
199
|
+
'browser_hover',
|
|
200
|
+
{
|
|
201
|
+
description: 'Hover ref/selector via CDP mouse movement, or synthetic mouseover/mouseenter fallback.',
|
|
202
|
+
inputSchema: { ref: z.string().optional(), selector: z.string().optional(), tabId },
|
|
203
|
+
},
|
|
204
|
+
(args) => call('hover', args).then(ok),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
tool(
|
|
208
|
+
'browser_scroll',
|
|
209
|
+
{
|
|
210
|
+
description: 'Scroll an element into view or scroll the window; changes viewport and may fire scroll listeners.',
|
|
211
|
+
inputSchema: {
|
|
212
|
+
ref: z.string().optional(),
|
|
213
|
+
selector: z.string().optional(),
|
|
214
|
+
dy: z.number().optional(),
|
|
215
|
+
dx: z.number().optional(),
|
|
216
|
+
align: z.enum(['start', 'center', 'end']).optional(),
|
|
217
|
+
to: z.enum(['top', 'bottom']).optional(),
|
|
218
|
+
smooth: z.boolean().optional(),
|
|
219
|
+
tabId,
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
(args) => call('scroll', args).then(ok),
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
tool(
|
|
226
|
+
'browser_select',
|
|
227
|
+
{
|
|
228
|
+
description: 'Select a value or exact visible label in a <select> and dispatch input/change events.',
|
|
229
|
+
inputSchema: {
|
|
230
|
+
ref: z.string().optional(),
|
|
231
|
+
selector: z.string().optional(),
|
|
232
|
+
value: z.string().optional(),
|
|
233
|
+
label: z.string().optional(),
|
|
234
|
+
tabId,
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
(args) => call('select', args).then(ok),
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
tool(
|
|
241
|
+
'browser_check',
|
|
242
|
+
{
|
|
243
|
+
description: 'Set a checkbox or radio to checked/unchecked with a native click when the state changes.',
|
|
244
|
+
inputSchema: {
|
|
245
|
+
ref: z.string().optional(),
|
|
246
|
+
selector: z.string().optional(),
|
|
247
|
+
checked: z.boolean().optional(),
|
|
248
|
+
tabId,
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
(args) => call('check', args).then(ok),
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
tool(
|
|
255
|
+
'browser_text',
|
|
256
|
+
{
|
|
257
|
+
description: 'Read page or element visible text with repeated blank lines collapsed.',
|
|
258
|
+
inputSchema: {
|
|
259
|
+
ref: z.string().optional(),
|
|
260
|
+
selector: z.string().optional(),
|
|
261
|
+
max: z.number().int().min(1).max(200000).optional(),
|
|
262
|
+
tabId,
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
(args) => call('text', args).then(ok),
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
tool(
|
|
269
|
+
'browser_dom',
|
|
270
|
+
{
|
|
271
|
+
description: "Read outerHTML or innerHTML for an element; defaults to the document's root element.",
|
|
272
|
+
inputSchema: {
|
|
273
|
+
ref: z.string().optional(),
|
|
274
|
+
selector: z.string().optional(),
|
|
275
|
+
mode: z.enum(['outer', 'inner']).optional(),
|
|
276
|
+
max: z.number().int().min(1).max(200000).optional(),
|
|
277
|
+
tabId,
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
(args) => call('dom', args).then(ok),
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
tool(
|
|
284
|
+
'browser_history',
|
|
285
|
+
{
|
|
286
|
+
description: 'Navigate tab history back/forward or reload it, then wait for the load event.',
|
|
287
|
+
inputSchema: {
|
|
288
|
+
op: z.enum(['back', 'forward', 'reload']),
|
|
289
|
+
timeoutMs: z.number().int().optional(),
|
|
290
|
+
tabId,
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
(args) => call('history', args).then(ok),
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
tool(
|
|
297
|
+
'browser_cookies',
|
|
298
|
+
{
|
|
299
|
+
description: 'Read cookies for an optional domain or URL, including HttpOnly cookies.',
|
|
300
|
+
inputSchema: { domain: z.string().optional(), url: z.string().optional() },
|
|
301
|
+
},
|
|
302
|
+
(args) => call('cookies', args).then(ok),
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
tool(
|
|
306
|
+
'browser_upload',
|
|
307
|
+
{
|
|
308
|
+
description: 'Attach local files to an <input type="file"> through CDP without opening the native picker.',
|
|
309
|
+
inputSchema: {
|
|
310
|
+
selector: z.string(),
|
|
311
|
+
files: z.array(z.string()).min(1),
|
|
312
|
+
tabId,
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
(args) => call('upload', args).then(ok),
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
tool(
|
|
319
|
+
'browser_screenshot',
|
|
320
|
+
{
|
|
321
|
+
description:
|
|
322
|
+
'Capture a PNG. Returns image content (viewport by default). Pass fullPage for the whole document, ' +
|
|
323
|
+
'or savePath to write the file and avoid a large inline payload.',
|
|
324
|
+
inputSchema: { fullPage: z.boolean().optional(), savePath: z.string().optional(), tabId },
|
|
325
|
+
},
|
|
326
|
+
async (args) => {
|
|
327
|
+
const result = await call('screenshot', { fullPage: Boolean(args.fullPage), tabId: args.tabId });
|
|
328
|
+
if (args.savePath) {
|
|
329
|
+
await writeFile(args.savePath, Buffer.from(result.data, 'base64'));
|
|
330
|
+
return ok({ saved: args.savePath, fullPage: result.fullPage, tabId: result.tabId });
|
|
331
|
+
}
|
|
332
|
+
if (result.data.length > 2_000_000) {
|
|
333
|
+
return ok({
|
|
334
|
+
tabId: result.tabId,
|
|
335
|
+
fullPage: result.fullPage,
|
|
336
|
+
note: 'image too large for inline content; pass savePath to write it to disk',
|
|
337
|
+
base64Length: result.data.length,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
return { content: [{ type: 'image', data: result.data, mimeType: 'image/png' }] };
|
|
341
|
+
},
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
tool(
|
|
345
|
+
'browser_navigate',
|
|
346
|
+
{
|
|
347
|
+
description: 'Navigate the bound/focused tab to an http(s) URL and wait for load completion.',
|
|
348
|
+
inputSchema: { url: z.string(), timeoutMs: z.number().int().optional(), tabId },
|
|
349
|
+
},
|
|
350
|
+
(args) => call('navigate', args).then(ok),
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
tool(
|
|
354
|
+
'browser_wait',
|
|
355
|
+
{
|
|
356
|
+
description:
|
|
357
|
+
'Wait until a condition holds: kind=selector (CSS present), kind=text (body text contains), ' +
|
|
358
|
+
'kind=xhr (a captured request URL contains the value), kind=idle (DOM stops growing) or ' +
|
|
359
|
+
'kind=networkidle (no new captured request for quietMs).',
|
|
360
|
+
inputSchema: {
|
|
361
|
+
kind: z.enum(['selector', 'text', 'xhr', 'idle', 'networkidle']),
|
|
362
|
+
value: z.string().default(''),
|
|
363
|
+
timeoutMs: z.number().int().min(100).max(120000).optional(),
|
|
364
|
+
quietMs: z.number().int().min(100).max(10000).optional(),
|
|
365
|
+
tabId,
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
(args) => call('wait', args).then(ok),
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
tool(
|
|
372
|
+
'browser_tabs',
|
|
373
|
+
{
|
|
374
|
+
description: 'List tabs, open a new tab, select one (also binds it), or close one.',
|
|
375
|
+
inputSchema: {
|
|
376
|
+
op: z.enum(['list', 'new', 'select', 'close']),
|
|
377
|
+
tabId: z.number().int().optional(),
|
|
378
|
+
url: z.string().optional(),
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
(args) => call('tabs', args).then(ok),
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
tool(
|
|
385
|
+
'browser_bind',
|
|
386
|
+
{
|
|
387
|
+
description: 'Bind subsequent commands to a tab (defaults to the currently focused tab).',
|
|
388
|
+
inputSchema: { tabId: z.number().int().optional() },
|
|
389
|
+
},
|
|
390
|
+
(args) => call('bind', args).then(ok),
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
let shuttingDown = false;
|
|
394
|
+
|
|
395
|
+
async function shutdown() {
|
|
396
|
+
if (shuttingDown) return;
|
|
397
|
+
shuttingDown = true;
|
|
398
|
+
try {
|
|
399
|
+
await server.close();
|
|
400
|
+
} catch {}
|
|
401
|
+
try {
|
|
402
|
+
await bridge?.close();
|
|
403
|
+
} catch {}
|
|
404
|
+
setTimeout(() => process.exit(0), 50);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// StdioServerTransport does not watch stdin for EOF. Without these handlers the
|
|
408
|
+
// process can outlive its MCP client and keep the loopback port occupied.
|
|
409
|
+
process.stdin.on('end', shutdown);
|
|
410
|
+
process.stdin.on('close', shutdown);
|
|
411
|
+
process.on('SIGINT', shutdown);
|
|
412
|
+
process.on('SIGTERM', shutdown);
|
|
413
|
+
|
|
414
|
+
const transport = new StdioServerTransport();
|
|
415
|
+
transport.onclose = shutdown;
|
|
416
|
+
await server.connect(transport);
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pagerig",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local bridge that lets an agent read and drive the user's already-logged-in Chrome.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"pagerig": "./mcp-server.mjs"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"mcp-server.mjs",
|
|
15
|
+
"lib/",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"SECURITY.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"mcp": "node mcp-server.mjs",
|
|
22
|
+
"build": "bun build mcp-server.mjs --compile --outfile dist/pagerig",
|
|
23
|
+
"test": "node test/e2e.mjs",
|
|
24
|
+
"version:set": "node scripts/version.mjs set",
|
|
25
|
+
"version:check": "node scripts/version.mjs check",
|
|
26
|
+
"pack:check": "npm pack --dry-run"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/hxhy00/pagerig.git"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/hxhy00/pagerig/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/hxhy00/pagerig#readme",
|
|
36
|
+
"keywords": [
|
|
37
|
+
"mcp",
|
|
38
|
+
"model-context-protocol",
|
|
39
|
+
"chrome",
|
|
40
|
+
"browser-automation",
|
|
41
|
+
"browser-control",
|
|
42
|
+
"ai-agent"
|
|
43
|
+
],
|
|
44
|
+
"mcpName": "io.github.hxhy00/pagerig",
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
50
|
+
"ws": "^8.18.3",
|
|
51
|
+
"zod": "^4.6.7"
|
|
52
|
+
}
|
|
53
|
+
}
|