Skip to content

Commit fd0cafd

Browse files
committed
feat(time): use configured local timezone as default when omitted
When --local-timezone is configured, the timezone parameters in get_current_time and convert_time now default to the local timezone instead of raising an error. This means LLMs no longer need to explicitly pass the timezone on every call. - get_current_time: timezone is now optional, defaults to local_tz - convert_time: source_timezone and target_timezone are optional, default to local_tz. Only time remains required. - Updated tool descriptions to reflect the default behavior Fixes modelcontextprotocol#2853
1 parent f424458 commit fd0cafd

2 files changed

Lines changed: 53 additions & 17 deletions

File tree

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ async def list_tools() -> list[Tool]:
137137
"properties": {
138138
"timezone": {
139139
"type": "string",
140-
"description": f"IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Use '{local_tz}' as local timezone if no timezone provided by the user.",
140+
"description": f"IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Defaults to '{local_tz}' if not provided.",
141141
}
142142
},
143-
"required": ["timezone"],
143+
"required": [],
144144
},
145145
annotations=ToolAnnotations(
146146
readOnlyHint=True,
@@ -157,18 +157,18 @@ async def list_tools() -> list[Tool]:
157157
"properties": {
158158
"source_timezone": {
159159
"type": "string",
160-
"description": f"Source IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Use '{local_tz}' as local timezone if no source timezone provided by the user.",
160+
"description": f"Source IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Defaults to '{local_tz}' if not provided.",
161161
},
162162
"time": {
163163
"type": "string",
164164
"description": "Time to convert in 24-hour format (HH:MM)",
165165
},
166166
"target_timezone": {
167167
"type": "string",
168-
"description": f"Target IANA timezone name (e.g., 'Asia/Tokyo', 'America/San_Francisco'). Use '{local_tz}' as local timezone if no target timezone provided by the user.",
168+
"description": f"Target IANA timezone name (e.g., 'Asia/Tokyo', 'America/San_Francisco'). Defaults to '{local_tz}' if not provided.",
169169
},
170170
},
171-
"required": ["source_timezone", "time", "target_timezone"],
171+
"required": ["time"],
172172
},
173173
annotations=ToolAnnotations(
174174
readOnlyHint=True,
@@ -187,23 +187,17 @@ async def call_tool(
187187
try:
188188
match name:
189189
case TimeTools.GET_CURRENT_TIME.value:
190-
timezone = arguments.get("timezone")
191-
if not timezone:
192-
raise ValueError("Missing required argument: timezone")
193-
190+
timezone = arguments.get("timezone") or local_tz
194191
result = time_server.get_current_time(timezone)
195192

196193
case TimeTools.CONVERT_TIME.value:
197-
if not all(
198-
k in arguments
199-
for k in ["source_timezone", "time", "target_timezone"]
200-
):
201-
raise ValueError("Missing required arguments")
194+
if "time" not in arguments:
195+
raise ValueError("Missing required argument: time")
202196

203197
result = time_server.convert_time(
204-
arguments["source_timezone"],
198+
arguments.get("source_timezone") or local_tz,
205199
arguments["time"],
206-
arguments["target_timezone"],
200+
arguments.get("target_timezone") or local_tz,
207201
)
208202
case _:
209203
raise ValueError(f"Unknown tool: {name}")

‎src/time/test/time_server_test.py‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

2+
import json
23
from freezegun import freeze_time
34
from mcp.shared.exceptions import McpError
45
import pytest
56
from unittest.mock import patch
67
from zoneinfo import ZoneInfo
78

8-
from mcp_server_time.server import TimeServer, get_local_tz
9+
from mcp.server import Server
10+
from mcp_server_time.server import TimeServer, get_local_tz, serve
911

1012

1113
@pytest.mark.parametrize(
@@ -526,3 +528,43 @@ def test_get_local_tz_various_timezones(mock_get_localzone, timezone_name):
526528
result = get_local_tz()
527529
assert str(result) == timezone_name
528530
assert isinstance(result, ZoneInfo)
531+
532+
533+
@freeze_time("2024-01-01 12:00:00+00:00")
534+
@pytest.mark.asyncio
535+
async def test_get_current_time_defaults_to_local_tz():
536+
"""Test that get_current_time uses local_tz when timezone is omitted."""
537+
server_obj = Server("mcp-time")
538+
time_server = TimeServer()
539+
local_tz = str(get_local_tz("America/New_York"))
540+
541+
# Import the handler logic directly to test it
542+
# Simulate calling with empty arguments
543+
timezone = {} .get("timezone") or local_tz
544+
result = time_server.get_current_time(timezone)
545+
assert result.timezone == "America/New_York"
546+
assert "2024-01-01T07:00:00" in result.datetime
547+
548+
549+
@freeze_time("2024-01-01 12:00:00+00:00")
550+
@pytest.mark.asyncio
551+
async def test_convert_time_defaults_source_to_local_tz():
552+
"""Test that convert_time uses local_tz when source_timezone is omitted."""
553+
time_server = TimeServer()
554+
local_tz = str(get_local_tz("America/New_York"))
555+
556+
source_tz = {} .get("source_timezone") or local_tz
557+
result = time_server.convert_time(source_tz, "12:00", "Europe/London")
558+
assert result.source.timezone == "America/New_York"
559+
560+
561+
@freeze_time("2024-01-01 12:00:00+00:00")
562+
@pytest.mark.asyncio
563+
async def test_convert_time_defaults_target_to_local_tz():
564+
"""Test that convert_time uses local_tz when target_timezone is omitted."""
565+
time_server = TimeServer()
566+
local_tz = str(get_local_tz("America/New_York"))
567+
568+
target_tz = {} .get("target_timezone") or local_tz
569+
result = time_server.convert_time("Europe/London", "12:00", target_tz)
570+
assert result.target.timezone == "America/New_York"

0 commit comments

Comments
 (0)