一个全面的FastAPI应用程序,托管多个模型上下文协议(MCP)服务器,每个服务器提供对不同公共API和服务的访问。
该项目包括5个不同的MCP服务器:
📁 项目结构
fastapi-multi-server-mcp/
├── main.py # FastAPI应用程序入口点
├── requirements.txt # Python依赖项
├── pyproject.toml # UV项目配置
├── .env.example # 环境变量模板
├── README.md # 此文件
├── servers/ # MCP服务器目录
│ ├── __init__.py
│ ├── weather_server.py # 天气API MCP服务器
│ ├── news_server.py # 新闻API MCP服务器
│ ├── currency_server.py # 货币API MCP服务器
│ └── quote_server.py # 引言API MCP服务器
├── utils/ # 实用模块
│ ├── __init__.py
│ ├── api_clients.py # HTTP客户端实用工具
│ └── config.py # 配置管理
├── tests/ # 测试套件
│ ├── __init__.py
│ ├── test_weather.py
│ ├── test_news.py
│ ├── test_currency.py
│ └── test_quote.py
└── docs/ # 文档
├── api_reference.md
└── deployment.md
# 克隆仓库
git clone <your-repo-url>
cd fastapi-multi-server-mcp
# 创建虚拟环境并安装依赖项
uv venv
source .venv/bin/activate # 在Windows上:.venv\Scripts\activate
uv pip install -r requirements.txt
pip install -r requirements.txt
在根目录创建一个.env文件,并添加您的API密钥:
# 天气API(OpenWeatherMap)
OPENWEATHER_API_KEY=your_openweather_api_key
# 新闻API
NEWS_API_KEY=your_news_api_key
# 汇率API
EXCHANGE_RATES_API_KEY=your_exchange_rates_api_key
# 服务器配置
PORT=10000
HOST=0.0.0.0
# 使用UV
uv run uvicorn main:app --reload --host 0.0.0.0 --port 10000 --reload
# 使用`npx`调试运行中的应用程序,
npx @modelcontextprotocol/inspector uv run uvicorn main:app
要连接特定服务器到检查器,
设置,
传输类型:StreamableHTTP
URL:
引言服务器:

新闻服务器:

天气服务器:

货币服务器:

截至2025年6月,Windsurf和Cursor支持两种MCP服务器传输类型:stdio和/sse,但不支持StreamableHTTP。因此建议使用stdio或/sse作为传输类型。但是,使用检查器,您可以连接到运行在FastAPI上的MCP服务器,无论其传输类型如何。
# 使用Gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:10000
一旦成功运行一切,
您的FastAPI服务器将在:http://localhost:10000可用,文档在http://localhost:10000/docs,您的MCP服务器将在以下位置可用:
# 运行所有测试
pytest
# 带覆盖率运行
pytest --cov=.
# 运行特定服务器测试
pytest tests/test_weather.py
get_current_weather - 获取城市的当前天气
get_weather_forecast - 获取5天天气预报
get_weather_by_coordinates - 根据经纬度获取天气
get_top_headlines - 获取头条新闻
search_news - 根据关键词搜索新闻文章
get_news_by_category - 根据类别获取新闻(商业、科技、体育等)
get_exchange_rates - 获取当前汇率
convert_currency - 在货币之间进行转换
get_supported_currencies - 列出所有支持的货币
get_random_quote - 获取随机启发性引言
get_quote_by_category - 根据类别获取引言
get_random_fact - 获取随机有趣事实
</details>
🚀 部署
您可以使用Docker部署此项目。示例Dockerfile已包含:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 10000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "10000"]
PORT=10000
HOST=0.0.0.0
LOG_LEVEL=info
WORKERS=4
📝 许可证
本项目根据MIT许可证发布 - 查看LICENSE.md文件了解详情。
📚 支持与文档
docs/目录中的文档🔄 更新和路线图