Python-MCP-Server

KshitizSareen/Python-MCP-Server

3.2

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.

Tools
1
Resources
0
Prompts
0

🌀️ 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, and mcp.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

🀝 Credits