为什么用 Chrome DevTools MCP?

OpenClaw 内置了托管浏览器(Managed Browser),但启动一个完整 Chrome 实例对服务器资源要求较高。

Chrome DevTools MCP 的优势:

  • 复用已登录的浏览器,无需重新登录
  • 保留 Cookie 和 Session,绕过二次验证
  • 资源占用低,不需要启动额外的 Chrome 实例
  • 操作真实页面,所见即所得

启动 Chrome 远程调试

要让 OpenClaw 连接你的 Chrome,需要先开启远程调试端口。

方法一:命令行启动

1
2
3
4
5
6
7
8
9
10
# Linux
google-chrome --remote-debugging-port=9222

# macOS
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--remote-debugging-port=9222

# Windows
"C:\Program Files\Google\Chrome\Application\chrome.exe" \
--remote-debugging-port=9222

方法二:chrome://inspect

  1. 打开 Chrome,访问 chrome://inspect/#remote-debugging
  2. 启用远程调试端口
  3. OpenClaw 自动检测并连接

方法三:OpenClaw 自动启动

1
openclaw browser start --profile openclaw

配置 OpenClaw

~/.openclaw/openclaw.json 中配置浏览器连接:

单实例配置

1
2
3
4
5
6
7
8
9
{
"browser": {
"profiles": {
"user": {
"cdpPort": 9222
}
}
}
}

多实例配置

适用于需要同时操控多个浏览器的场景:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"browser": {
"profiles": {
"openclaw": {
"cdpPort": 18800
},
"work": {
"cdpPort": 18801
},
"personal": {
"cdpPort": 18802
}
}
}
}

SSH 远程连接

如果你的 OpenClaw 运行在远程服务器,需要通过 SSH 隧道把调试端口转发到本地:

1
ssh -L 9222:127.0.0.1:9222 root@服务器IP

多个端口同理:

1
2
3
ssh -L 18800:127.0.0.1:18800 \
-L 18801:127.0.0.1:18801 \
root@服务器IP

⚠️ SSH 隧道需要保持连接,可以将命令写成脚本后台运行。

使用方式

Profile=“user”:操控已登录浏览器

直接连接你正在使用的 Chrome,保留所有登录状态:

1
2
3
4
5
browser({
profile: "user",
action: "navigate",
url: "https://mail.google.com"
})

适用场景:操作已登录的邮箱、后台管理系统等。

Profile=“chrome-relay”:扩展中继模式

通过 Chrome 扩展控制现有标签页,需要安装扩展:

1
2
3
4
5
# 安装扩展
openclaw browser extension install

# 查看扩展目录
openclaw browser extension path

安装步骤:

  1. 打开 Chrome → chrome://extensions
  2. 启用「开发者模式」
  3. 点击「加载已解压的扩展程序」,选择上述命令输出的目录
  4. 将扩展图标固定到工具栏
  5. 在目标标签页点击扩展图标,状态变为 ON 即可

扩展徽章状态说明:

徽章 含义
ON 已附加,可控制该标签页
正在连接中继
! 中继不可达

常见问题

1. tab not found

现象tabs 命令能列出标签页,但 screenshotsnapshot 报错。

原因:多个标签页同时附加时,共享同一个 WebSocket,Playwright 无法确定操作目标。

解决方案:一次只附加一个标签页,操作完再切换。

2. cdpReady: false

现象:状态显示 cdpReady: false

解决方案

  • 确认 Chrome 已开启远程调试端口
  • 确认端口未被防火墙拦截
  • 升级到最新版:npm install -g openclaw@latest

3. SSH 隧道断开

解决方案:使用 autossh 保持连接:

1
autossh -M 0 -f -N -L 9222:127.0.0.1:9222 root@服务器IP

4. Sandbox 中无法使用扩展中继

原因:Sandbox 会话默认 target="sandbox",而扩展中继需要控制 host 浏览器。

解决方案:在配置中允许 host 浏览器控制:

1
2
3
4
5
6
7
8
9
10
11
{
"agents": {
"defaults": {
"sandbox": {
"browser": {
"allowHostControl": true
}
}
}
}
}

然后在调用 browser 工具时指定 target="host"

参考资料