Cursor CLI Integration Skill
🤖 Cursor Agent - AI-powered coding assistant directly in your terminal.
What is Cursor CLI?
Cursor provides two CLI tools:
- 1.
cursor - Editor launcher (like code for VS Code) agent ⭐ - Interactive AI coding assistant with full tool access
Installation
CODEBLOCK0
Quick Start
Interactive Mode
CODEBLOCK1
Non-Interactive Mode (Scripts/CI)
CODEBLOCK2
Modes
| Mode | Description | Usage |
|---|
| Agent | Full access - can modify code | Default |
| Plan |
Read-only planning and analysis |
--plan or
/plan |
|
Ask | Q&A without making changes |
--mode=ask or
/ask |
Switch modes mid-conversation
CODEBLOCK3
Cloud Agent (Background Execution)
Run tasks in the cloud while you're away:
CODEBLOCK4
Track at: cursor.com/agents
Models
CODEBLOCK5
Advanced Options
Force Mode (Auto-approve)
CODEBLOCK6
Sandbox Control
CODEBLOCK7
Workspace & Worktree
CODEBLOCK8
Custom Headers & API Key
CODEBLOCK9
Output Formats
CODEBLOCK10
Authentication
CODEBLOCK11
Session Management
CODEBLOCK12
Cursor Rules Generation
CODEBLOCK13
MCP (Model Context Protocol) Management
CODEBLOCK14
Shell Integration
CODEBLOCK15
Use Cases
1. Code Refactoring
CODEBLOCK16
2. Bug Fixing
CODEBLOCK17
3. Feature Development
CODEBLOCK18
4. Code Review
CODEBLOCK19
5. Documentation
CODEBLOCK20
6. Testing
CODEBLOCK21
7. Performance Optimization
CODEBLOCK22
8. Migration
CODEBLOCK23
CI/CD Integration
CODEBLOCK24
Best Practices
✅ Do
- - Use
--plan mode first for complex changes - Review proposed changes before approving
- Use
--workspace to specify project directory - Save important sessions (they auto-save)
- Use
--force only in trusted environments
❌ Don't
- - Use
--yolo on production code without review - Share your API key
- Run in untrusted workspaces with INLINECODE11
- Ignore security warnings
OpenClaw Integration
Call agent from OpenClaw
CODEBLOCK25
Use in subagents
When spawning coding agents, you can use Cursor agent as an alternative:
CODEBLOCK26
Troubleshooting
Check version
CODEBLOCK27
Update to latest
CODEBLOCK28
View system info
CODEBLOCK29
Authentication issues
CODEBLOCK30
Workspace trust
If prompted about workspace trust in headless mode:
CODEBLOCK31
Configuration
Cursor agent reads from:
- -
~/.cursor/ - Configuration and cache - INLINECODE13 - Git worktrees
- Environment variables:
CURSOR_API_KEY, INLINECODE15
Keyboard Shortcuts (Interactive Mode)
| Shortcut | Action |
|---|
| INLINECODE16 | Toggle plan mode |
| INLINECODE17 |
Switch to plan mode |
|
/ask | Switch to ask mode |
|
/sandbox | Open sandbox menu |
|
/max-mode on | Enable max mode |
|
& <prompt> | Send to Cloud Agent |
Resources
- - 📖 Official Docs: https://cursor.com/docs/cli/overview
- 🌐 Cloud Agent: https://cursor.com/agents
- 💬 Community: https://forum.cursor.com
Version
Current Version: 2026.02.27-e7d2ef6
Check for updates: agent update
Examples
Example 1: Interactive Refactoring
CODEBLOCK32
Agent will:
- 1. Analyze current auth code
- Propose refactoring plan
- Show file changes
- Ask for approval
- Execute changes
Example 2: Automated Testing
CODEBLOCK33
Non-interactive, auto-approved test generation.
Example 3: Code Review in CI
CODEBLOCK34
Example 4: Cloud Background Task
CODEBLOCK35
Pro Tip: Use
agent ls frequently to review and resume past sessions. Context is preserved across sessions!
Cursor CLI 集成技能
🤖 Cursor Agent - 终端中的AI驱动编码助手。
什么是Cursor CLI?
Cursor提供两个CLI工具:
- 1. cursor - 编辑器启动器(类似于VS Code的code命令)
- agent ⭐ - 具有完整工具访问权限的交互式AI编码助手
安装
bash
macOS, Linux, WSL
curl https://cursor.com/install -fsS | bash
Windows PowerShell
irm https://cursor.com/install?win32=true | iex
验证安装
agent --version
快速开始
交互模式
bash
启动交互会话
agent
使用初始提示启动
agent 使用JWT令牌重构认证模块
恢复最新会话
agent resume
继续之前的会话
agent --continue
列出所有会话
agent ls
非交互模式(脚本/CI)
bash
使用特定提示运行
agent -p 查找并修复性能问题 --model gpt-5.2
代码审查
agent -p 审查这些更改是否存在安全问题 --output-format text
JSON输出以便解析
agent -p 分析此代码库结构 --output-format json
模式
| 模式 | 描述 | 用法 |
|---|
| Agent | 完全访问权限 - 可修改代码 | 默认 |
| Plan |
只读规划和分 | --plan 或 /plan |
|
Ask | 问答模式,不进行更改 | --mode=ask 或 /ask |
在对话中切换模式
/plan # 切换到计划模式
/ask # 切换到问答模式
Shift+Tab # 切换计划模式
云代理(后台执行)
在您离开时在云端运行任务:
bash
在云模式下启动
agent -c 重构认证模块并添加全面测试
在对话中发送到云端
& 重构认证模块并添加全面测试
跟踪地址:cursor.com/agents
模型
bash
列出可用模型
agent --list-models
使用特定模型
agent --model gpt-5.2
agent --model sonnet-4
agent --model sonnet-4-thinking
高级选项
强制模式(自动批准)
bash
自动批准所有命令
agent --force 构建功能
agent --yolo 构建功能 # 别名
沙箱控制
bash
启用/禁用沙箱
agent --sandbox enabled
agent --sandbox disabled
交互式沙箱菜单
/sandbox
工作区和工作树
bash
指定工作区
agent --workspace /path/to/project
在隔离的git工作树中启动
agent -w feature-branch
agent --worktree feature-branch --worktree-base main
自定义标头和API密钥
bash
设置API密钥
export CURSOR
APIKEY=your-key
agent --api-key your-key
添加自定义标头
agent -H X-Custom-Header: value
输出格式
bash
文本输出(默认)
agent -p 分析代码 --output-format text
JSON输出
agent -p 列出函数 --output-format json
流式JSON
agent -p 生成代码 --output-format stream-json --stream-partial-output
身份验证
bash
登录
agent login
检查状态
agent status
agent whoami
登出
agent logout
会话管理
bash
创建新的空聊天
agent create-chat
恢复特定聊天
agent --resume=chat-id-here
恢复最新会话
agent resume
agent --continue
Cursor规则生成
bash
交互式规则生成
agent generate-rule
agent rule
MCP(模型上下文协议)管理
bash
管理MCP服务器
agent mcp
自动批准MCP服务器
agent --approve-mcps
Shell集成
bash
安装shell集成
agent install-shell-integration
卸载
agent uninstall-shell-integration
使用场景
1. 代码重构
bash
agent 使用现代模式重构认证模块
2. Bug修复
bash
agent -p 查找并修复server.js中的内存泄漏
3. 功能开发
bash
agent 实现带验证的用户资料编辑功能
4. 代码审查
bash
审查未提交的更改
git diff | agent -p 审查这些更改是否存在安全问题
审查特定文件
agent 审查api/auth.js是否存在安全漏洞
5. 文档
bash
agent 为所有函数添加全面的JSDoc注释
6. 测试
bash
agent 为认证模块生成覆盖率达90%的单元测试
7. 性能优化
bash
agent 分析并优化user-service.js中的数据库查询
8. 迁移
bash
agent 从Express迁移到Fastify,保持所有功能不变
CI/CD集成
bash
#!/bin/bash
.github/workflows/code-review.yml
PR上的自动化代码审查
agent -p 审查更改文件是否存在安全和性能问题 \
--output-format json \
--trust \
> review-report.json
最佳实践
✅ 应该做
- - 对复杂更改先使用--plan模式
- 在批准前审查建议的更改
- 使用--workspace指定项目目录
- 保存重要会话(它们会自动保存)
- 仅在可信环境中使用--force
❌ 不应该做
- - 未经审查在生产代码上使用--yolo
- 分享您的API密钥
- 在不受信任的工作区中使用--trust运行
- 忽略安全警告
OpenClaw集成
从OpenClaw调用agent
javascript
// 交互式会话
exec({ command: agent 重构认证模块, pty: true })
// 非交互式
exec({ command: agent -p 分析代码 --output-format json })
在子代理中使用
在生成编码代理时,您可以使用Cursor agent作为替代:
javascript
sessions_spawn({
runtime: acp,
agentId: cursor, // 如果已配置
task: 重构认证模块
})
故障排除
检查版本
bash
agent --version
更新到最新版本
bash
agent update
查看系统信息
bash
agent about
身份验证问题
bash
重新认证
agent logout
agent login
工作区信任
如果在无头模式下提示工作区信任:
bash
agent --trust -p 您的提示
配置
Cursor agent读取自:
- - ~/.cursor/ - 配置和缓存
- ~/.cursor/worktrees/ - Git工作树
- 环境变量:CURSORAPIKEY、NOOPENBROWSER
键盘快捷键(交互模式)
| 快捷键 | 操作 |
|---|
| Shift+Tab | 切换计划模式 |
| /plan |
切换到计划模式 |
| /ask | 切换到问答模式 |
| /sandbox | 打开沙箱菜单 |
| /max-mode on | 启用最大模式 |
| & <提示> | 发送到云代理 |
资源
- - 📖 官方文档:https://cursor.com/docs/cli/overview
- 🌐 云代理:https://cursor.com/agents
- 💬 社区:https://forum.cursor.com
版本
当前版本:2026.02.27-e7d2ef6
检查更新:agent update
示例
示例1:交互式重构
bash
$ agent 重构用户认证系统
Agent将:
- 1. 分析当前认证代码
- 提出重构计划
- 显示文件更改
- 请求批准
- 执行更改
示例2:自动化测试
bash
$ agent -p 为src/api/*.js生成全面测试 --force
非交互式、自动批准的测试生成。
示例3:CI中的代码审查
yaml
.github/workflows/review.yml
run: |
agent -p 审查更改是否存在安全问题 \
--output-format json \
--trust \
> review.json
###