天气数据服务器

一个基于Azure AD认证和Azure容器实例部署的实时天气数据API服务,支持温度、天气条件和位置数据查询。

  • by PlumyCat
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 plumycat/my-first-mcp-server

使用说明

🌤️ MCP气象服务器

用于天气信息的模型上下文协议(MCP)服务器,具有Azure AD身份验证和Azure容器实例部署。

📋 目录

✨ 功能

  • 🌡️ 实时天气API 具有温度、条件和位置数据
  • 🔐 Azure AD身份验证 使用安全的JWT代币
  • ☁️ Azure部署 在具有专用注册表的容器实例上
  • 🤖 IA集成 :兼容Azure OpenAI et Claude API
  • 🐳 集装箱化 :Docker与HealthCheck和日志
  • 🧪 测试完成 :本地、Docker、具有身份验证的Azure
  • 📊 监控 :Azure管理和监控脚本

🚀 安装

选项1:安装PowerShell(Windows)

Prérequis

  • 带PowerShell 7的Windows 10/11+
  • 已安装Azure CLI
  • Docker桌面安装
  • 具有容器实例权限的Azure帐户

完全安装

  1. Cloner le存储库
git clone https://github.com/votre-username/mcp-weather-server.git
cd mcp-weather-server
  1. 环境变量配置
# Copier le fichier d'exemple
Copy-Item "env_example.txt" ".env"

# Éditer le fichier .env avec vos clés
notepad .env
  1. Azure部署
# Se connecter à Azure
az login

# Déployer le serveur (script principal)
.\deploy-azure.ps1

# Sécuriser avec Azure AD (après déploiement)
.\azure-secure.ps1
  1. 测试与验证
# Tester le déploiement complet
python test/test_azure_deployment.py

# Gérer le serveur Azure
.\azure-manage.ps1 status
.\azure-manage.ps1 logs

选项2:安装外壳(Linux/macOS)

Prérequis

  • Linux/MacOS与Bash
  • 已安装Azure CLI
  • Docker已安装
  • 具有容器实例权限的Azure帐户

完全安装

  1. Cloner le存储库
git clone https://github.com/votre-username/mcp-weather-server.git
cd mcp-weather-server
  1. 环境变量配置
# Copier le fichier d'exemple
cp env_example.txt .env

# Éditer le fichier .env avec vos clés
nano .env
  1. Azure部署
# Se connecter à Azure
az login

