多服务器MCP FastAPI项目
一个全面的FastAPI应用程序,托管多个模型上下文协议(MCP)服务器,每个服务器都提供对不同公共API和服务的访问。
工作演示
🚀 特性
该项目包括5个不同的MCP服务器:
- 天气服务器 –天气数据的OpenWeatherMap API集成
- 新闻服务器 –最新新闻文章的NewsAPI集成
- 货币服务器 –汇率和货币兑换
- 报价服务器 –鼓舞人心的引语和随机事实
📋 先决条件
- Python 3.13+
- 紫外线的 包管理器
- 外部服务的API密钥(请参阅配置部分)
📁 项目结构
fastapi-multi-server-mcp/
├── main.py # FastAPI application entry point
├── requirements.txt # Python dependencies
├── pyproject.toml # UV project configuration
├── .env.example # Environment variables template
├── README.md # This file
├── servers/ # MCP servers directory
│ ├── __init__.py
│ ├── weather_server.py # Weather API MCP server
│ ├── news_server.py # News API MCP server
│ ├── currency_server.py # Currency API MCP server
│ └── quote_server.py # Quotes API MCP server
├── utils/ # Utility modules
│ ├── __init__.py
│ ├── api_clients.py # HTTP client utilities
│ └── config.py # Configuration management
├── tests/ # Test suite
│ ├── __init__.py
│ ├── test_weather.py
│ ├── test_news.py
│ ├── test_currency.py
│ └── test_quote.py
└── docs/ # Documentation
├── api_reference.md
└── deployment.md🛠️ 安装
使用紫外线(推荐)
# Clone the repository
git clone
cd fastapi-multi-server-mcp
# Create virtual environment and install dependencies
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv pip install -r requirements.txt使用pip
pip install -r requirements.txt🔧 配置
创建一个 .env 根目录中带有API密钥的文件:
# Weather API (OpenWeatherMap)
OPENWEATHER_API_KEY=your_openweather_api_key
# News API
NEWS_API_KEY=your_news_api_key
# Exchange Rates API
EXCHANGE_RATES_API_KEY=your_exchange_rates_api_key
# Server Configuration
PORT=10000
HOST=0.0.0.0获取API密钥
- OpenWeatherFolder:注册地址 openweathermap.org
- 新闻API:获取免费的API密钥 newsapi.org
- 汇率:注册 汇率-api.com
- 引用API:在获取免费API密钥 quotesapi.com
🏃♂️ 运行应用程序
发展
# Using UV
uv run uvicorn main:app --reload --host 0.0.0.0 --port 10000 --reload
# Using `npx` to debug the running application,
npx @modelcontextprotocol/inspector uv run uvicorn main:app为了将特定服务器连接到检查器,
设置,
- 运输类型:
StreamableHTTP
- 网址:
- 新闻:http://localhost:10000/news/mcp/
- 天气:http://localhost:10000/weather/mcp/
- 对于货币:http://localhost:10000/currency/mcp/
- 对于报价:http://localhost:10000/quotes/mcp/
- 点击连接,将打开检查器窗口,如下所示:
报价服务器:
新闻服务器:
天气服务器:
货币服务器:
截至2025年6月,Windsurf和Cursor支持MCP服务器的两种传输类型: stdio 和 /sse,但不是 StreamableHTTP因此,使用 stdio 或 /sse 因为推荐运输类型。但是,使用检查器,您可以使用任何传输类型连接到在FastAPI上运行的MCP服务器。
生产
# Using Gunicorn
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:10000📚 API终点
一旦一切运行成功,
您的FastAPI服务器将在以下位置可用:http://localhost:10000,文件在http://localhost:10000/docs 您的MCP服务器将在以下网址提供:
- 天气服务器:http://localhost:10000/weather
- 新闻服务器:http://localhost:10000/news
- 货币服务器:http://localhost:10000/currency
- 报价服务器:http://localhost:10000/quotes
🧪 测试
# Run all tests
pytest
# Run with coverage
pytest --cov=.
# Run specific server tests
pytest tests/test_weather.py🛠️ 按服务器列出的可用工具
🖼️ Click to see the tool details
天气服务器(/Weather)
get_current_weather - Get current weather for a city
get_weather_forecast - Get 5-day weather forecast
get_weather_by_coordinates - Get weather by latitude/longitude新闻服务器(/News)
get_top_headlines - Get top news headlines
search_news - Search news articles by keyword
get_news_by_category - Get news by category (business, tech, sports, etc.)货币服务器(/货币)
get_exchange_rates - Get current exchange rates
convert_currency - Convert between currencies
get_supported_currencies - List all supported currencies报价服务器(/quotes)
get_random_quote - Get a random inspirational quote
get_quote_by_category - Get quotes by category
get_random_fact - Get a random interesting fact🚀 部署
码头工人
您可以使用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/目录 - 查看测试文件以获取使用示例
🔄 更新和路线图
- 添加对实时数据的WebSocket支持
- 为API响应实现缓存
- 添加速率限制和身份验证
- 创建用于监控的web仪表板
- 添加更多API集成(Spotify、Twitter等)
