天气AI代理
一个由微软代理框架、模型上下文协议(MCP)和Azure服务支持的会话式人工智能天气助手。
   
概述
该项目展示了一个生产就绪的人工智能代理系统,该系统通过自然语言查询提供对话式天气信息。它展示了:
- 🤖 微软代理框架:通过工具集成构建人工智能代理
- 🔧 模型上下文协议(MCP):人工智能系统的标准化工具接口
- ☁️ 微软铸造厂:Azure上的托管代理部署
- 📦 Azure容器应用:可扩展的无服务器容器托管
- 🎨 Next.js前端:具有流式响应的现代web UI
建筑
┌─────────────────┐ HTTPS ┌─────────────────┐ MCP/HTTP ┌─────────────────┐
│ Frontend │────────────────▶│ Backend │────────────────▶│ MCP Server │
│ (Next.js) │◀────────────────│ (AI Agent) │◀────────────────│ (Weather Tool) │
│ │ SSE │ │ │ │
│ - Chat UI │ │ - Agent │ │ - get_weather │
│ - Streaming │ │ - Foundry │ │ - Mock data │
│ │ │ - MCP Tool │ │ │
└─────────────────┘ └────────┬────────┘ └─────────────────┘
│
│
▼
┌─────────────────┐
│ Azure OpenAI │
│ (Foundry) │
│ - gpt-4o-mini │
└─────────────────┘特性
对于用户
- 💬 自然语言天气查询
- 🌍 任何城市的天气信息
- ⚡ 实时流媒体响应
- 📱 响应式移动和桌面UI
对于开发者
- 🏗️ 干净、模块化的架构
- 🐳 容器就绪部署
- 📖 全面的文件
- 🧪 包含测试脚本
- 🔐 Azure托管身份支持
快速开始
先决条件
- Python 3.12+
- Node.js 20+ (前端)
- Azure命令行界面 已认证(
az login) - Azure OpenAI资源 已部署模型
- 码头工人 (可选,用于集装箱化)
1.克隆存储库
git clone https://github.com/YOUR-ORG/spec2cloud-weather-agent.git
cd spec2cloud-weather-agent2.启动MCP服务器
cd src/mcp-server
pip install -r requirements.txt
python server.py
# Server runs at http://localhost:8000/mcp3.启动后端代理
cd src/backend
# Set environment variables
$env:AZURE_OPENAI_ENDPOINT="https://YOUR-RESOURCE.openai.azure.com/"
$env:AI_MODEL_DEPLOYMENT="gpt-4o-mini"
# Install and run
pip install -r requirements.txt
python main.py
# Agent runs at http://localhost:8088看 后端快速入门 详细设置。
4.测试后端
cd src/backend
.\test_agent.ps15.启动前端(即将推出)
cd src/frontend
npm install
npm run dev
# Frontend at http://localhost:3000项目结构
spec2cloud-weather-agent/
├── src/
│ ├── backend/ # ⭐ AI Agent (Microsoft Agent Framework)
│ │ ├── main.py # Agent implementation
│ │ ├── requirements.txt # Python dependencies
│ │ ├── Dockerfile # Container config
│ │ ├── README.md # Detailed docs
│ │ ├── QUICKSTART.md # 5-minute setup
│ │ └── run_local.ps1 # Dev script
│ │
│ ├── mcp-server/ # 🔧 Weather MCP Tool (FastMCP)
│ │ ├── server.py # MCP server implementation
│ │ ├── requirements.txt # Python dependencies
│ │ └── README.md # Documentation
│ │
│ └── frontend/ # 🎨 Web UI (Next.js) - Coming Soon
│
├── infra/ # 🏗️ Azure Infrastructure (Bicep)
│ ├── main.bicep # Main infrastructure
│ └── core/ # Reusable modules
│
├── specs/ # 📋 Requirements & Design
│ ├── PRD.md # Product requirements
│ └── FRD.md # Functional requirements
│
├── .github/ # 🛠️ Skills & Workflows
│ └── skills/ # Reusable patterns
│ ├── hosted-agents/ # Agent deployment patterns
│ └── mcp-builder/ # MCP server patterns
│
└── azure.yaml # Azure Developer CLI config组件详情
后端代理
- 框架:Microsoft代理框架
- 语言:Python 3.12+
- 模型:Azure OpenAI(gpt-4o-mini)
- API:带有SSE的OpenAI响应协议
- 部署:通过Foundry提供Azure容器应用程序
MCP服务器
- 框架:FastMCP
- 语言:Python 3.12+
- 工具:
get_weather(city)使用模拟数据 - 运输:具有流式传输协议的HTTP
前端
- 框架:Next.js 14+
- 语言:TypeScript
- 用户界面:顺风CSS
- 特性:聊天界面,SSE流媒体
📖 前端文档 _(即将推出)_
发展
本地开发
# Terminal 1: MCP Server
cd src/mcp-server && python server.py
# Terminal 2: Backend Agent
cd src/backend && .\run_local.ps1
# Terminal 3: Frontend (when available)
cd src/frontend && npm run dev测试
# Test backend
cd src/backend
.\test_agent.ps1
# Manual test with curl
curl -sS -H "Content-Type: application/json" -X POST http://localhost:8088/responses -d '{
"input": {"messages": [{"role": "user", "content": "What is the weather in Paris?"}]},
"stream": false
}'码头工人
# Build backend
cd src/backend
docker build -t weather-agent:latest .
docker run -p 8088:8088 -e AZURE_OPENAI_ENDPOINT="..." weather-agent:latest
# Build MCP server
cd src/mcp-server
docker build -t weather-mcp:latest .
docker run -p 8000:8000 weather-mcp:latest部署
使用Azure开发者命令行界面(推荐)
# Initialize (first time)
azd init
# Provision and deploy all resources
azd up
# Deploy updates
azd deploy手动部署
看 基础设施文档 用于使用Azure CLI和二头肌进行手动部署的步骤。
配置
环境变量
后端
AZURE_AI_PROJECT_ENDPOINT=https://{account}.services.ai.azure.com/api/projects/{project}
AZURE_OPENAI_ENDPOINT=https://{resource}.openai.azure.com/ # For local dev
AI_MODEL_DEPLOYMENT=gpt-4o-mini
MCP_SERVER_URL=http://127.0.0.1:8000/mcp # Local devMCP服务器
PORT=8000 # Optional, defaults to 8000有关更多详细信息,请参阅特定于组件的文档。
关键技术
- 微软代理框架:使用工具构建人工智能代理
- 模型上下文协议:标准化工具界面
- 微软铸造厂:代理托管和AI模型访问
- Azure容器应用:无服务器容器托管
- FastMCP:Python MCP服务器框架
- Next.js:用于web UI的React框架
文档
API 参考
后端代理API
端点: POST /responses
请求:
{
"input": {
"messages": [
{"role": "user", "content": "What's the weather in Tokyo?"}
]
},
"stream": false
}回应:
{
"output": [
{
"role": "assistant",
"content": "The current weather in Tokyo is..."
}
],
"usage": {"input_tokens": 25, "output_tokens": 50}
}流媒体:设置 "stream": true 用于SSE流媒体响应。
故障排除
常见问题
后端无法启动
- 确保设置了环境变量(请参见
.env.example) - 检查Azure身份验证:
az account show - 验证Azure OpenAI端点和部署名称
MCP服务器无法访问
- 确保MCP服务器在端口8000上运行
- 检查
MCP_SERVER_URL环境变量 - 直接测试:
curl http://localhost:8000/mcp
代理未调用天气工具
- 验证MCP服务器是否可以从后端访问
- 检查代理日志中的工具调用错误
- 确保MCP服务器URL正确
有关详细的故障排除,请参阅特定于组件的文档。
贡献
欢迎投稿!拜托:
许可证
此项目根据MIT许可证获得许可-请参阅 许可证.md 了解详情。
资源
文档
相关项目
支持
对于问题和疑问:
- 📖 请先查看组件自述文件
- 🐛 打开bug问题
- 💬 问题讨论
- 📧 联系团队
______________________________________________________________________
由...制作❤️ 使用Microsoft Agent框架和模型上下文协议
