MCP Weather Server
Project Overview
This project implements a Model Context Protocol (MCP) server designed to provide current weather information for a given location. It acts as a standardized interface, enabling AI agents or other client applications to fetch weather data without needing to interact directly with various external weather APIs. The core idea is to abstract the complexities of external services, offering a consistent protocol for data retrieval and error handling.
The server is built using Python with the FastAPI framework and currently retrieves weather data from the OpenWeatherMap API. While this project focuses on a weather tool, the MCP architecture is designed to be extensible, serving as a standardized gateway for an AI agent to access various tools and APIs (both internal and external) through a consistent protocol.
Key Features:
- Standardized MCP Interface: Adheres to a defined request/response structure (using Pydantic models) for easy integration.
- Weather Data Retrieval: Fetches current temperature, conditions, humidity, wind speed, and pressure.
- Abstraction of External API: Shields the client from the specifics of the OpenWeatherMap API.
- Error Handling: Provides clear, standardized MCP error messages for issues like invalid locations or API key problems.
- Asynchronous Operations: Uses
httpx
for non-blocking calls to the external weather API. - Data Validation: Leverages Pydantic for request and response data validation.
- Configuration Management: Loads sensitive configurations (like API keys) from a
.env
file. - Comprehensive Testing: Includes a client simulation script and a
pytest
suite for unit and integration tests. - Simple Agent CLI: A command-line interface (
weather_agent_cli.py
) demonstrates how an agent can interact with this MCP server.
Architecture
The server facilitates communication as follows:
- Client (AI Agent/Test Script) sends an MCP-formatted JSON request to the
/mcp/weather
endpoint. - MCP Weather Server (this application) parses the request and extracts necessary parameters (e.g., location).
- The MCP server's "weather tool" handler calls the OpenWeatherMap API with the location and its configured API key.
- OpenWeatherMap returns the raw weather data.
- The MCP server transforms this raw data into the standardized MCP response format.
- The MCP server sends the MCP-formatted JSON response back to the client.
Prerequisites
- Python 3.8+ (Python 3.13 used during development)
- An API key from OpenWeatherMap (for the "Current Weather Data" API).
Setup and Installation
- Clone the repository (if applicable):
- Create and activate a virtual environment:
- Install dependencies:
Ensure you have a
requirements.txt
file (see "Project Structure" below for typical contents). Then run: - Configure API Key:
- Create a
.env
file in the root of the project directory (e.g.,mcp-weather-server/.env
). - Add your OpenWeatherMap API key to it:
- Important: Ensure
.env
is listed in your.gitignore
file to prevent committing your API key to version control.
- Create a
Running the MCP Server
- Ensure your virtual environment is activated.
- Navigate to the project root directory (
mcp-weather-server
). - Start the server using Uvicorn:
app.main:app
: Points to theapp
FastAPI instance inapp/main.py
.-reload
: Enables auto-reloading during development. The server will restart if code changes are detected.-port 8000
: Specifies the port on which the server will listen.
The server will be accessible at http://localhost:8000
.
API Documentation (Auto-Generated)
FastAPI automatically generates interactive API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
These interfaces allow you to view detailed endpoint information, schemas, and try out the API directly from your browser.
API Endpoint
Get Current Weather
- URL:
/mcp/weather
- Method:
POST
- Request Body (JSON - MCP Format):
- Success Response (HTTP 200 OK - MCP Format):
- Application Error Response (HTTP 200 OK with error status - MCP Format):
(The MCP server returns HTTP 200 even for application-level errors, with the error details in the payload)
Testing
This project includes multiple ways to test the application:
1. Manual Testing with curl
or API Clients
You can send POST requests to the /mcp/weather
endpoint using curl
or tools like Postman or Insomnia.
Example curl
command:
2. End-to-End Client Simulation (test_mcp_client.py
)
A Python script (test_mcp_client.py
) located in the project root simulates a client making various requests to the running MCP server. This is useful for quick end-to-end checks.
- To Run:
- Ensure your MCP server is running (see "Running the MCP Server").
- Activate your virtual environment.
- In a separate terminal, from the project root, run:
3. Automated Tests with pytest
The project uses pytest
for unit and integration tests of the server's components. These tests are located in the tests/
directory and cover API endpoints (with mocked services) and service layer logic (with mocked external HTTP calls).
- Setup:
pytest
andpytest-asyncio
should be listed in yourrequirements.txt
and installed during the initial setup. - To Run:
- Activate your virtual environment.
- From the project root, run:Or for more verbose output:
Simple AI Agent CLI (weather_agent_cli.py
)
A command-line interface (CLI) agent (weather_agent_cli.py
) is provided in the project root to demonstrate how an agent can interact with the MCP Weather Server. It takes simple text commands to fetch and display weather information.
- To Run:
- Ensure your MCP server is running (see "Running the MCP Server").
- Activate your virtual environment.
- In a separate terminal, from the project root, run:
- Follow the prompts. Example commands:
weather in London
,weather Tokyo
,quit
.
Project Structure
Future Enhancements (Potential Ideas)
- Support for more weather data points (e.g., multi-day forecasts, UV index).
- Add more tools to the MCP server (e.g., news, calculator, calendar).
- Implement more sophisticated error handling and detailed logging.
- Dockerize the application for easier deployment and portability.
- Develop a more advanced AI agent with better NLP capabilities.
- Add authentication/authorization to the MCP server if it were to be exposed.
License
(Specify a license for your project here, e.g., MIT License. If unsure, you can add "To be determined" or research common open-source licenses.)
This server cannot be installed
A standardized API server that enables AI agents and client applications to fetch current weather information for any location without directly interacting with external weather APIs.
Related MCP Servers
- -securityAlicense-qualityA lightweight, modular API service that provides useful tools like weather, date/time, calculator, search, email, and task management through a RESTful interface, designed for integration with AI agents and automated workflows.Last updated -PythonMIT License
- -securityAlicense-qualityA Model Context Protocol server that retrieves current weather information for specified cities using the Open-Meteo API, requiring no API key.Last updated -4PythonApache 2.0
- AsecurityFlicenseAqualityA Model Context Protocol server that provides AI agents with tools to retrieve weather alerts and detailed forecasts for US locations using the National Weather Service API.Last updated -275TypeScript
- -security-license-qualityA Model Context Protocol server that provides current weather information and 3-day forecasts for specified cities using the Open-Meteo API.Last updated -Python