diff --git a/src/time/README.md b/src/time/README.md index 877551eff9..1f282cfaaf 100644 --- a/src/time/README.md +++ b/src/time/README.md @@ -37,7 +37,7 @@ The server speaks both eras of the Model Context Protocol over stdio, with nothi 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. -- **A tool failure is an `isError` result** whose text the model can read and act on, prefixed `Error executing tool : `. That covers an unknown or non-exact timezone (`Invalid timezone: ...`), an empty timezone (`Missing required 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). +- **A tool failure is an `isError` result** whose text the model can read and act on, prefixed `Error executing tool : `. That covers an unknown or non-exact timezone (`Invalid timezone: '' 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: `), 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). - **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`. - **An unknown tool name is a JSON-RPC `-32602` error** (`Unknown tool: `), not an `isError` result, as both protocol versions specify. - The server registers no prompts or resources. Their list methods answer with empty lists. diff --git a/src/time/src/mcp_server_time/server.py b/src/time/src/mcp_server_time/server.py index a7c1271483..dff7490e8f 100644 --- a/src/time/src/mcp_server_time/server.py +++ b/src/time/src/mcp_server_time/server.py @@ -117,11 +117,21 @@ def load_zoneinfo(timezone_name: str) -> ZoneInfo: def get_zoneinfo(timezone_name: str) -> ZoneInfo: - """`load_zoneinfo`, failing with a `ToolError` the model can read.""" + """`load_zoneinfo`, failing with a `ToolError` the model can read. + + The message is fixed and names only the input. zoneinfo raises a + different type per kind of bad key, and some of its messages carry the + host's path to the tz database: a key that names a tzdata directory + (`America`) is an `OSError` quoting that directory's absolute path + (#5559). So none of zoneinfo's own text reaches the client, as at startup + (`serve`). + """ try: return load_zoneinfo(timezone_name) except Exception as e: - raise ToolError(f"Invalid timezone: {e!s}") from e + raise ToolError( + f"Invalid timezone: {timezone_name!r} is not a known IANA timezone name" + ) from e class TimeServer: diff --git a/src/time/tests/test_eras.py b/src/time/tests/test_eras.py index e45a109624..60ad14d396 100644 --- a/src/time/tests/test_eras.py +++ b/src/time/tests/test_eras.py @@ -140,7 +140,7 @@ async def test_tool_failure_is_an_is_error_result_in_each_era( assert bad_zone.is_error is True assert text_of(bad_zone) == ( "Error executing tool get_current_time: " - "Invalid timezone: 'No time zone found with key Nope/Nowhere'" + "Invalid timezone: 'Nope/Nowhere' is not a known IANA timezone name" ) diff --git a/src/time/tests/test_protocol.py b/src/time/tests/test_protocol.py index 81ca2d9b88..389ff38213 100644 --- a/src/time/tests/test_protocol.py +++ b/src/time/tests/test_protocol.py @@ -676,19 +676,25 @@ async def test_get_current_time_argument_validation( ({"timezone": ""}, "Missing required argument: timezone"), ( {"timezone": "Invalid/Timezone"}, - "Invalid timezone: 'No time zone found with key Invalid/Timezone'", + "Invalid timezone: 'Invalid/Timezone' is not a known IANA timezone name", ), ( {"timezone": "europe/warsaw"}, - "Invalid timezone: 'No time zone found with key europe/warsaw'", + "Invalid timezone: 'europe/warsaw' is not a known IANA timezone name", ), ( {"timezone": "../etc/passwd"}, - "Invalid timezone: ZoneInfo keys must refer to subdirectories of TZPATH, got: ../etc/passwd", + "Invalid timezone: '../etc/passwd' is not a known IANA timezone name", ), ( {"timezone": "/etc/localtime"}, - "Invalid timezone: ZoneInfo keys may not be absolute paths, got: /etc/localtime", + "Invalid timezone: '/etc/localtime' is not a known IANA timezone name", + ), + # A tzdata directory, not a zone: zoneinfo's OSError quotes the host's + # absolute path to it, which must not reach the client (#5559). + ( + {"timezone": "America"}, + "Invalid timezone: 'America' is not a known IANA timezone name", ), ], ) @@ -1146,11 +1152,21 @@ async def test_convert_time_argument_validation( # Raised by the tool as a ToolError. ( {"source_timezone": "Bad/Zone", "time": "12:00", "target_timezone": "UTC"}, - "Invalid timezone: 'No time zone found with key Bad/Zone'", + "Invalid timezone: 'Bad/Zone' is not a known IANA timezone name", ), ( {"source_timezone": "UTC", "time": "12:00", "target_timezone": "Bad/Zone"}, - "Invalid timezone: 'No time zone found with key Bad/Zone'", + "Invalid timezone: 'Bad/Zone' is not a known IANA timezone name", + ), + # A tzdata directory: the fixed message, not zoneinfo's OSError with + # the host's path (#5559). + ( + {"source_timezone": "America", "time": "12:00", "target_timezone": "UTC"}, + "Invalid timezone: 'America' is not a known IANA timezone name", + ), + ( + {"source_timezone": "UTC", "time": "12:00", "target_timezone": "America"}, + "Invalid timezone: 'America' is not a known IANA timezone name", ), # Like get_current_time, an empty timezone is reported as a missing # argument rather than as zoneinfo's own message. @@ -1196,7 +1212,7 @@ async def test_convert_time_checks_timezones_before_the_time_string() -> None: {"source_timezone": "UTC", "time": "nope", "target_timezone": "Bad/Zone"}, ) assert result == tool_error( - "convert_time", "Invalid timezone: 'No time zone found with key Bad/Zone'" + "convert_time", "Invalid timezone: 'Bad/Zone' is not a known IANA timezone name" ) @@ -1334,7 +1350,12 @@ async def test_get_time_ago_argument_validation( ({"timezone": ""}, "Missing required argument: timezone"), ( {"timezone": "europe/warsaw"}, - "Invalid timezone: 'No time zone found with key europe/warsaw'", + "Invalid timezone: 'europe/warsaw' is not a known IANA timezone name", + ), + # A tzdata directory: no host path in the message (#5559). + ( + {"timezone": "America"}, + "Invalid timezone: 'America' is not a known IANA timezone name", ), # Past what timedelta can hold, and past year 1. ( @@ -1348,7 +1369,7 @@ async def test_get_time_ago_argument_validation( # The timezone is checked before the offset. ( {"timezone": "Bad/Zone", "days_ago": 10**10}, - "Invalid timezone: 'No time zone found with key Bad/Zone'", + "Invalid timezone: 'Bad/Zone' is not a known IANA timezone name", ), ], ) diff --git a/src/time/tests/test_server.py b/src/time/tests/test_server.py index 95f9747adf..84c15fa297 100644 --- a/src/time/tests/test_server.py +++ b/src/time/tests/test_server.py @@ -91,7 +91,7 @@ def test_get_current_time_with_invalid_timezone(): time_server = TimeServer() with pytest.raises( ToolError, - match=r"Invalid timezone: 'No time zone found with key Invalid/Timezone'", + match=r"Invalid timezone: 'Invalid/Timezone' is not a known IANA timezone name", ): time_server.get_current_time("Invalid/Timezone") @@ -103,13 +103,13 @@ def test_get_current_time_with_invalid_timezone(): "invalid_tz", "12:00", "Europe/London", - "Invalid timezone: 'No time zone found with key invalid_tz'", + "Invalid timezone: 'invalid_tz' is not a known IANA timezone name", ), ( "Europe/Warsaw", "12:00", "invalid_tz", - "Invalid timezone: 'No time zone found with key invalid_tz'", + "Invalid timezone: 'invalid_tz' is not a known IANA timezone name", ), ( "Europe/Warsaw", @@ -542,7 +542,7 @@ def test_get_zoneinfo_rejects_a_key_the_tz_database_does_not_list(): patch("mcp_server_time.server.known_timezones", return_value=without_warsaw), pytest.raises( ToolError, - match="Invalid timezone: 'No time zone found with key Europe/Warsaw'", + match="Invalid timezone: 'Europe/Warsaw' is not a known IANA timezone name", ), ): get_zoneinfo("Europe/Warsaw")