Cursor MCP 是 Claude 桌面应用与 Cursor 编辑器之间的桥梁,支持无缝的人工智能驱动自动化和多实例管理。它是更广泛的模型上下文协议(MCP)生态系统的一部分,允许 Cursor 通过标准化接口与其他各种人工智能模型和服务进行交互。
import { ClaudeMCP } from 'cursor-mcp/claude'
// 连接到 Claude 的桌面应用
const claude = await ClaudeM
CP.connect()
// 执行由 AI 驱动的操作
await claude.generateCode({
prompt: '创建一个 React 组件',
context: currentFileContent,
language: 'typescript'
})
// 获取 AI 建议
const suggestions = await claude.getSuggestions({
code: selectedText,
type: 'refactor'
})
import { MCPRegistry } from 'cursor-mcp/registry'
// 注册可用的 MCP
MCPRegistry.register('claude', ClaudeMCP)
MCPRegistry.register('github-copilot', CopilotMCP)
// 使用不同的 AI 服务
const claude = await MCPRegistry.get('claude')
const copilot = await MCPRegistry.get('github-copilot')
// 比较建议
const claudeSuggestions = await claude.getSuggestions(context)
const copilotSuggestions = await copilot.getSuggestions(context)
import { BaseMCP, MCPProvider } from 'cursor-mcp/core'
class CustomMCP extends BaseMCP implements MCPProvider {
async connect() {
// 自定义连接逻辑
}
async generateSuggestions(context: CodeContext) {
// 自定义 AI 集成
}
}
// 注册自定义 MCP
MCPRegistry.register('custom-ai', CustomMCP)
该工具可以通过环境变量或配置文件进行配置:
%LOCALAPPDATA%\cursor-mcp\config\config.json~/Library/Application Support/cursor-mcp/config/config.json~/.config/cursor-mcp/config.json示例配置:
{
"mcp": {
"claude": {
"enabled": true,
"apiKey": "${CLAUDE_API_KEY}",
"contextWindow": 100000
},
"providers": {
"github-copilot": {
"enabled": true,
"auth": "${GITHUB_TOKEN}"
}
}
},
"autoStart": true,
"maxInstances": 4,
"windowArrangement": "grid",
"logging": {
"level": "info",
"file": "cursor-mcp.log"
}
}
# 以管理员身份运行
Invoke-WebRequest -Uri "https://github.com/your-org/cursor-mcp/releases/latest/download/cursor-mcp-windows.zip" -OutFile "cursor-mcp.zip"
Expand-Archive -Path "cursor-mcp.zip" -DestinationPath "."
.\windows.ps1
# 使用 sudo 运行
curl -L "https://github.com/your-org/cursor-mcp/releases/latest/download/cursor-mcp-macos.zip" -o "cursor-mcp.zip"
unzip cursor-mcp.zip
sudo ./macos.sh
# 使用 sudo 运行
curl -L "https://github.com/your-org/cursor-mcp/releases/latest/download/cursor-mcp-linux.zip" -o "cursor-mcp.zip"
unzip cursor-mcp.zip
sudo ./linux.sh
import { CursorInstanceManager } from 'cursor-mcp'
// 获取实例管理器
const manager = CursorInstanceManager.getInstance()
// 启动新的 Cursor 实例
await manager.startNewInstance()
// 获取所有正在运行的实例
const instances = await manager.getRunningInstances()
// 聚焦特定实例
await manager.focusInstance(instances[0])
// 关闭所有实例
await manager.closeAllInstances()
import { WindowManager } from 'cursor-mcp'
const windowManager = WindowManager.getInstance()
// 查找所有 Cursor 窗口
const windows = await windowManager.findCursorWindows()
// 聚焦窗口
await windowManager.focusWindow(windows[0])
// 并排排列窗口
await windowManager.arrangeWindows(windows, 'sideBySide')
// 最小化所有窗口
for (const window of windows) {
await windowManager.minimizeWindow(window)
}
import { InputAutomationService } from 'cursor-mcp'
const inputService = InputAutomationService.getInstance()
// 输入文本
await inputService.typeText('你好,世界!')
// 发送键盘快捷键
if (process.platform === 'darwin') {
await inputService.sendKeys(['command', 'c'])
} else {
await inputService.sendKeys(['control', 'c'])
}
// 鼠标操作
await inputService.moveMouse(100, 100)
await inputService.mouseClick('left')
await inputService.mouseDrag(100, 100, 200, 200)
此工具作为 Cursor 和 MCP 服务器之间的中间层:
Cursor 集成:
MCP 协议翻译:
服务器通信:
graph LR
A[Cursor 编辑器] <--> B[Cursor MCP 桥梁]
B <--> C[Claude 桌面 MCP]
B <--> D[GitHub Copilot MCP]
B <--> E[自定义 AI MCPs]
代码补全请求:
// 1. Cursor 事件(文件更改)
// 当用户在 Cursor 中输入时:
function calculateTotal(items) {
// 计算项目的总价格| <-- 光标位置
// 2. 桥梁翻译
const event = {
type: 'completion_request',
context: {
file: 'shopping-cart.ts',
line: 2,
prefix: '// 计算项目的总价格',
language: 'typescript',
cursor_position: 43
}
}
// 3. MCP 协议消息
await mcpServer.call('generate_completion', {
prompt: event.context,
max_tokens: 150,
temperature: 0.7
})
// 4. 响应翻译
// 桥梁转换 MCP 响应:
const response = `return items.reduce((total, item) => {
return total + (item.price * item.quantity);
}, 0);`
// 5. Cursor 集成
// 桥梁在光标位置注入代码
代码重构:
// 1. Cursor 事件(命令)
// 用户选择代码并触发重构命令
const oldCode = `
if (user.age >= 18) {
if (user.hasLicense) {
if (car.isAvailable) {
rentCar(user, car);
}
}
}
`
// 2. 桥梁翻译
const event = {
type: 'refactor_request',
context: {
selection: oldCode,
command: 'simplify_nesting'
}
}
// 3. MCP 协议消息
await mcpServer.call('refactor_code', {
code: event.context.selection,
style: 'simplified',
maintain_logic: true
})
// 4. 响应翻译
const response = `
const canRentCar = user.age >= 18
&& user.hasLicense
&& car.isAvailable;
if (canRentCar) {
rentCar(user, car);
}
`
// 5. Cursor 集成
// 桥梁替换选中的代码
多文件上下文:
// 1. Cursor 事件(文件依赖)
// 当用户请求帮助组件时
// 2. 桥梁翻译
const event = {
type: 'context_request',
files: {
'UserProfile.tsx': '...',
'types.ts': '...',
'api.ts': '...'
},
focus_file: 'UserProfile.tsx'
}
// 3. MCP 协议消息
await mcpServer.call('analyze_context', {
files: event.files,
primary_file: event.focus_file,
analysis_type: 'component_dependencies'
})
// 4. 响应处理
// 桥梁跨请求维护上下文
文件系统监控:
import { FileSystemWatcher } from 'cursor-mcp/watcher'
const watcher = new FileSystemWatcher({
paths: ['/path/to/cursor/workspace'],
events: ['change', 'create', 'delete']
})
watcher.on('change', async (event) => {
const mcpMessage = await bridge.translateEvent(event)
await mcpServer.send(mcpMessage)
})
窗口集成:
import { CursorWindow } from 'cursor-mcp/window'
const window = new CursorWindow()
// 注入 AI 响应
await window.injectCode({
position: cursorPosition,
code: mcpResponse.code,
animate: true // 平滑打字动画
})
// 处理用户交互
window.onCommand('refactor', async (selection) => {
const mcpMessage = await bridge.createRefactorRequest(selection)
const response = await mcpServer.send(mcpMessage)
await window.applyRefactoring(response)
})
上下文管理:
import { ContextManager } from 'cursor-mcp/context'
const context = new ContextManager()
// 跟踪文件依赖
await context.addFile('component.tsx')
await context.trackDependencies()
// 维护对话历史
context.addMessage({
role: 'user',
content: '重构这个组件'
})
// 发送到 MCP 服务器
const response = await mcpServer.send({
type: 'refactor',
context: context.getFullContext()
})
# 克隆仓库
git clone https://github.com/your-org/cursor-mcp.git
cd cursor-mcp
# 安装依赖
npm install
# 构建项目
npm run build
# 运行测试
npm test
# 运行所有测试
npm test
# 运行特定测试套件
npm test -- window-management
# 运行带有覆盖率
npm run test:coverage
我们欢迎贡献!请参阅我们的 贡献指南 了解详细信息。
该项目根据 MIT 许可证发布 - 详情见 LICENSE 文件。