实操审校说明
本文内容基于 MCP 官方文档的核心概念与 Server/Client 开发流程整理,属于可实操教程。读者按教程操作前,建议先确认本地环境和官方 SDK 版本,因为 MCP SDK 仍在持续迭代,部分类名、CLI 命令或配置写法可能随版本变化。
建议测试环境
| 项目 | 建议版本 / 条件 |
| 操作系统 | Windows 10/11、macOS 或 Linux |
| Python 路线 | Python 3.10+ |
| Node.js 路线 | Node.js 18+ |
| 必需能力 | 会使用命令行、会创建虚拟环境或 npm 项目 |
| 验证工具 | MCP Inspector 或支持 MCP 的客户端 |
实操前置条件
- 能正常访问 MCP 官方文档。
- 已安装 Python 或 Node.js。
- 能在终端里执行
pip install或npm install。 - 如果要接入 Claude Desktop、Cursor、VS Code 等客户端,需要按对应客户端的最新 MCP 配置方式填写 server 信息。
可验证结果
完成教程后,至少应该能验证以下结果:
- MCP Server 可以正常启动。
- 客户端能发现 Server 暴露的 tool/resource/prompt。
- 调用工具时能看到输入参数和返回结果。
- MCP Inspector 中能测试工具调用。
版本风险提示
MCP 是快速发展的协议,最容易变化的是 SDK 包名、客户端配置文件位置和工具注册 API。遇到报错时,优先对照官方文档更新命令,不要盲目复制旧示例。
官方参考:
- MCP 入门:https://modelcontextprotocol.io/docs/getting-started/intro
- 构建 MCP Server:https://modelcontextprotocol.io/docs/develop/build-server
- MCP Inspector:https://modelcontextprotocol.io/docs/tools/inspector
MCP 协议实战教程:从零开始让 AI 学会"动手干活"
📅 更新时间:2026 年 6 月
👤 适合人群:零基础新手,有 Python 或 Node.js 基础更佳
⏱ 预计阅读时间:30 分钟
一、MCP 是什么?
MCP(Model Context Protocol,模型上下文协议) 是 Anthropic 在 2024 年底提出的开放标准协议,目前已成为 AI 应用开发的核心标准。
📌 一句话理解
以前 AI 只能"说话",有了 MCP,AI 能读文件、查数据库、调 API、操控你的开发环境。
🔌 类比:AI 世界的 USB-C
就像 USB-C 统一了所有设备的充电接口一样,MCP 统一了 AI 连接外部工具的方式:
传统方式:每个 AI 工具 → 各自实现连接方法 → 代码碎片化、重复开发
MCP 方式:统一协议标准 → 所有 AI 工具 + 所有数据源 → 即插即用
MCP 能做什么?
| 能力类型 | 具体示例 |
| 📁 文件操作 | 读取/写入本地文件、搜索文件内容 |
| 🗄️ 数据库查询 | 连接 MySQL、PostgreSQL、SQLite |
| 🌐 API 调用 | 调用 GitHub、Slack、Google 等第三方 API |
| 🖥️ 开发环境 | 运行代码、执行命令、操控 VS Code |
| 🔍 网页搜索 | 实时搜索互联网内容 |
二、核心概念(3 个角色)
┌─────────────────────────────────────────────────┐
│ MCP 架构图 │
│ │
│ ┌──────────────┐ MCP ┌──────────────┐ │
│ │ MCP Client │ ◄──────► │ MCP Server │ │
│ │(Claude/GPT)│ Protocol │(文件/DB/API)│ │
│ └──────────────┘ └──────────────┘ │
│ ▲ │
│ │ 用户提问 │
│ ┌────┴─────┐ │
│ │ 你 │ │
│ └──────────┘ │
└─────────────────────────────────────────────────┘
- Host(宿主):Claude Desktop、Cursor、Windsurf 等 AI 应用
- MCP Client:内嵌在 Host 里,负责和 MCP Server 通信
- MCP Server:暴露工具给 AI 用,可以是文件系统、数据库、任何 API
三、环境准备
3.1 安装必需工具
方案 A:Python 开发者
# 确认 Python 版本(需要 3.10+)
python --version
# 安装 MCP SDK
pip install mcp
方案 B:Node.js 开发者
# 确认 Node.js 版本(需要 18+)
node --version
# 安装 MCP SDK
npm install @modelcontextprotocol/sdk
3.2 安装 Claude Desktop(MCP 客户端)
- 访问 https://claude.ai/download 下载 Claude Desktop
- 安装并登录 Claude 账号(免费账号即可)
- 打开 Claude Desktop,点击左上角设置图标
四、5 分钟上手:连接文件系统
这是最简单的 MCP Server,让 Claude 能读取你电脑上的文件。
4.1 配置 Claude Desktop
找到配置文件(Windows):
%APPDATA%\Claude\claude_desktop_config.json
编辑内容如下:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"C:/Users/你的用户名/Documents"
]
}
}
}
⚠️ 把 你的用户名 替换成你实际的 Windows 用户名
4.2 重启 Claude Desktop
保存配置后完全退出并重启 Claude Desktop。
4.3 验证是否生效
在 Claude Desktop 中输入:
帮我列出 Documents 文件夹里的所有文件
如果 Claude 能列出你电脑上的文件,就成功了!🎉
五、动手写一个 MCP Server(Python 版)
下面我们写一个能查询天气的 MCP Server。
5.1 创建项目
mkdir my-mcp-server
cd my-mcp-server
pip install mcp httpx
5.2 编写 Server 代码
创建文件 weather_server.py:
import asyncio
import httpx
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp import types
# 创建 MCP Server 实例
app = Server("weather-server")
@app.list_tools()
async def list_tools() -> list[types.Tool]:
"""告诉 MCP Client 我有哪些工具"""
return [
types.Tool(
name="get_weather",
description="获取指定城市的天气信息",
inputSchema={
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称,例如:北京、上海、广州"
}
},
"required": ["city"]
}
)
]
@app.call_tool()
async def call_tool(name: str, arguments: dict) -> list[types.TextContent]:
"""处理工具调用请求"""
if name == "get_weather":
city = arguments.get("city", "北京")
# 调用免费天气 API(wttr.in)
async with httpx.AsyncClient() as client:
response = await client.get(
f"https://wttr.in/{city}?format=3&lang=zh",
timeout=10
)
weather_info = response.text
return [types.TextContent(
type="text",
text=f"🌤️ {city} 的天气:{weather_info}"
)]
raise ValueError(f"未知工具:{name}")
# 启动 Server
async def main():
async with stdio_server() as (read_stream, write_stream):
await app.run(
read_stream,
write_stream,
app.create_initialization_options()
)
if __name__ == "__main__":
asyncio.run(main())
5.3 注册到 Claude Desktop
编辑 claude_desktop_config.json:
{
"mcpServers": {
"weather": {
"command": "python",
"args": ["C:/path/to/my-mcp-server/weather_server.py"]
}
}
}
重启 Claude Desktop,然后问它:"北京今天天气怎么样?" 🌞
六、常用的现成 MCP Server
不用每次都自己写!社区已经有大量现成的 MCP Server:
| Server 名称 | 功能 | 安装命令 |
| filesystem | 读写本地文件 | 见上方示例 |
| github | 操作 GitHub 仓库 | npx @modelcontextprotocol/server-github |
| sqlite | 查询 SQLite 数据库 | npx @modelcontextprotocol/server-sqlite |
| brave-search | 实时网页搜索 | npx @modelcontextprotocol/server-brave-search |
| puppeteer | 控制浏览器 | npx @modelcontextprotocol/server-puppeteer |
| memory | AI 持久化记忆 | npx @modelcontextprotocol/server-memory |
完整列表:https://github.com/modelcontextprotocol/servers
七、在 Cursor 中使用 MCP
Cursor 从 0.43 版本开始支持 MCP,配置方式类似:
- 打开 Cursor → Settings → MCP
- 点击"Add MCP Server"
- 填写 Name、Command、Arguments
- 保存后即可在 Cursor 对话中使用
八、常见问题
Q:MCP Server 启动后 Claude 看不到工具?
原因:配置文件路径写错,或 Node.js/Python 没有安装好。
解决:
- 检查配置文件 JSON 格式是否正确(可用在线 JSON 验证工具)
- 在命令行手动运行
command + args看是否有报错 - 确保完全重启了 Claude Desktop(任务管理器检查)
Q:可以同时使用多个 MCP Server 吗?
可以!在配置文件中的 mcpServers 对象里添加多个条目就行:
{
"mcpServers": {
"filesystem": { ... },
"github": { ... },
"weather": { ... }
}
}
Q:MCP Server 安全吗?
MCP Server 在你本地运行,数据不会上传。但要注意:
- 只安装来源可信的 MCP Server
- 不要给 MCP Server 授予超出需要的权限
- 定期检查 MCP Server 的日志
九、进阶方向
掌握基础后,可以继续探索:
- 多工具协作 — 让 AI 同时使用多个工具完成复杂任务
- Resources 资源类型 — 除工具外,MCP 还支持暴露文档、数据供 AI 读取
- Prompts 模板 — 预定义 Prompt 模板,让 AI 按固定格式响应
- 生产部署 — 把 MCP Server 部署到云端,实现多用户共享
总结
| 概念 | 一句话 |
| MCP 是什么 | AI 连接外部工具的标准协议 |
| 三个角色 | Host + Client + Server |
| 最快体验 | 配置 filesystem server,5 分钟搞定 |
| 自己开发 | Python/Node.js SDK,几十行代码 |
| 现成资源 | GitHub 上有几百个现成 Server |
🎯 下一步推荐:配置好 filesystem MCP 后,尝试让 Claude 帮你整理文件夹、读取本地文档并总结。
*参考资料:微软 MCP 官方入门课程 | Anthropic MCP 文档 | CSDN MCP 实战指南(2026)*