天气MCP
一种基于模型控制协议(MCP)的服务,允许用户通过坐标查询天气预报,并接收美国各州的天气警报。
服务介绍
Weather MCP Service
A weather information service based on the Model Control Protocol (MCP), providing functions for weather forecasts and alert queries.
Features
- Fetch weather alerts for U.S. states (
get_alerts) - Query weather forecasts by latitude and longitude (
get_forecast)
Tech Stack
- Python 3.11+
- MCP (Model Control Protocol)
- FastMCP Server
- LangGraph + LangChain
- SSE (Server-Sent Events) for transmission
Installation
-
Clone the repository:
bash
git clone https://github.com/haichaozheng/weather-mcp.git
cd weather-mcp -
Create a virtual environment:
bashUsing Python's standard library
python -m venv weather_venv
Activate the virtual environment (Windows)
weather_venv\Scripts\activate
Activate the virtual environment (Linux/Mac)
source weather_venv/bin/activate
-
Install dependencies:
bash
pip install -r requirements.txt -
Configure environment variables:
- Create a
.envfile, following the format of.env.example - Add necessary API keys
- Create a
Usage
-
Start the Weather server:
bash
python weather/weather.pyThe server will start at http://localhost:8000, using SSE for transmission.
-
In another terminal window, run the client:
bash
python weather/mcp_client.pyThe client will connect to the server and perform a series of weather query tests.
API Functions
Weather Alert Query
python
get_alerts(state: str) -> str
state: Two-letter U.S. state code (e.g., CA, NY)- Returns: A list of active weather alerts for that state
Weather Forecast Query
python
get_forecast(latitude: float, longitude: float) -> str
latitude: Latitude of the locationlongitude: Longitude of the location- Returns: The weather forecast for that location
Project Structure
weather-mcp/
├── weather/
│ ├── weather.py # Main server file
│ ├── mcp_client.py # Client test file
├── requirements.txt # Project dependencies
├── .env.example # Example environment variables
└── README.md # This document
Environment Variable Configuration
The project uses a .env file to store environment variables and sensitive information. Please follow these steps to set it up:
-
Copy the environment variable template file:
bash
cp .env.example .env -
Edit the
.envfile, filling in your actual configurations:MOONSHOT_API_KEY=your_actual_api_key
-
Ensure that the
.envfile is not committed to version control.