Skip to content

Commit 55b6e7b

Browse files
cliffhallclaude
andcommitted
fix(time): keep the host's tzdata path out of invalid-timezone errors
A timezone that names a tz database directory (`America`) makes zoneinfo raise an OSError whose text quotes the absolute path to the tzdata install, and `get_zoneinfo` passed that text to the client. Every invalid timezone now gets one fixed message that names only the input: `Invalid timezone: '<name>' is not a known IANA timezone name`, the same wording `serve()` already uses for a bad --local-timezone. Closes #5559 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 7f383a8 commit 55b6e7b

5 files changed

Lines changed: 48 additions & 17 deletions

File tree

‎src/time/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The server speaks both eras of the Model Context Protocol over stdio, with nothi
3737

3838
The server is built on the SDK's high-level `MCPServer`. Each tool declares an `outputSchema` and returns its result twice: as `structuredContent`, and as the same object in indented JSON in a text block, for a client that does not read structured output.
3939

40-
- **A tool failure is an `isError` result** whose text the model can read and act on, prefixed `Error executing tool <name>: `. That covers an unknown or non-exact timezone (`Invalid timezone: ...`), an empty timezone (`Missing required argument: <argument>`), a time not in `HH:MM` 24-hour form, a wall-clock time skipped by a clock change, and an argument missing or of the wrong type (the SDK's validation message).
40+
- **A tool failure is an `isError` result** whose text the model can read and act on, prefixed `Error executing tool <name>: `. That covers an unknown or non-exact timezone (`Invalid timezone: '<name>' is not a known IANA timezone name`, a fixed message that names only the input and never the host's tz database path), an empty timezone (`Missing required argument: <argument>`), a time not in `HH:MM` 24-hour form, a wall-clock time skipped by a clock change, and an argument missing or of the wrong type (the SDK's validation message).
4141
- **An ambiguous time is reported, not guessed.** On the day a clock change repeats a wall-clock time (a DST fall-back, such as 01:30 on 2024-11-03 in `America/New_York`), `convert_time` returns its first occurrence in `source` and `target`, and the second in `later_occurrence`: a `note` saying the time occurs twice, with that occurrence's own `source`, `target` and `time_difference`. For any other time `later_occurrence` is `null`.
4242
- **An unknown tool name is a JSON-RPC `-32602` error** (`Unknown tool: <name>`), not an `isError` result, as both protocol versions specify.
4343
- The server registers no prompts or resources. Their list methods answer with empty lists.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,21 @@ def load_zoneinfo(timezone_name: str) -> ZoneInfo:
117117

118118

119119
def get_zoneinfo(timezone_name: str) -> ZoneInfo:
120-
"""`load_zoneinfo`, failing with a `ToolError` the model can read."""
120+
"""`load_zoneinfo`, failing with a `ToolError` the model can read.
121+
122+
The message is fixed and names only the input. zoneinfo raises a
123+
different type per kind of bad key, and some of its messages carry the
124+
host's path to the tz database: a key that names a tzdata directory
125+
(`America`) is an `OSError` quoting that directory's absolute path
126+
(#5559). So none of zoneinfo's own text reaches the client, as at startup
127+
(`serve`).
128+
"""
121129
try:
122130
return load_zoneinfo(timezone_name)
123131
except Exception as e:
124-
raise ToolError(f"Invalid timezone: {e!s}") from e
132+
raise ToolError(
133+
f"Invalid timezone: {timezone_name!r} is not a known IANA timezone name"
134+
) from e
125135

126136

127137
class TimeServer:

‎src/time/tests/test_eras.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async def test_tool_failure_is_an_is_error_result_in_each_era(
140140
assert bad_zone.is_error is True
141141
assert text_of(bad_zone) == (
142142
"Error executing tool get_current_time: "
143-
"Invalid timezone: 'No time zone found with key Nope/Nowhere'"
143+
"Invalid timezone: 'Nope/Nowhere' is not a known IANA timezone name"
144144
)
145145

146146

‎src/time/tests/test_protocol.py‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -676,19 +676,25 @@ async def test_get_current_time_argument_validation(
676676
({"timezone": ""}, "Missing required argument: timezone"),
677677
(
678678
{"timezone": "Invalid/Timezone"},
679-
"Invalid timezone: 'No time zone found with key Invalid/Timezone'",
679+
"Invalid timezone: 'Invalid/Timezone' is not a known IANA timezone name",
680680
),
681681
(
682682
{"timezone": "europe/warsaw"},
683-
"Invalid timezone: 'No time zone found with key europe/warsaw'",
683+
"Invalid timezone: 'europe/warsaw' is not a known IANA timezone name",
684684
),
685685
(
686686
{"timezone": "../etc/passwd"},
687-
"Invalid timezone: ZoneInfo keys must refer to subdirectories of TZPATH, got: ../etc/passwd",
687+
"Invalid timezone: '../etc/passwd' is not a known IANA timezone name",
688688
),
689689
(
690690
{"timezone": "/etc/localtime"},
691-
"Invalid timezone: ZoneInfo keys may not be absolute paths, got: /etc/localtime",
691+
"Invalid timezone: '/etc/localtime' is not a known IANA timezone name",
692+
),
693+
# A tzdata directory, not a zone: zoneinfo's OSError quotes the host's
694+
# absolute path to it, which must not reach the client (#5559).
695+
(
696+
{"timezone": "America"},
697+
"Invalid timezone: 'America' is not a known IANA timezone name",
692698
),
693699
],
694700
)
@@ -1146,11 +1152,21 @@ async def test_convert_time_argument_validation(
11461152
# Raised by the tool as a ToolError.
11471153
(
11481154
{"source_timezone": "Bad/Zone", "time": "12:00", "target_timezone": "UTC"},
1149-
"Invalid timezone: 'No time zone found with key Bad/Zone'",
1155+
"Invalid timezone: 'Bad/Zone' is not a known IANA timezone name",
11501156
),
11511157
(
11521158
{"source_timezone": "UTC", "time": "12:00", "target_timezone": "Bad/Zone"},
1153-
"Invalid timezone: 'No time zone found with key Bad/Zone'",
1159+
"Invalid timezone: 'Bad/Zone' is not a known IANA timezone name",
1160+
),
1161+
# A tzdata directory: the fixed message, not zoneinfo's OSError with
1162+
# the host's path (#5559).
1163+
(
1164+
{"source_timezone": "America", "time": "12:00", "target_timezone": "UTC"},
1165+
"Invalid timezone: 'America' is not a known IANA timezone name",
1166+
),
1167+
(
1168+
{"source_timezone": "UTC", "time": "12:00", "target_timezone": "America"},
1169+
"Invalid timezone: 'America' is not a known IANA timezone name",
11541170
),
11551171
# Like get_current_time, an empty timezone is reported as a missing
11561172
# argument rather than as zoneinfo's own message.
@@ -1196,7 +1212,7 @@ async def test_convert_time_checks_timezones_before_the_time_string() -> None:
11961212
{"source_timezone": "UTC", "time": "nope", "target_timezone": "Bad/Zone"},
11971213
)
11981214
assert result == tool_error(
1199-
"convert_time", "Invalid timezone: 'No time zone found with key Bad/Zone'"
1215+
"convert_time", "Invalid timezone: 'Bad/Zone' is not a known IANA timezone name"
12001216
)
12011217

12021218

@@ -1334,7 +1350,12 @@ async def test_get_time_ago_argument_validation(
13341350
({"timezone": ""}, "Missing required argument: timezone"),
13351351
(
13361352
{"timezone": "europe/warsaw"},
1337-
"Invalid timezone: 'No time zone found with key europe/warsaw'",
1353+
"Invalid timezone: 'europe/warsaw' is not a known IANA timezone name",
1354+
),
1355+
# A tzdata directory: no host path in the message (#5559).
1356+
(
1357+
{"timezone": "America"},
1358+
"Invalid timezone: 'America' is not a known IANA timezone name",
13381359
),
13391360
# Past what timedelta can hold, and past year 1.
13401361
(
@@ -1348,7 +1369,7 @@ async def test_get_time_ago_argument_validation(
13481369
# The timezone is checked before the offset.
13491370
(
13501371
{"timezone": "Bad/Zone", "days_ago": 10**10},
1351-
"Invalid timezone: 'No time zone found with key Bad/Zone'",
1372+
"Invalid timezone: 'Bad/Zone' is not a known IANA timezone name",
13521373
),
13531374
],
13541375
)

‎src/time/tests/test_server.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_get_current_time_with_invalid_timezone():
9191
time_server = TimeServer()
9292
with pytest.raises(
9393
ToolError,
94-
match=r"Invalid timezone: 'No time zone found with key Invalid/Timezone'",
94+
match=r"Invalid timezone: 'Invalid/Timezone' is not a known IANA timezone name",
9595
):
9696
time_server.get_current_time("Invalid/Timezone")
9797

@@ -103,13 +103,13 @@ def test_get_current_time_with_invalid_timezone():
103103
"invalid_tz",
104104
"12:00",
105105
"Europe/London",
106-
"Invalid timezone: 'No time zone found with key invalid_tz'",
106+
"Invalid timezone: 'invalid_tz' is not a known IANA timezone name",
107107
),
108108
(
109109
"Europe/Warsaw",
110110
"12:00",
111111
"invalid_tz",
112-
"Invalid timezone: 'No time zone found with key invalid_tz'",
112+
"Invalid timezone: 'invalid_tz' is not a known IANA timezone name",
113113
),
114114
(
115115
"Europe/Warsaw",
@@ -542,7 +542,7 @@ def test_get_zoneinfo_rejects_a_key_the_tz_database_does_not_list():
542542
patch("mcp_server_time.server.known_timezones", return_value=without_warsaw),
543543
pytest.raises(
544544
ToolError,
545-
match="Invalid timezone: 'No time zone found with key Europe/Warsaw'",
545+
match="Invalid timezone: 'Europe/Warsaw' is not a known IANA timezone name",
546546
),
547547
):
548548
get_zoneinfo("Europe/Warsaw")

0 commit comments

Comments
 (0)