cdp-browser-mcp

v0.2.0
cdp-browser-mcp Checked 4 days ago Published 2 months ago

MCP server that lets an AI assistant inspect running browsers over the Chrome DevTools Protocol: enumerate browser processes, capture console logs, network…

These tools were reported by the server itself during our smoke test (tools/list), not copied from the README.

browser_list_processes 列出浏览器进程

枚举本机正在运行的浏览器进程,标出哪些可以通过 CDP 附加(需要 --remote-debugging-port)。Firefox/Safari 不走 CDP,会被标记为不可附加。

ParameterTypeDescription
filterstring按浏览器名或命令行过滤
includeChildrenboolean是否包含 renderer/gpu 等子进程,默认 false

browser_discover 扫描调试端口

扫描本机的 DevTools HTTP 端点(默认 9222–9400),找出已经开启远程调试的浏览器。

ParameterTypeDescription
frominteger起始端口,默认 9222
hoststring目标主机,默认 127.0.0.1
portsarray额外要探测的端口
tointeger结束端口,默认 9400

browser_launch 启动浏览器

以独立 user-data-dir 启动一个带远程调试端口的 Chromium 浏览器(不污染你的个人配置),默认立即附加会话。

ParameterTypeDescription
argsarray额外的 Chromium 启动参数
attachboolean是否启动后自动附加会话,默认 true
emulateVisibleboolean强制页面报告为可见(覆盖 headless 下的 hidden 状态),便于测试运行时逻辑/动画/游戏循环
executablestring浏览器可执行文件绝对路径,优先级高于 kind
headlessboolean是否使用 --headless=new
hoststring
keepProfileboolean关闭后保留临时 profile,默认 false
kindstring
portinteger调试端口,缺省自动挑选空闲端口
targetIdstring附加后要切换到的标签页
urlstring启动后打开的 URL
userDataDirstring指定持久化 profile 目录;缺省用临时目录
watchAllTargetsboolean是否为所有标签页(含之后新开的)都开启抓取,默认 false

browser_connect 连接浏览器

附加到已开启调试端口的浏览器实例(或给定完整 WebSocket 地址),建立 CDP 会话。

ParameterTypeDescription
autoAttachboolean是否自动附加页面,默认 true
emulateVisibleboolean强制页面报告为可见(覆盖 headless 下的 hidden 状态)
hoststring
portinteger调试端口;省略时自动扫描
targetIdstring要切换到的标签页 ID;省略时自动选第一个可用页面
watchAllTargetsboolean连到浏览器后是否为所有标签页(含之后新开的)都开启抓取,默认 false 只抓当前页
webSocketUrlstring完整 browser WebSocket 地址,优先于 port

browser_close 关闭连接或浏览器

断开当前 CDP 会话;可选按端口关闭由本服务启动的浏览器实例,或全部关闭。

ParameterTypeDescription
closeAllboolean关闭本服务启动的所有实例
killBrowserboolean同时关闭当前连接对应的已启动实例
portinteger要关闭的已启动实例端口

browser_installed 查找已安装浏览器

列出本机已安装的 Chromium 系浏览器及其可执行文件路径,供 browser_launch 使用。

target_list 列出标签页

列出当前浏览器中可附加的页面。默认隐藏扩展页 / service worker / devtools 等后台目标(Edge、Chrome 装着扩展时这类目标往往有十几个),需要全量时把 includeBackground 设为 true。

ParameterTypeDescription
includeBackgroundboolean是否包含扩展页、service worker、offscreen 文档等后台目标,默认 false

target_select 切换标签页

附加到指定标签页并把它设为当前抓取对象;后续 console / network / DOM 工具都作用在这个页面上。

ParameterTypeDescription
targetId *string目标 ID,可用 target_list 查看

session_info 会话状态

查看当前会话的连接信息、目标页、缓冲计数与告警。常用于判断“现在到底连着谁”。

session_dump 导出原始会话数据

把当前缓冲里的全部数据按 CapturedData 结构返回(不做落盘),适合你想自己加工时用。

ParameterTypeDescription
includeDomboolean
includePerformanceboolean
includeScreenshotboolean
includeStorageboolean

console_read 读取 console 日志

