Skip to content

Commit cb01940

Browse files
cliffhallclaude
andcommitted
fix(time): make get_time_ago's offsets strict integers
Lax validation turned "2" and true into offsets, so a malformed call produced a cutoff the caller never asked for. A fraction, numeric string or boolean is now rejected, as the integer schema says. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 44d3659 commit cb01940

3 files changed

Lines changed: 12 additions & 9 deletions

File tree

‎src/time/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Requires MCP Python SDK 2.x (`mcp>=2.3,<3`).
2323
- `get_time_ago` - Get the time a given number of days, hours, minutes and seconds ago in a specific timezone, for questions such as "what happened in the last 5 hours?".
2424
- Optional arguments:
2525
- `timezone` (string): IANA timezone name; defaults to the server's local timezone
26-
- `days_ago`, `hours_ago`, `minutes_ago`, `seconds_ago` (non-negative integers, default 0): how far back to go. They add up, so two and a half hours ago is `hours_ago: 2, minutes_ago: 30`.
26+
- `days_ago`, `hours_ago`, `minutes_ago`, `seconds_ago` (non-negative JSON integers, default 0; a fraction, numeric string or boolean is rejected): how far back to go. They add up, so two and a half hours ago is `hours_ago: 2, minutes_ago: 30`.
2727
- The offset is elapsed time, counted back from the current instant, so across a daylight-saving change "a day ago" is exactly 24 hours ago and its wall-clock reading can differ from now by an hour. An offset reaching before year 1 is an error.
2828

2929
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.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,17 @@ def get_time_ago(
310310
description=f"IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Optional: defaults to the server's local timezone, '{local_tz}', when omitted."
311311
),
312312
] = local_tz,
313-
days_ago: Annotated[int, Field(description="Whole days before now", ge=0)] = 0,
313+
days_ago: Annotated[
314+
int, Field(description="Whole days before now", ge=0, strict=True)
315+
] = 0,
314316
hours_ago: Annotated[
315-
int, Field(description="Whole hours before now", ge=0)
317+
int, Field(description="Whole hours before now", ge=0, strict=True)
316318
] = 0,
317319
minutes_ago: Annotated[
318-
int, Field(description="Whole minutes before now", ge=0)
320+
int, Field(description="Whole minutes before now", ge=0, strict=True)
319321
] = 0,
320322
seconds_ago: Annotated[
321-
int, Field(description="Whole seconds before now", ge=0)
323+
int, Field(description="Whole seconds before now", ge=0, strict=True)
322324
] = 0,
323325
) -> TimeResult:
324326
require("timezone", timezone)

‎src/time/tests/test_protocol.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,10 +1093,11 @@ async def test_get_time_ago_echoes_the_requested_name_not_a_canonical_one() -> N
10931093
{"hours_ago": -1},
10941094
["hours_ago\n Input should be greater than or equal to 0"],
10951095
),
1096-
(
1097-
{"days_ago": 1.5},
1098-
["days_ago\n Input should be a valid integer, got a number with a"],
1099-
),
1096+
# The offsets are strict integers: no fraction, numeric string or
1097+
# boolean is coerced into a different cutoff.
1098+
({"days_ago": 1.5}, ["days_ago\n Input should be a valid integer"]),
1099+
({"hours_ago": "2"}, ["hours_ago\n Input should be a valid integer"]),
1100+
({"hours_ago": True}, ["hours_ago\n Input should be a valid integer"]),
11001101
({"minutes_ago": "ten"}, ["minutes_ago\n Input should be a valid integer"]),
11011102
({"seconds_ago": None}, ["seconds_ago\n Input should be a valid integer"]),
11021103
({"timezone": None}, ["timezone\n Input should be a valid string"]),

0 commit comments

Comments
 (0)