current_time
Retrieve the current date and time in a specified format and timezone using the time-mcp server. Enhance time-aware applications by providing accurate and customizable time data.
Instructions
Get the current date and time.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | Yes | The format of the time, default is empty string | YYYY-MM-DD HH:mm:ss |
| timezone | No | The timezone of the time, IANA timezone name, e.g. Asia/Shanghai |
Input Schema (JSON Schema)
{
"properties": {
"format": {
"default": "YYYY-MM-DD HH:mm:ss",
"description": "The format of the time, default is empty string",
"enum": [
"h:mm A",
"h:mm:ss A",
"YYYY-MM-DD HH:mm:ss",
"YYYY-MM-DD",
"YYYY-MM",
"MM/DD/YYYY",
"MM/DD/YY",
"YYYY/MM/DD",
"YYYY/MM"
],
"type": "string"
},
"timezone": {
"description": "The timezone of the time, IANA timezone name, e.g. Asia/Shanghai",
"type": "string"
}
},
"required": [
"format"
],
"type": "object"
}
Implementation Reference
- src/index.ts:40-56 (handler)Main handler for the 'current_time' tool call within the CallToolRequestSchema. Validates arguments using checkCurrentTimeArgs, computes the current time using getCurrentTime, and returns formatted response content.case 'current_time': { if (!checkCurrentTimeArgs(args)) { throw new Error(`Invalid arguments for tool: [${name}]`); } const { format, timezone } = args; const result = getCurrentTime(format, timezone); return { success: true, content: [ { type: 'text', text: `Current UTC time is ${result.utc}, and the time in ${result.timezone} is ${result.local}.`, }, ], }; }
- src/tools.ts:3-33 (schema)Defines the Tool object for 'current_time', including name, description, and detailed inputSchema with properties for format and timezone.export const CURRENT_TIME: Tool = { name: 'current_time', description: 'Get the current date and time.', inputSchema: { type: 'object', properties: { format: { type: 'string', description: 'The format of the time, default is empty string', enum: [ 'h:mm A', 'h:mm:ss A', 'YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD', 'YYYY-MM', 'MM/DD/YYYY', 'MM/DD/YY', 'YYYY/MM/DD', 'YYYY/MM', ], default: 'YYYY-MM-DD HH:mm:ss', }, timezone: { type: 'string', description: 'The timezone of the time, IANA timezone name, e.g. Asia/Shanghai', default: undefined, }, }, required: ['format'], }, };
- src/index.ts:30-34 (registration)Registers the 'current_time' tool (via CURRENT_TIME export) in the tools list provided to clients via ListToolsRequest.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [CURRENT_TIME, RELATIVE_TIME, DAYS_IN_MONTH, GET_TIMESTAMP, CONVERT_TIME, GET_WEEK_YEAR], }; });
- src/index.ts:160-169 (helper)Helper function that computes current UTC time and local time in specified or guessed timezone, formatting as requested.function getCurrentTime(format: string, timezone?: string) { const utcTime = dayjs.utc(); const localTimezone = timezone ?? dayjs.tz.guess(); const localTime = dayjs().tz(localTimezone); return { utc: utcTime.format(format), local: localTime.format(format), timezone: localTimezone, }; }
- src/index.ts:203-211 (helper)Type guard helper to validate arguments for the current_time tool.function checkCurrentTimeArgs(args: unknown): args is { format: string, timezone?: string } { return ( typeof args === 'object' && args !== null && 'format' in args && typeof args.format === 'string' && ('timezone' in args ? typeof args.timezone === 'string' : true) ); }