Skip to content

Commit be004b1

Browse files
authored
Merge pull request #5027 from modelcontextprotocol/v2/fix/5001-time-invalid-local-timezone
fix(time): exit with one line on an invalid --local-timezone (#5001)
2 parents 0e51df4 + 73db339 commit be004b1

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from importlib.metadata import version
33
from enum import Enum
44
import json
5+
import sys
56
from typing import Sequence
67

7-
from zoneinfo import ZoneInfo
8+
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
89
from tzlocal import get_localzone_name # ← returns "Europe/Paris", etc.
910

1011
from mcp.server import Server
@@ -139,6 +140,18 @@ def convert_time(
139140
async def serve(local_timezone: str | None = None) -> None:
140141
server = Server("mcp-time", version=SERVER_VERSION)
141142
time_server = TimeServer()
143+
if local_timezone:
144+
# Fail before the transport opens, with one line naming the bad value
145+
# rather than a traceback. zoneinfo raises a different type per kind of
146+
# bad key (unknown name, a tzdata directory, a path, a null byte), and
147+
# its messages can carry a filesystem path, so none of them is shown.
148+
try:
149+
ZoneInfo(local_timezone)
150+
except (ZoneInfoNotFoundError, ValueError, OSError):
151+
sys.exit(
152+
f"Error: invalid --local-timezone {local_timezone!r}: "
153+
"not a known IANA timezone name"
154+
)
142155
local_tz = str(get_local_tz(local_timezone))
143156

144157
@server.list_tools()

‎src/time/tests/test_entrypoints.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import runpy
1212
import shutil
13+
import subprocess
1314
import sys
1415
from pathlib import Path
1516
from unittest.mock import AsyncMock, MagicMock, patch
@@ -114,3 +115,22 @@ async def test_console_script_boots_over_stdio() -> None:
114115
)
115116
assert [tool.name for tool in tools.tools] == ["get_current_time", "convert_time"]
116117
assert result.model_dump(by_alias=True, mode="json")["isError"] is False
118+
119+
120+
def test_console_script_rejects_an_invalid_local_timezone() -> None:
121+
# #5001: one line on stderr naming the value, a non-zero exit, and no
122+
# traceback.
123+
script = shutil.which("mcp-server-time", path=str(Path(sys.executable).parent))
124+
assert script is not None, "the console script is not installed in this venv"
125+
proc = subprocess.run(
126+
[script, "--local-timezone", "Not/AZone"],
127+
stdin=subprocess.DEVNULL,
128+
capture_output=True,
129+
text=True,
130+
timeout=60,
131+
)
132+
assert proc.returncode == 1
133+
assert proc.stdout == ""
134+
assert proc.stderr.splitlines() == [
135+
"Error: invalid --local-timezone 'Not/AZone': not a known IANA timezone name"
136+
]

‎src/time/tests/test_protocol.py‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from mcp import ClientSession
3535
from mcp.shared.memory import create_client_server_memory_streams
3636
from pydantic import BaseModel
37-
from zoneinfo import ZoneInfoNotFoundError
3837

3938
from mcp_server_time.server import serve
4039

@@ -236,16 +235,31 @@ async def test_empty_local_timezone_override_is_treated_as_absent() -> None:
236235
assert result == {"tools": expected_tools("Africa/Cairo")}
237236

238237

239-
# KNOWN BUG #5001: an invalid --local-timezone crashes serve() with a traceback instead of a clean error; the fix changes this assertion.
240-
async def test_invalid_local_timezone_fails_before_the_transport_opens() -> None:
241-
# An unknown `--local-timezone` raises out of `serve()` itself, before
242-
# stdio is opened: the process dies with a traceback instead of starting.
238+
@pytest.mark.parametrize(
239+
"local_timezone",
240+
[
241+
"Not/AZone", # unknown name: ZoneInfoNotFoundError
242+
"America", # a tzdata directory, not a zone: OSError
243+
"../etc/passwd", # outside TZPATH: ValueError
244+
],
245+
)
246+
async def test_invalid_local_timezone_fails_before_the_transport_opens(
247+
local_timezone: str,
248+
) -> None:
249+
# An invalid `--local-timezone` stops `serve()` before stdio is opened,
250+
# with a one-line message naming the value and a non-zero exit, rather
251+
# than a traceback (#5001). A `SystemExit` whose code is a string prints
252+
# that string to stderr and exits 1.
243253
def unexpected_stdio_server():
244254
raise AssertionError("stdio_server must not be reached")
245255

246256
with patch("mcp_server_time.server.stdio_server", unexpected_stdio_server):
247-
with pytest.raises(ZoneInfoNotFoundError, match="Not/AZone"):
248-
await serve("Not/AZone")
257+
with pytest.raises(SystemExit) as exit_info:
258+
await serve(local_timezone)
259+
assert exit_info.value.code == (
260+
f"Error: invalid --local-timezone {local_timezone!r}: "
261+
"not a known IANA timezone name"
262+
)
249263

250264

251265
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)