MCP
建议
配置 MCP 时,建议先让 AI 帮你生成配置草稿、检查路径和命令格式,再由你人工确认密钥、权限和写入范围。
MCP(Model Context Protocol)是一套把外部工具、数据库、文档和 API 接入 AI 工具的标准协议。配置 MCP 后,Claude Code、Codex、Cursor 等客户端可以按授权范围调用这些服务,而不是让你手动复制数据到对话里。
常见用途:
- 查询数据库、Redis 缓存或业务后台
- 读取最新框架文档
- 操作 GitHub、Issue、监控、设计稿等外部系统
- 暴露团队内部脚本或自动化工具
安全提醒
MCP 会把外部能力交给 AI 工具调用。数据库写权限、生产环境密钥、文件系统权限要谨慎开放;不确定时只给只读权限。
配置路径
不同客户端的 MCP 配置位置不同:
| 工具 | 配置位置 | 说明 |
|---|---|---|
| Claude Code | ~/.claude.json | 用户级和本地项目级 MCP 配置会记录在这里 |
| Codex | ~/.codex/config.toml | Codex CLI 与 Codex App 会读取同一份配置 |
| Cursor | ~/.cursor/mcp.json | Cursor 原生 MCP 配置 |
| VS Code | 项目根目录 .vscode/mcp.json | GitHub Copilot Agent mode 可读取 |
Claude Code 也可以用命令添加 MCP:
bash
claude mcp add <name> -- <command> [args...]
claude mcp listCodex 可以用命令添加 MCP:
bash
codex mcp add <name> -- <command> [args...]
codex mcp listClaude Code 配置格式
Claude Code 的 JSON 配置使用 mcpServers 字段。下面是通用结构:
json
{
"mcpServers": {
"server-name": {
"command": "npx",
"args": ["-y", "some-mcp-server"],
"env": {
"API_KEY": "your-api-key"
}
}
}
}如果是团队共享的 .mcp.json,建议只写命令和参数,密钥用环境变量传入。
Codex 配置格式
Codex 的 MCP 配置写在 ~/.codex/config.toml:
toml
[mcp_servers.server-name]
command = "npx"
args = ["-y", "some-mcp-server"]
[mcp_servers.server-name.env]
API_KEY = "your-api-key"HTTP 类型的 MCP 可以直接写 URL,例如 OpenAI Docs MCP:
toml
[mcp_servers.openaiDeveloperDocs]
url = "https://developers.openai.com/mcp"推荐 MCP
常用类型:
- 数据库:
universal-db-mcp、PostgreSQL、MySQL、Redis - 文档:Context7、OpenAI Docs MCP
- 浏览器:Playwright MCP
- 代码托管:GitHub MCP
- 项目管理:Linear、Jira
- 监控与错误追踪:Sentry
- 支付与业务 API:Stripe 等厂商官方 MCP
选择 MCP 时优先看三点:
- 是否来自官方或可信维护者
- 是否需要写权限
- 是否会读取敏感数据
universal-db-mcp
universal-db-mcp 适合把 MySQL、PostgreSQL、Redis 接入 AI 工具。建议先从只读账号开始;只有在明确需要写入时,再加入 --danger-allow-write。
MySQL
Claude Code / 通用 JSON:
json
{
"mcpServers": {
"mysql-dev": {
"command": "npx",
"args": [
"universal-db-mcp",
"--type", "mysql",
"--host", "your-mysql-host",
"--port", "3306",
"--user", "readonly_user",
"--password", "your-password",
"--database", "your_database"
]
}
}
}Codex config.toml:
toml
[mcp_servers.mysql-dev]
command = "npx"
args = [
"universal-db-mcp",
"--type", "mysql",
"--host", "your-mysql-host",
"--port", "3306",
"--user", "readonly_user",
"--password", "your-password",
"--database", "your_database"
]PostgreSQL
json
{
"mcpServers": {
"postgres-dev": {
"command": "npx",
"args": [
"universal-db-mcp",
"--type", "postgres",
"--host", "your-postgres-host",
"--port", "5432",
"--user", "readonly_user",
"--password", "your-password",
"--database", "your_database"
]
}
}
}如果确实需要写权限,再追加:
json
"--danger-allow-write"注意
--danger-allow-write 会允许 AI 工具执行写入类操作。生产库不要开启,测试库也建议先做好备份。
Redis
json
{
"mcpServers": {
"redis-cache": {
"command": "npx",
"args": [
"universal-db-mcp",
"--type", "redis",
"--host", "your-redis-host",
"--port", "6379",
"--password", "your-password",
"--database", "0"
]
}
}
}Redis 更容易被误删或误写,建议只在测试环境开放写权限。
Context7
Context7 用于给 AI 工具提供最新的库文档和代码示例。写代码遇到框架版本差异时,可以在提示词里加一句 use context7。
Claude Code / 通用 JSON:
json
{
"mcpServers": {
"context7": {
"command": "npx",
"args": [
"-y",
"@upstash/context7-mcp@latest"
]
}
}
}如果你使用 Context7 API Key,可以放到 env:
json
{
"mcpServers": {
"context7": {
"command": "npx",
"args": ["-y", "@upstash/context7-mcp@latest"],
"env": {
"CONTEXT7_API_KEY": "your-context7-api-key"
}
}
}
}Codex config.toml:
toml
[mcp_servers.context7]
command = "npx"
args = ["-y", "@upstash/context7-mcp@latest"]资源网站
- Model Context Protocol 官方文档
- Official MCP Registry
- MCP Registry 说明
- modelcontextprotocol/servers
- Claude Code MCP 文档
- OpenAI Docs MCP
- Context7
- Context7 MCP 安装说明
建议
一个任务只启用必要的 MCP。工具越多,模型越容易选错工具,也越难排查权限和调用来源。