AI 技术三剑客:Skill、SubAgent 与 MCP 详解
SubAgent 是多智能体系统(Multi-Agent System)中承担特定子任务的独立执行单元,由主智能体(Main Agent 或 Coordinator)创建和调度,专注于完成明确定义的子任务,并将结果返回给主智能体进行汇总。:MCP(Model Context Protocol)是由 Anthropic 在 2024 年 11 月推出的开放标准协议,用于在 AI 应用程序与外部数据源
📖 写在前面
在 AI 快速发展的今天,三个核心概念正在重塑我们与 AI 的交互方式:Skill(技能)、SubAgent(子智能体) 和 MCP(模型上下文协议)。
这三者虽然都服务于让 AI 更好地完成任务的终极目标,但它们各自解决的问题、应用场景和实现方式截然不同。本文将深入剖析这三者的本质区别、适用场景和最佳实践。
🎯 一、核心概念速览
1.1 三者定位对比
┌─────────────────────────────────────────────────────────────────┐
│ AI 能力增强体系 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Skill │ │ SubAgent │ │ MCP │ │
│ │ (技能) │ │ (子智能体) │ │ (协议) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ "怎么做更好" "分工协作" "连接什么" │
│ How to do Work together What to connect │
│ │
│ 教授 AI 专业 拆解复杂任务 连接外部系统 │
│ 领域知识方法 并行执行处理 工具数据资源 │
│ │
└─────────────────────────────────────────────────────────────────┘
1.2 快速对比表
|
维度 |
Skill |
SubAgent |
MCP |
|---|---|---|---|
| 本质 |
嵌入式专业知识封装 |
任务分工的执行单元 |
标准化通信协议 |
| 类比 |
专家培训手册 |
专业团队协作 |
USB 接口标准 |
| 解决什么 |
AI 不知道"如何做" |
单个 AI 难以"同时做" |
AI 无法"接触到" |
| 作用范围 |
提升单个 AI 能力 |
扩展系统处理规模 |
统一数据/工具接入 |
| 技术层级 |
Prompt + 指令工程 |
多智能体架构 |
系统 API 协议 |
| 主要载体 |
配置文件/指令集 |
独立 Agent 实例 |
Server/Client |
| 激活方式 |
上下文匹配自动激活 |
主 Agent 派单调用 |
客户端主动连接 |
| 适用场景 |
领域专家任务、流程标准化 |
复杂任务拆解、并行处理 |
数据访问、工具集成 |
🔧 二、Skill(技能):嵌入 AI 的专业能力
2.1 什么是 Skill?
定义:Skill 是将特定领域的专业知识、工作流程和最佳实践打包成可复用的指令模块,嵌入到 AI 的上下文中,使 AI 在处理相关任务时能够自动激活并运用这些专业能力。
官方定义(来自 Anthropic):
"Skills bundle instructions, templates, code, and tool permissions in a testable way."
核心本质:
-
Skills 教 Claude 如何更好地做事
-
嵌入式专业知识封装
-
上下文驱动的自动激活机制
2.2 Skill 的组成结构
┌─────────────────────────────────────────────────────────────┐
│ Skill 结构 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 元数据(Metadata) │
│ ├─ 名称:如 "python-expert" │
│ ├─ 描述:Python 开发专家技能 │
│ └─ 触发关键词:["python", "代码", "调试"] │
│ │
│ 2. 系统提示(System Prompt) │
│ ├─ 角色定义:"你是一位资深 Python 开发者..." │
│ ├─ 行为准则:遵循 PEP 8,注重类型提示... │
│ └─ 输出格式:优先返回可执行代码... │
│ │
│ 3. 知识库(Knowledge Base) │
│ ├─ 领域知识:Python 最佳实践、常见陷阱... │
│ ├─ 代码模板:项目结构、设计模式... │
│ └─ 问题解决方案:调试技巧、性能优化... │
│ │
│ 4. 工具权限(Tool Permissions) │
│ ├─ 允许使用的工具:Read, Write, Bash, WebSearch │
│ └─ 工具使用约束:Write 只能操作 .py 文件... │
│ │
│ 5. 示例(Examples) │
│ └─ Few-shot 示例:展示期望的输出格式和质量 │
│ │
└─────────────────────────────────────────────────────────────┘
2.3 Skill 工作原理
用户输入
↓
【意图识别】
↓
匹配 Skill 触发条件?
├─ 是 → 加载 Skill 内容到上下文
│ ↓
│ 【增强 Prompt】
│ 原始 Prompt + Skill 系统提示 + 知识库
│ ↓
│ AI 处理(带专业能力)
│ ↓
│ 专业输出
│
└─ 否 → 普通 AI 处理
↓
常规输出
2.4 Skill 的典型应用场景
|
场景类别 |
示例 |
核心价值 |
|---|---|---|
| 编程开发 |
Python 专家、前端工程师、SQL 优化师 |
统一代码风格、最佳实践 |
| 内容创作 |
技术文档写作、营销文案生成、翻译专家 |
保证输出质量一致性 |
| 数据分析 |
数据清洗专家、可视化图表生成 |
标准化分析流程 |
| 业务流程 |
需求分析、测试用例生成、代码审查 |
固化专家经验 |
| 领域知识 |
法律顾问、医疗咨询、财务分析 |
嵌入专业知识 |
2.5 Skill 最佳实践
DO(应该做的):
✓ 明确定义触发条件,确保精准激活
✓ 提供清晰的 Few-shot 示例
✓ 限制工具权限范围,减少误用风险
✓ 编写可测试的 Skill(输入 → 输出验证)
✓ 模块化设计,单个 Skill 聚责单一职责
✓ 定期更新知识库内容
DON'T(不应该做的):
✗ 一个 Skill 包含过多不相关领域
✗ 硬编码过长的系统提示(影响上下文效率)
✗ 忽略输出格式规范
✗ 混合多个触发场景
✗ 缺少边界约束和错误处理
2.6 Skill vs System Prompt
|
维度 |
Skill |
System Prompt |
|---|---|---|
| 激活机制 |
自动触发,按需加载 |
始终存在,全局生效 |
| 上下文效率 |
高(只加载相关内容) |
低(始终占用上下文) |
| 可维护性 |
模块化,易于管理 |
耦合高,修改影响全局 |
| 可测试性 |
独立测试 |
需要整体测试 |
| 适用规模 |
大量专业领域场景 |
简单、通用场景 |
🤖 三、SubAgent(子智能体):任务分工的执行者
3.1 什么是 SubAgent?
定义:SubAgent 是多智能体系统(Multi-Agent System)中承担特定子任务的独立执行单元,由主智能体(Main Agent 或 Coordinator)创建和调度,专注于完成明确定义的子任务,并将结果返回给主智能体进行汇总。
核心本质:
-
任务拆解与分工的产物
-
独立执行单元
-
专业化的 Worker Agent
官方定义(来自 Anthropic Multi-Agent 实践):
"Workers are agents executing subtasks (also called subagents/子智能体), with the main agent acting as the 'command center' responsible for task decomposition and allocation."
3.2 SubAgent 在多智能体系统中的位置
┌─────────────────────────────────────────────────────────────────┐
│ Multi-Agent System │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────┐ │
│ │ Main Agent │ │
│ │ (协调器) │ │
│ │ Coordinator │ │
│ └───────┬───────┘ │
│ │ │
│ ┌───────────────┼───────────────┐ │
│ │ │ │ │
│ ┌───────▼───────┐ ┌────▼─────┐ ┌─────▼──────┐ │
│ │ SubAgent 1 │ │SubAgent 2│ │ SubAgent 3 │ │
│ │ (研究员) │ │ (程序员) │ │ (测试员) │ │
│ │ │ │ │ │ │ │
│ │ 收集行业数据 │ │ 编写代码 │ │ 执行测试 │ │
│ └───────────────┘ └──────────┘ └────────────┘ │
│ │ │ │ │
│ └───────────────┼───────────────┘ │
│ │ │
│ ┌───────▼───────┐ │
│ │ 结果汇总 │ │
│ │ 整合输出 │ │
│ └───────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
3.3 SubAgent 工作流程
阶段 1: 任务分解(Task Decomposition)
┌─────────────────────────────────────────────────────────────┐
│ Main Agent 接收用户复杂任务 │
│ ↓ │
│ 分析任务结构,识别可并行的子任务 │
│ ↓ │
│ 创建执行计划:Task 1, Task 2, Task 3... │
└─────────────────────────────────────────────────────────────┘
阶段 2: SubAgent 创建与派单(Delegation)
┌─────────────────────────────────────────────────────────────┐
│ 为每个子任务创建专门的 SubAgent │
│ ↓ │
│ 分配资源:上下文、工具权限、时间限制 │
│ ↓ │
│ 派单:SubAgent 1 → Task 1, SubAgent 2 → Task 2... │
└─────────────────────────────────────────────────────────────┘
阶段 3: 并行执行(Parallel Execution)
┌─────────────────────────────────────────────────────────────┐
│ SubAgent 1 ═══════════════════════════╗ │
│ ├─ 独立上下文 ║ 同时执行 │
│ ├─ 专用工具权限 ║ │
│ └─ 专注子任务 ═════════════════════╝ │
│ │
│ SubAgent 2 ═══════════════════════════╗ │
│ ├─ 独立上下文 ║ 同时执行 │
│ ├─ 专用工具权限 ║ │
│ └─ 专注子任务 ═════════════════════╝ │
└─────────────────────────────────────────────────────────────┘
阶段 4: 结果汇总(Result Aggregation)
┌─────────────────────────────────────────────────────────────┐
│ 各 SubAgent 完成任务,返回结果 │
│ ↓ │
│ Main Agent 汇总所有结果 │
│ ↓ │
│ 整合、验证、生成最终输出 │
└─────────────────────────────────────────────────────────────┘
3.4 SubAgent 的类型
|
类型 |
职责 |
示例场景 |
|---|---|---|
| 研究型 SubAgent |
信息收集、数据分析、市场调研 |
收集竞品信息、分析行业趋势 |
| 创作型 SubAgent |
内容生成、文案撰写 |
生成营销文案、编写技术文档 |
| 执行型 SubAgent |
代码编写、任务执行 |
编写功能代码、执行测试 |
| 审核型 SubAgent |
质量检查、合规审查 |
代码审查、内容校验 |
| 协调型 SubAgent |
子任务管理、资源调度 |
复杂项目的子任务协调 |
3.5 SubAgent 的优势
1. 任务并行化
单个 Agent 处理时间: T1 + T2 + T3 + T4 = 100 分钟
多 Agent 并行处理: max(T1, T2, T3, T4) = 30 分钟
性能提升: 70% ↑(Anthropic 实践数据)
2. 专业分工
单一 Agent 需要:
├─ 理解所有领域的知识
├─ 掌握所有技能
└─ 频繁上下文切换
多 Agent 系统:
├─ 每个 Agent 专注单一领域
├─ 深度专业知识
└─ 减少上下文干扰
3. 容错与韧性
单个 Agent 失败 → 整个任务失败
多 Agent 系统:
SubAgent 2 失败
↓
Main Agent 检测到错误
↓
重新派单或跳过该子任务
↓
整体任务继续进行
4. 可扩展性
新需求出现 → 添加新的 SubAgent 类型
无需修改现有 Agent 逻辑
3.6 SubAgent 最佳实践
设计原则(来自 Anthropic):
1. 单一职责原则
└─ 每个 SubAgent 只负责一个明确的子任务
2. 独立性原则
└─ SubAgent 之间尽量减少直接依赖
3. 可测试原则
└─ 每个 SubAgent 可独立测试验证
4. 资源限制原则
└─ 明确每个 SubAgent 的上下文和时间限制
5. 错误处理原则
└─ 为每个 SubAgent 设计失败恢复机制
实施建议:
# 伪代码示例
class MultiAgentSystem:
def create_subagent(self, task_type, instructions, tools):
"""创建专门的 SubAgent"""
return SubAgent(
role=task_type,
system_prompt=instructions,
allowed_tools=tools,
timeout=300 # 5分钟超时
)
def delegate_and_execute(self, main_task):
"""任务分解与派单执行"""
# 1. 分解任务
subtasks = self.decompose_task(main_task)
# 2. 创建 SubAgents
agents = [
self.create_subagent(
task_type=t.type,
instructions=t.instructions,
tools=t.required_tools
)
for t in subtasks
]
# 3. 并行执行
results = self.execute_parallel(agents, subtasks)
# 4. 汇总结果
return self.aggregate_results(results)
3.7 SubAgent vs Skill
这是最容易混淆的两个概念,关键区别在于:
|
维度 |
SubAgent |
Skill |
|---|---|---|
| 本质 |
独立的执行单元 |
嵌入的知识模块 |
| 运行方式 |
并行/串行独立运行 |
注入同一 AI 上下文 |
| 上下文 |
每个有独立上下文 |
共享主上下文 |
| 通信 |
通过消息/接口通信 |
直接在上下文中访问 |
| 适用场景 |
需要并行处理的复杂任务 |
需要专业知识的单任务 |
| 类比 |
专业团队成员 |
专家培训手册 |
选择指南:
需要同时做多个事?
├─ 是 → 使用 SubAgent(并行执行)
└─ 否 → 需要专业知识?
├─ 是 → 使用 Skill(增强能力)
└─ 否 → 普通 Prompt 足够
🔌 四、MCP(模型上下文协议):连接 AI 与外部世界
4.1 什么是 MCP?
定义:MCP(Model Context Protocol)是由 Anthropic 在 2024 年 11 月推出的开放标准协议,用于在 AI 应用程序与外部数据源、工具和系统之间建立安全、双向的连接。
官方定义(来自 Anthropic):
"MCP is an open standard enabling developers to build secure, two-way connections between data sources and AI-powered tools."
核心本质:
-
AI 世界的 USB 接口
-
标准化通信协议
-
连接器与桥接层
关键特点:
┌─────────────────────────────────────────────────────────────┐
│ MCP 的核心价值 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 统一标准 │
│ └─ 像USB-C一样,一个接口适配多种设备 │
│ │
│ 2. 双向连接 │
│ └─ AI 可以读取数据,也可以执行操作 │
│ │
│ 3. 安全可控 │
│ └─ 明确的权限控制和数据流向 │
│ │
│ 4. 生态丰富 │
│ └─ 大量现成的 MCP Server 可直接使用 │
│ │
└─────────────────────────────────────────────────────────────┘
4.2 MCP 架构
┌─────────────────────────────────────────────────────────────────┐
│ MCP 架构图 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ │
│ │ AI Application│ (Claude Desktop, Cursor, 等) │
│ │ (MCP Client) │ │
│ └──────┬───────┘ │
│ │ │
│ │ MCP Protocol (JSON-RPC over stdio/SSE) │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ MCP Host │ │
│ │ ┌────────────┐ ┌────────────┐ ┌────────────┐ │ │
│ │ │ MCP Server │ │ MCP Server │ │ MCP Server │ │ │
│ │ │ #1 │ │ #2 │ │ #3 │ │ │
│ │ │ (数据库) │ │ (文件系统) │ │ (API) │ │ │
│ │ └────────────┘ └────────────┘ └────────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ PostgreSQL │ │ 本地文件 │ │ GitHub API │ │
│ │ 数据库 │ │ │ │ │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
4.3 MCP 核心概念
1. Resources(资源)
# 定义可供 AI 读取的数据
{
"uri": "postgres://users/123",
"name": "用户信息",
"description": "获取指定用户的详细信息",
"mimeType": "application/json"
}
2. Tools(工具)
# 定义可供 AI 调用的功能
{
"name": "execute_sql",
"description": "执行 SQL 查询",
"inputSchema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "SQL 语句"}
},
"required": ["query"]
}
}
3. Prompts(提示模板)
# 定义预配置的提示模板
{
"name": "code_review",
"description": "代码审查提示",
"arguments": [
{
"name": "code",
"description": "要审查的代码",
"required": true
}
]
}
4.4 MCP 工作流程
用户请求
↓
AI 应用(MCP Client)
↓
【MCP Protocol】
↓
查询可用的 MCP Servers
↓
┌──────────────────────────────────────────────────────────┐
│ 选择合适的 MCP Server │
│ ├─ 数据查询 → Database MCP Server │
│ ├─ 文件操作 → Filesystem MCP Server │
│ └─ API 调用 → GitHub MCP Server │
└──────────────────────────────────────────────────────────┘
↓
通过 MCP Protocol 调用
↓
MCP Server 处理请求
↓
┌──────────────────────────────────────────────────────────┐
│ 返回结果 │
│ ├─ Resource: 数据内容 │
│ ├─ Tool: 执行结果 │
│ └─ Prompt: 格式化输出 │
└──────────────────────────────────────────────────────────┘
↓
AI 使用返回的数据生成最终响应
4.5 MCP Server 示例
// filesystem-mcp-server 示例(简化版)
import { MCPServer } from '@modelcontextprotocol/sdk/server';
const server = new MCPServer({
name: "filesystem",
version: "1.0.0"
});
// 定义 Resources - 读取文件
server.registerResource({
uri: "file://{path}",
name: "文件内容",
description: "读取指定文件的内容",
handler: async (uri) => {
const path = uri.pathname;
const content = await fs.readFile(path, 'utf-8');
return {
contents: [{ uri, text: content }]
};
}
});
// 定义 Tools - 写入文件
server.registerTool({
name: "write_file",
description: "写入内容到文件",
inputSchema: {
type: "object",
properties: {
path: { type: "string" },
content: { type: "string" }
},
required: ["path", "content"]
},
handler: async (args) => {
await fs.writeFile(args.path, args.content);
return { success: true };
}
});
// 启动 Server
server.start();
4.6 MCP 的优势
|
优势 |
说明 |
|---|---|
| 标准化 |
统一协议,无需为每个数据源单独集成 |
| 安全性 |
明确的权限控制,数据流向可追溯 |
| 可组合 |
多个 MCP Server 可以组合使用 |
| 可扩展 |
轻松添加新的数据源和工具 |
| 语言无关 |
支持 Python、TypeScript、Go 等多种语言 |
| 生态丰富 |
大量现成的 MCP Server 可直接使用 |
4.7 现有 MCP Server 生态
数据库类:
├─ Postgres MCP Server
├─ MySQL MCP Server
├─ SQLite MCP Server
└─ MongoDB MCP Server
文件系统类:
├─ Filesystem MCP Server (本地文件)
├─ Google Drive MCP Server
└─ OneDrive MCP Server
开发工具类:
├─ GitHub MCP Server
├─ GitLab MCP Server
├─ Jira MCP Server
└─ Linear MCP Server
API 集成类:
├─ Slack MCP Server
├─ Notion MCP Server
├─ AWS MCP Server
└─ Google Cloud MCP Server
实用工具类:
├─ Web Search MCP Server
├─ Browser MCP Server
├─ Puppeteer MCP Server
└─ Memory MCP Server
🔄 五、三者关系与协同使用
5.1 三者在 AI 系统中的位置
┌─────────────────────────────────────────────────────────────────┐
│ AI 系统架构全景 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 应用层 │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │
│ │ │ Chat Bot │ │ Code Editor │ │ Data Analyst│ │ │
│ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ AI 能力增强层(Skill) │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │
│ │ │Python Expert │ │Writer Skill │ │Analyst Skill │ │ │
│ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 任务执行层(SubAgent) │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │
│ │ │ Researcher │ │ Coder │ │ Reviewer │ │ │
│ │ │ SubAgent │ │ SubAgent │ │ SubAgent │ │ │
│ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 数据与工具连接层(MCP) │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │
│ │ │Database MCP │ │Filesystem MCP│ │ GitHub MCP │ │ │
│ │ │ Server │ │ Server │ │ Server │ │ │
│ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 外部世界 │ │
│ │ 数据库 · 文件系统 · API · 云服务 · Web │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
5.2 协同使用示例
场景:构建一个自动化代码审查系统
# 1. 使用 MCP 连接数据源
mcp_servers = {
"github": GitHubMCPServer(), # 获取 PR 代码
"jira": JiraMCPServer(), # 获取需求描述
"sonarqube": SonarMCPServer() # 获取代码质量指标
}
# 2. 使用 Skill 嵌入审查专业知识
code_review_skill = Skill(
name="code_reviewer",
system_prompt="你是一位资深代码审查专家...",
knowledge_base={
"best_practices": "...",
"security_checklist": "...",
"performance_patterns": "..."
}
)
# 3. 使用 SubAgent 分工协作
class CodeReviewOrchestrator:
def __init__(self):
self.main_agent = MainAgent()
def review_pull_request(self, pr_url):
# 任务分解
subtasks = [
{
"type": "security_review",
"subagent": SecurityReviewerSubAgent(),
"skill": SecuritySkill(),
"mcp": "github"
},
{
"type": "performance_review",
"subagent": PerformanceReviewerSubAgent(),
"skill": PerformanceSkill(),
"mcp": "sonarqube"
},
{
"type": "requirement_check",
"subagent": RequirementCheckerSubAgent(),
"skill": RequirementAnalysisSkill(),
"mcp": "jira"
}
]
# 并行执行
results = self.execute_parallel(subtasks, pr_url)
# 汇总结果
return self.aggregate_review(results)
5.3 选择决策树
你的问题是?
│
├─ 需要连接外部数据/工具?
│ └─ 是 → 使用 MCP
│
├─ 任务复杂需要多人协作?
│ ├─ 需要同时处理多个子任务?
│ │ └─ 是 → 使用 SubAgent(并行处理)
│ └─ 需要专业领域知识?
│ └─ 是 → 使用 Skill(嵌入专业知识)
│
└─ 只是简单的单一任务?
└─ 普通 Prompt 足够
5.4 组合模式
┌─────────────────────────────────────────────────────────────────┐
│ 常见组合模式 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 模式 1: MCP + Skill │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 通过 MCP 连接数据源 + 用 Skill 处理数据 │ │
│ │ │ │
│ │ 示例:Database MCP + DataAnalyst Skill │ │
│ │ → AI 可以访问数据库 + 懂得如何分析数据 │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
│ 模式 2: MCP + SubAgent │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 每个 SubAgent 通过专门的 MCP 访问资源 │ │
│ │ │ │
│ │ 示例:Researcher SubAgent → WebSearch MCP │ │
│ │ Coder SubAgent → GitHub MCP │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
│ 模式 3: Skill + SubAgent │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 每个 SubAgent 加载专门的 Skill │ │
│ │ │ │
│ │ 示例:Security SubAgent + SecurityExpert Skill │ │
│ │ Performance SubAgent + PerformanceSkill │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
│ 模式 4: MCP + Skill + SubAgent(终极组合) │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 复杂任务的完整解决方案 │ │
│ │ │ │
│ │ 示例:自动化软件开发流程 │ │
│ │ - Requirement SubAgent + AnalystSkill + Jira MCP │ │
│ │ - Developer SubAgent + CodingSkill + GitHub MCP │ │
│ │ - Tester SubAgent + TestingSkill + Jenkins MCP │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
📊 六、详细对比表
6.1 核心特征对比
|
特征 |
Skill |
SubAgent |
MCP |
|---|---|---|---|
| 技术类型 |
Prompt 工程 |
多智能体架构 |
通信协议 |
| 主要作用 |
嵌入专业知识 |
任务并行执行 |
连接外部系统 |
| 抽象层级 |
应用层 |
执行层 |
协议层 |
| 独立性 |
依赖主上下文 |
完全独立运行 |
独立进程 |
| 通信方式 |
上下文注入 |
消息传递 |
JSON-RPC |
| 激活机制 |
意图匹配自动激活 |
主 Agent 派单 |
客户端调用 |
| 资源消耗 |
低(共享上下文) |
高(多实例) |
中(独立进程) |
| 开发难度 |
低 |
中高 |
中 |
| 维护成本 |
低 |
中 |
中 |
| 可测试性 |
高 |
高 |
高 |
6.2 适用场景对比
|
场景 |
推荐方案 |
原因 |
|---|---|---|
| 领域专家任务 |
Skill |
需要专业知识,不需要并行 |
| 多步骤流程 |
Skill |
可将流程标准化为 Skill |
| 复杂任务拆解 |
SubAgent |
需要并行处理多个子任务 |
| 大规模信息收集 |
SubAgent |
多 Agent 并行收集效率高 |
| 访问数据库 |
MCP |
需要标准化数据访问 |
| 集成第三方 API |
MCP |
统一接口,便于维护 |
| 代码审查 |
Skill + SubAgent + MCP |
三者组合:专业知识+分工+数据访问 |
| 数据分析 |
Skill + MCP |
专业知识(Skill)+ 数据连接(MCP) |
| 内容生成 |
Skill |
不需要外部数据,只需专业能力 |
| 自动化测试 |
SubAgent + MCP |
分工执行(SubAgent)+ 系统(MCP) |
6.3 技术实现对比
# Skill 实现示例
skill_definition = {
"name": "python_expert",
"description": "Python 开发专家",
"trigger": ["python", "代码"],
"system_prompt": """你是一位资深 Python 开发者...
遵循 PEP 8,使用类型提示...""",
"examples": [
{"input": "写一个函数", "output": "def example()..."}
],
"allowed_tools": ["Read", "Write", "Bash"]
}
# SubAgent 实现示例
subagent = Agent(
name="researcher",
role="信息收集专家",
system_prompt="你负责收集和整理信息...",
tools=["WebSearch", "Read"],
parent_agent=main_agent
)
# MCP Server 实现示例
server = MCPServer(
name="database",
resources=[
Resource(uri="db://query", handler=query_handler)
],
tools=[
Tool(name="execute_sql", handler=sql_handler)
]
)
🎯 七、最佳实践与建议
7.1 Skill 最佳实践
# 推荐的 Skill 结构
name: "web-scraper-expert"
version: "1.0.0"
# 元数据
metadata:
author: "Your Name"
created: "2025-01-20"
tags: ["web-scraping", "python", "data"]
# 触发条件
triggers:
keywords: ["爬虫", "抓取", "scraper"]
file_types: [".py", ".html"]
# 系统提示
system_prompt: |
你是一位网络爬虫专家,精通各种爬虫技术...
你遵循以下原则:
1. 遵守 robots.txt
2. 添加适当的请求头
3. 使用合理的延迟
# 知识库
knowledge_base:
best_practices: |
- 使用 requests + BeautifulSoup 简单任务
- 使用 Scrapy 处理大规模爬取
- 使用 Selenium 处理 JavaScript 渲染
common_pitfalls: |
- 忘记设置 User-Agent
- 没有处理异常情况
- 请求过快被 IP 封禁
# 工具权限
tool_permissions:
allowed:
- Read
- Write
- Bash
- WebSearch
constraints:
Write:
allowed_patterns:
- "*.py"
- "*.json"
7.2 SubAgent 最佳实践
# Anthropic 推荐的 SubAgent 设计
class SubAgentDesign:
"""
设计原则:
1. 单一职责 - 每个 SubAgent 只做一件事
2. 独立性 - 尽量减少 SubAgent 间的依赖
3. 可测试性 - 每个 SubAgent 可独立测试
4. 资源限制 - 明确超时和上下文限制
"""
def design_subagent(self, task_spec):
return {
"name": task_spec.name,
"role": task_spec.role,
"system_prompt": task_spec.instructions,
"tools": task_spec.required_tools,
"context_limit": 8000, # 限制上下文
"timeout": 300, # 5分钟超时
"retry_policy": {
"max_retries": 3,
"backoff": "exponential"
},
"output_format": task_spec.expected_output,
"error_handling": task_spec.error_strategy
}
# 示例:创建一个研究型 SubAgent
researcher = SubAgentDesign().design_subagent(
TaskSpec(
name="market_researcher",
role="市场研究员",
instructions="收集指定市场的竞品信息...",
required_tools=["WebSearch", "Read"],
expected_output="JSON 格式的竞品对比表",
error_strategy="return_partial_results"
)
)
7.3 MCP Server 最佳实践
// MCP Server 开发最佳实践
import { MCPServer, Resource, Tool } from '@modelcontextprotocol/sdk';
// 1. 清晰的命名和描述
const server = new MCPServer({
name: "company-database", // ✅ 清晰的名称
version: "1.0.0",
description: "访问公司内部数据库" // ✅ 准确的描述
});
// 2. Resources - 只读数据访问
server.registerResource({
uri: "db://employee/{id}",
name: "员工信息",
description: "获取指定员工的详细信息",
mimeType: "application/json",
handler: async (uri, { log }) => {
// ✅ 参数验证
const id = validateEmployeeId(uri.pathname);
// ✅ 错误处理
try {
const employee = await db.employees.findUnique({
where: { id }
});
return {
contents: [{
uri,
text: JSON.stringify(employee, null, 2)
}]
};
} catch (error) {
log.error(`获取员工信息失败: ${error}`);
throw new Error("无法获取员工信息");
}
}
});
// 3. Tools - 可执行操作
server.registerTool({
name: "create_employee",
description: "创建新员工记录",
inputSchema: {
type: "object",
properties: {
name: { type: "string", description: "员工姓名" },
email: { type: "string", description: "员工邮箱" },
department: { type: "string", description: "所属部门" }
},
required: ["name", "email", "department"]
},
// ✅ 输入验证
validate: (args) => {
if (!isValidEmail(args.email)) {
throw new Error("无效的邮箱格式");
}
},
handler: async (args, { log }) => {
// ✅ 事务处理
return await db.$transaction(async (tx) => {
const employee = await tx.employees.create({
data: args
});
log.info(`创建员工成功: ${employee.id}`);
return {
success: true,
employeeId: employee.id
};
});
}
});
// 4. Prompts - 预配置提示模板
server.registerPrompt({
name: "employee_report",
description: "生成员工报告",
arguments: [
{
name: "employee_id",
description: "员工 ID",
required: true
}
],
handler: async (args) => {
const employee = await getEmployee(args.employee_id);
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `请为以下员工生成工作总结报告:\n${JSON.stringify(employee, null, 2)}`
}
}
]
};
}
});
// 5. 启动 Server
await server.start({
endpoint: "stdio"
});
7.4 组合使用最佳实践
# 完整示例:自动化文档生成系统
class DocumentationSystem:
"""
组合使用 Skill + SubAgent + MCP
"""
def __init__(self):
# MCP - 连接数据源
self.mcp_clients = {
"github": GitHubMCPClient(),
"notion": NotionMCPClient(),
"filesystem": FilesystemMCPClient()
}
# Skill - 嵌入专业知识
self.skills = {
"technical_writer": TechnicalWriterSkill(),
"code_analyzer": CodeAnalyzerSkill(),
"formatter": FormatterSkill()
}
# SubAgent - 任务分工
self.subagents = {}
def generate_documentation(self, repo_url: str):
# 1. 主 Agent 分解任务
subtasks = self.decompose_task(repo_url)
# 2. 创建 SubAgents 并分配任务
for task in subtasks:
agent = self.create_subagent(
task_type=task.type,
skill=self.skills[task.required_skill],
mcp_client=self.mcp_clients[task.required_mcp]
)
self.subagents[task.id] = agent
# 3. 并行执行
results = self.execute_parallel(subtasks)
# 4. 汇总结果
return self.aggregate_results(results)
def create_subagent(self, task_type: str, skill: Skill, mcp_client: MCPClient):
"""创建专门的 SubAgent"""
return SubAgent(
name=f"{task_type}_agent",
system_prompt=skill.system_prompt,
knowledge_base=skill.knowledge_base,
tools=mcp_client.available_tools,
context_limit=8000,
timeout=300
)
📝 总结
核心要点
-
Skill(技能):嵌入 AI 的专业知识,解决"如何做更好"
-
SubAgent(子智能体):任务分工的执行单元,解决"如何协作做"
-
MCP(模型上下文协议):连接外部世界的标准,解决"连接什么"
快速决策
需要什么能力?
├─ 连接外部数据/工具?
│ └─ 使用 MCP
├─ 嵌入专业知识?
│ └─ 使用 Skill
├─ 并行处理多个任务?
│ └─ 使用 SubAgent
└─ 复杂场景?
└─ 组合使用 MCP + Skill + SubAgent
设计哲学
|
层面 |
MCP |
Skill |
SubAgent |
|---|---|---|---|
| 核心问题 |
连接 |
能力 |
协作 |
| 设计哲学 |
标准化 > 灵活性 |
专精 > 通用 |
分工 > 集中 |
| 最佳实践 |
统一接口,安全可控 |
单一职责,精准触发 |
独立自治,明确接口 |
更多推荐


所有评论(0)