Skip to main content
Glama

🌤️ MCP 天气智能体

Python 3.13+ OpenAI MCP Playwright

一个由 OpenAI模型上下文协议(MCP) 驱动的智能多区域天气助手。

通过自然语言聊天界面,获取 以色列美国 的实时天气预报和警报。该智能体会智能地将问题路由到相应的数据源,并从实时网页数据和官方天气 API 汇总答案。


✨ 功能特性

  • 🤖 AI 驱动聊天:使用 GPT-4o-mini 进行对话式天气查询

  • 🌍 双数据源

    • 🇮🇱 以色列:通过 Playwright 从 Weather2day 实时抓取网页数据

    • 🇺🇸 美国:官方国家气象局 API

  • 🔄 智能工具路由:智能体根据你的问题自动选择正确的工具

  • 💬 自然语言:用纯英文提问;智能体理解上下文和意图

  • 🔐 安全:API 密钥存储在 .env 中,并被排除在版本控制之外

  • 🎯 交互式:实时聊天循环,以色列查询时可见浏览器自动化过程


Related MCP server: Weather MCP Agent

📋 目录


🚀 快速开始

3 分钟内完成启动:

# 1. Install dependencies
uv sync
uv run playwright install chromium

# 2. Create .env file with your OpenAI API key
copy .env.example .env
# Edit .env and add: OPENAI_API_KEY=sk-...

# 3. Run the agent
uv run .\host.py

# 4. Ask a weather question!
# Example: "What is the weather forecast for Jerusalem?"

📦 环境要求

组件

版本

用途

Python

3.13+

运行时环境

OpenAI API 密钥

GPT-4o-mini 模型访问权限

uv

最新版

快速 Python 包管理器

Chromium

最新版

浏览器自动化(Playwright)

网络

API 访问和网页抓取


🔧 安装配置

1. 安装依赖

在项目目录中打开终端并运行:

# Install Python dependencies
uv sync

# Download Chromium browser for Playwright (required for Israel weather)
uv run playwright install chromium

首次安装需要较长时间? 是的——Playwright 会下载 Chromium(约 300 MB)。这是一次性操作。

2. 配置 OpenAI API 密钥

在项目根目录创建 .env 文件:

# Option A: Copy the template
copy .env.example .env

# Option B: Create manually
echo OPENAI_API_KEY=your_openai_api_key > .env

your_openai_api_key 替换为你在 platform.openai.com/account/api-keys 获取的实际 OpenAI API 密钥。

⚠️ 重要提示:切勿提交 .env 文件或分享你的 API 密钥。.gitignore 已将其排除在外。

3. 验证安装

测试配置:

uv run .\host.py

你应该看到:

Connected to server with tools: [...]

MCP Client Started!
Type your queries or 'quit' to exit.

Query:

Ctrl+C 或输入 quit 退出。


▶️ 运行智能体

启动交互式聊天:

uv run .\host.py

智能体以交互模式启动。输入天气问题并按回车。智能体将:

  1. ✅ 解析你的问题

  2. 🔍 路由到相应的天气工具(以色列或美国)

  3. 🌐 获取实时数据(以色列查询可能会打开浏览器窗口)

  4. 📊 处理并格式化响应

  5. 💬 返回答案

输入 quit 退出。

对于以色列查询:Chromium 浏览器窗口会自动打开。这是正常现象——智能体正在使用 Playwright 与 Weather2day 交互。查询完成后窗口会自动关闭。


🏗️ 架构

