name: claude-code-mcp description: 配置和构建 Model Context Protocol (MCP) 服务器,用于 Claude Code 集成。设置数据库、文件系统、git 和 API 连接。使用 TypeScript/Python SDK 构建自定义 MCP 服务器,实现工具和资源,配置传输(stdio、HTTP),并部署到生产环境。
Claude Code MCP
规范:https://modelcontextprotocol.io/specification/2025-11-25 (2025年11月)
在需要时使用此技能:
- 为官方/社区 MCP 服务器配置
.claude/.mcp.json - 构建自定义 MCP 服务器(TypeScript 或 Python)
- 部署和加固远程 MCP 服务器(HTTP 传输)
快速开始(通过 npx 的本地 stdio)
- 创建或编辑
.claude/.mcp.json:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": { "POSTGRES_URL": "${DATABASE_URL}" }
}
}
}
- 提供环境变量并验证连接:
export DATABASE_URL="postgresql://user:pass@localhost:5432/db"
claude mcp list
claude mcp get postgres
注意事项:
- 默认配置文件路径通常是
.claude/.mcp.json。如果需要,可以通过claude --mcp-config <path>覆盖。 - 在服务器配置中优先使用
python3作为 Python 解释器,除非你的环境确保使用python。
权限管理(推荐)
# 允许服务器所有工具(通配符)
claude mcp add --allow "mcp__postgres__*" postgres -- npx -y @modelcontextprotocol/server-postgres
# 仅允许特定工具
claude mcp add --allow "mcp__postgres__query,mcp__postgres__list_tables" postgres -- npx -y @modelcontextprotocol/server-postgres
# 拒绝特定工具
claude mcp add --deny "mcp__filesystem__write_file" filesystem -- npx -y @modelcontextprotocol/server-filesystem ./data
生产环境防护(必需)
- 假设工具输出不可信(提示注入)。重用前进行消毒/结构化。
- 默认最小权限:只读数据库、范围化的文件系统允许列表、最小化工具允许列表。
- 将机密信息保留在
.mcp.json之外;通过环境变量或运行时秘密管理器注入。 - 添加超时、重试和速率限制;记录所有工具调用以进行审计。
下一步阅读
- 选择现有服务器:
references/mcp-servers.md - 构建自定义服务器:
references/mcp-custom.md - 实现模式(DB/API/文件系统):
references/mcp-patterns.md - 安全加固(OAuth、作用域、注入防御):
references/mcp-security.md - 模板:
assets/database/template-mcp-database.md、assets/filesystem/template-mcp-filesystem.md、assets/api/template-mcp-api.md、assets/deployment/template-mcp-docker.md - 精选链接:
data/sources.json