Skip to content

Commit d39f76e

Browse files
authored
Merge pull request #13 from caiyunapp/wind
Use metric:v2 units in API requests
2 parents b43c4e5 + 3796c81 commit d39f76e

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mcp-caiyun-weather"
3-
version = "0.1.1"
3+
dynamic = ["version"]
44
description = "Caiyun Weather MCP Server"
55
readme = "README.md"
66
maintainers = [
@@ -33,6 +33,9 @@ mcp-caiyun-weather = "mcp_caiyun_weather:main"
3333
requires = ["hatchling"]
3434
build-backend = "hatchling.build"
3535

36+
[tool.hatch.version]
37+
path = "src/mcp_caiyun_weather/__init__.py"
38+
3639
[dependency-groups]
3740
dev = [
3841
"pytest>=8.0.0",

src/mcp_caiyun_weather/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from . import server
22

3+
__version__ = "0.1.2"
4+
35

46
def main():
57
"""Main entry point for the package."""
68
server.main()
79

810

911
# Optionally expose other important items at package level
10-
__all__ = ["main", "server"]
12+
__all__ = ["main", "server", "__version__"]

src/mcp_caiyun_weather/server.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ async def make_request(client: httpx.AsyncClient, url: str, params: dict) -> dic
1616
return response.json()
1717

1818

19+
def format_ratio_as_percent(value: float) -> str:
20+
"""Convert 0~1 ratio values to percentage strings."""
21+
return f"{value * 100:.0f}%"
22+
23+
1924
@mcp.tool()
2025
async def get_realtime_weather(
2126
lng: float = Field(
@@ -31,14 +36,14 @@ async def get_realtime_weather(
3136
result = await make_request(
3237
client,
3338
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/realtime",
34-
{"lang": "en_US"},
39+
{"lang": "en_US", "unit": "metric:v2"},
3540
)
3641
result = result["result"]["realtime"]
3742
return f"""
3843
Temperature: {result["temperature"]}°C
39-
Humidity: {result["humidity"]}%
40-
Wind: {result["wind"]["speed"]} m/s, From north clockwise {result["wind"]["direction"]}°
41-
Precipitation: {result["precipitation"]["local"]["intensity"]}%
44+
Humidity: {format_ratio_as_percent(result["humidity"])}
45+
Wind: {result["wind"]["speed"]} km/h, From north clockwise {result["wind"]["direction"]}°
46+
Precipitation: {result["precipitation"]["local"]["intensity"]} mm/hr
4247
Air Quality:
4348
PM2.5: {result["air_quality"]["pm25"]} μg/m³
4449
PM10: {result["air_quality"]["pm10"]} μg/m³
@@ -72,7 +77,7 @@ async def get_hourly_forecast(
7277
result = await make_request(
7378
client,
7479
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/hourly",
75-
{"hourlysteps": "72", "lang": "en_US"},
80+
{"hourlysteps": "72", "lang": "en_US", "unit": "metric:v2"},
7681
)
7782
hourly = result["result"]["hourly"]
7883
forecast = "72-Hour Forecast:\n"
@@ -89,7 +94,7 @@ async def get_hourly_forecast(
8994
Temperature: {temp}°C
9095
Weather: {skycon}
9196
Rain Probability: {rain_prob}%
92-
Wind: {wind_speed}m/s, {wind_dir}°
97+
Wind: {wind_speed} km/h, {wind_dir}°
9398
------------------------"""
9499
return forecast
95100
except Exception as e:
@@ -111,7 +116,7 @@ async def get_weekly_forecast(
111116
result = await make_request(
112117
client,
113118
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/daily",
114-
{"dailysteps": "7", "lang": "en_US"},
119+
{"dailysteps": "7", "lang": "en_US", "unit": "metric:v2"},
115120
)
116121
daily = result["result"]["daily"]
117122
forecast = "7-Day Forecast:\n"
@@ -151,7 +156,12 @@ async def get_historical_weather(
151156
result = await make_request(
152157
client,
153158
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/hourly",
154-
{"hourlysteps": "24", "begin": str(timestamp), "lang": "en_US"},
159+
{
160+
"hourlysteps": "24",
161+
"begin": str(timestamp),
162+
"lang": "en_US",
163+
"unit": "metric:v2",
164+
},
155165
)
156166
hourly = result["result"]["hourly"]
157167
history = "Past 24-Hour Weather:\n"
@@ -185,7 +195,7 @@ async def get_weather_alerts(
185195
result = await make_request(
186196
client,
187197
f"https://api.caiyunapp.com/v2.6/{api_token}/{lng},{lat}/weather",
188-
{"alert": "true", "lang": "en_US"},
198+
{"alert": "true", "lang": "en_US", "unit": "metric:v2"},
189199
)
190200
alerts = result["result"].get("alert", {}).get("content", [])
191201
if not alerts:

0 commit comments

Comments
 (0)