python-MCPServer拉取和使用部署
系统环境
系统 macOS 26.0.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
本地启动
- 点开 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