time_tool
Convert and format timestamps, manage timezones, and retrieve current time in ISO, local, or custom formats. Ideal for time-based data processing and synchronization tasks.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | Time format (iso, timestamp, local, custom) | |
| pattern | No | Custom format pattern (required if format=custom) | |
| targetTimezone | No | Target timezone for timestamp conversion (e.g., America/New_York) | |
| timestamp | No | Timestamp to convert | |
| timezone | No | Timezone (e.g., Asia/Shanghai) |
Implementation Reference
- src/tools/time_tool.ts:91-140 (handler)The primary handler function for the time_tool. It extracts parameters from the request, dispatches to specific action handlers (get_current_time, format_time, from_timestamp, to_timestamp), manages errors, and formats the response according to MCP protocol.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 of all tools, including 'time_tool', by scanning src/tools directory, importing each tool module, extracting schema and handler, and adding to global tools list and handlers map. Tool name derived from filename.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) {