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

python-MCPServer拉取和使用部署

系统环境

系统 macOS 26.0.1
本次学习使用的项目

环境搭建

  1. 虚拟环境搭建
cd pyworkspace/news_server
uv venv
uv init 

创建一个虚拟环境以后,手动安装fastmcp,原项目中使用 mcp-server-fastmcp,依赖找不到,可能是变更了。目前使用如下方式可以解决

uv add fastmcp# 清理 requirements.txt 中的问题包
sed -i '/mcp-server-fastmcp/d' requirements.txt  # Linux/Mac
# 或手动编辑文件 ,删除requirements.txt  中的mcp-server-fastmcp并保存文件
# 安装依赖
uv pip install -r requirements.txt
# 查看依赖结果
uv pip list

本地启动

  1. 点开 Server.py文件,替换 mcp.run(transport="streamable-http") 本地启动测试;更推荐作为服务使用 参考该文档不同传输模式对比

在这里插入图片描述

docekr 部署

当前项目需要修改的点如下,修改的目的,docker 中部署指定 host 和 port
server.py 中
1.
mcp = FastMCP("NewsServer", host="0.0.0.0", port=8000)
2.

    # 使用stdio传输# Bind to 0.0.0.0 so the service is reachable from outside the container# and ensure it listens on port 8000 (the container's exposed port).# FastMCP.run forwards args to the underlying ASGI server (uvicorn),# so passing host/port here makes the containerized service accessible.try:mcp.run(transport="streamable-http", host="0.0.0.0", port=8000)except TypeError:# If the FastMCP.run implementation doesn't accept host/port keyword args,# fall back to the default call so the module still runs.mcp.run(transport="streamable-http")
import asyncio
import httpx
import feedparser
from datetime import datetime
from typing import List, Dict
from mcp.server.fastmcp import FastMCPmcp = FastMCP("NewsServer", host="0.0.0.0", port=8000)class NewsFetcher:def __init__(self):self.client = Noneself.news_sources = {"tech": ["https://www.ithome.com/rss/",],"general": ["http://www.people.com.cn/rss/politics.xml"],"international": ["http://rss.cnn.com/rss/edition.rss","https://feeds.bbci.co.uk/news/world/rss.xml"]}async def initialize(self):"""初始化HTTP客户端"""if self.client is None:self.client = httpx.AsyncClient(timeout=30.0,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'})return Trueasync def fetch_hot_news(self, category: str = "general", limit: int = 20) -> List[Dict]:"""获取热点新闻"""await self.initialize()news_list = []sources = self.news_sources.get(category, self.news_sources
http://www.dtcms.com/a/610281.html

相关文章:

  • 17.TCP编程(二)和序列化
  • x^3 - 3x + 1 = 0
  • Nacos 9848启动端口占用 Failed to bind to address 0.0.0.0/0.0.0.0:9848
  • 前端react 开发 图书列表分页
  • 做vip视频网站赚钱吗最近一周新闻大事件
  • 大数据安全技术实验:Hadoop环境部署
  • 【07】特征匹配算法:ORB算法深度解析与实现
  • vite7更新了哪些内容
  • Aemulo2.0门禁卡复制卡片后修改设置卡片备注名称
  • IP应用场景全图谱:你的IP属于哪一类?
  • 微网站开发 在线商城一键部署wordpress
  • Rust实战:使用Clap和Tokio构建现代CLI应用
  • 中移建设有限公司网站猎头可以做单的网站
  • PostIn V1.3.4版本发布,新增性能测试执行明细,ldap/企业微信/钉钉登录调整为社区版本功能
  • MySQL——表的约束
  • springboot对接xxl-job
  • 企业百度网站建设网络策划是做什么的
  • 网站项目开发流程有哪七步网站素材 按钮
  • Spring Boot 全局异常处理 + 参数校验进阶:让接口告别 “500 报错” 和 “脏数据”
  • Frame structure and physical resources(帧结构与物理资源)
  • 进程状态
  • 做网站ps注意事项个人备案网站可以做电商吗
  • 如何用工控做网站重庆建设安全管理网
  • Java_泛型入门
  • 华为OD机试双机位A卷 - 机器人活动区域 (Python C++ JAVA JS GO)
  • 安卓C语言编译器——高效编程工具,助力开发者提升编程效率
  • 求大神帮忙做网站网站开发收费表
  • 基于uWebSockets开源库实现一个web服务
  • 网站地图后缀WordPress分类中文404错误
  • c 网站做死循环中国建设银行总部网站