From bb4cabaa7694995ed0fb5662c12639ff98e24cbd Mon Sep 17 00:00:00 2001 From: Michael DeMarco Date: Wed, 19 Mar 2025 00:06:04 -0700 Subject: [PATCH] fix: better support for windows timezones --- src/time/README.md | 121 +++++++++++++------------ src/time/pyproject.toml | 1 + src/time/src/mcp_server_time/server.py | 12 ++- src/time/test/time_server_test.py | 1 - src/time/uv.lock | 17 +++- 5 files changed, 90 insertions(+), 62 deletions(-) diff --git a/src/time/README.md b/src/time/README.md index eed504bb30..7e8e41baa8 100644 --- a/src/time/README.md +++ b/src/time/README.md @@ -2,12 +2,11 @@ A Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection. -### Available Tools +## Available Tools - `get_current_time` - Get current time in a specific timezone or system timezone. - Required arguments: - `timezone` (string): IANA timezone name (e.g., 'America/New_York', 'Europe/London') - - `convert_time` - Convert time between timezones. - Required arguments: - `source_timezone` (string): Source IANA timezone name @@ -16,10 +15,10 @@ A Model Context Protocol server that provides time and timezone conversion capab ## Installation -### Using uv (recommended) +### Using `uv` (recommended) When using [`uv`](https://docs.astral.sh/uv/) no specific installation is needed. We will -use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run *mcp-server-time*. +use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run `mcp-server-time*. ### Using PIP @@ -42,7 +41,7 @@ python -m mcp_server_time Add to your Claude settings:
-Using uvx +Using uvx ```json "mcpServers": { @@ -55,7 +54,7 @@ Add to your Claude settings:
-Using docker +Using Docker ```json "mcpServers": { @@ -68,7 +67,7 @@ Add to your Claude settings:
-Using pip installation +Using pip ```json "mcpServers": { @@ -82,10 +81,10 @@ Add to your Claude settings: ### Configure for Zed -Add to your Zed settings.json: +Add to your Zed `settings.json`:
-Using uvx +Using uvx ```json "context_servers": [ @@ -98,7 +97,7 @@ Add to your Zed settings.json:
-Using pip installation +Using pip ```json "context_servers": { @@ -115,6 +114,7 @@ Add to your Zed settings.json: By default, the server automatically detects your system's timezone. You can override this by adding the argument `--local-timezone` to the `args` list in the configuration. Example: + ```json { "command": "python", @@ -125,54 +125,60 @@ Example: ## Example Interactions 1. Get current time: -```json -{ - "name": "get_current_time", - "arguments": { - "timezone": "Europe/Warsaw" - } -} -``` -Response: -```json -{ - "timezone": "Europe/Warsaw", - "datetime": "2024-01-01T13:00:00+01:00", - "is_dst": false -} -``` + + ```json + { + "name": "get_current_time", + "arguments": { + "timezone": "Europe/Warsaw" + } + } + ``` + + Response: + + ```json + { + "timezone": "Europe/Warsaw", + "datetime": "2024-01-01T13:00:00+01:00", + "is_dst": false + } + ``` 2. Convert time between timezones: -```json -{ - "name": "convert_time", - "arguments": { - "source_timezone": "America/New_York", - "time": "16:30", - "target_timezone": "Asia/Tokyo" - } -} -``` -Response: -```json -{ - "source": { - "timezone": "America/New_York", - "datetime": "2024-01-01T12:30:00-05:00", - "is_dst": false - }, - "target": { - "timezone": "Asia/Tokyo", - "datetime": "2024-01-01T12:30:00+09:00", - "is_dst": false - }, - "time_difference": "+13.0h", -} -``` + + ```json + { + "name": "convert_time", + "arguments": { + "source_timezone": "America/New_York", + "time": "16:30", + "target_timezone": "Asia/Tokyo" + } + } + ``` + + Response: + + ```json + { + "source": { + "timezone": "America/New_York", + "datetime": "2024-01-01T12:30:00-05:00", + "is_dst": false + }, + "target": { + "timezone": "Asia/Tokyo", + "datetime": "2024-01-01T12:30:00+09:00", + "is_dst": false + }, + "time_difference": "+13.0h", + } + ``` ## Debugging -You can use the MCP inspector to debug the server. For uvx installations: +You can use the MCP inspector to debug the server. For `uvx` installations: ```bash npx @modelcontextprotocol/inspector uvx mcp-server-time @@ -203,13 +209,12 @@ docker build -t mcp/time . ## Contributing -We encourage contributions to help expand and improve mcp-server-time. Whether you want to add new time-related tools, enhance existing functionality, or improve documentation, your input is valuable. +We encourage contributions to help expand and improve `mcp-server-time`. Whether you want to add new time-related tools, enhance existing functionality, or improve documentation, your input is valuable. -For examples of other MCP servers and implementation patterns, see: -https://github.com/modelcontextprotocol/servers +For examples of other MCP servers and implementation patterns, see [https://github.com/modelcontextprotocol/servers] -Pull requests are welcome! Feel free to contribute new ideas, bug fixes, or enhancements to make mcp-server-time even more powerful and useful. +Pull requests are welcome! Feel free to contribute new ideas, bug fixes, or enhancements to make `mcp-server-time` even more powerful and useful. ## License -mcp-server-time is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository. +`mcp-server-time` is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository. diff --git a/src/time/pyproject.toml b/src/time/pyproject.toml index 70924951c7..05a76a5eb5 100644 --- a/src/time/pyproject.toml +++ b/src/time/pyproject.toml @@ -20,6 +20,7 @@ dependencies = [ "mcp>=1.0.0", "pydantic>=2.0.0", "tzdata>=2024.2", + "tzlocal>=5.3.1", ] [project.scripts] diff --git a/src/time/src/mcp_server_time/server.py b/src/time/src/mcp_server_time/server.py index 67c9024b52..a937d59736 100644 --- a/src/time/src/mcp_server_time/server.py +++ b/src/time/src/mcp_server_time/server.py @@ -2,7 +2,7 @@ from enum import Enum import json from typing import Sequence - +from tzlocal import get_localzone from zoneinfo import ZoneInfo from mcp.server import Server from mcp.server.stdio import stdio_server @@ -39,10 +39,18 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo: if local_tz_override: return ZoneInfo(local_tz_override) - # Get local timezone from datetime.now() + # First, try to get local timezone from tzlocal + try: + local_tz = get_localzone() + return ZoneInfo(str(local_tz)) + except Exception as e: + pass + + # If that fails, try getting local timezone from datetime.now() tzinfo = datetime.now().astimezone(tz=None).tzinfo if tzinfo is not None: return ZoneInfo(str(tzinfo)) + raise McpError("Could not determine local timezone - tzinfo is None") diff --git a/src/time/test/time_server_test.py b/src/time/test/time_server_test.py index 8129fb5b5f..37f86016a5 100644 --- a/src/time/test/time_server_test.py +++ b/src/time/test/time_server_test.py @@ -1,4 +1,3 @@ - from freezegun import freeze_time from mcp.shared.exceptions import McpError import pytest diff --git a/src/time/uv.lock b/src/time/uv.lock index a8d820abac..47ae3dd5a9 100644 --- a/src/time/uv.lock +++ b/src/time/uv.lock @@ -1,4 +1,5 @@ version = 1 +revision = 1 requires-python = ">=3.10" [[package]] @@ -39,7 +40,7 @@ name = "click" version = "8.1.7" source = { registry = "https://pypi.org/simple" } dependencies = [ - { name = "colorama", marker = "platform_system == 'Windows'" }, + { name = "colorama", marker = "sys_platform == 'win32'" }, ] sdist = { url = "https://files.pythonhosted.org/packages/96/d3/f04c7bfcf5c1862a2a5b845c6b2b360488cf47af55dfa79c98f6a6bf98b5/click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de", size = 336121 } wheels = [ @@ -165,6 +166,7 @@ dependencies = [ { name = "mcp" }, { name = "pydantic" }, { name = "tzdata" }, + { name = "tzlocal" }, ] [package.dev-dependencies] @@ -180,6 +182,7 @@ requires-dist = [ { name = "mcp", specifier = ">=1.0.0" }, { name = "pydantic", specifier = ">=2.0.0" }, { name = "tzdata", specifier = ">=2024.2" }, + { name = "tzlocal", specifier = ">=5.3.1" }, ] [package.metadata.requires-dev] @@ -474,6 +477,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/a6/ab/7e5f53c3b9d14972843a647d8d7a853969a58aecc7559cb3267302c94774/tzdata-2024.2-py2.py3-none-any.whl", hash = "sha256:a48093786cdcde33cad18c2555e8532f34422074448fbc874186f0abd79565cd", size = 346586 }, ] +[[package]] +name = "tzlocal" +version = "5.3.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "tzdata", marker = "sys_platform == 'win32'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/8b/2e/c14812d3d4d9cd1773c6be938f89e5735a1f11a9f184ac3639b93cef35d5/tzlocal-5.3.1.tar.gz", hash = "sha256:cceffc7edecefea1f595541dbd6e990cb1ea3d19bf01b2809f362a03dd7921fd", size = 30761 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/c2/14/e2a54fabd4f08cd7af1c07030603c3356b74da07f7cc056e600436edfa17/tzlocal-5.3.1-py3-none-any.whl", hash = "sha256:eb1a66c3ef5847adf7a834f1be0800581b683b5608e74f86ecbcef8ab91bb85d", size = 18026 }, +] + [[package]] name = "uvicorn" version = "0.32.1"