读取已捕获的 console 输出,支持按级别、关键字、时间、来源页过滤。适用于排查报错、定位日志来源。

ParameterTypeDescription
errorsOnlyboolean只要 error 级别与异常
formatstring输出格式,默认 json
level级别过滤:log/debug/info/warn/error/trace 等,可传数组
limitinteger最多返回条数,默认 200
searchstring文本包含匹配
sincenumber仅返回该时间戳(epoch ms)之后的条目
targetIdstring限定某个标签页

console_clear 清空 console 缓冲

清空当前会话缓冲的 console 数据(不影响已经落盘的产物)。

network_list 列出网络请求

列出捕获到的网络请求,可按 URL 关键字、方法、资源类型、状态码过滤,也可只看失败或 4xx/5xx。

ParameterTypeDescription
failuresOnlyboolean只要加载失败的请求
httpErrorsOnlyboolean只要 4xx/5xx
limitinteger最多返回条数,默认 200
methodstringHTTP 方法,如 GET/POST
searchstringURL 包含匹配
sincenumber仅返回该时间戳之后的请求
status状态码,或 failed/error 表示只要失败
targetIdstring
typestring资源类型:document/script/xhr/fetch/image 等

network_detail 查看单个请求详情

按 requestId 查看单个请求的完整信息:请求头、响应头、Post body、timing、initiator、cookie 等。

ParameterTypeDescription
requestId *stringCDP requestId,可用 network_list 获取

network_body 获取响应体

取指定请求的响应体内容(文本)。二进制响应建议结合 mimeType 自行判断是否需要 base64。

ParameterTypeDescription
requestId *string
maxCharsinteger最多返回字符数,默认 20000

network_clear 清空网络缓冲

清空当前会话缓冲的网络请求记录。

page_errors 页面错误列表

列出捕获到的未捕获异常、console.error、日志错误与渲染进程崩溃,含 URL 与调用栈。

ParameterTypeDescription
limitinteger最多返回条数,默认 100

page_dom 抓取页面 DOM

获取当前页面的 DOM:可返回原始 HTML、适合 LLM 阅读的缩进结构大纲,或两者。默认 both,并自动截断超长 HTML。

ParameterTypeDescription
depthinteger大纲层级深度,默认 4
maxHtmlCharsintegerHTML 最大字符数,默认 50000
maxNodesinteger大纲最多节点数,默认 800
maxTextLengthinteger大纲里文本节点的最大长度,默认 60
modestring输出内容,默认 both
skipHiddenboolean是否跳过 script/style/noscript,默认 true

page_evaluate 执行 JavaScript

在当前页面上下文执行一段 JavaScript 并返回结果(支持 await Promise),等价于 DevTools Console。

ParameterTypeDescription
expression *string要执行的 JS 表达式或语句
awaitPromiseboolean是否等待返回的 Promise,默认 true
returnByValueboolean是否按值返回,默认 true
timeoutMsinteger执行超时(毫秒),默认 30000;长任务可调大避免被砍断

page_screenshot 页面截图

对当前页面截图。图片数据默认不回传(体积大),需要看图时把 returnBase64 设为 true。

ParameterTypeDescription
formatstring图片格式,默认 png
fullPageboolean是否尝试整页截图,默认 false
maxBytesinteger返回 base64 的体积上限,默认 800000 字节
qualityintegerjpeg/webp 质量 0-100
returnBase64boolean是否返回 base64 数据,默认 false

resources_list 页面资源树(Sources 面板)

返回开发者工具「源代码 / Sources > 页面」面板里那棵树:按 frame 分层,列出页面实际加载的全部资源(文档、样式表、脚本、图片、字体、音视频等),并交叉带上对应的 requestId / 状态码 / 大小。与 network_list 互补:这里给的是页面资源视角(含已进入页面但未必在 Network 缓冲里的资源),network_list 给的是 HTTP 请求视角。

ParameterTypeDescription
targetIdstring限定某个标签页,默认当前页

resources_get 读取资源内容(按 URL)

按 URL 读取页面资源的内容,等同于在开发者工具 Sources 面板里点开某个文件看源码。会自动采用 UTF-8 纠偏,避免无 charset 的中文文本变乱码。图片/音视频等二进制会返回 base64。

