简介
本章节主要介绍如何在 Claude Code 中设置 MCP。
模型上下文协议 (MCP) 是一个开放协议,使 LLM 能够访问外部工具和数据源。有关 MCP 的更多详细信息,请参阅 MCP 文档。
配置 MCP 服务器
添加 MCP stdio 服务器
# 基本语法claude mcp add <name> <command> [args...]# 示例:添加本地服务器claude mcp add my-server -e API_KEY=123 -- /path/to/server arg1 arg2添加 MCP SSE 服务器
# 基本语法claude mcp add --transport sse <name> <url># 示例:添加 SSE 服务器claude mcp add --transport sse sse-server https://example.com/sse-endpoint# 示例:添加带有自定义标头的 SSE 服务器claude mcp add --transport sse api-server https://api.example.com/mcp -e X-API-Key=your-key添加 MCP HTTP 服务器
# 基本语法claude mcp add --transport http <name> <url># 示例:添加可流式传输的 HTTP 服务器claude mcp add --transport http http-server https://example.com/mcp# 示例:添加带有身份验证标头的 HTTP 服务器claude mcp add --transport http secure-server https://api.example.com/mcp -e Authorization="Bearer your-token"管理您的 MCP 服务器
# 列出所有已配置的服务器claude mcp list# 获取特定服务器的详细信息claude mcp get my-server# 删除服务器claude mcp remove my-server
MCP 服务器作用域
作用域层次结构和优先级
MCP 服务器配置遵循清晰的优先级层次结构:
- 本地作用域(最高优先级)
- 项目作用域
- 用户作用域(最低优先级)
当同名服务器存在于多个作用域时,系统会优先选择较高优先级的配置。
本地作用域
本地作用域是默认配置级别,存储在您的项目特定用户设置中。
# 添加本地作用域服务器(默认)claude mcp add my-private-server /path/to/server# 显式指定本地作用域claude mcp add my-private-server -s local /path/to/server项目作用域
项目作用域通过
.mcp.json
文件实现团队协作:# 添加项目作用域服务器claude mcp add shared-server -s project /path/to/server生成的
.mcp.json
文件格式:{"mcpServers": {"shared-server": {"command": "/path/to/server","args": [],"env": {}}}}用户作用域
用户作用域提供跨项目访问能力:
# 添加用户作用域服务器claude mcp add my-user-server -s user /path/to/server
远程 MCP 服务器身份验证
添加需要身份验证的远程服务器
# 添加需要 OAuth 的 SSE 或 HTTP 服务器claude mcp add --transport sse github-server https://api.github.com/mcp使用 /mcp 命令进行身份验证
在 Claude Code 中使用
/mcp
命令:> /mcp这将打开交互式菜单,您可以:
- 查看服务器连接状态
- 进行 OAuth 身份验证
- 清除现有身份验证
- 查看服务器功能
完成 OAuth 流程
- 您的浏览器会自动打开到 OAuth 提供商
- 在浏览器中完成身份验证
- Claude Code 接收并安全存储访问令牌
- 服务器连接变为活动状态
Postgres MCP 服务器连接
添加 Postgres MCP 服务器
claude mcp add postgres-server /path/to/postgres-mcp-server --connection-string "postgresql://user:pass@localhost:5432/mydb"使用 Claude 查询数据库
> describe the schema of our users table> what are the most recent orders in the system?> show me the relationship between customers and invoices
JSON 配置添加 MCP 服务器
从 JSON 添加服务器
# 基本语法claude mcp add-json <name> '<json>'# 示例:添加 stdio 服务器claude mcp add-json weather-api '{"type":"stdio","command":"/path/to/weather-cli","args":["--api-key","abc123"],"env":{"CACHE_DIR":"/tmp"}}'验证服务器添加
claude mcp get weather-api
Claude Desktop 导入
从 Claude Desktop 导入服务器
claude mcp add-from-claude-desktop选择要导入的服务器
运行命令后,通过交互式对话框选择服务器。
验证导入结果
claude mcp list
Claude Code 作为 MCP 服务器
启动 MCP 服务器模式
claude mcp serve客户端配置示例
{"command": "claude","args": ["mcp", "serve"],"env": {}}
MCP 资源使用
列出可用资源
在提示中输入
@
查看可用资源。引用特定资源
> Can you analyze @github:issue://123 and suggest a fix?> Please review the API documentation at @docs:file://api/authentication多资源引用
> Compare @postgres:schema://users with @docs:file://database/user-model
MCP 提示命令
查看可用提示
输入
/
查看所有命令,包括 MCP 提示命令。执行提示命令
> /mcp__github__list_prs> /mcp__github__pr_review 456> /mcp__jira__create_issue "Bug in login flow" high