mcp-server-time 0.5.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from .server import serve
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def main():
|
|
5
|
+
"""MCP Time Server - Time and timezone conversion functionality for MCP"""
|
|
6
|
+
import argparse
|
|
7
|
+
import asyncio
|
|
8
|
+
|
|
9
|
+
parser = argparse.ArgumentParser(
|
|
10
|
+
description="give a model the ability to handle time queries and timezone conversions"
|
|
11
|
+
)
|
|
12
|
+
parser.add_argument("--local-timezone", type=str, help="Override local timezone")
|
|
13
|
+
|
|
14
|
+
args = parser.parse_args()
|
|
15
|
+
asyncio.run(serve(args.local_timezone))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
if __name__ == "__main__":
|
|
19
|
+
main()
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
from datetime import datetime, timedelta
|
|
2
|
+
from enum import Enum
|
|
3
|
+
import json
|
|
4
|
+
from typing import Sequence
|
|
5
|
+
|
|
6
|
+
from zoneinfo import ZoneInfo
|
|
7
|
+
from mcp.server import Server
|
|
8
|
+
from mcp.server.stdio import stdio_server
|
|
9
|
+
from mcp.types import Tool, TextContent, ImageContent, EmbeddedResource
|
|
10
|
+
from mcp.shared.exceptions import McpError
|
|
11
|
+
|
|
12
|
+
from pydantic import BaseModel
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TimeTools(str, Enum):
|
|
16
|
+
GET_CURRENT_TIME = "get_current_time"
|
|
17
|
+
CONVERT_TIME = "convert_time"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class TimeResult(BaseModel):
|
|
21
|
+
timezone: str
|
|
22
|
+
datetime: str
|
|
23
|
+
is_dst: bool
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class TimeConversionResult(BaseModel):
|
|
27
|
+
source: TimeResult
|
|
28
|
+
target: TimeResult
|
|
29
|
+
time_difference: str
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class TimeConversionInput(BaseModel):
|
|
33
|
+
source_tz: str
|
|
34
|
+
time: str
|
|
35
|
+
target_tz_list: list[str]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo:
|
|
39
|
+
if local_tz_override:
|
|
40
|
+
return ZoneInfo(local_tz_override)
|
|
41
|
+
|
|
42
|
+
# Get local timezone from datetime.now()
|
|
43
|
+
tzinfo = datetime.now().astimezone(tz=None).tzinfo
|
|
44
|
+
if tzinfo is not None:
|
|
45
|
+
return ZoneInfo(str(tzinfo))
|
|
46
|
+
raise McpError("Could not determine local timezone - tzinfo is None")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def get_zoneinfo(timezone_name: str) -> ZoneInfo:
|
|
50
|
+
try:
|
|
51
|
+
return ZoneInfo(timezone_name)
|
|
52
|
+
except Exception as e:
|
|
53
|
+
raise McpError(f"Invalid timezone: {str(e)}")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class TimeServer:
|
|
57
|
+
def get_current_time(self, timezone_name: str) -> TimeResult:
|
|
58
|
+
"""Get current time in specified timezone"""
|
|
59
|
+
timezone = get_zoneinfo(timezone_name)
|
|
60
|
+
current_time = datetime.now(timezone)
|
|
61
|
+
|
|
62
|
+
return TimeResult(
|
|
63
|
+
timezone=timezone_name,
|
|
64
|
+
datetime=current_time.isoformat(timespec="seconds"),
|
|
65
|
+
is_dst=bool(current_time.dst()),
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def convert_time(
|
|
69
|
+
self, source_tz: str, time_str: str, target_tz: str
|
|
70
|
+
) -> TimeConversionResult:
|
|
71
|
+
"""Convert time between timezones"""
|
|
72
|
+
source_timezone = get_zoneinfo(source_tz)
|
|
73
|
+
target_timezone = get_zoneinfo(target_tz)
|
|
74
|
+
|
|
75
|
+
try:
|
|
76
|
+
parsed_time = datetime.strptime(time_str, "%H:%M").time()
|
|
77
|
+
except ValueError:
|
|
78
|
+
raise ValueError("Invalid time format. Expected HH:MM [24-hour format]")
|
|
79
|
+
|
|
80
|
+
now = datetime.now(source_timezone)
|
|
81
|
+
source_time = datetime(
|
|
82
|
+
now.year,
|
|
83
|
+
now.month,
|
|
84
|
+
now.day,
|
|
85
|
+
parsed_time.hour,
|
|
86
|
+
parsed_time.minute,
|
|
87
|
+
tzinfo=source_timezone,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
target_time = source_time.astimezone(target_timezone)
|
|
91
|
+
source_offset = source_time.utcoffset() or timedelta()
|
|
92
|
+
target_offset = target_time.utcoffset() or timedelta()
|
|
93
|
+
hours_difference = (target_offset - source_offset).total_seconds() / 3600
|
|
94
|
+
|
|
95
|
+
if hours_difference.is_integer():
|
|
96
|
+
time_diff_str = f"{hours_difference:+.1f}h"
|
|
97
|
+
else:
|
|
98
|
+
# For fractional hours like Nepal's UTC+5:45
|
|
99
|
+
time_diff_str = f"{hours_difference:+.2f}".rstrip("0").rstrip(".") + "h"
|
|
100
|
+
|
|
101
|
+
return TimeConversionResult(
|
|
102
|
+
source=TimeResult(
|
|
103
|
+
timezone=source_tz,
|
|
104
|
+
datetime=source_time.isoformat(timespec="seconds"),
|
|
105
|
+
is_dst=bool(source_time.dst()),
|
|
106
|
+
),
|
|
107
|
+
target=TimeResult(
|
|
108
|
+
timezone=target_tz,
|
|
109
|
+
datetime=target_time.isoformat(timespec="seconds"),
|
|
110
|
+
is_dst=bool(target_time.dst()),
|
|
111
|
+
),
|
|
112
|
+
time_difference=time_diff_str,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
async def serve(local_timezone: str | None = None) -> None:
|
|
117
|
+
server = Server("mcp-time")
|
|
118
|
+
time_server = TimeServer()
|
|
119
|
+
local_tz = str(get_local_tz(local_timezone))
|
|
120
|
+
|
|
121
|
+
@server.list_tools()
|
|
122
|
+
async def list_tools() -> list[Tool]:
|
|
123
|
+
"""List available time tools."""
|
|
124
|
+
return [
|
|
125
|
+
Tool(
|
|
126
|
+
name=TimeTools.GET_CURRENT_TIME.value,
|
|
127
|
+
description="Get current time in a specific timezones",
|
|
128
|
+
inputSchema={
|
|
129
|
+
"type": "object",
|
|
130
|
+
"properties": {
|
|
131
|
+
"timezone": {
|
|
132
|
+
"type": "string",
|
|
133
|
+
"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.",
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
"required": ["timezone"],
|
|
137
|
+
},
|
|
138
|
+
),
|
|
139
|
+
Tool(
|
|
140
|
+
name=TimeTools.CONVERT_TIME.value,
|
|
141
|
+
description="Convert time between timezones",
|
|
142
|
+
inputSchema={
|
|
143
|
+
"type": "object",
|
|
144
|
+
"properties": {
|
|
145
|
+
"source_timezone": {
|
|
146
|
+
"type": "string",
|
|
147
|
+
"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.",
|
|
148
|
+
},
|
|
149
|
+
"time": {
|
|
150
|
+
"type": "string",
|
|
151
|
+
"description": "Time to convert in 24-hour format (HH:MM)",
|
|
152
|
+
},
|
|
153
|
+
"target_timezone": {
|
|
154
|
+
"type": "string",
|
|
155
|
+
"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.",
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
"required": ["source_timezone", "time", "target_timezone"],
|
|
159
|
+
},
|
|
160
|
+
),
|
|
161
|
+
]
|
|
162
|
+
|
|
163
|
+
@server.call_tool()
|
|
164
|
+
async def call_tool(
|
|
165
|
+
name: str, arguments: dict
|
|
166
|
+
) -> Sequence[TextContent | ImageContent | EmbeddedResource]:
|
|
167
|
+
"""Handle tool calls for time queries."""
|
|
168
|
+
try:
|
|
169
|
+
match name:
|
|
170
|
+
case TimeTools.GET_CURRENT_TIME.value:
|
|
171
|
+
timezone = arguments.get("timezone")
|
|
172
|
+
if not timezone:
|
|
173
|
+
raise ValueError("Missing required argument: timezone")
|
|
174
|
+
|
|
175
|
+
result = time_server.get_current_time(timezone)
|
|
176
|
+
|
|
177
|
+
case TimeTools.CONVERT_TIME.value:
|
|
178
|
+
if not all(
|
|
179
|
+
k in arguments
|
|
180
|
+
for k in ["source_timezone", "time", "target_timezone"]
|
|
181
|
+
):
|
|
182
|
+
raise ValueError("Missing required arguments")
|
|
183
|
+
|
|
184
|
+
result = time_server.convert_time(
|
|
185
|
+
arguments["source_timezone"],
|
|
186
|
+
arguments["time"],
|
|
187
|
+
arguments["target_timezone"],
|
|
188
|
+
)
|
|
189
|
+
case _:
|
|
190
|
+
raise ValueError(f"Unknown tool: {name}")
|
|
191
|
+
|
|
192
|
+
return [
|
|
193
|
+
TextContent(type="text", text=json.dumps(result.model_dump(), indent=2))
|
|
194
|
+
]
|
|
195
|
+
|
|
196
|
+
except Exception as e:
|
|
197
|
+
raise ValueError(f"Error processing mcp-server-time query: {str(e)}")
|
|
198
|
+
|
|
199
|
+
options = server.create_initialization_options()
|
|
200
|
+
async with stdio_server() as (read_stream, write_stream):
|
|
201
|
+
await server.run(read_stream, write_stream, options)
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: mcp-server-time
|
|
3
|
+
Version: 0.5.1
|
|
4
|
+
Summary: A Model Context Protocol server providing tools for time queries and timezone conversions for LLMs
|
|
5
|
+
Author-email: Mariusz 'maledorak' Korzekwa <mariusz@korzekwa.dev>
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: llm,mcp,time,timezone
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Requires-Dist: mcp>=1.0.0
|
|
15
|
+
Requires-Dist: pydantic>=2.0.0
|
|
16
|
+
Requires-Dist: tzdata>=2024.2
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# Time MCP Server
|
|
20
|
+
|
|
21
|
+
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.
|
|
22
|
+
|
|
23
|
+
### Available Tools
|
|
24
|
+
|
|
25
|
+
- `get_current_time` - Get current time in a specific timezone or system timezone.
|
|
26
|
+
- Required arguments:
|
|
27
|
+
- `timezone` (string): IANA timezone name (e.g., 'America/New_York', 'Europe/London')
|
|
28
|
+
|
|
29
|
+
- `convert_time` - Convert time between timezones.
|
|
30
|
+
- Required arguments:
|
|
31
|
+
- `source_timezone` (string): Source IANA timezone name
|
|
32
|
+
- `time` (string): Time in 24-hour format (HH:MM)
|
|
33
|
+
- `target_timezone` (string): Target IANA timezone name
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
### Using uv (recommended)
|
|
38
|
+
|
|
39
|
+
When using [`uv`](https://docs.astral.sh/uv/) no specific installation is needed. We will
|
|
40
|
+
use [`uvx`](https://docs.astral.sh/uv/guides/tools/) to directly run *mcp-server-time*.
|
|
41
|
+
|
|
42
|
+
### Using PIP
|
|
43
|
+
|
|
44
|
+
Alternatively you can install `mcp-server-time` via pip:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install mcp-server-time
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
After installation, you can run it as a script using:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
python -m mcp_server_time
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Configuration
|
|
57
|
+
|
|
58
|
+
### Configure for Claude.app
|
|
59
|
+
|
|
60
|
+
Add to your Claude settings:
|
|
61
|
+
|
|
62
|
+
<details>
|
|
63
|
+
<summary>Using uvx</summary>
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
"mcpServers": {
|
|
67
|
+
"time": {
|
|
68
|
+
"command": "uvx",
|
|
69
|
+
"args": ["mcp-server-time"]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
</details>
|
|
74
|
+
|
|
75
|
+
<details>
|
|
76
|
+
<summary>Using pip installation</summary>
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
"mcpServers": {
|
|
80
|
+
"time": {
|
|
81
|
+
"command": "python",
|
|
82
|
+
"args": ["-m", "mcp_server_time"]
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
</details>
|
|
87
|
+
|
|
88
|
+
### Configure for Zed
|
|
89
|
+
|
|
90
|
+
Add to your Zed settings.json:
|
|
91
|
+
|
|
92
|
+
<details>
|
|
93
|
+
<summary>Using uvx</summary>
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
"context_servers": [
|
|
97
|
+
"mcp-server-time": {
|
|
98
|
+
"command": "uvx",
|
|
99
|
+
"args": ["mcp-server-time"]
|
|
100
|
+
}
|
|
101
|
+
],
|
|
102
|
+
```
|
|
103
|
+
</details>
|
|
104
|
+
|
|
105
|
+
<details>
|
|
106
|
+
<summary>Using pip installation</summary>
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
"context_servers": {
|
|
110
|
+
"mcp-server-time": {
|
|
111
|
+
"command": "python",
|
|
112
|
+
"args": ["-m", "mcp_server_time"]
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
```
|
|
116
|
+
</details>
|
|
117
|
+
|
|
118
|
+
### Customization - System Timezone
|
|
119
|
+
|
|
120
|
+
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.
|
|
121
|
+
|
|
122
|
+
Example:
|
|
123
|
+
```json
|
|
124
|
+
{
|
|
125
|
+
"command": "python",
|
|
126
|
+
"args": ["-m", "mcp_server_time", "--local-timezone=America/New_York"]
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Example Interactions
|
|
131
|
+
|
|
132
|
+
1. Get current time:
|
|
133
|
+
```json
|
|
134
|
+
{
|
|
135
|
+
"name": "get_current_time",
|
|
136
|
+
"arguments": {
|
|
137
|
+
"timezone": "Europe/Warsaw"
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
Response:
|
|
142
|
+
```json
|
|
143
|
+
{
|
|
144
|
+
"timezone": "Europe/Warsaw",
|
|
145
|
+
"datetime": "2024-01-01T13:00:00+01:00",
|
|
146
|
+
"is_dst": false
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
2. Convert time between timezones:
|
|
151
|
+
```json
|
|
152
|
+
{
|
|
153
|
+
"name": "convert_time",
|
|
154
|
+
"arguments": {
|
|
155
|
+
"source_timezone": "America/New_York",
|
|
156
|
+
"time": "16:30",
|
|
157
|
+
"target_timezone": "Asia/Tokyo"
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
Response:
|
|
162
|
+
```json
|
|
163
|
+
{
|
|
164
|
+
"source": {
|
|
165
|
+
"timezone": "America/New_York",
|
|
166
|
+
"datetime": "2024-01-01T12:30:00-05:00",
|
|
167
|
+
"is_dst": false
|
|
168
|
+
},
|
|
169
|
+
"target": {
|
|
170
|
+
"timezone": "Asia/Tokyo",
|
|
171
|
+
"datetime": "2024-01-01T12:30:00+09:00",
|
|
172
|
+
"is_dst": false
|
|
173
|
+
},
|
|
174
|
+
"time_difference": "+13.0h",
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Debugging
|
|
179
|
+
|
|
180
|
+
You can use the MCP inspector to debug the server. For uvx installations:
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
npx @modelcontextprotocol/inspector uvx mcp-server-time
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Or if you've installed the package in a specific directory or are developing on it:
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
cd path/to/servers/src/time
|
|
190
|
+
npx @modelcontextprotocol/inspector uv run mcp-server-time
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Examples of Questions for Claude
|
|
194
|
+
|
|
195
|
+
1. "What time is it now?" (will use system timezone)
|
|
196
|
+
2. "What time is it in Tokyo?"
|
|
197
|
+
3. "When it's 4 PM in New York, what time is it in London?"
|
|
198
|
+
4. "Convert 9:30 AM Tokyo time to New York time"
|
|
199
|
+
|
|
200
|
+
## Contributing
|
|
201
|
+
|
|
202
|
+
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.
|
|
203
|
+
|
|
204
|
+
For examples of other MCP servers and implementation patterns, see:
|
|
205
|
+
https://github.com/modelcontextprotocol/servers
|
|
206
|
+
|
|
207
|
+
Pull requests are welcome! Feel free to contribute new ideas, bug fixes, or enhancements to make mcp-server-time even more powerful and useful.
|
|
208
|
+
|
|
209
|
+
## License
|
|
210
|
+
|
|
211
|
+
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.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
mcp_server_time/__init__.py,sha256=VKnEmf4xCekty3To3ieP8c2QPuMWpwG0Wqzj_CMv8S0,500
|
|
2
|
+
mcp_server_time/__main__.py,sha256=FDLumTMOiCP_XQ7s9oPpcR-_7pIpotjYesP4Mrcspsc,41
|
|
3
|
+
mcp_server_time/server.py,sha256=qLzQArhLDa-nb60JjtweZjQNAr1nA9HyAu83AXA2dAk,7170
|
|
4
|
+
mcp_server_time-0.5.1.dist-info/METADATA,sha256=DXvG8RjNgmevsBhcOOpHwbNpPaya0NT5zD-HhX3hm08,5059
|
|
5
|
+
mcp_server_time-0.5.1.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
6
|
+
mcp_server_time-0.5.1.dist-info/entry_points.txt,sha256=AUO7UvtOMJ-7nnOkgz-ZuEZ3xzmWLgtRmuzAr1L9Gxs,57
|
|
7
|
+
mcp_server_time-0.5.1.dist-info/RECORD,,
|