天气代理

一个基于AI的天气代理,结合GitHub Models LLM和MCP服务器提供实时天气数据和对话式天气预报。

  • by StephenDryden
  • C#
  • 本地部署
  • 更新于
在 GitHub 查看 stephendryden/agent-weatherman

使用说明

气象员🌤️

一个用C#构建的复杂的人工智能天气代理,将GitHub Models LLM与MCP(模型上下文协议)服务器相结合,用于实时天气数据。该代理充当友好的气象员,提供对话式的天气预报和见解。

特性

  • 🤖 AI驱动的对话:使用GitHub模型API(GPT-4o-mini)进行自然语言交互
  • 🌦️ 实时天气数据:连接到MCP服务器以获取当前天气和预报
  • 🐳 Docker就绪:容器化,便于部署到AWS ECS或任何容器平台
  • ⚡ 最小配置:使用环境变量进行简单设置
  • 📱 交互式控制台:类似聊天的天气查询界面
  • 🔗 MCP协议:与气象数据源进行基于标准的通信

建筑

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│  Weather Agent  │───▶│  GitHub Models  │    │   MCP Server    │
│   (Console UI)  │    │   LLM API       │    │ (Weather Data)  │
└─────────────────┘    └─────────────────┘    └─────────────────┘
         │                        │                        │
         └────────────────────────┼────────────────────────┘
                                  │
                            WebSocket/HTTP

先决条件

快速开始

1.克隆和构建

git clone https://github.com/StephenDryden/agent-weatherman.git
cd agent-weatherman
make all  # or: dotnet build && docker build -t agent-weatherman .

2.配置

设置您的GitHub令牌:

export GITHUB_TOKEN="your-github-personal-access-token"

3.运行代理

选项A:在本地运行

make run
# or manually:
export WEATHERAGENT_AgentSettings__GitHubToken="$GITHUB_TOKEN"
export WEATHERAGENT_AgentSettings__McpServerUrl="ws://localhost:3000"
dotnet run --project AgentWeatherman/AgentWeatherman.csproj

选项B:使用Docker运行

make docker-build
make docker-run-local

配置

代理可以通过环境变量或 appsettings.json:

环境变量

变量 描述 默认值
WEATHERAGENT_AgentSettings__GitHubToken GitHub个人访问令牌 必填
WEATHERAGENT_AgentSettings__McpServerUrl MCP服务器WebSocket URL ws://localhost:3000
WEATHERAGENT_AgentSettings__ModelName GitHub模型名称 gpt-4o-mini
WEATHERAGENT_AgentSettings__MaxTokens 每个响应的最大令牌数 1000
WEATHERAGENT_AgentSettings__Temperature LLM温度(0.0-1.0) 0.7

应用程序参数

{
  "AgentSettings": {
    "GitHubToken": "your-token-here",
    "GitHubModelsEndpoint": "https://models.inference.ai.azure.com",
    "ModelName": "gpt-4o-mini",
    "McpServerUrl": "ws://localhost:3000",
    "MaxTokens": 1000,
    "Temperature": 0.7
  }
}

使用示例

运行后,您可以自然地与天气代理交互:

You: What's the weather like in London today?

🌤️ Weather Agent: Good morning! Let me check the current weather in London for you.

Looking at the conditions right now, London is experiencing partly cloudy skies with a temperature of 18°C (64°F). There's a gentle breeze from the southwest at about 12 km/h, and humidity is sitting at a comfortable 65%.

For today, you can expect the temperature to reach a pleasant 22°C (72°F) this afternoon with the clouds gradually clearing. It's a great day for outdoor activities! I'd recommend a light jacket for the morning, but you should be comfortable in just a t-shirt by midday.

Would you like me to check the forecast for the next few days as well?

Docker部署

构建并运行

# Build the Docker image
make docker-build

# Run with local MCP server
make docker-run-local

# Run with remote MCP server
MCP_SERVER_URL="ws://your-mcp-server:3000" make docker-run

AWS ECR部署

# Set AWS credentials and region
export AWS_ACCOUNT_ID="123456789012"
export AWS_REGION="us-west-2"

# Login to ECR and push
make ecr-login
make ecr-push

MCP服务器集成

代理需要一个提供天气工具的MCP服务器。服务器应实现:

所需工具

  1. get_current天气

    • 参数: location (字符串)
    • 返回:当前天气状况
  2. get_weather_forecast

    • 参数: location (字符串), days (编号)
    • 返回:多日天气预报

MCP协议

代理通过WebSocket使用标准MCP协议进行通信:

{
  "jsonrpc": "2.0",
  "id": "unique-id",
  "method": "tools/call",
  "params": {
    "name": "get_current_weather",
    "arguments": {
      "location": "London, UK"
    }
  }
}

发展

项目结构

AgentWeatherman/
├── Models/
│   ├── Configuration/     # Settings and configuration
│   ├── GitHub/           # GitHub Models API DTOs
│   └── Mcp/             # MCP protocol models
├── Services/
│   ├── GitHubLlmService.cs      # GitHub Models API client
│   ├── McpClientService.cs      # MCP protocol client
│   └── WeatherAgentService.cs   # Main agent orchestrator
├── Program.cs           # Application entry point
└── appsettings.json    # Default configuration

可用的生成命令

make help              # Show all available commands
make build             # Build the solution
make run               # Run locally
make docker-build      # Build Docker image
make docker-run-local  # Run container with local MCP server
make clean             # Clean build artifacts
make test              # Run tests

添加新功能

  1. 新型LLM模型:更新 ModelName 在配置中
  2. 其他天气工具:扩展 McpClientService 采用新的工具方法
  3. 增强对话:修改中的系统提示 WeatherAgentService

故障排除

常见问题

  1. GitHub API身份验证错误

    • 验证您的GitHub令牌是否具有正确的权限
    • 支票令牌尚未过期
  2. MCP服务器连接失败

    • 确保MCP服务器正在运行且可访问
    • 验证WebSocket URL格式: ws://host:port
  3. Docker网络问题

    • 使用 --network host 用于本地MCP服务器
    • 确保远程服务器的容器网络正常

日志记录

该应用程序使用结构化日志记录。通过以下方式设置日志级别:

export DOTNET_ENVIRONMENT="Development"  # Verbose logging
# or
export Logging__LogLevel__Default="Debug"

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 如果适用,添加测试
  5. 提交拉取请求

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

支持

对于问题和疑问:

  • 在GitHub上创建问题
  • 检查上面的故障排除部分
  • 查看MCP服务器文档以了解集成详细信息