Abishek-Raj-M/weather-mcp-server
If you are the rightful owner of weather-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The Weather MCP Tool is a Model Context Protocol server that provides real-time weather information using WeatherAPI.com, enabling GitHub Copilot to access weather data through a local MCP server built with Python, Flask, and Fast MCP.
Weather MCP Tool
A Model Context Protocol (MCP) server that provides real-time weather information using WeatherAPI.com. This tool allows GitHub Copilot to access weather data through a local MCP server built with Python, Flask, and Fast MCP.
Features
- Real-time Weather Data: Current weather conditions for any location
- Weather Forecasts: 1-10 day weather forecasts with alerts
- Location Search: Find locations by name or coordinates
- Weather Alerts: Active weather warnings and alerts
- Caching: Intelligent caching to respect API rate limits
- MCP Integration: Full Model Context Protocol support for GitHub Copilot
Quick Start
TL;DR: After setup, run
python run.py --flaskfor HTTP server orpython run.pyfor MCP mode.
1. Setup Virtual Environment
# Navigate to project directory
cd weather-mcp-tool
# Create virtual environment (REQUIRED)
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate
# Verify activation (should show (venv) in prompt)
2. Install Dependencies
# Upgrade pip
python -m pip install --upgrade pip
# Install core dependencies
pip install -r requirements.txt
# Install development dependencies (optional)
pip install -r requirements-dev.txt
3. Configure Environment
# Copy environment template
copy .env.example .env # Windows
# cp .env.example .env # macOS/Linux
# Edit .env with your configuration
# WEATHER_API_KEY=1401cb7b45b742bab23105426252509
4. Run the Server
# Start MCP server (default mode)
python run.py
# Or start Flask HTTP server
python run.py --flask
Available Tools
get_current_weather
Get current weather conditions for any location.
# Example usage in Copilot
# "What's the current weather in New York?"
Parameters:
location(str): City name, coordinates, or location identifierinclude_air_quality(bool): Include air quality data (default: False)
get_weather_forecast
Get weather forecast for 1-10 days.
# Example usage in Copilot
# "Can you get me a 7-day weather forecast for London?"
Parameters:
location(str): City name, coordinates, or location identifierdays(int): Number of forecast days (1-10, default: 3)include_alerts(bool): Include weather alerts (default: False)
search_locations
Search for locations by name or coordinates.
# Example usage in Copilot
# "Find weather stations near coordinates 40.7128,-74.0060"
Parameters:
query(str): Location search query
get_weather_alerts
Get active weather alerts and warnings.
# Example usage in Copilot
# "Are there any weather alerts for Miami today?"
Parameters:
location(str): City name, coordinates, or location identifier
Configuration
Environment Variables (.env file)
# Weather API Configuration
WEATHER_API_KEY=your_api_key_here
WEATHER_API_BASE_URL=https://api.weatherapi.com/v1
# MCP Server Configuration
MCP_SERVER_PORT=5000
MCP_SERVER_HOST=localhost
# Logging Configuration
LOG_LEVEL=INFO
# Caching Configuration
CACHE_TTL=900 # 15 minutes
GitHub Copilot Integration
Create ~/.config/github-copilot/mcp.json:
{
"servers": {
"weather": {
"command": "path/to/weather-mcp-tool/venv/Scripts/python",
"args": ["path/to/weather-mcp-tool/src/main.py"],
"cwd": "path/to/weather-mcp-tool",
"env": {
"WEATHER_API_KEY": "1401cb7b45b742bab23105426252509"
}
}
}
}
Development
Running Tests
# Ensure virtual environment is activated
source venv/bin/activate # or venv\Scripts\activate
# Run all tests
pytest
# Run with coverage
pytest --cov=src
# Run specific test file
pytest tests/test_weather_service.py -v
Code Quality
# Format code
black src/
# Lint code
flake8 src/
# Type checking
mypy src/
# Sort imports
isort src/
Development Server
# Run Flask development server
python run.py --flask
# Run MCP server (for GitHub Copilot integration)
python run.py
API Endpoints (Flask Mode)
When running in Flask mode (--flask or RUN_MODE=flask):
GET /- Service informationGET /health- Health checkGET /tools- List available MCP toolsGET /stats- Service statistics and cache info
Project Structure
weather-mcp-tool/
├── venv/ # Virtual environment
├── src/
│ ├── __init__.py # Package initialization
│ ├── main.py # Main MCP server entry point
│ ├── weather_service.py # Weather API service
│ ├── tools.py # MCP tool definitions
│ └── models.py # Pydantic models
├── tests/
│ ├── test_weather_service.py
│ ├── test_tools.py
│ └── test_integration.py
├── .env # Environment variables
├── .env.example # Environment template
├── requirements.txt # Core dependencies
├── requirements-dev.txt # Development dependencies
├── pyproject.toml # Python project config
└── README.md # This file
Dependencies
Core Dependencies (requirements.txt)
mcp>=1.0.0- Model Context Protocol frameworkflask>=2.3.0- Web framework for HTTP endpointsrequests>=2.31.0- HTTP client for API callspython-dotenv>=1.0.0- Environment variable managementpydantic>=2.5.0- Data validation and serializationuvicorn>=0.24.0- ASGI servergunicorn>=21.2.0- WSGI server
Development Dependencies (requirements-dev.txt)
pytest>=7.4.0- Testing frameworkpytest-cov>=4.1.0- Coverage reportingblack>=23.9.0- Code formattingflake8>=6.1.0- Code lintingmypy>=1.6.0- Type checking
Error Handling
The system includes comprehensive error handling:
- API Errors: Weather API connectivity issues
- Validation Errors: Invalid parameters or data
- Network Errors: Timeout and connection issues
- Configuration Errors: Missing API keys or invalid settings
All errors are properly logged and return structured error responses.
Caching
The weather service implements intelligent caching:
- Cache TTL: 15 minutes for weather data (configurable)
- Alert Cache: 5 minutes for weather alerts
- Location Cache: Uses standard TTL (locations don't change often)
- Memory Cache: Simple in-memory cache (can be extended to Redis)
Rate Limiting
Respects WeatherAPI.com rate limits:
- Free tier: 1 million calls per month
- Caching reduces actual API calls
- Built-in request deduplication
Security
- API Key Protection: Stored in .env file, never in code
- Input Validation: All inputs validated and sanitized
- Error Handling: No sensitive data in error responses
- Environment Isolation: Uses virtual environment
Troubleshooting
Common Issues
-
Import Error for Flask/MCP:
- Ensure virtual environment is activated
- Run
pip install -r requirements.txt
-
API Key Error:
- Check
.envfile exists and containsWEATHER_API_KEY - Verify API key is valid
- Check
-
Connection Error:
- Check internet connectivity
- Verify WeatherAPI.com is accessible
-
Port Already in Use:
- Change
MCP_SERVER_PORTin.envfile - Or kill process using the port
- Change
Debug Mode
Enable debug logging:
LOG_LEVEL=DEBUG python src/main.py
Health Check
Test server health:
# MCP mode - check logs
python src/main.py
# Flask mode - HTTP endpoint
curl http://localhost:5000/health
Contributing
- Fork the repository
- Create a feature branch
- Make changes with tests
- Run quality checks:
black,flake8,mypy,pytest - Submit a pull request
License
MIT License - see LICENSE file for details.
Additional Documentation
- - Complete guide for integrating with GitHub Copilot
- - Production deployment guide with Docker, systemd, and monitoring
- - Configuration file templates
Support
For issues and questions:
- Check the troubleshooting section
- Review logs for error details
- Check the additional documentation guides
- Open an issue with:
- Error messages
- Environment details
- Steps to reproduce