在 Cursor 中使用 MCP 设置多 Agent 工作流

随着项目复杂度的增加,单个 AI Agent 可能不再足够。Cursor 通过 MCP(模型上下文协议)服务器支持多 Agent 工作流,允许多个专业 Agent 同时协作处理项目的不同方面。本指南向你展示如何有效地设置和协调多个 Agent。
什么是多 Agent 协调?
多 Agent 协调允许你:
- 拆分复杂任务 到专业 Agent 之间(例如,一个负责前端,一个负责后端)
- 并行工作 在代码库的不同部分
- 保持关注点分离 为特定领域配备专门的 Agent
- 扩展你的 AI 辅助 随着项目增长
先决条件
在设置多 Agent 工作流之前:
- Cursor Pro 或更高版本 - 多 Agent 功能需要付费计划
- MCP 服务器支持 - 确保在 Cursor 设置中启用了 MCP
- Git 仓库 - 多 Agent 工作流最适合使用版本控制
设置工作树配置
.cursor/worktrees.json 文件是多 Agent 协调的关键。
步骤 1:创建配置文件
在项目根目录创建 .cursor/worktrees.json:
{
"worktrees": [
{
"id": "frontend-agent",
"name": "Frontend Agent",
"description": "Handles UI components, styling, and client-side logic",
"directories": ["src/components", "src/pages", "src/styles", "public"],
"rules": [
"Use React and TypeScript",
"Follow the existing component patterns",
"Use Tailwind CSS for styling",
"Ensure responsive design"
]
},
{
"id": "backend-agent",
"name": "Backend Agent",
"description": "Manages API endpoints, database models, and server logic",
"directories": ["src/api", "src/models", "src/middleware", "migrations"],
"rules": [
"Use Express.js with TypeScript",
"Follow RESTful conventions",
"Implement proper error handling",
"Add input validation"
]
},
{
"id": "test-agent",
"name": "Testing Agent",
"description": "Writes and maintains test suites",
"directories": ["tests", "src/__tests__", "cypress"],
"rules": [
"Use Jest for unit tests",
"Use React Testing Library for component tests",
"Aim for 80%+ coverage",
"Write integration tests for API endpoints"
]
}
]
}
步骤 2:配置 MCP 服务器
将 MCP 服务器添加到你的 Cursor 设置(Cmd/Ctrl + Shift + P → "Cursor Settings"):
{
"mcpServers": {
"task-coordinator": {
"command": "npx",
"args": ["-y", "@cursor-task/coordinator"],
"env": {
"WORKTREE_CONFIG": "./.cursor/worktrees.json"
}
},
"file-sync": {
"command": "npx",
"args": ["-y", "@cursor-task/file-sync"]
}
}
}
使用多 Agent 模式
启动多 Agent 会话
- 打开 Composer(
Cmd/Ctrl + I) - 点击 Agent 选择器(Composer 顶部)
- 选择 "Multi-Agent" 模式
- 选择要激活的 Agent
任务分配
Agent 会根据你引用的文件自动接收任务:
@src/components/UserProfile.tsx @src/api/users.ts
实现一个用户资料页面,从 API 获取数据并显示。
前端 Agent 应该处理 UI,后端 Agent 应该确保 API 端点返回正确的数据。
手动任务路由
你也可以将任务定向到特定 Agent:
[@frontend-agent] 创建一个响应式导航组件,带有移动端汉堡菜单
[@backend-agent] 添加一个 /api/navigation 端点,根据用户角色返回菜单项
Agent 之间的通信
Agent 通过共享的任务文件系统进行通信。
任务文件
任务在 .cursor/tasks/ 中跟踪:
.cursor/
tasks/
TASK-001-frontend-nav.md
TASK-002-backend-api.md
TASK-003-integration-test.md
每个任务文件包含:
# TASK-001: Navigation Component
## Status: IN_PROGRESS
## Assigned: frontend-agent
## Dependencies: None
## Description
Create a responsive navigation component...
## Acceptance Criteria
- [ ] Mobile hamburger menu works
- [ ] Desktop horizontal layout
- [ ] Active state highlighting
## Notes
- Use the existing Button component
- Follow the design in Figma (link)
Agent 交接
当一个 Agent 完成另一个 Agent 依赖的任务时:
## Handoff Notes
Completed by: frontend-agent
Handed to: test-agent
The Navigation component is in src/components/Navigation.tsx.
Props interface is defined. Ready for testing.
多 Agent 工作流的最佳实践
1. 定义清晰的边界
每个 Agent 应该有明确的范围:
{
"id": "database-agent",
"directories": ["src/db", "migrations", "seeds"],
"rules": [
"Only modify files in the assigned directories",
"Run migrations before committing changes",
"Document schema changes in CHANGELOG.md"
]
}
2. 使用共享契约
定义 Agent 之间同意的接口:
// src/types/shared.ts
// This file is read by ALL agents
export interface ApiResponse<T> {
data: T;
success: boolean;
error?: string;
}
export interface User {
id: string;
email: string;
name: string;
role: 'admin' | 'user';
}
3. 实现冲突解决
当 Agent 修改相同的文件时:
{
"conflictResolution": {
"strategy": "last-write-wins",
"notification": true,
"manualReview": ["src/types/shared.ts", "package.json"]
}
}
4. 监控 Agent 活动
跟踪每个 Agent 正在做什么:
# View active tasks
cat .cursor/tasks/*.md | grep "Status: IN_PROGRESS"
# Check recent agent commits
git log --oneline --all --grep="agent:" | head -20
示例:完整的多 Agent 工作流
让我们构建一个三个 Agent 协同工作的功能:
阶段 1:后端 Agent
[@backend-agent] 创建一个带有 CRUD 操作的 /api/products 端点。
使用 src/db/index.ts 中的现有数据库连接。
遵循 src/types/shared.ts 中的 ApiResponse 接口。
阶段 2:前端 Agent(在后端提交后开始)
[@frontend-agent] 创建一个从 /api/products 获取数据的 ProductList 组件。
使用 React Query 进行数据获取。
在响应式网格中显示产品。
阶段 3:测试 Agent(在前端提交后开始)
[@test-agent] 为 ProductList 组件和 /api/products 端点编写测试。
包括错误状态测试。
目标覆盖率达到 85%+。
多 Agent 问题故障排除
| 问题 | 解决方案 |
|---|---|
| Agent 互相覆盖更改 | 在 worktrees.json 中定义更严格的目录边界 |
| 任务未被接收 | 检查任务文件是否在 .cursor/tasks/ 中且状态正确 |
| Agent 在共享文件上冲突 | 将文件添加到 conflictResolution 中的 manualReview |
| 一个 Agent 空闲 | 确保任务依赖项被标记为已完成 |
高级:自定义 MCP 服务器
对于专业工作流,创建自定义 MCP 服务器:
// mcp-server-custom.js
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
const server = new Server({
name: 'custom-task-router',
version: '1.0.0'
}, {
capabilities: {
tools: {}
}
});
server.setRequestHandler('tools/call', async (request) => {
if (request.params.name === 'route-task') {
const { task, agentPool } = request.params.arguments;
// Custom routing logic
const bestAgent = selectBestAgent(task, agentPool);
return {
content: [{
type: 'text',
text: `Task routed to ${bestAgent}`
}]
};
}
});