ParameterTypeDescription
url *string资源 URL,可用 resources_list 或 network_list 获取
asBase64boolean强制按 base64 返回原始字节,适合图片/音视频
maxCharsinteger最多返回字符数,默认 20000

performance_metrics 性能指标

获取 Performance.getMetrics 指标,以及导航计时、绘制时间和 JS 堆占用。

storage_read 读取页面存储

读取当前页面的 Cookies、localStorage、sessionStorage 与 IndexedDB 数据库名。

cdp_send 发送任意 CDP 命令

向页面(默认)或浏览器(browserLevel)发送任意 Chrome DevTools Protocol 命令,返回完整结果。一次性解锁 Emulation.setDeviceMetricsOverride、Page.bringToFront、Input.dispatchKeyEvent、DOM.focus 等未单独封装的能力。

ParameterTypeDescription
method *stringCDP 方法名,例如 "Emulation.setDeviceMetricsOverride" 或 "Input.dispatchKeyEvent"
browserLevelboolean为 true 时命令发往浏览器根(不带 sessionId),如 Target.* / Browser.*
paramsobject命令参数对象
sessionIdstring显式指定页面会话 ID;省略则用当前附加页
timeoutMsinteger命令超时(毫秒),默认 15000

page_click 点击页面

在视口坐标或 CSS 选择器中心点击鼠标(真实 Input.dispatchMouseEvent,press+release)。用于游戏/Canvas/表单等合成事件无效的场景。

ParameterTypeDescription
x *number视口 X 坐标(与 selector 二选一)
y *number视口 Y 坐标(与 selector 二选一)
buttonstring鼠标键,默认 left
doubleboolean是否双击(clickCount=2)
selectorstring要点击的元素 CSS 选择器;命中后自动滚动到视野中心并取其中心

page_key 按键 / 按住 / 松开

通过 Input.dispatchKeyEvent 发送真实键盘事件。action=press(默认)为按下并松开;action=down 仅按下(按住,可跨多帧,适合游戏移动);action=up 仅松开。

ParameterTypeDescription
key *string按键,如 "d"、"Enter"、"ArrowUp"、" "(空格)
actionstringpress=按下并松开(默认),down=仅按下,up=仅松开
codestringCDP code,如 "KeyD";省略时按 key 自动推导
modifiersinteger修饰键位掩码:1=Alt 2=Ctrl 4=Meta 8=Shift

page_type 输入文本

通过 Input.insertText 在当前焦点处输入文本(与真实打字一致,IME 无关),用于表单、聊天框等。

ParameterTypeDescription
text *string要输入的文本

page_wait_for 等待页面就绪

轮询直到某个 CSS 选择器出现、页面文本包含某串、或某段 JS 表达式返回真。替代手写固定 sleep,可靠性更高。

ParameterTypeDescription
pollMsinteger轮询间隔(毫秒),默认 250
predicatestring等待返回真值的 JS 表达式,如 "window.ready === true" 或 "(() => window.ready)()"
selectorstring等待出现的选择器(与 text / predicate 三选一)
textstring等待页面文本包含该串(与 selector / predicate 三选一)
timeoutMsinteger最大等待时间(毫秒),默认 10000

page_reload 刷新页面

通过 Page.reload 重新加载当前页面(比 page_evaluate 执行 location.reload() 更可靠)。

page_back 浏览器后退

在当前标签页执行 history.back() 返回上一页。

capture_start 开始一轮抓取

清空缓冲并记录起始时间,之后发生的 console / 网络 / 错误都会被计入本轮抓取。

ParameterTypeDescription
targetIdstring可选,顺带切换到该标签页

capture_status 抓取状态

查看当前会话的缓冲计数、目标页与是否处于某一轮抓取中。

capture_stop 结束抓取并汇总

结束当前这一轮抓取,返回统计与建议摘要(不落盘;要落盘用 capture_save)。

ParameterTypeDescription
includeDomboolean
includePerformanceboolean
includeScreenshotboolean
includeStorageboolean

capture_save 一键保存

把抓到的全部内容落盘成一个目录:session.json、console.json/csv、network.har、network.csv、metrics.json、storage.json、dom.html、截图、离线 HTML 报告 report.html、summary.md 与 manifest.json。

