DAY 6

Live Weather Tool 🌦️🌍🤖

Create a live weather tool using Python to supercharge your AI agent's capabilities. Learn how to integrate weather APIs and display real-time weather updates.

⏱ 15 mins
⚡ +50 XP

Day 6 — Add a Live Weather Tool

MCP & AI Agents — Agent Bootcamp Part 2 — RohithBuilds

Every tool your agent has so far works with data you created — exam dates, your notes. Today you'll give it a tool that pulls real, live data from the internet: current weather for any city in the world, using a free API that needs no signup and no API key.

Step 1 — Recap & Today's Plan

The plan is short and familiar — you've done this shape of work four times already:

  • Write a logic function that calls a public weather API
  • Wrap it with @mcp.tool()
  • Test it in Inspector
  • Then — the fun part — ask your Day 5 chat UI a weather question and watch it pick up the new tool with zero changes to app.py

Step 2 — Install requests

This is a new tool for this course's venv:

pip install requests

Expected Output

Terminal showing requests installing successfully

Step 3 — Build the Weather Lookup

We'll use Open-Meteo — a free weather API that needs no key. It works in two steps: turn a city name into coordinates (geocoding), then ask for the weather at those coordinates.

Open server.py and add import requests to your imports. Then add this above if __name__ == "__main__"::

WEATHER_CODES = {
    0: "clear sky",
    1: "mainly clear",
    2: "partly cloudy",
    3: "overcast",
    45: "fog",
    48: "freezing fog",
    51: "light drizzle",
    53: "moderate drizzle",
    55: "dense drizzle",
    61: "slight rain",
    63: "moderate rain",
    65: "heavy rain",
    71: "slight snow",
    73: "moderate snow",
    75: "heavy snow",
    80: "rain showers",
    81: "moderate rain showers",
    82: "violent rain showers",
    95: "thunderstorm",
}


def get_weather_logic(city: str) -> str:
    geo = requests.get(
        "https://geocoding-api.open-meteo.com/v1/search",
        params={"name": city, "count": 1}
    ).json()

    if not geo.get("results"):
        return f"Could not find a location called '{city}'."

    location = geo["results"][0]
    lat = location["latitude"]
    lon = location["longitude"]
    name = location["name"]
    country = location.get("country", "")

    weather = requests.get(
        "https://api.open-meteo.com/v1/forecast",
        params={"latitude": lat, "longitude": lon, "current_weather": True}
    ).json()

    current = weather["current_weather"]
    condition = WEATHER_CODES.get(
        current["weathercode"], f"weather code {current['weathercode']}"
    )

    return (
        f"{name}, {country}: {current['temperature']}°C, "
        f"{condition}, wind {current['windspeed']} km/h"
    )

WEATHER_CODES covers the most common conditions. Anything not in the dictionary still works — it just shows the raw code as a fallback.

Step 4 — Add get_weather as an MCP Tool

Add the wrapper, same pattern as every tool this week:

@mcp.tool()
def get_weather(city: str) -> str:
    """Get the current weather for a city. Works for cities worldwide."""
    return get_weather_logic(city)

Your server now has 5 tools: exam_countdown, add_note, list_notes, delete_note, and get_weather — plus the resources and prompts from Day 3.

Step 5 — Restart Inspector & Test get_weather

mcp dev server.py

In the Tools tab, click get_weather, enter city: Bengaluru, and run it.

Expected Output

MCP Inspector Tools tab now listing 5 tools including get_weather MCP Inspector showing get_weather returning current temperature and conditions for Bengaluru

Try a few more cities — Mumbai, Delhi, Tokyo, anything. No restarts, no new keys — Open-Meteo covers the whole world.

Step 6 — The Big Test: Ask Your Chat UI

Here's the payoff. Make sure app.py from Day 5 is running:

python app.py

Open http://127.0.0.1:5000 and ask:

  • What's the weather in Bengaluru right now?

Expected Output

Chat UI showing Rohi answering a live weather question for Bengaluru

You didn't touch app.py, agent.py, or templates/index.html — yet your chat UI can now answer weather questions. This is the entire point of MCP: add a tool to the server, and every client that uses it gets the new capability automatically.

Now try something that needs reasoning on top of the data:

  • Should I carry an umbrella in Mumbai today?

Expected Output

Chat UI showing Rohi fetching Mumbai's weather and giving advice about carrying an umbrella based on the conditions

Groq fetched the real weather data via your tool, then reasoned about it to answer a question the tool itself was never designed to answer. That combination — real data plus reasoning — is what makes MCP-powered agents useful.

Step 7 — Fallback Test Without Inspector

Add to test_local.py:

from server import get_weather_logic

print(get_weather_logic("Bengaluru"))
print(get_weather_logic("Mumbai"))
print(get_weather_logic("Nonexistentcityxyz"))

Run it:

python test_local.py

Expected Output

Terminal showing weather results for two valid cities and a not-found message for an invalid city

Notice the third line handles a city that doesn't exist gracefully — no crash, just a clear message. Good tools fail politely.

Step 8 — Save Your Progress

pip freeze > requirements.txt

Your server now has 5 tools, 2 resources, and 2 prompts — all built across 6 days, all running through the same chat UI. One day left.

✅ Day 6 Complete

Here is what you accomplished today:

Key Takeaways

  • Connected your MCP server to a real, free public API ✅
  • Built get_weather with graceful error handling ✅
  • Tested it live in Inspector for multiple cities ✅
  • Asked your Day 5 chat UI live weather questions — no code changes needed ✅
  • Watched your agent combine real data with reasoning ✅

What Is Coming Tomorrow

On Day 7 — the final day — you will:

  • Push your complete project to GitHub
  • Deploy it live on Render, just like Part 1, Day 7
  • Write a README explaining your MCP architecture
  • Get a public URL anyone can use to chat with your MCP-powered agent

See you there — last day! 🚀

← Previous Lesson