┌─────────────────────────────────────────────────────────────────┐
│                        OpenAI (GPT-4o-mini)                     │
│                      (Tool Selection & Reasoning)               │
└────────────────────────────┬────────────────────────────────────┘
                             │
                ┌────────────┴────────────┐
                │                         │
        ┌───────▼────────┐       ┌───────▼────────┐
        │  MCP Host      │       │   MCP Host     │
        │  (client.py)   │       │   (client.py)  │
        └───────┬────────┘       └───────┬────────┘
                │                         │
        ┌───────▼──────────────┐  ┌──────▼─────────────┐
        │ weather_Israel.py    │  │ weather_USA.py    │
        │ (Playwright + Web)   │  │ (NWS API)         │
        └───────┬──────────────┘  └──────┬─────────────┘
                │                         │
        ┌───────▼──────────────┐  ┌──────▼─────────────┐
        │   Weather2day.co.il  │  │    api.weather.gov│
        │  (Live Web Browser)  │  │   (REST API)      │
        └──────────────────────┘  └───────────────────┘

数据流

  1. 你在聊天中提出问题

  2. 主机将问题连同可用工具发送给 OpenAI

  3. OpenAI 根据问题选择要调用的工具

  4. 相应的 MCP 服务器执行并返回数据

  5. 主机格式化并返回答案


💬 示例查询

🇮🇱 以色列天气

What is the weather forecast for Jerusalem?
Give me the forecast for Tel Aviv.
What is the weather like in Haifa today?
Show me the forecast for Eilat.
What is the weather in Beer Sheva?
Is it going to rain in Kfar Saba tomorrow?
Tell me about the weather in Nazareth.
What's the forecast for Tiberias?

支持的城市:耶路撒冷、特拉维夫、海法、埃拉特、贝尔谢巴、卡法萨巴、提比里亚、拿撒勒,以及 Weather2day 识别的大多数以色列主要城市。

🇺🇸 美国天气

What is the weather forecast for New York City? (40.7128, -74.0060)
What's the weather at latitude 34.0522, longitude -118.2437? (Los Angeles)
Are there any active weather alerts in California?
What weather alerts are in effect in New York?
Give me the forecast for Chicago coordinates. (41.8781, -87.6298)
Are there any severe weather warnings in Texas?

格式

  • 天气预报:使用经纬度坐标

  • 天气警报:使用两字母州代码(CANYTX 等)

🎯 智能体行为

智能体理解上下文:

  • ✅ "耶路撒冷的天气怎么样?" → 使用以色列工具

  • ✅ "我要去纽约,天气预报如何?" → 使用美国工具(40.71, -74.01)

  • ✅ "告诉我特拉维夫和洛杉矶的天气" → 同时调用两个工具

  • ✅ "芝加哥现在在下雨吗?" → 路由到美国工具


📂 项目结构

MCP Weather/
├── host.py                  # Main chat host (OpenAI + tool orchestration)
├── client.py                # MCP client connector
├── weather_Israel.py        # Israel MCP server (Playwright + Web)
├── weather_USA.py           # USA MCP server (NWS API)
├── pyproject.toml           # Dependencies and project metadata
├── uv.lock                  # Locked dependency versions
├── .env.example             # Template for environment variables
├── .env                     # Your API keys (git-ignored)
├── README.md                # This file
└── .gitignore               # Git exclusion rules

🐛 故障排除

OPENAI_API_KEY is missing

问题:智能体找不到你的 API 密钥。

解决方案

  1. 确认 .env 存在于项目根目录(与 host.py 同一文件夹)

  2. 检查文件内容是否包含:OPENAI_API_KEY=sk-your-actual-key-here

  3. 重启主机:uv run .\host.py

❌ Chromium 浏览器无法启动

问题:"Failed to launch browser" 错误。

解决方案

uv run playwright install chromium

如果仍然失败,请检查磁盘空间(Chromium 需要约 500 MB)。

❌ 连接超时或 API 错误

问题:"net::ERR_CONNECTION_RESET" 或请求失败。

解决方案

  • 检查你的网络连接

  • platform.openai.com 验证你的 OpenAI API 密钥是否有效

  • 检查 API 使用量和计费情况

❌ 以色列浏览器打开但搜索失败

问题:城市搜索没有返回建议。

