Skip to content
Merged
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
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mcp-caiyun-weather"
version = "0.1.1"
dynamic = ["version"]
description = "Caiyun Weather MCP Server"
readme = "README.md"
maintainers = [
Expand Down Expand Up @@ -33,6 +33,9 @@ mcp-caiyun-weather = "mcp_caiyun_weather:main"
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.version]
path = "src/mcp_caiyun_weather/__init__.py"

[dependency-groups]
dev = [
"pytest>=8.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/mcp_caiyun_weather/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from . import server

__version__ = "0.1.2"


def main():
"""Main entry point for the package."""
server.main()


# Optionally expose other important items at package level
__all__ = ["main", "server"]
__all__ = ["main", "server", "__version__"]
28 changes: 19 additions & 9 deletions src/mcp_caiyun_weather/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ async def make_request(client: httpx.AsyncClient, url: str, params: dict) -> dic
return response.json()


def format_ratio_as_percent(value: float) -> str:
"""Convert 0~1 ratio values to percentage strings."""
return f"{value * 100:.0f}%"


@mcp.tool()
async def get_realtime_weather(
lng: float = Field(
Expand All @@ -31,14 +36,14 @@ async def get_realtime_weather(
result = await make_request(
client,
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/realtime",
{"lang": "en_US"},
{"lang": "en_US", "unit": "metric:v2"},
)
result = result["result"]["realtime"]
return f"""
Temperature: {result["temperature"]}°C
Humidity: {result["humidity"]}%
Wind: {result["wind"]["speed"]} m/s, From north clockwise {result["wind"]["direction"]}°
Precipitation: {result["precipitation"]["local"]["intensity"]}%
Humidity: {format_ratio_as_percent(result["humidity"])}
Wind: {result["wind"]["speed"]} km/h, From north clockwise {result["wind"]["direction"]}°
Precipitation: {result["precipitation"]["local"]["intensity"]} mm/hr
Air Quality:
PM2.5: {result["air_quality"]["pm25"]} μg/m³
PM10: {result["air_quality"]["pm10"]} μg/m³
Expand Down Expand Up @@ -72,7 +77,7 @@ async def get_hourly_forecast(
result = await make_request(
client,
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/hourly",
{"hourlysteps": "72", "lang": "en_US"},
{"hourlysteps": "72", "lang": "en_US", "unit": "metric:v2"},
)
hourly = result["result"]["hourly"]
forecast = "72-Hour Forecast:\n"
Expand All @@ -89,7 +94,7 @@ async def get_hourly_forecast(
Temperature: {temp}°C
Weather: {skycon}
Rain Probability: {rain_prob}%
Wind: {wind_speed}m/s, {wind_dir}°
Wind: {wind_speed} km/h, {wind_dir}°
------------------------"""
return forecast
except Exception as e:
Expand All @@ -111,7 +116,7 @@ async def get_weekly_forecast(
result = await make_request(
client,
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/daily",
{"dailysteps": "7", "lang": "en_US"},
{"dailysteps": "7", "lang": "en_US", "unit": "metric:v2"},
)
daily = result["result"]["daily"]
forecast = "7-Day Forecast:\n"
Expand Down Expand Up @@ -151,7 +156,12 @@ async def get_historical_weather(
result = await make_request(
client,
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/hourly",
{"hourlysteps": "24", "begin": str(timestamp), "lang": "en_US"},
{
"hourlysteps": "24",
"begin": str(timestamp),
"lang": "en_US",
"unit": "metric:v2",
},
)
hourly = result["result"]["hourly"]
history = "Past 24-Hour Weather:\n"
Expand Down Expand Up @@ -185,7 +195,7 @@ async def get_weather_alerts(
result = await make_request(
client,
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/weather",
{"alert": "true", "lang": "en_US"},
{"alert": "true", "lang": "en_US", "unit": "metric:v2"},
)
alerts = result["result"].get("alert", {}).get("content", [])
if not alerts:
Expand Down