KshitizSareen/Python-MCP-Server
If you are the rightful owner of Python-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 henry@mcphub.com.
This project demonstrates how to build a FastMCP server that interacts with the WeatherAPI, logs weather forecasts to a file, and uses prompts, tools, and resources to work with that data in a structured way.
π€οΈ MCP Weather Server with FastMCP
This project demonstrates how to build a FastMCP server that interacts with the WeatherAPI, logs weather forecasts to a file, and uses prompts, tools, and resources to work with that data in a structured way.
π Features
- β
Fetch weather data for any city and save it to a
notes.txt
file - β Summarize logged weather entries into "Hot Days" and "Cold Days"
- β
Use
mcp.tool
,mcp.prompt
, andmcp.resource
to model agent capabilities - β
Get the latest weather in Houston dynamically via
@mcp.resource
π οΈ Prerequisites
- Python 3.9+
- uv (or
pip
if preferred) - WeatherAPI key β Get one here
π¦ Setup Instructions
1. Clone or Download
git clone <your_repo_url>
cd your_project
2. Install Dependencies
If you're using uv
:
uv venv
source .venv/bin/activate # or .\.venv\Scripts\activate on Windows
uv pip install httpx
Or using pip:
python -m venv .venv
source .venv/bin/activate
pip install httpx
π§ Configuration
Edit the line in get_weather_data
to use your API key:
"key": "API_KEY" # <-- replace with your actual WeatherAPI key
π§ Code Overview
β Create the MCP Server
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("MCP Server for Weather")
π§ Tool: Fetch & Save Weather
@mcp.tool()
def get_weather_and_add_to_notes(city: str, days: int)
This tool fetches weather data for the next x
days and appends it to notes.txt
in this format:
The temperature is 25 C, on the day of 2025-06-10 in city Berlin
π¬ Prompt: Summarize Notes
@mcp.prompt()
def note_summary_prompt() -> str:
The prompt reads notes.txt
and classifies entries into:
- Hot Days (temperature > 20Β°C)
- Cold Days (temperature β€ 20Β°C)
π Resource: Latest Weather in Houston
@mcp.resource("weather://latest")
def latest_weather_in_houston() -> str:
This dynamic resource fetches todayβs forecast in Houston from the WeatherAPI in real-time.
βΆοΈ Install the Server
To install the server, write
uv run mcp install main.py
Youβll now have a working MCP server ready to be queried by agents!
π Notes File
All weather logs are stored in a local file named:
notes.txt
Make sure it's in the same directory as your script, or update the NOTES_FILE
path accordingly.
β Example Query (Tool Call)
{
"tool": "get_weather_and_add_to_notes",
"parameters": {
"city": "Berlin",
"days": 3
}
}
π¬ Example Output (Prompt)
Hot Days:
- The temperature is 25 C, on the day of 2025-06-10 in city Berlin
Cold Days:
- The temperature is 15 C, on the day of 2025-06-11 in city Berlin