🚀 Claude Code Skills 进阶:自定义 Agent 与 MCP 集成
进阶第 3 篇 — 让 Claude Code 长出三头六臂
🎯 这篇讲什么
Claude Code 最强大的隐藏能力:自定义子代理 和 MCP 外部工具集成。学会这两招,它不再是一个 AI,而是一个 AI 团队。
👥 自定义 Subagent:让 Claude 拥有专业分身
概念
Subagent 是运行在 Claude Code 内部的专业子 AI。你可以为不同任务定义不同性格、不同工具的 Agent。
你: "帮我重构 auth 模块,顺便做安全审查"
│
▼
┌─────────────┐ ┌──────────────────┐
│ Claude Code │────→│ @security-review │ 安全审查
│ (主控) │ └──────────────────┘
│ │ ┌──────────────────┐
│ │────→│ @db-expert │ 数据库优化
└─────────────┘ └──────────────────┘
创建 Agent
在 .claude/agents/ 目录创建 .md 文件:
# .claude/agents/security-reviewer.md
---
name: security-reviewer
description: 专注安全审查的代码审计专家
model: sonnet
tools: [Read, Bash]
---
你是一个资深安全工程师。审查代码时重点关注:
- 注入漏洞(SQL、XSS、命令注入)
- 认证与授权缺陷
- 硬编码密钥
- 不安全的反序列化
- 依赖库已知漏洞
检查完输出一份带风险等级的报告。
使用方式
在对话中 @名字 调用:
用户: @security-reviewer 审查 src/auth/ 目录
用户: @db-expert 优化这几条查询,然后 @security-reviewer 检查改动
Agent 可以嵌套调用
用户: @architect 设计新的 API 结构
→ @architect 内部调用 @db-expert 检查数据模型
→ @architect 内部调用 @security-reviewer 验证设计
→ 输出完整方案
Agent 文件位置优先级
| 位置 | 范围 | 用途 |
|---|---|---|
.claude/agents/ | 项目 | 团队共享 |
~/.claude/agents/ | 全局 | 个人专用 |
🔌 MCP:接入外部工具和数据源
MCP(Model Context Protocol)让 Claude Code 能连接数据库、API、浏览器等外部服务。
架构
Claude Code ──→ MCP Server (GitHub) ──→ GitHub API
──→ MCP Server (Postgres) ──→ 数据库
──→ MCP Server (Puppeteer) ──→ 浏览器
安装 MCP 服务
# GitHub 集成(管理 Issue、PR)
claude mcp add -s user github -- npx @modelcontextprotocol/server-github
# PostgreSQL 查询
claude mcp add -s local postgres -- npx @anthropic-ai/server-postgres \
--connection-string postgresql://localhost/mydb
# 浏览器自动化
claude mcp add puppeteer -- npx @anthropic-ai/server-puppeteer
MCP 作用域
| 参数 | 范围 | 存储位置 |
|---|---|---|
-s user | 全局所有项目 | ~/.claude.json |
-s local | 当前项目(你个人) | .claude/settings.local.json |
-s project | 当前项目(团队) | .claude/settings.json |
在对话中使用
安装后,Claude Code 自动发现 MCP 工具。直接说话就行:
用户: 查一下这个项目的 open issues,按优先级排序
→ Claude 自动调用 GitHub MCP
用户: 查数据库 users 表里最近注册的 10 个用户
→ Claude 自动调用 Postgres MCP
资源引用语法
@github:issue://123 # 引 GitHub Issue
@github:pr://456 # 引 GitHub PR
🎛️ MCP 高级配置
仅限特定会话使用
claude --bare -p "分析数据" \
--mcp-config mcp-servers.json \
--strict-mcp-config
--strict-mcp-config:只加载指定配置,忽略其他所有 MCP 服务。
性能优化
# 限制 MCP 输出大小,防止撑爆上下文
export MAX_MCP_OUTPUT_TOKENS=50000
# 在 MCP 配置中允许大结果
# 加 maxResultSizeChars 注解,最大 500K 字符
🔗 Agent + MCP 联动
最强大的用法:Agent 调用 MCP 工具。
# .claude/agents/data-analyst.md
---
name: data-analyst
description: 数据分析专家
model: sonnet
tools: [Read, Bash]
---
你是数据分析师。需要查数据库时,直接调用 PostgreSQL MCP。
分析结果输出为带图表的 Markdown 报告。
用户: @data-analyst 分析上周用户增长趋势
→ Agent 自动调用 MCP 查数据库
→ 分析数据
→ 输出带洞察的报告
📋 常用 MCP 服务列表
| 服务 | 安装命令 | 用途 |
|---|---|---|
| GitHub | npx @modelcontextprotocol/server-github | Issue/PR/仓库管理 |
| PostgreSQL | npx @anthropic-ai/server-postgres | 数据库查询 |
| Puppeteer | npx @anthropic-ai/server-puppeteer | 浏览器自动化 |
| Filesystem | npx @modelcontextprotocol/server-filesystem | 文件系统访问 |
| Slack | 社区维护 | Slack 消息管理 |
| Jira | 社区维护 | 项目管理 |
❓ 常见问题 (FAQ)
Q1: Agent 和直接问 Claude 有什么区别?
Agent 有独立的系统 prompt 和工具集。比如 security-reviewer 只看代码安全,不会被其他上下文干扰。结果更专业、更一致。
Q2: Agent 最多能几层嵌套?
取决于配置。默认能嵌套调用,但不建议超过 3 层(Agent A → Agent B → Agent C)。
Q3: MCP 服务需要额外付费吗?
MCP 协议免费。但部分 MCP 服务(如 GitHub API)有自己的用量限制。PostgreSQL MCP 连本地数据库完全免费。
Q4: 多个 MCP 服务会冲突吗?
不会。每个 MCP 服务独立运行,工具名自动加 mcp__<服务名>__ 前缀。
Q5: 怎么关掉不用的 MCP 服务?
claude mcp remove <name>。或者在 settings 里禁掉。
Q6: 团队怎么共享 Agent 和 MCP 配置?
Agent 放 .claude/agents/(Git 提交)。MCP 用 -s project 安装,配置写入 .claude/settings.json。
⚠️ 实操避坑
| 坑 | 正确做法 |
|---|---|
| Agent 的 prompt 写太泛 | Agent 越专一越好。"安全审查员"比"代码助手"有效 |
| MCP 连接信息写死密码 | 用环境变量,别硬编码在命令里 |
| 太多 Agent 导致 token 爆涨 | 按需创建,不要一口气定义 10 个 |
| MCP 服务装了没反应 | /mcp 进入管理界面检查连接状态 |
--bare 模式下忘了传 MCP 配置 | 用 --mcp-config 显式指定 |
标签:#AI #ClaudeCode #进阶 #Agent #MCP