Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions weather-server-python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,41 @@ dependencies = [
requires = [ "hatchling",]
build-backend = "hatchling.build"

[dependency-groups]
dev = [
"ruff>=0.14.8",
]

[project.scripts]
weather = "weather:main"

[tool.ruff]
line-length = 120
target-version = "py310"
extend-exclude = ["README.md"]

[tool.ruff.lint]
select = [
"C4", # flake8-comprehensions
"C90", # mccabe
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"PERF", # Perflint
"PL", # Pylint
"UP", # pyupgrade
]
ignore = ["PERF203", "PLC0415", "PLR0402"]
mccabe.max-complexity = 24 # Default is 10

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/server/fastmcp/test_func_metadata.py" = ["E501"]
"tests/shared/test_progress_notifications.py" = ["PLW0603"]

[tool.ruff.lint.pylint]
allow-magic-value-types = ["bytes", "float", "int", "str"]
max-args = 23 # Default is 5
max-branches = 23 # Default is 12
max-returns = 13 # Default is 6
max-statements = 102 # Default is 50
34 changes: 34 additions & 0 deletions weather-server-python/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions weather-server-python/weather.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any

import httpx
from mcp.server.fastmcp import FastMCP

Expand All @@ -9,12 +10,10 @@
NWS_API_BASE = "https://api.weather.gov"
USER_AGENT = "weather-app/1.0"


async def make_nws_request(url: str) -> dict[str, Any] | None:
"""Make a request to the NWS API with proper error handling."""
headers = {
"User-Agent": USER_AGENT,
"Accept": "application/geo+json"
}
headers = {"User-Agent": USER_AGENT, "Accept": "application/geo+json"}
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, headers=headers, timeout=30.0)
Expand All @@ -23,17 +22,19 @@ async def make_nws_request(url: str) -> dict[str, Any] | None:
except Exception:
return None


def format_alert(feature: dict) -> str:
"""Format an alert feature into a readable string."""
props = feature["properties"]
return f"""
Event: {props.get('event', 'Unknown')}
Area: {props.get('areaDesc', 'Unknown')}
Severity: {props.get('severity', 'Unknown')}
Description: {props.get('description', 'No description available')}
Instructions: {props.get('instruction', 'No specific instructions provided')}
Event: {props.get("event", "Unknown")}
Area: {props.get("areaDesc", "Unknown")}
Severity: {props.get("severity", "Unknown")}
Description: {props.get("description", "No description available")}
Instructions: {props.get("instruction", "No specific instructions provided")}
"""


@mcp.tool()
async def get_alerts(state: str) -> str:
"""Get weather alerts for a US state.
Expand All @@ -53,6 +54,7 @@ async def get_alerts(state: str) -> str:
alerts = [format_alert(feature) for feature in data["features"]]
return "\n---\n".join(alerts)


@mcp.tool()
async def get_forecast(latitude: float, longitude: float) -> str:
"""Get weather forecast for a location.
Expand Down Expand Up @@ -80,18 +82,20 @@ async def get_forecast(latitude: float, longitude: float) -> str:
forecasts = []
for period in periods[:5]: # Only show next 5 periods
forecast = f"""
{period['name']}:
Temperature: {period['temperature']}°{period['temperatureUnit']}
Wind: {period['windSpeed']} {period['windDirection']}
Forecast: {period['detailedForecast']}
{period["name"]}:
Temperature: {period["temperature"]}°{period["temperatureUnit"]}
Wind: {period["windSpeed"]} {period["windDirection"]}
Forecast: {period["detailedForecast"]}
"""
forecasts.append(forecast)

return "\n---\n".join(forecasts)


def main():
# Initialize and run the server
mcp.run(transport='stdio')
mcp.run(transport="stdio")


if __name__ == "__main__":
main()