原因说明:Weather2day 的自动补全仅在 /forecast 页面上有效。工具会在每次搜索前自动返回该页面,但如果失败:

解决方案

  • 确认 Chromium 是最新版本:uv run playwright install --with-deps chromium

  • 确保你能访问 weather2day.co.il

  • 检查浏览器弹窗或 Cookie 横幅(智能体会处理这些,但极慢的连接可能会超时)

❌ "A server fails to connect"

问题:MCP 服务器连接错误。

解决方案

# Run from the project directory
Set-Location .\project-template
uv run .\host.py

❓ 常见问题

问:智能体如何决定使用哪个工具?
答:OpenAI 的 GPT-4o-mini 会读取你的问题,并根据上下文(提到的国家/地区、提供的坐标等)选择最佳工具。这不是基于规则的——它使用语言理解。

问:我可以在一次查询中询问多个地区吗?
答:可以!智能体可以调用多个工具。试试:"特拉维夫和纽约的天气怎么样?"

问:为什么以色列浏览器会可见地打开?
答:Playwright 通过向实时搜索框输入内容、选择结果并读取页面来自动化 Weather2day。无头模式(不可见)不适用于该网站大量使用 JavaScript 的自动补全功能,因此浏览器以可见模式运行。

问:这需要多少费用?
答:你按 API 调用次数向 OpenAI 付费。每次查询约花费 $0.001–$0.01,具体取决于响应长度。以色列网页抓取是免费的;美国 API 也是免费的(NWS 是公共数据)。

问:如果我想添加更多地区怎么办?
答:创建一个新的 MCP 服务器(例如 weather_Europe.py),并在 host.pyself.mcp_clients 下注册。参考现有服务器的模式。

问:我的 API 密钥安全吗?
答:安全。你的 .env 已被 Git 排除,仅存储在本地。切勿在代码或公共聊天中粘贴你的密钥。

问:我可以将其用于生产环境吗?
答:这是一个演示项目。对于生产环境:

  • 使用异步请求队列

  • 添加日志记录和错误恢复

  • 缓存天气预报(天气每小时变化,而非每次查询都变化)

  • 设置监控和警报

  • 使用基于环境的配置


🔒 安全

⚠️ 应该做

不应该做

✅ 保持 .env 本地且私密

❌ 将 .env 提交到 Git

✅ 定期轮换 API 密钥

❌ 分享你的 API 密钥

✅ 在生产环境中使用环境变量

❌ 在代码中硬编码机密信息

✅ 检查 API 计费限额

❌ 让配额无限制

✅ API 调用使用 HTTPS

❌ 在生产环境中使用 HTTP

如果你的 API 密钥已泄露

  1. 立即在 OpenAI 控制台 中撤销它

  2. 生成一个新密钥

  3. 更新 .env


📝 许可证

本项目按原样提供,仅供教育用途。详情请参阅 LICENSE。


🤝 贡献

有改进智能体的想法?欢迎贡献!

  1. Fork 本仓库

  2. 创建功能分支(git checkout -b feature/amazing-feature

  3. 提交你的更改(git commit -m 'Add amazing feature'

  4. 推送到分支(git push origin feature/amazing-feature

  5. 打开 Pull Request


📞 支持

  • 🐛 通过 GitHub Issues 报告错误

  • 💡 在 Discussions 中建议功能

  • 📧 对于安全问题,请私下发送邮件(不要公开提交 issue)


祝预报愉快!🌦️☀️

F
license - not found
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

View all related MCP servers

Related MCP Connectors

  • US weather, alerts, earthquakes and elevation for AI agents, from NWS/NOAA and USGS. No API keys.

  • US weather & geo for AI agents: forecasts, alerts, earthquakes, elevation, geocoding. No keys.

  • Get US weather forecasts, active alerts, and current observations.

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/SaraDev841/MCP-Playwright-Weather'

If you have feedback or need assistance with the MCP directory API, please join our Discord server