做了个 opencli 插件,通过 Chrome DevTools Protocol 直接操控 Cursor IDE 。不是调 CLI ,是真的在操控 IDE 的 UI——你在终端发的 prompt ,打开 Cursor 对话还在。
GitHub: https://github.com/toheart/cursor-agent
Cursor 3 出了独立 Agent 窗口之后,我日常需要:
之前用 Cursor CLI 方案,四个问题一直没解决:上下文断裂( CLI 和 IDE 是两个会话)、展示效果差、粒度粗、配置麻烦。
Cursor 是 Electron 应用 → 底层是 Chromium → 支持 CDP ( Chrome DevTools Protocol )。
启动时加个参数:
Cursor.exe --remote-debugging-port=9226
然后通过 WebSocket 连上去,就能 Runtime.evaluate 执行 JS 、Input.insertText 输入文本、Input.dispatchMouseEvent 模拟点击、Page.captureScreenshot 截图。
本质上就是把 F12 开发者工具能干的事,封装成了终端命令。
opencli cursor status # 检查 CDP 连接
opencli cursor list # 列出所有窗口(自动识别 Agent/Editor )
opencli cursor send "prompt" # 发 Prompt 并等待 AI 回复
opencli cursor read # 读取当前对话内容
opencli cursor model # 列出可用模型
opencli cursor model-switch "sonnet" # 模糊匹配切模型
opencli cursor history # 翻历史会话
opencli cursor screenshot # 截图保存 PNG
opencli cursor new-chat # 新建对话
opencli cursor project # 列出项目( Agent 窗口)
opencli cursor project-switch "my-app" # 切项目
所有命令自动适配 Cursor 3 的 Agent 独立窗口和传统 Editor 嵌入式聊天面板,不需要手动指定窗口类型。
列出窗口:
┌─────┬────────┬──────────────────────────────────────┐
│ Idx │ Type │ Title │
├─────┼────────┼──────────────────────────────────────┤
│ 1 │ Editor │ blog-openclaw-soul.md - cursor-agent │
│ 2 │ Agent │ Cursor Agents │
│ 3 │ Editor │ SKILL.md - ai-pipeline │
└─────┴────────┴──────────────────────────────────────┘
切模型:
┌─────┬────────────┬────────┬─────────┐
│ Idx │ Name │ Tier │ Current │
├─────┼────────────┼────────┼─────────┤
│ 1 │ Composer 2 │ Fast │ │
│ 2 │ Sonnet 4.6 │ Medium │ │
│ 3 │ Opus 4.6 │ High │ ✓ │
└─────┴────────────┴────────┴─────────┘
CLI 方案的会话活在 CLI 进程里——退了就没了。
CDP 方案的会话活在 IDE 里——你从终端、从脚本、从远程触发的每一条 prompt ,都真实存在于 Cursor IDE 的对话历史中。回到电脑前打开 Cursor ,刚才的对话就在那里,直接接着聊。
# 一行安装,零依赖( esbuild 内联打包,不需要 npm install )
opencli plugin install github:toheart/cursor-agent/cursor-cdp
# 确认连接
opencli cursor status
前提:Cursor 需要带 --remote-debugging-port=9226 参数启动(在快捷方式里加就行,需要先关闭所有 Cursor 窗口再重启)。
#!/bin/bash
opencli cursor project-switch "my-backend"
opencli cursor new-chat
opencli cursor model-switch "opus"
opencli cursor send "分析 src/auth 模块有没有安全隐患"
opencli cursor screenshot --output report.png
五行脚本,完整的自动化编程流程。
项目开源,基于 opencli 插件体系: https://github.com/toheart/cursor-agent
有问题楼下聊,欢迎 Star 。