MCP 工具集成与 Skills 工作流
MCP 让 Claude Code 连接外部世界——数据库、浏览器、API、文件系统。Skills 把重复操作变成一条命令。
MCP + Skills 架构
graph TD
CLAUDE[Claude Code] --> MCP[MCP 工具层]
CLAUDE --> SKILLS[Skills 层]
MCP --> FS[文件系统 MCP]
MCP --> DB[数据库 MCP]
MCP --> BROWSER[浏览器 MCP]
MCP --> GH[GitHub MCP]
MCP --> NOTION[Notion MCP]
SKILLS --> S1[/review-pr\n代码审查]
SKILLS --> S2[/deploy\n部署脚本]
SKILLS --> S3[/standup\n日报生成]
SKILLS --> S4[/doc\n文档生成]
FS --> F1[读写项目文件]
DB --> D1[查询/分析数据]
BROWSER --> B1[截图/抓取]
GH --> G1[PR/Issue 操作]
style MCP fill:#e3f2fd,stroke:#1565c0,stroke-width:2px
style SKILLS fill:#c8e6c9,stroke:#388e3c,stroke-width:2px
MCP 配置与 Skills 创建
"""
MCP 服务器配置管理器 + Skills 模板生成器
"""
import json
from dataclasses import dataclass, field
from pathlib import Path
@dataclass
class MCPServerConfig:
"""MCP 服务器配置"""
name: str
command: str
args: list[str]
env: dict[str, str] = field(default_factory=dict)
description: str = ""
@dataclass
class SkillTemplate:
"""Claude Code Skill 模板"""
name: str # slash command 名称(不含/)
description: str
prompt_content: str # Skill 的 prompt 内容
input_vars: list[str] = field(default_factory=list)
class MCPManager:
"""MCP 服务器配置管理"""
COMMON_SERVERS = {
"filesystem": MCPServerConfig(
name="filesystem",
command="npx",
args=["-y", "@modelcontextprotocol/server-filesystem", "/home/user/projects"],
description="文件系统访问 — 读写本地文件",
),
"github": MCPServerConfig(
name="github",
command="npx",
args=["-y", "@modelcontextprotocol/server-github"],
env={"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_YOUR_TOKEN"},
description="GitHub 集成 — PR/Issue/仓库操作",
),
"postgres": MCPServerConfig(
name="postgres",
command="npx",
args=["-y", "@modelcontextprotocol/server-postgres",
"postgresql://localhost/mydb"],
description="PostgreSQL — 直接查询数据库",
),
"puppeteer": MCPServerConfig(
name="puppeteer",
command="npx",
args=["-y", "@modelcontextprotocol/server-puppeteer"],
description="浏览器自动化 — 截图/表单/爬取",
),
"notion": MCPServerConfig(
name="notion",
command="npx",
args=["-y", "@notionhq/notion-mcp-server"],
env={"OPENAPI_MCP_HEADERS": '{"Authorization": "Bearer ntn_YOUR_TOKEN"}'},
description="Notion 集成 — 读写页面和数据库",
),
"slack": MCPServerConfig(
name="slack",
command="npx",
args=["-y", "@modelcontextprotocol/server-slack"],
env={
"SLACK_BOT_TOKEN": "xoxb-YOUR-BOT-TOKEN",
"SLACK_TEAM_ID": "T0YOUR_TEAM",
},
description="Slack 集成 — 发送消息/读取频道",
),
}
@classmethod
def generate_config(cls, server_names: list[str]) -> dict:
"""生成 ~/.claude/settings.json 的 mcpServers 配置段"""
mcp_config = {}
for name in server_names:
if name in cls.COMMON_SERVERS:
srv = cls.COMMON_SERVERS[name]
entry = {"command": srv.command, "args": srv.args}
if srv.env:
entry["env"] = srv.env
mcp_config[name] = entry
return {"mcpServers": mcp_config}
@classmethod
def print_catalog(cls):
print("=== 可用 MCP 服务器目录 ===\n")
for name, srv in cls.COMMON_SERVERS.items():
print(f" [{name}] {srv.description}")
print(f" 命令: {srv.command} {' '.join(srv.args[:2])}...")
class SkillsBuilder:
"""Skills 模板构建器"""
SKILL_TEMPLATES = [
SkillTemplate(
name="review-pr",
description="标准化 PR 代码审查",
prompt_content="""请对当前 Git 分支的变更进行代码审查。
步骤:
1. 运行 `git diff main...HEAD` 获取所有变更
2. 分析每个文件的变更
3. 按以下维度评估:
- 🐛 Bug 风险(必须修复)
- ⚡ 性能问题(建议优化)
- 🔒 安全隐患(必须修复)
- 📖 可读性(建议改进)
4. 输出审查报告,格式:
- 总体评价(1-2句)
- 必须修复的问题(如有)
- 建议改进的问题(如有)
- 值得肯定的地方""",
),
SkillTemplate(
name="standup",
description="根据 Git 历史生成日报",
prompt_content="""生成今天的工作日报。
步骤:
1. 运行 `git log --since='24 hours ago' --author="$(git config user.email)" --oneline`
2. 读取今天有变更的文件
3. 生成日报,格式:
## 今日完成
- (基于 commit 提炼)
## 进行中
- (未完成的工作)
## 阻碍/风险
- 无(如有请说明)
## 明日计划
- (基于当前上下文推测)""",
),
SkillTemplate(
name="doc",
description="为指定文件生成文档",
prompt_content="""为当前文件生成完整文档。
要求:
- 模块级 docstring(说明用途和使用示例)
- 每个公开函数的 docstring(参数、返回值、异常、示例)
- 使用 Google 风格
- 不修改任何业务逻辑
- 对复杂算法添加行内注释
完成后运行 `pydoc` 验证文档格式正确。""",
),
SkillTemplate(
name="security-check",
description="安全漏洞快速扫描",
prompt_content="""对代码库进行安全漏洞快速扫描。
检查范围:
1. SQL 注入风险(字符串拼接 SQL)
2. XSS 漏洞(未转义用户输入)
3. 硬编码敏感信息(密码/API Key/Token)
4. 不安全的依赖(已知 CVE)
5. 不当的权限检查(缺少认证/授权)
输出:
- 按严重程度分类(Critical / High / Medium / Low)
- 每个问题附上文件路径和行号
- 提供修复代码示例""",
),
]
@classmethod
def create_skill_file(cls, skill: SkillTemplate, output_dir: Path):
"""创建 Skill 文件"""
output_dir.mkdir(parents=True, exist_ok=True)
skill_file = output_dir / f"{skill.name}.md"
content = f"""---
description: {skill.description}
---
{skill.prompt_content}
"""
skill_file.write_text(content)
print(f"✅ 创建 Skill: {skill_file}")
@classmethod
def setup_skills(cls, project_root: Path):
"""在项目中初始化 Skills 目录"""
skills_dir = project_root / ".claude" / "commands"
for skill in cls.SKILL_TEMPLATES:
cls.create_skill_file(skill, skills_dir)
print(f"\n🎉 Skills 已创建在 {skills_dir}")
print("使用方式: 在 claude 中输入 /review-pr、/standup 等")
# 演示
manager = MCPManager()
manager.print_catalog()
print("\n=== 推荐配置(开发者套餐)===")
config = manager.generate_config(["filesystem", "github", "postgres"])
print(json.dumps(config, indent=2))
print("\n=== Skills 模板预览 ===")
for skill in SkillsBuilder.SKILL_TEMPLATES:
print(f" /{skill.name}: {skill.description}")
MCP 服务器安装速查
| MCP 服务器 | 安装命令 | 主要能力 |
|---|---|---|
| filesystem | npx -y @modelcontextprotocol/server-filesystem | 本地文件读写 |
| github | npx -y @modelcontextprotocol/server-github | PR/Issue/仓库 |
| postgres | npx -y @modelcontextprotocol/server-postgres | 数据库查询 |
| puppeteer | npx -y @modelcontextprotocol/server-puppeteer | 浏览器自动化 |
| notion | npx -y @notionhq/notion-mcp-server | Notion 读写 |
| slack | npx -y @modelcontextprotocol/server-slack | Slack 消息 |
行动清单
- [ ] 安装 filesystem MCP 并测试:
claude '列出 ~/projects 下所有 Python 项目' - [ ] 配置 GitHub MCP,让 Claude Code 可以直接查看和操作 PR
- [ ] 在项目中创建
/review-prSkill,下次 PR 提交前运行一次标准化审查 - [ ] 设置
/standupSkill,每天花 10 秒生成日报而非手动回忆 - [ ] 数据库 MCP 需谨慎:生产库只给只读权限,开发/测试库才给写权限
- [ ] 参考
claude-skills-guide学习如何构建更复杂的参数化 Skill
下一章:04-Claude-for-Work团队协作/01-团队工作区配置与权限管理 — 把 Claude 的效率扩展到整个团队。