ParameterTypeDescription
dirstring直接指定输出目录(绝对路径);缺省用 captures/<会话名>
includeDomboolean是否抓取 DOM 快照,默认 true
includePerformanceboolean是否抓取性能指标,默认 true
includeScreenshotboolean是否抓取截图,默认 true
includeStorageboolean是否抓取存储数据,默认 true
includeSummaryboolean是否返回 Markdown 摘要文本,默认 true
maxRowsintegerHTML 报告里每表最多渲染行数,默认 500
namestring输出目录名,缺省用会话 ID
rootDirstring输出根目录,缺省 <cwd>/captures

capture_list_saved 列出已保存的抓取

列出之前保存过的抓取目录(含内存记录与 captures 根目录下的结果)。

ParameterTypeDescription
rootDirstring要扫描的根目录,缺省 <cwd>/captures

events_subscribe 订阅实时事件

订阅实时事件流:所有新发生的 console 输出、网络请求(请求/响应/完成/失败)、异常、导航、标签页增减、性能指标采样与页面画面帧都会即时进入该订阅。返回 subscriptionId,之后用 events_wait(阻塞等待)或 events_read(立即取)消费。

ParameterTypeDescription
bufferinteger保留的最大事件数,默认 500(画面帧订阅默认 40)
channelsarray通道:console / network / error / navigation / target / metric / frame,省略表示全部
errorsOnlyboolean只看错误与失败请求
framesobject订阅页面实时画面(Page.startScreencast),可配置画质与帧率
levelarrayconsole 级别过滤,如 ["error","warn"]
maxDurationMsinteger持续这么久后自动退订
maxEventsinteger匹配到这么多条后自动退订
performanceSampleMsinteger每隔多少毫秒采样一次性能指标并产出 metric 事件
pushstring是否主动向客户端推送通知,默认 notification(若客户端不支持会自动退化为轮询)
searchstring关键字过滤,命中 URL / 文本 / 载荷
targetIdstring只看某个标签页

events_wait 等待实时事件

长轮询:等到有匹配的实时事件就立刻返回,最多等待 timeoutMs(默认 15 秒,上限 120 秒)。传入上次返回的 cursor 就不会漏也不会重。这是任何客户端都能用的实时消费方式。

ParameterTypeDescription
subscriptionId *stringevents_subscribe 返回的订阅 ID
cursorinteger上次消费到的游标位置
includeFrameDataboolean画面帧事件是否包含 base64 数据,默认 false
limitinteger本次最多返回多少条,默认 50
mininteger至少攒够几条才返回,默认 1
timeoutMsinteger最长等待毫秒数,默认 15000

events_read 读取实时事件快照

立即读取订阅里已积累的实时事件,不等待。适合先看一眼再决定是否阻塞等待。

ParameterTypeDescription
subscriptionId *string
cursorinteger只返回该游标之后的事件
includeFrameDataboolean
limitinteger最多返回条数,默认 50

events_unsubscribe 取消订阅

结束订阅并释放缓冲。会自动停止没人用的性能采样与画面推流。

ParameterTypeDescription
subscriptionId *string

events_list 列出实时订阅

列出所有订阅及其过滤条件、已匹配条数、缓冲条数与当前游标。

ParameterTypeDescription
includeHiddenboolean是否包含内部订阅(磁盘录制用),默认 false

recording_start 开始实时录制到磁盘

把实时事件流持续追加写入 JSONL 文件(每条一行),适合长时间监控而不怕内存爆掉;可同时把页面画面帧按序存成图片。用 recording_stop 结束并拿统计。

ParameterTypeDescription
channelsarray只录制指定通道,省略表示全部
dirstring直接指定输出目录(绝对路径),优先于 name
namestring输出目录名,缺省 live-<时间戳>
performanceSampleMsinteger同时开启性能采样写入事件流
rootDirstring输出根目录,缺省 <cwd>/captures
saveFramesboolean是否把画面帧存到 frames/ 目录,默认 false

recording_stop 停止录制

结束实时录制并输出统计信息与产物路径。

ParameterTypeDescription
recordingIdstring要停止的录制 ID;省略则停止全部