mcp_weather_scraper

mcp_weather_scraper

3.1

If you are the rightful owner of mcp_weather_scraper 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.

MCP Weather Scraper is an experimental project using Model Context Protocol (MCP) with OpenAI and FastAPI to fetch real-time weather data.

๐ŸŒฆ๏ธ MCP Weather Scraper

This project is an experimental implementation of the Model Context Protocol (MCP) using a lightweight LLM via ** OpenAI** and FastAPI to fetch and structure real-time weather information from open web sources. The goal is to explore how LLMs can interact with tools and serve as intelligent agents for retrieving and reasoning over unstructured web data.



๐Ÿš€ Features

  • โœ… MCP-compliant server with weather scraping via browser search
  • โœ… Integration with OpenAI LLM (e.g., gpt-3.5-turbo)
  • โœ… FastAPI server provides weather info as callable MCP tool
  • โœ… Automatic HTML parsing using selectolax for performance
  • โœ… LLM handles unstructured web content extraction into structured schema
  • โœ… Streamlit app frontend for user interaction
  • โœ… Response caching using functools.lru_cache

๐Ÿง  Refresh Before You Dive In

Top 5 Concepts to Brush Up On for This Repo

๐Ÿงฉ Concept๐Ÿ” What It Isโš™๏ธ Why It Matters
Model Context Protocol (MCP)A new protocol for tool-calling in LLMsPowers structured AI-agent communication
UvicornFast ASGI server for Python web appsHosts the FastAPI-based MCP server
SelectolaxHigh-speed HTML parserEfficiently scrapes and extracts weather data
functools.lru_cacheBuilt-in Python decorator to cache function callsBoosts performance by avoiding repeated fetches
Token Usage Metrics (OpenAI)Info on how many tokens were used in an LLM callHelps track cost and optimize prompt design

๐Ÿ’ก Even if you're familiar with Python and APIs, these tools represent cutting-edge AI stack engineering and are worth a quick look!


๐Ÿ“Š Token Usage & Performance Metrics

The Streamlit UI now includes:

  • โฑ๏ธ Response Time
    Time taken to fetch and process weather info

  • ๐Ÿง  Prompt Tokens
    Tokens used in the LLM prompt

  • ๐Ÿ’ฌ Completion Tokens
    Tokens generated in the LLM response

  • ๐Ÿ”ข Total Tokens
    Total token count per request, useful for cost tracking

These are displayed in a clean visual layout under each result card.


๐Ÿ–ฅ๏ธ Streamlit App Preview


Requirements

  • Python 3.9 or higher
  • Dependencies listed in requirements.txt

๐Ÿ› ๏ธ Setup

  1. Clone the repo
    git clone https://github.com/your-username/mcp_weather_scraper.git
    cd mcp_weather_scraper
    
  2. Create and activate a virtual environment
    python -m venv .venv
    .venv\Scripts\activate  # On Windows
    
  3. Install dependencies
    pip install -r requirements.txt
    
  4. Set environment variables Create a .env file in the root directory and add your OpenAI API key
    OPENAI_API_KEY=your_openai_api_key
    
  5. Running the Server
    uvicorn server:app --reload
    
    The server will be available at http://localhost:8000. You can access the API documentation at:
  6. Making a Request
    python client.py
    
    OR
    curl -X POST http://localhost:8000/weather -H "Content-Type: application/json" -d '{"location": "Seattle"}'
    
    The script sends a POST request with the following payload:
    {
      "location": "Seattle"
    }
    
    The server will respond with weather data in JSON format, such as:
    {
      "location": "Seattle",
      "temperature": "15ยฐC",
      "humidity": "80%",
      "air_quality": "Good",
      "condition": "Cloudy"
    }
    

๐Ÿ“ฆ Folder Structure

.
๐Ÿ“ mcp_weather_scraper/
โ”‚
โ”œโ”€โ”€ assets/
โ”‚   โ””โ”€โ”€ streamlit_screenshot.png
โ”œโ”€โ”€ server.py          # MCP-compatible tool server
โ”œโ”€โ”€ client.py          # MCP client that interacts with model + tools
โ”œโ”€โ”€ data_models.py     # Pydantic schemas for request/response
โ”œโ”€โ”€ utils.py           # HTML cleaning, scraping, etc.
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ .env

๐Ÿ“„ License

This project is licensed under the MIT License.