模型上下文协议(MCP)实践指南
python SDK :https://github.com/modelcontextprotocol/python-sdk/tree/main?tab=readme-ov-file
- step1 : 安装mcp
pip install "mcp[cli]"
要求python 大于3.10.
- step2 测试demo
mcp dev server.py
出现下面的错误,
[06/10/25 20:14:37] ERROR npx not found. Please ensure Node.js and npm are properly installed and added to your system PATH. You may need to restart your terminal after installation. cli.py:308
解决办法是安装相应的依赖
sudo apt install nodejs npm
这一步会安装很多很多依赖,需要谨慎操作。
安装完这些依赖后再运行, 还需要安装一个工具@modelcontextprotocol/inspector, 不过这个是自动安装的
Need to install the following packages:@modelcontextprotocol/inspector
Ok to proceed? (y) y
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: '@modelcontextprotocol/sdk@1.12.1',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'concurrently@9.1.2',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'open@10.1.2',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'typescript@5.8.3',
npm WARN EBADENGINE required: { node: '>=14.17' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'commander@13.1.0',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'pkce-challenge@4.1.0',
npm WARN EBADENGINE required: { node: '>=16.20.0' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'express@5.1.0',
npm WARN EBADENGINE required: { node: '>= 18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'eventsource@3.0.7',
npm WARN EBADENGINE required: { node: '>=18.0.0' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'express-rate-limit@7.5.0',
npm WARN EBADENGINE required: { node: '>= 16' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'pkce-challenge@5.0.0',
npm WARN EBADENGINE required: { node: '>=16.20.0' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'eventsource-parser@3.0.2',
npm WARN EBADENGINE required: { node: '>=18.0.0' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'body-parser@2.2.0',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'merge-descriptors@2.0.0',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'router@2.2.0',
npm WARN EBADENGINE required: { node: '>= 18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'send@1.2.0',
npm WARN EBADENGINE required: { node: '>= 18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'serve-static@2.2.0',
npm WARN EBADENGINE required: { node: '>= 18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'default-browser@5.2.1',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'is-inside-container@1.0.0',
npm WARN EBADENGINE required: { node: '>=14.16' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'is-wsl@3.1.0',
npm WARN EBADENGINE required: { node: '>=16' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'bundle-name@4.1.0',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'default-browser-id@5.0.0',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'run-applescript@7.0.0',
npm WARN EBADENGINE required: { node: '>=18' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'path-to-regexp@8.2.0',
npm WARN EBADENGINE required: { node: '>=16' },
npm WARN EBADENGINE current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
file:///home/pinefield/.npm/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/inspector/cli/build/cli.js:30const CLIENT_PORT = process.env.CLIENT_PORT ?? "6274";^SyntaxError: Unexpected token '?'at Loader.moduleStrategy (internal/modules/esm/translators.js:133:18)at async link (internal/modules/esm/module_job.js:42:21)
[06/10/25 20:17:54] ERROR Dev server failed
自动安装完后会有一个检查, 上面的检查结果表明刚才安装的依赖的版本都不符合要求, 并且最终程序出错了。
解决办法, 升级node和npm版本, 有2种方式:
1种方式是直接升级:
# Remove old version
sudo apt remove nodejs npm# Install latest LTS
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
但是通常会遇到网络问题无法完成
另一种方式是使用 Node Version Manager (推荐):
# For Linux/macOS
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Restart terminal, then:
nvm install --lts
nvm use --lts
成功运行结果如下:
(leo_py311) pinefield@edge-gpu-01:/data/joyiot/leo/codes/mcp_tutorial$ mcp dev server.py
Starting MCP inspector...
⚙️ Proxy server listening on port 6277
🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀
打开这个页面, 显示如下:
如果想要在Claude app中使用刚才写好的mcp server, 需要执行安装
mcp install server.py
安装成功会有如下提示:
然后重启Claude app, 查看mcp设置:
点Edit Config 可以直接打开配置文件。
配置文件如下:
{"mcpServers": {"Demo": {"command": "E:\\anaconda3\\Scripts\\uv.EXE","args": ["run","--with","mcp[cli]","mcp","run","E:\\work\\LLM\\codes\\mcp_tutorial\\server.py"]}}
}
如果一切正常的话, 可以在Claude app首页看到这样的图标, 这表明mcp server已经被添加好了。
使用:
当在Claude app中进行对话时, 会自动识别是否需要调用相应服务, 首次使用时会有确认的提示: