Implements environment variable management for storing and accessing API keys and configuration settings like OpenAI and OpenWeatherMap credentials.
Uses OpenAI's GPT-3.5-turbo model to power the AI chatbot interface, enabling natural language processing and conversational interactions for weather queries.
Serves as the implementation language for the MCP server, providing the runtime environment for the weather information service.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Weather MCP Serverwhat's the weather forecast for Tokyo this weekend?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Weather AI Chatbot with MCP Integration
A Python-based AI chatbot that uses OpenAI's GPT model and integrates with a Model Context Protocol (MCP) server to provide real-time weather information from OpenWeatherMap API.
Features
π€ AI-Powered Chatbot: Uses OpenAI's GPT-3.5-turbo for natural language processing
π€οΈ Real-time Weather Data: Access current weather and forecasts for any city worldwide
π MCP Integration: Implements Model Context Protocol for modular service architecture
π¬ Conversational Interface: Natural language interaction with weather queries
π Detailed Weather Information: Temperature, humidity, wind, pressure, and more
ποΈ Weather Forecasts: Multi-day weather predictions
π Conversation Memory: Maintains context across conversation turns
Related MCP server: Weather MCP
Architecture
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Chatbot β β MCP Client β β MCP Server β
β (OpenAI) βββββΊβ (HTTP) βββββΊβ (Weather API) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββPrerequisites
Python 3.8 or higher
OpenAI API key
OpenWeatherMap API key
Installation
Clone or download the project files
Install dependencies:
pip install -r requirements.txtSet up environment variables:
cp env.example .envEdit
.envand add your API keys:OPENAI_API_KEY=your_openai_api_key_here OPENWEATHER_API_KEY=your_openweather_api_key_here MCP_SERVER_HOST=localhost MCP_SERVER_PORT=8000
Getting API Keys
OpenAI API Key
Go to OpenAI Platform
Sign up or log in
Navigate to API Keys section
Create a new API key
Copy the key to your
.envfile
OpenWeatherMap API Key
Go to OpenWeatherMap
Sign up for a free account
Navigate to API Keys section
Generate a new API key
Copy the key to your
.envfile
Usage
Starting the MCP Weather Server
First, start the MCP server that provides weather data:
python weather_mcp_server.pyThe server will start on http://localhost:8000 by default.
Running the Chatbot
In a new terminal, start the chatbot:
python chatbot.pyExample Conversations
π€ You: What's the weather in London?
π€ Assistant: Current weather in London, GB:
β’ Temperature: 18Β°C (feels like 16Β°C)
β’ Conditions: scattered clouds
β’ Humidity: 72%
β’ Wind Speed: 3.2 m/s
The weather in London is quite pleasant today! It's 18Β°C with scattered clouds, which makes for a nice day to be outdoors. The humidity is moderate at 72%, and there's a gentle breeze at 3.2 m/s. You might want to bring a light jacket since it feels like 16Β°C.
π€ You: What's the forecast for New York this week?
π€ Assistant: Weather forecast for New York, US:
π
2024-01-15:
Temperature: 12.3Β°C (min: 8.2Β°C, max: 16.4Β°C)
Conditions: clear sky
π
2024-01-16:
Temperature: 14.1Β°C (min: 10.1Β°C, max: 18.2Β°C)
Conditions: few clouds
π
2024-01-17:
Temperature: 11.8Β°C (min: 7.9Β°C, max: 15.7Β°C)
Conditions: light rain
The weather in New York this week looks quite variable! You'll have clear skies on Monday, some clouds on Tuesday, and light rain on Wednesday. Temperatures will range from around 8Β°C to 18Β°C, so you'll want to dress in layers. Don't forget an umbrella for Wednesday!
π€ You: Tell me a joke
π€ Assistant: Here's a weather-related joke for you:
Why don't meteorologists like to go outside?
Because they're afraid of getting caught in their own forecasts! π
Actually, meteorologists are quite brave - they have to deal with people complaining about the weather whether they predict it correctly or not!
π€ You: quit
π Goodbye! Have a great day!Available Commands
quit,exit,bye- End the conversationreset- Clear conversation history
Weather Query Examples
The chatbot can understand various ways to ask about weather:
"What's the weather in Paris?"
"How's the weather in Tokyo?"
"Weather forecast for London"
"Temperature in New York"
"What's the weather like in Sydney?"
"Forecast for Berlin this week"
"Weather in Toronto tomorrow"
MCP Server Endpoints
The MCP server provides the following endpoints:
POST /mcp- Handle MCP requestsGET /capabilities- Get server capabilities
Available Methods
weather/get_current
Parameters:
city(required),country_code(optional)Returns: Current weather information
weather/get_forecast
Parameters:
city(required),country_code(optional),days(optional, max 5)Returns: Weather forecast information
Testing
You can test the MCP client independently:
python mcp_client.pyThis will test the weather service with sample requests.
Error Handling
The system includes comprehensive error handling for:
API connection issues
Invalid city names
Missing API keys
Network timeouts
Malformed responses
Configuration
You can customize the following settings in your .env file:
MCP_SERVER_HOST- MCP server hostname (default: localhost)MCP_SERVER_PORT- MCP server port (default: 8000)OPENAI_API_KEY- Your OpenAI API keyOPENWEATHER_API_KEY- Your OpenWeatherMap API key
Troubleshooting
Common Issues
"OPENAI_API_KEY environment variable is required"
Make sure you've set up your
.envfile correctlyVerify your OpenAI API key is valid
"OPENWEATHER_API_KEY environment variable is required"
Ensure your OpenWeatherMap API key is in the
.envfileCheck that the API key is active
"Failed to connect to MCP server"
Make sure the MCP server is running (
python weather_mcp_server.py)Check that the host and port settings are correct
"City not found" errors
Verify the city name spelling
Try adding a country code (e.g., "London, GB")
Debug Mode
To enable debug logging, modify the logging level in the Python files:
logging.basicConfig(level=logging.DEBUG)Contributing
Feel free to contribute to this project by:
Reporting bugs
Suggesting new features
Improving documentation
Adding new weather data sources
License
This project is open source and available under the MIT License.
Acknowledgments
OpenAI for providing the GPT API
OpenWeatherMap for weather data
The MCP community for the protocol specification