当前位置: 首页 > backend >正文

模型上下文协议(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中进行对话时, 会自动识别是否需要调用相应服务, 首次使用时会有确认的提示:
在这里插入图片描述
在这里插入图片描述

http://www.xdnf.cn/news/13357.html

相关文章:

  • 医院系统源码核心功能开发探索:问诊、陪诊、预约挂号、电子处方
  • 华为仓颉语言初识:并发编程之同步机制(下)
  • 分布式ID最新最佳实践?UUIDv7介绍
  • 进程间通信详解(二):System V IPC 三件套全面解析
  • API网关是什么?原理、功能与架构应用全解析
  • 单位的oa系统能不能在家电脑登陆?办公网址在手机上怎么访问?
  • 如何xml序列化 和反序列化类中包含的类
  • tomcat的websocket协议升级。如何从报文交换变成全双工通信?session对象的注册和绑定?
  • nginx配置中有无‘‘/’’的区别
  • mybatis 关联映射---一对一关联映射
  • LAMP-Cloud与RuoYi-Cloud技术架构对比
  • 大模型驱动的具身智能: 发展与挑战--综述--中国电信人工智能研究院--2024.8.29
  • 风中低语:Linux 信号处理的艺术与实践
  • 新一代 Rust Web 框架的高性能之选
  • 利用ngx_stream_return_module构建简易 TCP/UDP 响应网关
  • ubuntu-root密码遗忘重设方法
  • 校验枚举类类型的入参合法性的统一方案
  • 基于算法竞赛的c++编程(28)结构体的进阶应用
  • DP 1.4 to HDMI 2.1 (DSC) ,8k@60Hz
  • 【WebRTC-14】webrtc是如何创建视频的软/硬编码器?
  • AR 珠宝佩戴,突破传统的购物新体验​
  • visual studio 2022更改主题为深色
  • 学校招生小程序源码介绍
  • Web安全漏洞详解及解决方案
  • LarkXR 赋能AI x XR数字供应链:引领智能设计、数字孪生与零售新未来
  • Android 中使用 OkHttp 创建多个 Client
  • NLP学习路线图(三十七): 问答系统
  • 数据工程全景指南:从基础概念到最佳实践
  • 多面体编译,具体操作模式
  • 使用 origin -> master 强制覆盖本地 master