# Rendre les scripts exécutables
chmod +x deploiement/*.sh

# Déployer le serveur (script principal)
./deploiement/deploy-azure.sh

# Sécuriser avec Azure AD (optionnel - depuis Windows)
# Ou utiliser les scripts de gestion Linux
./deploiement/azure-manage.sh status
  1. 测试与验证
# Tester le déploiement
python test/test_azure_deployment.py

# Gérer le serveur Azure
./deploiement/azure-manage.sh logs
./deploiement/azure-manage.sh restart

⚙️ 配置

环境变量(.env)

# Configuration du serveur
PORT=8000
HOST=0.0.0.0
ENVIRONMENT=production
LOG_LEVEL=INFO

# Azure AD (requis pour la sécurisation)
AZURE_AD_TENANT_ID=your_tenant_id_here
AZURE_AD_CLIENT_ID=your_client_id_here
AZURE_AD_CLIENT_SECRET=your_client_secret_here

# URL du serveur Azure (mise à jour automatique)
AZURE_SERVER_URL=http://your-container.azurecontainer.io:8000

# APIs IA (optionnel pour tests)
AZURE_OPENAI_API_KEY=your_azure_openai_key_here
AZURE_OPENAI_ENDPOINT=your_azure_openai_endpoint_here
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4
ANTHROPIC_API_KEY=your_claude_api_key_here

# APIs météo (optionnel)
OPENWEATHERMAP_API_KEY=your_openweathermap_key_here
WEATHERAPI_KEY=your_weatherapi_key_here

配置Azure AD

  1. 创建Azure AD应用程序
# Via Azure CLI
az ad app create --display-name "mcp-weather-server" --sign-in-audience "AzureADMyOrg"
  1. 配置权限
  • 添加必要的API权限
  • 生成客户秘密
  • 记录租户ID、客户端ID和客户端机密

🎯 使用

快速部署

Windows(PowerShell):

# Déploiement + sécurisation en une fois
.\deploy-azure.ps1 && .\azure-secure.ps1

Linux/macOS(Shell):

# Déploiement complet
./deploiement/deploy-azure.sh

服务器管理

窗户:

# État du serveur
.\azure-manage.ps1 status

# Voir les logs
.\azure-manage.ps1 logs

# Redémarrer
.\azure-manage.ps1 restart

# URL du serveur
.\azure-manage.ps1 url

Linux/macOS:

# État du serveur
./deploiement/azure-manage.sh status

# Voir les logs
./deploiement/azure-manage.sh logs

# Redémarrer
./deploiement/azure-manage.sh restart

🧪 测试

可用测试

# Test complet du déploiement Azure
python test/test_azure_deployment.py

# Test des APIs IA localement
python test/test_azure_openai_api.py
python test/test_claude_api.py

# Test du container Docker local
python test/test_docker_local.py

# Comparaison des APIs IA
python test/compare_ai_apis.py

测试环境

# Tester différents environnements
python test/test_environments.py --env local
python test/test_environments.py --env docker
python test/test_environments.py --env azure

☁️ Azure部署

建筑

┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   Azure CLI     │───▶│  Container       │───▶│  Azure Container│
│   (Local)       │    │  Registry (ACR)  │    │  Instances (ACI)│
└─────────────────┘    └──────────────────┘    └─────────────────┘
                                │
                                ▼
                       ┌──────────────────┐
                       │   Docker Image   │
                       │  MCP Weather     │
                       │  + FastAPI       │
                       │  + Azure AD      │
                       └──────────────────┘

部署过程

  1. 创建Azure容器注册表(ACR)
  2. 构建和推送Docker映像
  3. 部署到Azure容器实例(ACI)
  4. 网络和DNS配置
  5. 使用Azure AD进行安全保护

创造的资源

  • 资源组 : mcp-weather-rg
  • 集装箱登记处 : mcpweatherXXXXXX.azurecr.io
  • 容器实例 : mcp-weather-server
  • 公共IP 使用FQDN: mcp-weather-XXXXXX.francecentral.azurecontainer.io

🔐 保护Azure AD

安全工作流

  1. 不安全服务器 (初始部署后)

    • 无需身份验证即可访问
    • 基本功能测试
  2. 激活Azure AD (通过 azure-secure.ps1)

    • 集装箱存在抑制
    • 使用Azure AD变量重新创建
    • 安全模式激活
  3. 安全服务器 (最终结果)

    • 身份验证持有者令牌要求
    • 验证JWT Azure AD
    • 带身份验证的完整测试

认证

// Exemple d'appel authentifié
const response = await fetch('http://your-server:8000/mcp', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer ' + azureAdToken,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    jsonrpc: "2.0",
    id: 1,
    method: "tools/call",
    params: {
      name: "get_weather",
      arguments: { city: "Paris" }
    }
  })
});

📁 项目结构

mcp-weather-server/
├── 📄 README.md                    # Documentation principale
├── 📄 requirements.txt             # Dépendances Python
├── 📄 Dockerfile                   # Image Docker principale
├── 📄 docker-compose.yml           # Orchestration locale
├── 📄 .env                         # Variables d'environnement
├── 📄 env_example.txt              # Exemple de configuration
├── 📄 mcp_config_example.json      # Configuration MCP exemple
├── 📄 .gitignore                   # Fichiers ignorés par Git
│
├── 📂 src/                         # Code source
│   ├── 📄 __init__.py
│   ├── 📄 main.py                  # Point d'entrée MCP stdio
│   ├── 📄 server.py                # Serveur MCP principal
│   ├── 📄 http_server.py           # Serveur HTTP/REST
│   ├── 📄 auth.py                  # Authentification Azure AD
│   └── 📂 tools/
│       ├── 📄 __init__.py
│       └── 📄 weather.py           # Outil météo MCP
│
├── 📂 test/                        # Tests et validation
│   ├── 📄 README_TESTS.md          # Documentation des tests
│   ├── 📄 test_azure_deployment.py # Test déploiement Azure
│   ├── 📄 test_azure_openai_api.py # Test Azure OpenAI
│   ├── 📄 test_claude_api.py       # Test Claude API
│   ├── 📄 test_docker_local.py     # Test Docker local
│   ├── 📄 test_mcp_server.py       # Test serveur MCP
│   ├── 📄 test_environments.py     # Test multi-environnements
│   ├── 📄 compare_ai_apis.py       # Comparaison APIs IA
│   ├── 📄 test_with_ai.py          # Tests avec IA
│   └── 📄 run_local.py             # Exécution locale
│
├── 📂 deploiement/                 # Scripts de déploiement
│   ├── 📄 README_LINUX.md          # Documentation Linux
│   ├── 📄 deploy-azure.sh          # Déploiement Azure (Linux)
│   ├── 📄 azure-setup.sh           # Configuration Azure (Linux)
│   ├── 📄 azure-manage.sh          # Gestion Azure (Linux)
│   ├── 📄 azure-cleanup.sh         # Nettoyage Azure (Linux)
│   ├── 📄 azure-update-http.sh     # Mise à jour HTTP (Linux)
│   ├── 📄 test-linux-scripts.sh    # Tests scripts Linux
│   ├── 📄 deploy-azure.ps1         # Déploiement Azure (Windows)
│   ├── 📄 azure-setup.ps1          # Configuration Azure (Windows)
│   ├── 📄 azure-manage.ps1         # Gestion Azure (Windows)
│   ├── 📄 azure-cleanup.ps1        # Nettoyage Azure (Windows)
│   └── 📄 azure-update-http.ps1    # Mise à jour HTTP (Windows)
│
├── 📄 deploy-azure.ps1             # Script de déploiement principal
├── 📄 azure-manage.ps1             # Script de gestion principal
├── 📄 azure-secure.ps1             # Script de sécurisation Azure AD
├── 📄 azure-secure-keyvault.ps1    # Sécurisation avec Key Vault
└── 📄 azure-get-token.ps1          # Génération de tokens Azure AD

🔧 主要脚本

Windows(PowerShell)

脚本 描述 用法
deploy-azure.ps1 Azure上的全面部署 .\deploy-azure.ps1
azure-secure.ps1 使用Azure AD进行安全保护 .\azure-secure.ps1
azure-manage.ps1 Azure服务器管理 .\azure-manage.ps1 status
azure-get-token.ps1 Azure AD令牌测试 .\azure-get-token.ps1

Linux/macOS(Shell)

脚本 描述 用法
deploy-azure.sh Azure上的全面部署 ./deploiement/deploy-azure.sh
azure-manage.sh Azure服务器管理 ./deploiement/azure-manage.sh status
azure-setup.sh Azure初始配置 ./deploiement/azure-setup.sh
azure-cleanup.sh 资源清理 ./deploiement/azure-cleanup.sh

🎯 推荐工作流

第一次安装

  1. 初始部署 (选择您的平台)

    • 窗户: .\deploy-azure.ps1
    • Linux: ./deploiement/deploy-azure.sh
  2. 安全 (推荐)

    • 窗户: .\azure-secure.ps1
  3. 验证

    • python test/test_azure_deployment.py

日常使用

  • 检查状态 : azure-manage status
  • 查看日志 : azure-manage logs
  • 重新启动 : azure-manage restart
  • 测试员 : python test/test_azure_deployment.py

🔍 故障排除

常见问题

  1. 无法访问的服务器

    • 检查状态: azure-manage status
    • 查阅日志: azure-manage logs
  2. 身份验证失败

    • 检查中的Azure AD变量 .env
    • 测试员le令牌: azure-get-token.ps1
  3. 重新启动的容器

    • 网络配置问题(主机=0.0.0)
    • 缺少依赖项(Fastapi、Uvicorn)

支持

  • 📖 完整文档: README_TESTS.md
  • 🐧 Linux指南: deploiement/README_LINUX.md
  • 🧪 测试:文件 test/

📝 许可证

MIT许可证-有关详细信息,请参阅许可证文件。

🤝 贡献

欢迎捐款!感谢:

  1. 分叉项目
  2. 创建功能分支
  3. 提交您的更改
  4. 向分支推
  5. 打开拉取请求

🌤️ MCP天气服务器-具有Azure广告和云部署的智能天气!