🌦️ Building a Production-Ready Modular Weather Data CLI using Async APIs and MCP

Arpit DubeyArpit Dubey
4 min read

📘 Introduction

In today’s real-time data economy, delivering up-to-the-minute weather information is not just a convenience—it's a necessity. This project introduces a modular, asynchronous command-line weather application developed in Python. It integrates with the OpenWeatherMap API, and leverages Model Context Protocol (MCP) to expose its capabilities to LLM-based tools and agents. This blog delves deep into how this system works, its architecture, and the rationale behind technical choices.


🎯 Problem Statement

Weather applications often suffer from two major issues:

  1. Synchronous Bottlenecks: Traditional synchronous HTTP clients can block execution and delay user interaction.

  2. Poor Modularity: Existing tools are not reusable or pluggable into larger intelligent systems.

✅ Goals:

  • Ensure real-time, asynchronous data fetching.

  • Make it CLI-friendly and agent-compatible using FastMCP.

  • Structure the project for ease of extension and automation.


🧰 Tech Stack & Tools

ComponentTechnology
LanguagePython 3.10+
HTTP Clienthttpx (async)
Weather APIOpenWeatherMap
Protocol LayerFastMCP (Model Context Protocol)
Execution StyleCLI & AI-Agent Tooling

🧱 Project Architecture

weather/
├── weather.py         # Async API handlers and CLI tool exposure
├── pyproject.toml     # Dependency management (Poetry)
├── README.md          # Project documentation

🌐 Core Components

  • make_weather_request(url): Core async function to call OpenWeatherMap with retries and error handling.

  • get_weather(city, country='IN'): Returns current weather based on city.

  • get_forecast(city, country='IN'): Returns one weather snapshot per day for the next 5 days.


🔄 Inside the Engine Room – How It Works

Step 1: MCP Initialization

from mcp.server.fastmcp import FastMCP
mcp = FastMCP("weather")

FastMCP creates a server where decorated functions become accessible as command-line/agent tools.

Step 2: Async API Requests using httpx

async with httpx.AsyncClient() as client:
    response = await client.get(url, timeout=30.0)

Using async I/O prevents the CLI from freezing, even when multiple cities are queried in parallel.

Step 3: Forecast Parser Logic

The system selects one time point per day (between 11:00 and 15:00) to summarize daily forecasts. This keeps the forecast clean, relevant, and user-friendly.


🛠️ Getting Started – Step-by-Step Setup

🔧 Prerequisites

  • Python 3.10+

  • OpenWeatherMap API key (free tier works well)

📦 Installation

pip install httpx mcp

🌐 Export Environment Variable

export OPENWEATHER_API_KEY='your_api_key_here'

▶️ Run the App

python weather.py

You’ll now have a CLI environment where you can call:

await get_weather("Mumbai")
await get_forecast("Delhi")

🧪 Sample Output

Current Weather for Mumbai, IN:
Condition: Rain (moderate rain)
Temperature: 29.5°C (Feels like: 33.1°C)
Humidity: 84%
Wind Speed: 4.6 m/s
5-Day Forecast for Delhi:
---
Date: 2024-06-08
Condition: Clouds (scattered clouds)
Temperature: 35°C (Feels like: 37°C)
Humidity: 60%

🌟 Why MCP and Async Matter

  • Async = Speed: Reduce wait time by 2x during high-frequency calls.

  • MCP = Modularity: The @mcp.tool() decorator makes this usable in LangChain, CrewAI, or your own LLM.

  • CLI/Agent Ready: Tools can be reused in workflows, automation pipelines, or chat-based interfaces.


📈 Use Cases

  • Integrate into LLM assistants (e.g., ChatGPT Plugins, LangChain Agents).

  • Plug into smart IoT dashboards.

  • Use for climate-based automation in agriculture, logistics.


📌 Future Enhancements

Feature IdeaDescription
🔔 Weather AlertsIntegrate severe weather notification system.
🌍 Global CoverageSupport multilingual/country-specific responses.
📊 REST API ModeTurn into a FastAPI-powered microservice.
📉 Caching LayerAvoid repeated API calls for same city queries.

🗂️ GitHub + Run Instructions

Repo: github.com/aarpitdubey/project/tree/main/mcp

🧪 Local Setup:

git clone https://github.com/aarpitdubey/project.git
cd project/mcp
pip install -r requirements.txt
python weather.py

🎓 Conclusion

This project is more than a weather fetcher—it’s a modular, intelligent component that fits right into modern AI/agent-based ecosystems. With a clean async architecture and MCP tooling, it showcases how CLI tools can evolve to support LLMs, DevOps, and real-world workflows at scale.

Ready to integrate weather into your AI system? This project is your perfect starting point.

0
Subscribe to my newsletter

Read articles from Arpit Dubey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arpit Dubey
Arpit Dubey

A Full Stack Web Developer who is familiar with java, python, Java script, AI, ML & DL. Block chain and I'm ENFJ-A so, feel free to talk. Thank you