Skip to main content
Glama
README.md
# ๐ŸŒค๏ธ MCP Weather Agent

[![Python 3.13+](https://img.shields.io/badge/Python-3.13%2B-blue?style=flat-square)](https://www.python.org/)
[![OpenAI](https://img.shields.io/badge/OpenAI-GPT--4o--mini-brightgreen?style=flat-square)](https://openai.com/)
[![MCP](https://img.shields.io/badge/MCP-1.27%2B-orange?style=flat-square)](https://modelcontextprotocol.io/)
[![Playwright](https://img.shields.io/badge/Playwright-Browser%20Automation-9c27b0?style=flat-square)](https://playwright.dev/)

An intelligent, multi-region weather assistant powered by **OpenAI** and the **Model Context Protocol (MCP)**. 

Get real-time weather forecasts and alerts for **Israel** and the **United States** through a natural language chat interface. The agent intelligently routes questions to the appropriate data source and compiles answers from live web data and official weather APIs.

---

## โœจ Features

- ๐Ÿค– **AI-Powered Chat**: Conversational weather queries using GPT-4o-mini
- ๐ŸŒ **Dual Data Sources**:
  - ๐Ÿ‡ฎ๐Ÿ‡ฑ **Israel**: Live web scraping from Weather2day via Playwright
  - ๐Ÿ‡บ๐Ÿ‡ธ **USA**: Official National Weather Service API
- ๐Ÿ”„ **Smart Tool Routing**: The agent automatically selects the right tool based on your question
- ๐Ÿ’ฌ **Natural Language**: Ask questions in plain English; the agent understands context and intent
- ๐Ÿ” **Secure**: API keys stored in `.env` and excluded from version control
- ๐ŸŽฏ **Interactive**: Real-time chat loop with visible browser automation for Israel queries

---

## ๐Ÿ“‹ Table of Contents

- [Quick Start](#-quick-start)
- [Requirements](#-requirements)
- [Setup](#-setup)
- [Running the Agent](#-running-the-agent)
- [Architecture](#-architecture)
- [Example Queries](#-example-queries)
- [Troubleshooting](#-troubleshooting)
- [FAQ](#-faq)
- [Security](#-security)

---

## ๐Ÿš€ Quick Start

Get up and running in 3 minutes:

```powershell
# 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?"
```

---

## ๐Ÿ“ฆ Requirements

| Component | Version | Purpose |
|-----------|---------|---------|
| **Python** | 3.13+ | Runtime environment |
| **OpenAI API Key** | โ€” | GPT-4o-mini model access |
| **uv** | Latest | Fast Python package manager |
| **Chromium** | Latest | Browser automation (Playwright) |
| **Internet** | โ€” | API access and web scraping |

---

## ๐Ÿ”ง Setup

### 1. Install Dependencies

Open a terminal in the project directory and run:

```powershell
# Install Python dependencies
uv sync

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

**First time taking a while?** Yesโ€”Playwright downloads Chromium (~300 MB). This is a one-time operation.

### 2. Configure OpenAI API Key

Create a `.env` file in the project root:

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

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

Replace `your_openai_api_key` with your actual OpenAI API key from [platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys).

**โš ๏ธ Important**: Never commit `.env` or share your API key. The `.gitignore` already excludes it.

### 3. Verify Installation

Test the setup:

```powershell
uv run .\host.py
```

You should see:
```
Connected to server with tools: [...]

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

Query:
```

Press `Ctrl+C` or type `quit` to exit.

---

## โ–ถ๏ธ Running the Agent

Start the interactive chat:

```powershell
uv run .\host.py
```

The agent launches in interactive mode. Type a weather question and press Enter. The agent will:

1. โœ… Parse your question
2. ๐Ÿ” Route to the appropriate weather tool (Israel or USA)
3. ๐ŸŒ Fetch real-time data (may open a browser window for Israel)
4. ๐Ÿ“Š Process and format the response
5. ๐Ÿ’ฌ Return the answer

Type `quit` to exit.

**For Israel queries**: A Chromium browser window opens automatically. This is normalโ€”the agent is using Playwright to interact with Weather2day. The window closes when the query completes.

---

## ๐Ÿ—๏ธ Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        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)      โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

**Data Flow**:
1. You ask a question in the chat
2. The host sends it to OpenAI with available tools
3. OpenAI selects which tool to call based on the question
4. The appropriate MCP server executes and returns data
5. The host formats and returns the answer

---

## ๐Ÿ’ฌ Example Queries

### ๐Ÿ‡ฎ๐Ÿ‡ฑ Israel Weather

```
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?
```

**Supported Cities**: Jerusalem, Tel Aviv, Haifa, Eilat, Beer Sheva, Kfar Saba, Tiberias, Nazareth, and most major Israeli cities recognized by Weather2day.

### ๐Ÿ‡บ๐Ÿ‡ธ USA Weather

```
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?
```

**Format**:
- **Forecasts**: Use latitude/longitude coordinates
- **Alerts**: Use two-letter state codes (`CA`, `NY`, `TX`, etc.)

### ๐ŸŽฏ Smart Agent Behavior

The agent understands context:
- โœ… "What's the weather in Jerusalem?" โ†’ Uses Israel tool
- โœ… "I'm visiting New York, what's the forecast?" โ†’ Uses USA tool (40.71, -74.01)
- โœ… "Tell me about weather in Tel Aviv and Los Angeles" โ†’ Calls both tools
- โœ… "Is it raining in Chicago right now?" โ†’ Routes to USA tool

---

## ๐Ÿ“‚ Project Structure

```
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
```

---

## ๐Ÿ› Troubleshooting

### โŒ `OPENAI_API_KEY is missing`

**Problem**: The agent can't find your API key.

**Solution**:
1. Verify `.env` exists in the project root (same folder as `host.py`)
2. Check the file contains: `OPENAI_API_KEY=sk-your-actual-key-here`
3. Restart the host: `uv run .\host.py`

### โŒ Chromium browser won't launch

**Problem**: "Failed to launch browser" error.

**Solution**:
```powershell
uv run playwright install chromium
```

If still failing, check disk space (Chromium needs ~500 MB).

### โŒ Connection timeout or API errors

**Problem**: "net::ERR_CONNECTION_RESET" or request failures.

**Solution**:
- Check your internet connection
- Verify your OpenAI API key is valid at [platform.openai.com](https://platform.openai.com)
- Check API usage and billing

### โŒ Israel browser opens but search fails

**Problem**: City search doesn't return suggestions.

**Explanation**: The Weather2day autocomplete only works on the `/forecast` page. The tool automatically returns to that page before each search, but if it fails:

**Solution**:
- Verify Chromium is up to date: `uv run playwright install --with-deps chromium`
- Ensure you have internet connectivity to `weather2day.co.il`
- Check for browser pop-ups or cookie banners (the agent handles these, but very slow connections may timeout)

### โŒ "A server fails to connect"

**Problem**: MCP server connection error.

**Solution**:
```powershell
# Run from the project directory
Set-Location .\project-template
uv run .\host.py
```

---

## โ“ FAQ

**Q: How does the agent decide which tool to use?**  
A: OpenAI's GPT-4o-mini reads your question and selects the best tool based on the context (country/region mentioned, coordinates provided, etc.). It's not rule-basedโ€”it uses language understanding.

**Q: Can I ask about multiple regions in one query?**  
A: Yes! The agent can call multiple tools. Try: "What's the weather in Tel Aviv and New York?"

**Q: Why does the Israel browser open visibly?**  
A: Playwright automates Weather2day by typing into the live search, selecting results, and reading the page. Headless mode (invisible) doesn't work with this site's JavaScript-heavy autocomplete, so the browser runs visibly.

**Q: How much does this cost?**  
A: You're billed by OpenAI based on API calls. Each query costs ~$0.001โ€“$0.01 depending on response length. Israel web scraping is free; USA API is free (NWS is public).

**Q: What if I want to add more regions?**  
A: Create a new MCP server (e.g., `weather_Europe.py`) and register it in `host.py` under `self.mcp_clients`. See the existing servers for the pattern.

**Q: Is my API key safe?**  
A: Yes. Your `.env` is excluded from Git and stored locally only. Never paste your key in code or public chat.

**Q: Can I use this in production?**  
A: This is a demo project. For production:
- Use async request queuing
- Add logging and error recovery
- Cache forecasts (weather changes hourly, not per-query)
- Set up monitoring and alerts
- Use environment-based configuration

---

## ๐Ÿ”’ Security

| โš ๏ธ **Do** | โŒ **Don't** |
|-----------|-----------|
| โœ… Keep `.env` local and private | โŒ Commit `.env` to Git |
| โœ… Rotate API keys regularly | โŒ Share your API key |
| โœ… Use environment variables in production | โŒ Hardcode secrets in code |
| โœ… Check API billing limits | โŒ Leave quota unlimited |
| โœ… Use HTTPS for API calls | โŒ Use HTTP in production |

**If your API key is exposed**:
1. Revoke it immediately in [OpenAI Dashboard](https://platform.openai.com/account/billing/overview)
2. Generate a new key
3. Update `.env`

---

## ๐Ÿ“ License

This project is provided as-is for educational purposes. See LICENSE for details.

---

## ๐Ÿค Contributing

Have ideas to improve the agent? Contributions are welcome!

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

---

## ๐Ÿ“ž Support

- ๐Ÿ› Report bugs via GitHub Issues
- ๐Ÿ’ก Suggest features in Discussions
- ๐Ÿ“ง For security issues, email privately (do not open a public issue)

---

**Happy forecasting! ๐ŸŒฆ๏ธโ˜€๏ธ**

TDQS

B3.4/5.0

Scored across 4 tools

Disambiguation4/5

Each tool maps to a distinct browser-automation step (open site, enter city, select result, extract content), so they are separable in principle. However, they form a rigid ordered pipeline with no way to tell from the names alone that they must be chained, and enter vs. select could be momentarily confused by a hurried agent.

Naming Consistency4/5

All names are snake_case and share a predictable verb_..._israel suffix (open/enter/select/get_weather_forecast_..._israel). Minor structural deviation: open_weather_forecast_israel omits the 'city' segment the other three include, but the pattern is still readable and consistent.

Tool Count4/5

Four tools is a reasonable count for this narrow scraping workflow. It is slightly granular, since a single 'fetch forecast for city' operation is split into four browser steps, but each step is individually actionable and the set stays well-scoped.

Completeness3/5

The surface covers the happy path open -> search -> select -> extract, but has no tool to close/reset the browser session, no error or no-result handling, and despite the generic server name 'MCP Weather Agent' it only supports Israeli locations. Agents can complete the core task but will hit dead ends on edge cases.

Maintenance

ActivityMaintained
ResponsivenessNo issues