time_tool
Get current time, convert between time formats and timezones, or work with timestamps using ISO 8601 strings and Unix timestamps.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | The operation to perform. | |
| time_str | No | An ISO 8601 time string (e.g., '2025-03-15T10:00:00Z'). Required for 'format_time' and 'to_timestamp'. | |
| timezone | No | The target timezone (e.g., 'UTC', 'America/New_York', 'Asia/Shanghai'). | |
| timestamp | No | Unix timestamp in milliseconds. Required for 'from_timestamp'. | |
| format_options | No | Formatting options for the time string, based on Intl.DateTimeFormat. |
Implementation Reference
- src/tools/time_tool.ts:91-140 (handler)The primary handler function for the 'time_tool'. It processes the request parameters, dispatches to specific action handlers (get_current_time, format_time, from_timestamp, to_timestamp), and returns a standardized MCP response or error.export default async (request: any) => { try { const { action, time_str, timezone, timestamp, format_options } = request.params.arguments; let result: string | number; switch (action) { case "get_current_time": result = await getCurrentTime(timezone, format_options); break; case "format_time": if (!time_str) throw new Error("time_str is required for format_time"); result = await formatTime(time_str, timezone, format_options); break; case "from_timestamp": if (timestamp === undefined) throw new Error("timestamp is required for from_timestamp"); result = await fromTimestamp(timestamp, timezone, format_options); break; case "to_timestamp": if (!time_str) throw new Error("time_str is required for to_timestamp"); result = await toTimestamp(time_str); break; default: throw new Error(`Invalid action: ${action}`); } return { content: [ { type: "text", text: JSON.stringify({ result }, null, 2), }, ], }; } catch (error: any) { return { content: [ { type: "text", text: JSON.stringify({ error: error.message }, null, 2), }, ], isError: true }; } };
- src/tools/time_tool.ts:12-49 (schema)JSON schema defining the input parameters for the time_tool, including actions, time strings, timezones, timestamps, and formatting options.export const schema = { name: "time_tool", description: "Get and convert time, supporting timezones, formatting, and timestamp operations.", type: "object", properties: { action: { type: "string", description: "The operation to perform.", enum: ["get_current_time", "format_time", "from_timestamp", "to_timestamp"], }, time_str: { type: "string", description: "An ISO 8601 time string (e.g., '2025-03-15T10:00:00Z'). Required for 'format_time' and 'to_timestamp'." }, timezone: { type: "string", description: "The target timezone (e.g., 'UTC', 'America/New_York', 'Asia/Shanghai')." }, timestamp: { type: "number", description: "Unix timestamp in milliseconds. Required for 'from_timestamp'." }, format_options: { type: "object", description: "Formatting options for the time string, based on Intl.DateTimeFormat.", properties: { year: { type: "string", enum: ["numeric", "2-digit"] }, month: { type: "string", enum: ["numeric", "2-digit", "long", "short", "narrow"] }, day: { type: "string", enum: ["numeric", "2-digit"] }, hour: { type: "string", enum: ["numeric", "2-digit"] }, minute: { type: "string", enum: ["numeric", "2-digit"] }, second: { type: "string", enum: ["numeric", "2-digit"] }, timeZoneName: { type: "string", enum: ["long", "short"] } } } }, required: ["action"] };
- src/handler/ToolHandler.ts:109-130 (registration)Dynamic registration loop in loadTools function that scans src/tools directory, imports time_tool.ts (deriving name 'time_tool' from filename), registers its schema and default handler in global tools and handlers maps.for (const file of toolFiles) { const toolPath = path.join(toolsDir, file); try { // 如果是重新加载,清除模块缓存 if (reload) clearModuleCache(toolPath); // 导入模块,重新加载时添加时间戳防止缓存 const importPath = 'file://' + toolPath + (reload ? `?update=${Date.now()}` : ''); const { default: tool, schema, destroy } = await import(importPath); const toolName = path.parse(toolPath).name; // 注册工具 tools.push({ name: toolName, description: tool.description, inputSchema: schema, destroy: destroy }); // 注册处理函数 handlers[toolName] = async (request: ToolRequest) => { return await tool(request); }; } catch (error) {
- src/tools/time_tool.ts:2-10 (helper)Utility helper function used by action handlers to validate if a provided timezone is valid.function isValidTimeZone(tz: string) { if (!tz) return false; try { Intl.DateTimeFormat(undefined, { timeZone: tz }); return true; } catch (ex) { return false; } }
- src/tools/time_tool.ts:52-58 (helper)Helper function for 'get_current_time' action: gets current time in specified timezone with optional formatting.async function getCurrentTime(timezone: string | undefined, format_options: object | undefined) { const now = new Date(); if (timezone && !isValidTimeZone(timezone)) { throw new Error(`Invalid timezone: ${timezone}`); } return new Intl.DateTimeFormat('en-US', { ...format_options, timeZone: timezone }).format(now); }