Skip to content

Commit 400ffd5

Browse files
committed
fix(time): validate canonical IANA timezone name casing against available_timezones
Signed-off-by: Suparva <[email protected]>
1 parent 0763da6 commit 400ffd5

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎src/time/src/mcp_server_time/server.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import sys
1616
from typing import Any
1717

18-
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
18+
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError, available_timezones
1919
from tzlocal import get_localzone_name # ← returns "Europe/Paris", etc.
2020

2121
import jsonschema
@@ -81,7 +81,10 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo:
8181

8282
def get_zoneinfo(timezone_name: str) -> ZoneInfo:
8383
try:
84-
return ZoneInfo(timezone_name)
84+
tz = ZoneInfo(timezone_name)
85+
if timezone_name not in available_timezones():
86+
raise ZoneInfoNotFoundError(f"No time zone found with key {timezone_name}")
87+
return tz
8588
except Exception as e:
8689
raise MCPError(code=INVALID_PARAMS, message=f"Invalid timezone: {str(e)}")
8790

@@ -174,6 +177,10 @@ async def serve(local_timezone: str | None = None) -> None:
174177
# its messages can carry a filesystem path, so none of them is shown.
175178
try:
176179
ZoneInfo(local_timezone)
180+
if local_timezone not in available_timezones():
181+
raise ZoneInfoNotFoundError(
182+
f"No time zone found with key {local_timezone}"
183+
)
177184
except (ZoneInfoNotFoundError, ValueError, OSError):
178185
sys.exit(
179186
f"Error: invalid --local-timezone {local_timezone!r}: "

‎src/time/tests/test_server.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ def test_get_current_time_with_invalid_timezone():
9090
time_server.get_current_time("Invalid/Timezone")
9191

9292

93+
def test_get_current_time_with_case_mismatched_timezone():
94+
time_server = TimeServer()
95+
with pytest.raises(
96+
MCPError,
97+
match=r"Invalid timezone: 'No time zone found with key europe/warsaw'",
98+
):
99+
time_server.get_current_time("europe/warsaw")
100+
101+
93102
@pytest.mark.parametrize(
94103
"source_tz,time_str,target_tz,expected_error",
95104
[

0 commit comments

Comments
 (0)