Skip to content

Commit b96efe6

Browse files
committed
fix(time): default missing timezones to local timezone
1 parent b1e1eb1 commit b96efe6

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

src/time/src/mcp_server_time/server.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ def get_zoneinfo(timezone_name: str) -> ZoneInfo:
5858

5959

6060
class TimeServer:
61-
def get_current_time(self, timezone_name: str) -> TimeResult:
61+
def __init__(self, local_timezone: str | None = None):
62+
self.local_timezone = str(get_local_tz(local_timezone))
63+
64+
def get_current_time(self, timezone_name: str | None = None) -> TimeResult:
6265
"""Get current time in specified timezone"""
66+
timezone_name = timezone_name or self.local_timezone
6367
timezone = get_zoneinfo(timezone_name)
6468
current_time = datetime.now(timezone)
6569

@@ -71,9 +75,11 @@ def get_current_time(self, timezone_name: str) -> TimeResult:
7175
)
7276

7377
def convert_time(
74-
self, source_tz: str, time_str: str, target_tz: str
78+
self, source_tz: str | None, time_str: str, target_tz: str | None
7579
) -> TimeConversionResult:
7680
"""Convert time between timezones"""
81+
source_tz = source_tz or self.local_timezone
82+
target_tz = target_tz or self.local_timezone
7783
source_timezone = get_zoneinfo(source_tz)
7884
target_timezone = get_zoneinfo(target_tz)
7985

@@ -122,8 +128,8 @@ def convert_time(
122128

123129
async def serve(local_timezone: str | None = None) -> None:
124130
server = Server("mcp-time")
125-
time_server = TimeServer()
126-
local_tz = str(get_local_tz(local_timezone))
131+
time_server = TimeServer(local_timezone)
132+
local_tz = time_server.local_timezone
127133

128134
@server.list_tools()
129135
async def list_tools() -> list[Tool]:
@@ -140,7 +146,7 @@ async def list_tools() -> list[Tool]:
140146
"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.",
141147
}
142148
},
143-
"required": ["timezone"],
149+
"required": [],
144150
},
145151
annotations=ToolAnnotations(
146152
readOnlyHint=True,
@@ -168,7 +174,7 @@ async def list_tools() -> list[Tool]:
168174
"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.",
169175
},
170176
},
171-
"required": ["source_timezone", "time", "target_timezone"],
177+
"required": ["time"],
172178
},
173179
annotations=ToolAnnotations(
174180
readOnlyHint=True,
@@ -188,22 +194,16 @@ async def call_tool(
188194
match name:
189195
case TimeTools.GET_CURRENT_TIME.value:
190196
timezone = arguments.get("timezone")
191-
if not timezone:
192-
raise ValueError("Missing required argument: timezone")
193-
194197
result = time_server.get_current_time(timezone)
195198

196199
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")
200+
if "time" not in arguments:
201+
raise ValueError("Missing required argument: time")
202202

203203
result = time_server.convert_time(
204-
arguments["source_timezone"],
204+
arguments.get("source_timezone"),
205205
arguments["time"],
206-
arguments["target_timezone"],
206+
arguments.get("target_timezone"),
207207
)
208208
case _:
209209
raise ValueError(f"Unknown tool: {name}")

src/time/test/time_server_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ def test_get_current_time_with_invalid_timezone():
9191
time_server.get_current_time("Invalid/Timezone")
9292

9393

94+
def test_get_current_time_defaults_to_configured_local_timezone():
95+
with freeze_time("2024-01-01 12:00:00+00:00"):
96+
time_server = TimeServer("Europe/London")
97+
98+
result = time_server.get_current_time()
99+
100+
assert result.timezone == "Europe/London"
101+
assert result.datetime == "2024-01-01T12:00:00+00:00"
102+
103+
104+
def test_convert_time_defaults_missing_timezones_to_configured_local_timezone():
105+
with freeze_time("2024-01-01 00:00:00+00:00"):
106+
time_server = TimeServer("Europe/London")
107+
108+
result = time_server.convert_time(None, "12:00", "Asia/Tokyo")
109+
110+
assert result.source.timezone == "Europe/London"
111+
assert result.source.datetime == "2024-01-01T12:00:00+00:00"
112+
assert result.target.timezone == "Asia/Tokyo"
113+
assert result.target.datetime == "2024-01-01T21:00:00+09:00"
114+
115+
94116
@pytest.mark.parametrize(
95117
"source_tz,time_str,target_tz,expected_error",
96118
[

0 commit comments

Comments
 (0)