Claude Code MCP Server 配置教程:用 MCP 协议扩展 AI 的能力边界
MCP(Model Context Protocol)是 Anthropic 推出的开放协议,让 AI 工具能够连接外部数据源和服务。通过配置 MCP Server,你可以让 Claude Code 直接操作数据库、访问 GitHub、管理文件系统,甚至发送 Slack 消息。本文带你从原理到实战,全面掌握 MCP 配置。
一、什么是 MCP
MCP 的全称是 Model Context Protocol(模型上下文协议)。它定义了一套标准化的接口,让 AI 模型可以安全地访问外部工具和数据。
你可以把 MCP 理解为"AI 的 USB 接口"——只要设备(工具)遵循 MCP 标准,就可以即插即用地被 AI 调用。
MCP 的核心组件
- MCP Server:提供工具和数据的服务端,暴露标准化的接口
- MCP Client:AI 应用端(如 Claude Code),通过协议调用 Server 提供的功能
- Tools:Server 暴露的可执行操作(如读写文件、查询数据库)
- Resources:Server 提供的只读数据源(如文档、配置)
- Prompts:Server 预定义的提示模板
MCP 的通信方式
| 传输方式 | 说明 | 适用场景 |
|---|---|---|
stdio |
通过标准输入输出通信 | 本地工具,最常用 |
sse |
Server-Sent Events | 远程服务器 |
streamable-http |
HTTP 流式传输 | 云端部署 |
二、Claude Code 中配置 MCP Server
2.1 配置文件位置
Claude Code 支持三个层级的 MCP 配置:
- 全局配置:
~/.claude/settings.json,所有项目共享 - 项目配置:
.claude/settings.json,仅当前项目生效 - 命令行:启动时通过
claude --mcp-config指定
2.2 基本配置结构
{
"mcpServers": {
"server-name": {
"command": "执行命令",
"args": ["参数列表"],
"env": {
"ENV_VAR": "环境变量"
}
}
}
}
2.3 使用 claude mcp 命令管理
# 添加 MCP Server
claude mcp add filesystem -- npx -y @anthropic-ai/mcp-filesystem --dir /path/to/dir
# 列出已配置的 MCP Servers
claude mcp list
# 删除 MCP Server
claude mcp remove filesystem
# 查看某个 Server 的详细配置
claude mcp get filesystem
三、常用 MCP Server 配置实战
3.1 文件系统 MCP Server
让 Claude Code 能够安全地读写指定目录中的文件。
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@anthropic-ai/mcp-filesystem",
"--dir", "/home/user/projects",
"--dir", "/home/user/documents"
]
}
}
}
配置后,Claude Code 会获得 read_file、write_file、list_directory 等工具,但只能操作指定目录。
3.2 数据库 MCP Server(PostgreSQL)
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@anthropic-ai/mcp-postgres"
],
"env": {
"POSTGRES_HOST": "localhost",
"POSTGRES_PORT": "5432",
"POSTGRES_DB": "myapp",
"POSTGRES_USER": "dbuser",
"POSTGRES_PASSWORD": "dbpassword"
}
}
}
}
配置后可以直接在对话中执行 SQL 查询:
# 在 Claude Code 中
> 查询最近 7 天的用户注册数量,按天统计
# Claude Code 会自动调用 postgres MCP Server 执行:
# SELECT DATE(created_at) as date, COUNT(*) as count
# FROM users
# WHERE created_at >= NOW() - INTERVAL '7 days'
# GROUP BY DATE(created_at)
# ORDER BY date;
3.3 GitHub MCP Server
{
"mcpServers": {
"github": {
"command": "npx",
"args": [
"-y",
"@anthropic-ai/mcp-github"
],
"env": {
"GITHUB_TOKEN": "ghp_xxxxxxxxxxxx"
}
}
}
}
可用工具:创建 Issue、查看 PR、合并分支、搜索代码等。
3.4 Slack MCP Server
{
"mcpServers": {
"slack": {
"command": "npx",
"args": [
"-y",
"@anthropic-ai/mcp-slack"
],
"env": {
"SLACK_BOT_TOKEN": "xoxb-xxxxxxxxxxxx"
}
}
}
}
3.5 浏览器自动化 MCP Server(Puppeteer)
{
"mcpServers": {
"puppeteer": {
"command": "npx",
"args": [
"-y",
"@anthropic-ai/mcp-puppeteer"
]
}
}
}
可以让 Claude Code 自动化浏览器操作:打开网页、截图、填表、点击等。非常适合 E2E 测试和网页数据抓取。
3.6 MySQL MCP Server
{
"mcpServers": {
"mysql": {
"command": "npx",
"args": [
"-y",
"mcp-mysql"
],
"env": {
"MYSQL_HOST": "127.0.0.1",
"MYSQL_PORT": "3306",
"MYSQL_USER": "root",
"MYSQL_PASSWORD": "yourpassword",
"MYSQL_DATABASE": "myapp"
}
}
}
}
四、创建自定义 MCP Server
如果现有的 MCP Server 不满足需求,你可以用 TypeScript 快速创建自己的。
4.1 项目初始化
mkdir my-mcp-server && cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk
npm install -D typescript @types/node
4.2 实现一个天气查询 MCP Server
// src/index.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "weather-server",
version: "1.0.0",
});
// 定义工具
server.tool(
"get_weather",
"查询指定城市的天气信息",
{
city: z.string().describe("城市名称,如 北京、上海"),
unit: z.enum(["celsius", "fahrenheit"]).default("celsius")
.describe("温度单位"),
},
async ({ city, unit }) => {
// 这里接入真实天气 API
const response = await fetch(
`https://api.weatherapi.com/v1/current.json?key=${process.env.WEATHER_API_KEY}&q=${city}`
);
const data = await response.json();
const temp = unit === "celsius"
? data.current.temp_c
: data.current.temp_f;
return {
content: [{
type: "text",
text: JSON.stringify({
city: data.location.name,
temperature: temp,
unit: unit,
condition: data.current.condition.text,
humidity: data.current.humidity,
wind_kph: data.current.wind_kph,
}, null, 2)
}]
};
}
);
// 启动
const transport = new StdioServerTransport();
await server.connect(transport);
4.3 编译并注册
# 编译
npx tsc
# 在 Claude Code 中注册
claude mcp add weather -- node /path/to/my-mcp-server/dist/index.js
4.4 使用自定义 MCP Server
# 在 Claude Code 中直接对话
> 查一下北京今天的天气
# Claude Code 会自动调用 weather MCP Server 的 get_weather 工具
五、多 Server 组合配置
一个项目可以同时配置多个 MCP Server,让 Claude Code 拥有多种能力:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-filesystem", "--dir", "./"]
},
"postgres": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-postgres"],
"env": {
"POSTGRES_HOST": "localhost",
"POSTGRES_DB": "myapp",
"POSTGRES_USER": "dev",
"POSTGRES_PASSWORD": "devpass"
}
},
"github": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-github"],
"env": {
"GITHUB_TOKEN": "ghp_xxxx"
}
}
}
}
六、安全最佳实践
- 最小权限原则:文件系统 MCP 只开放必要目录
- 数据库只读模式:开发环境外的数据库配置 readonly 用户
- 环境变量管理:敏感信息放在
.env文件,不提交到 Git - 审查 MCP Server 源码:使用第三方 MCP Server 前,检查其权限范围
- 日志审计:生产环境启用 MCP 操作日志,记录所有工具调用
七、排错指南
| 问题 | 可能原因 | 解决方案 |
|---|---|---|
| MCP Server 无法启动 | npx 包下载失败 | 检查网络,或预先 npm install -g |
| 工具列表为空 | Server 启动报错 | 手动运行命令检查错误输出 |
| 数据库连接失败 | 环境变量未生效 | 确认 env 字段配置正确 |
| 权限被拒绝 | 文件系统目录未授权 | 检查 --dir 参数是否包含目标路径 |
| 响应超时 | 外部 API 延迟 | 检查 Server 实现中的超时设置 |
总结
MCP 协议极大地扩展了 Claude Code 的能力范围,让它从一个代码编辑工具进化为全栈开发的智能助手。建议根据项目需求,逐步添加文件系统、数据库、GitHub 等常用 MCP Server,打造属于自己的 AI 工作流。
接口配置参考:https://9m8m.com/docs/
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)