Created
November 15, 2025 00:52
-
-
Save sboesen/2baad4f77704d4bfc9e35f2d06f35688 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S uv run | |
| # /// script | |
| # dependencies = ["fastmcp", "httpx"] | |
| # /// | |
| """ | |
| MCP server exposing weather tool using FastMCP. | |
| """ | |
| from fastmcp import FastMCP | |
| import httpx | |
| # Initialize the FastMCP server | |
| mcp = FastMCP("WeatherTool") | |
| @mcp.tool() | |
| def get_weather( | |
| city: str, | |
| country: str = "", | |
| scale: str = "celsius" | |
| ) -> str: | |
| """ | |
| Fetch current weather information for a specified location. | |
| Args: | |
| city: The city name (e.g., "Seattle", "Tokyo") | |
| country: Optional country name (e.g., "USA", "Japan") | |
| scale: Temperature scale - "celsius" or "fahrenheit" (default: "celsius") | |
| Returns: | |
| A string describing the current weather conditions. | |
| """ | |
| try: | |
| # wttr.in accepts location as "city,country" or just "city" | |
| location = f"{city},{country}" if country else city | |
| url = f"https://wttr.in/{location}?format=j1" | |
| response = httpx.get(url, timeout=10.0) | |
| response.raise_for_status() | |
| data = response.json() | |
| # Extract current weather from the API response | |
| current = data["current_condition"][0] | |
| temp_c = float(current["temp_C"]) | |
| temp_f = float(current["temp_F"]) | |
| condition = current["weatherDesc"][0]["value"] | |
| humidity = current["humidity"] | |
| # Use the requested scale | |
| if scale.lower() == "fahrenheit": | |
| temp = temp_f | |
| temp_unit = "°F" | |
| else: | |
| temp = temp_c | |
| temp_unit = "°C" | |
| location_display = f"{city}, {country}" if country else city | |
| return f"The weather in {location_display} is {temp:.1f}{temp_unit} ({condition}). Humidity: {humidity}%." | |
| except httpx.HTTPError as e: | |
| return f"Error fetching weather data: {e}" | |
| except (KeyError, ValueError) as e: | |
| return f"Error parsing weather data: {e}" | |
| if __name__ == "__main__": | |
| mcp.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment