Skip to main content
Glama
yongsingyou

Weather MCP Server

by yongsingyou
README.md
# Weather MCP Server and Client

A simple example of using fastmcp to create a weather MCP (Model Control Protocol) server and client.

## Features

- **Weather Server**: Provides current weather and forecast data for major cities
- **Weather Client**: Demonstrates how to interact with the MCP server
- **Tools Available**:
  - `get_weather(city)` - Get current weather for a city
  - `get_forecast(city, days)` - Get weather forecast for 1-7 days
  - `list_cities()` - Get list of available cities
- **Resources**: Access to weather data via URI-based resources

## Installation

1. Install dependencies:
```bash
pip install fastmcp asyncio-extras python-dateutil
```

2. Or install from requirements:
```bash
pip install -r requirements.txt
```

## Usage

### Running the Server

```bash
python weather_server.py
```

The server will start and provide weather tools for the following cities:
- New York
- London  
- Tokyo
- Sydney

### Running the Client

```bash
python weather_client.py
```

Choose between:
1. Demo mode - Shows example interactions
2. Interactive mode - Allows manual testing of commands

### Available Cities

The server provides mock weather data for:
- New York
- London
- Tokyo
- Sydney

### Example Usage

```python
# Get current weather
weather = await get_weather("New York")

# Get 5-day forecast  
forecast = await get_forecast("London", 5)

# List all available cities
cities = await list_cities()
```

## API Reference

### Tools

#### get_weather(city: str)
Get current weather information for a city.

**Returns:**
- temperature (°C)
- humidity (%)
- condition (sunny, cloudy, rainy, etc.)
- wind_speed (km/h)
- pressure (hPa)
- timestamp

#### get_forecast(city: str, days: int = 3)
Get weather forecast for a city.

**Parameters:**
- city: City name
- days: Number of days (1-7)

**Returns:**
- List of daily forecasts with temperature highs/lows, conditions, etc.

#### list_cities()
Get list of available cities.

**Returns:**
- List of city names

### Resources

- `weather://current/{city}` - Current weather data for a city

## Extending the Example

To connect this to a real weather API:

1. Replace the `MOCK_WEATHER_DATA` with API calls
2. Add API key configuration
3. Handle API rate limits and errors
4. Add more cities and weather parameters

## Notes

- This example uses mock data for demonstration
- In production, you'd connect to a real weather API like OpenWeatherMap
- The client examples show the structure but don't make actual MCP calls
- Refer to fastmcp documentation for complete MCP protocol implementation