Skip to content

Commit 3f5e9ea

Browse files
authored
Merge pull request #5086 from modelcontextprotocol/v2/chore/5080-release-cherry-picks
v1.0.0: cherry-pick #5077 and #5078 onto the release branch
2 parents d812043 + 96ec9ea commit 3f5e9ea

5 files changed

Lines changed: 54 additions & 7 deletions

File tree

‎src/memory/__tests__/persistence.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ describe("memory persistence over the protocol", () => {
211211
// which is not the ENOENT that means "no graph yet".
212212
await fs.mkdir(filePath);
213213
const { client } = await open();
214+
// Node 26 appends the path to the message; earlier versions do not
215+
// (#5076). The server passes Node's message through either way.
216+
const bare = "EISDIR: illegal operation on a directory, read";
217+
const nodeMajor = Number(process.versions.node.split(".")[0]);
218+
const expected = nodeMajor >= 26 ? `${bare} '${filePath}'` : bare;
214219

215220
for (const [name, args] of [
216221
["read_graph", {}],
@@ -220,9 +225,7 @@ describe("memory persistence over the protocol", () => {
220225
] as const) {
221226
const result = await call(client, name, args);
222227
expect(result.isError).toBe(true);
223-
expect(textOf(result)).toBe(
224-
"EISDIR: illegal operation on a directory, read",
225-
);
228+
expect(textOf(result)).toBe(expected);
226229
}
227230
await expect(
228231
client.readResource({ uri: "memory://knowledge-graph" }),

‎src/time/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Requires MCP Python SDK 1.x (`mcp>=1.29.0,<2`). SDK 2.0 renamed APIs this server
2020
- `time` (string): Time in 24-hour format (HH:MM)
2121
- `target_timezone` (string): Target IANA timezone name
2222

23+
A timezone name, here and in `--local-timezone`, must be an exact IANA key, case included: `Europe/Warsaw` is accepted and `europe/warsaw` is rejected on every platform, including macOS, whose case-insensitive filesystem would otherwise resolve it.
24+
2325
## Installation
2426

2527
### Using uv (recommended)

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from datetime import datetime, timedelta
2+
from functools import cache
23
from importlib.metadata import version
34
from enum import Enum
45
import json
56
import sys
67
from typing import Sequence
78

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

1112
from mcp.server import Server
@@ -65,9 +66,30 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo:
6566
return ZoneInfo("UTC")
6667

6768

69+
@cache
70+
def known_timezones() -> frozenset[str]:
71+
"""The exact IANA keys this host's tz database holds, read once."""
72+
return frozenset(available_timezones())
73+
74+
75+
def load_zoneinfo(timezone_name: str) -> ZoneInfo:
76+
"""`ZoneInfo(timezone_name)`, accepting only an exact IANA key.
77+
78+
zoneinfo looks a key up as a file path, so on a case-insensitive
79+
filesystem (macOS's APFS) `europe/warsaw` resolves to `Europe/Warsaw`
80+
while Linux rejects it (#5060). Checking the key against the database's
81+
own list makes every platform reject it. `ZoneInfo` runs first so a path
82+
or a directory keeps the error zoneinfo gives it.
83+
"""
84+
zone = ZoneInfo(timezone_name)
85+
if timezone_name not in known_timezones():
86+
raise ZoneInfoNotFoundError(f"No time zone found with key {timezone_name}")
87+
return zone
88+
89+
6890
def get_zoneinfo(timezone_name: str) -> ZoneInfo:
6991
try:
70-
return ZoneInfo(timezone_name)
92+
return load_zoneinfo(timezone_name)
7193
except Exception as e:
7294
raise McpError(
7395
ErrorData(code=INVALID_PARAMS, message=f"Invalid timezone: {str(e)}")
@@ -155,7 +177,7 @@ async def serve(local_timezone: str | None = None) -> None:
155177
# bad key (unknown name, a tzdata directory, a path, a null byte), and
156178
# its messages can carry a filesystem path, so none of them is shown.
157179
try:
158-
ZoneInfo(local_timezone)
180+
load_zoneinfo(local_timezone)
159181
except (ZoneInfoNotFoundError, ValueError, OSError):
160182
sys.exit(
161183
f"Error: invalid --local-timezone {local_timezone!r}: "

‎src/time/tests/test_protocol.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ async def test_empty_local_timezone_override_is_treated_as_absent() -> None:
241241
"Not/AZone", # unknown name: ZoneInfoNotFoundError
242242
"America", # a tzdata directory, not a zone: OSError
243243
"../etc/passwd", # outside TZPATH: ValueError
244+
"europe/warsaw", # not the exact key, on any filesystem (#5060)
244245
],
245246
)
246247
async def test_invalid_local_timezone_fails_before_the_transport_opens(

‎src/time/tests/test_server.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
from unittest.mock import patch
55
from zoneinfo import ZoneInfo
66

7-
from mcp_server_time.server import TimeServer, get_local_tz
7+
from mcp_server_time.server import (
8+
TimeServer,
9+
get_local_tz,
10+
get_zoneinfo,
11+
known_timezones,
12+
)
813

914

1015
@pytest.mark.parametrize(
@@ -525,3 +530,17 @@ def test_get_local_tz_various_timezones(mock_get_localzone, timezone_name):
525530
result = get_local_tz()
526531
assert str(result) == timezone_name
527532
assert isinstance(result, ZoneInfo)
533+
534+
535+
def test_get_zoneinfo_rejects_a_key_the_tz_database_does_not_list():
536+
# #5060: on a case-insensitive filesystem `ZoneInfo` resolves a key that
537+
# is not an exact IANA name. Simulate that on any platform: `ZoneInfo`
538+
# accepts "Europe/Warsaw", but the database's key list does not hold it.
539+
without_warsaw = known_timezones() - {"Europe/Warsaw"}
540+
with patch("mcp_server_time.server.known_timezones", return_value=without_warsaw):
541+
with pytest.raises(
542+
McpError,
543+
match="Invalid timezone: 'No time zone found with key Europe/Warsaw'",
544+
):
545+
get_zoneinfo("Europe/Warsaw")
546+
assert str(get_zoneinfo("Europe/Warsaw")) == "Europe/Warsaw"

0 commit comments

Comments
 (0)