2025全新实战指南:10分钟搭建跨平台MCP开发环境,兼容Python与Node.js双生态
一、环境准备:跨平台兼容方案
在开始MCP开发前,确保你的系统满足以下条件:
💡 避坑提示: Windows用户需启用开发者模式解决长路径问题 macOS需运行 xcode-select --install 安装命令行工具
二、核心工具安装:UV替代传统包管理
1. 安装UV(超高速Python包管理器)
# 一键安装脚本(跨平台)
curl -LsSf https://astral.sh/uv/install.sh | sh # 验证安装
uv --version
# 预期输出: uv 0.2.0 (2025-07更新)
2. 配置UV镜像加速(国内用户必做)
# 设置清华镜像源
uv config set registry.index-url "https://pypi.tuna.tsinghua.edu.cn/simple" # 永久生效(写入.zshrc/.bashrc)
echo 'export UV_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"' >> ~/.zshrc
三、Python环境配置(MCP服务端)
1. 创建隔离虚拟环境
# 新建mcp-dev目录并进入
mkdir mcp-dev && cd mcp-dev # 使用UV创建虚拟环境
uv venv .venv # 激活环境
# Windows: .\.venv\Scripts\activate
source .venv/bin/activate # Linux/macOS
2. 安装MCP核心库
# 安装官方工具包
uv pip install "fast-mcp>=1.5" mcp-toolkit anthropic-mcp # 验证安装
python -c "from fast_mcp import __version__; print(f'FastMCP v{__version__}')"
四、Node.js环境配置(MCP客户端)
1. 初始化项目
npm init -y # 安装TypeScript(推荐)
npm install -D typescript @types/node
2. 添加MCP客户端SDK
# 安装Anthropic官方客户端
npm install @anthropic/mcp-client @mcp-tools/cli # 创建tsconfig.json
npx tsc --init --target es2022 --module commonjs
##五、双环境联动测试
1. 启动Mock MCP服务端
创建 demo_server.py:
from fast_mcp import FastMCP, MCPTool @MCPTool(name="greet")
def greet(name: str, context: dict) -> str: return f"你好, {name}! 当前用户: {context.get('user')}" if __name__ == "__main__": server = FastMCP(port=8080) server.register_tool(greet) server.run()
启动服务:
uv pip install uvicorn # 安装Web服务器
uvicorn demo_server:server --port 8080
2. Node.js客户端调用测试
创建 client_test.ts:
import { MCPClient } from'@anthropic/mcp-client'; const client = new MCPClient('http://localhost:8080'); asyncfunction testGreet() {
const response = await client.execute({ tool_name: 'greet', parameters: { name: 'MCP开发者' }, context: { user: 'test@example.com' } }); console.log(response.result);
// 预期输出: 你好, MCP开发者! 当前用户: test@example.com
} testGreet();
运行客户端:
npx ts-node client_test.ts
六、开发环境增强配置
1. VS Code推荐插件
2. 调试配置(.vscode/launch.json)
{ "configurations": [ { "name": "启动MCP服务", "type": "python", "request": "launch", "module": "uvicorn", "args": ["demo_server:server", "--port=8080"] }, { "name": "执行客户端测试", "type": "node", "request": "launch", "runtimeArgs": ["-r", "ts-node/register"], "args": ["${workspaceFolder}/client_test.ts"] } ]
}
七、常见问题排障指南
问题1:UV安装超时 解决方案:
# 手动下载二进制(Linux示例)
curl -LO https://github.com/astral-sh/uv/releases/download/v0.2.0/uv-linux-x86_64
chmod +x uv-linux-x86_64
sudo mv uv-linux-x86_64 /usr/local/bin/uv
问题2:跨域请求被拦截 修复方案:在服务端添加CORS支持
# 在demo_server.py中添加
from fast_mcp.middleware import CORSMiddleware server = FastMCP(port=8080)
server.add_middleware(CORSMiddleware, allow_origins=["*"])
问题3:Node客户端类型错误 处理步骤:
# 1. 确保安装TypeScript
npm install -D typescript # 2. 生成类型声明
npx mcp-tools generate-types --output src/mcp-types.d.ts
八、下一步学习建议
完成环境配置后,推荐实践路径:
- 修改greet工具:添加多语言支持
- 连接真实AI模型:集成Claude/DeepSeek API
- 部署到云服务:尝试Vercel一键部署
npm install -g vercel
vercel deploy --env MCP_URL=http://localhost:8080
🚀 效率提示:使用Anthropic提供的环境检测工具快速验证:
npx @mcp-tools/check-env
# 输出结果应全部为绿色[PASS]
推荐阅读: MCP零基础学习(1)| MCP 协议核心原理解析 主流自动化测试框架:技术解析与实战手册