Python-MCP-Server

KshitizSareen/Python-MCP-Server

3.1

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 dayong@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