get-current-time
Retrieve the current time in ISO, readable, Unix, or RFC3339 format with optional timezone specification.
Instructions
Get the current time in various formats
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | Yes | Format to return the time in | |
| timezone | No | Optional timezone (default: local system timezone) |
Implementation Reference
- src/datetime_mcp_server/server.py:231-249 (registration)Registration of the 'get-current-time' tool in the list_tools handler, including its name, description, and input schema.types.Tool( name="get-current-time", description="Get the current time in various formats", inputSchema={ "type": "object", "properties": { "format": { "type": "string", "enum": ["iso", "readable", "unix", "rfc3339"], "description": "Format to return the time in" }, "timezone": { "type": "string", "description": "Optional timezone (default: local system timezone)" } }, "required": ["format"] } ),
- Handler execution logic for 'get-current-time' tool. Extracts format and optional timezone from arguments, computes current time (handling pytz if available), formats using format_time helper, and returns as TextContent.elif name == "get-current-time": if not arguments: raise ValueError("Missing arguments") time_format = arguments.get("format") timezone = arguments.get("timezone") if not time_format: raise ValueError("Missing format argument") # Handle timezone if provided, otherwise use system timezone if timezone: try: import pytz tz = pytz.timezone(timezone) now = datetime.datetime.now(tz) except ImportError: return [ types.TextContent( type="text", text="The pytz library is not available. Using system timezone instead." ), types.TextContent( type="text", text=format_time(datetime.datetime.now(), time_format) ) ] except Exception as e: return [ types.TextContent( type="text", text=f"Error with timezone '{timezone}': {str(e)}. Using system timezone instead." ), types.TextContent( type="text", text=format_time(datetime.datetime.now(), time_format) ) ] else: now = datetime.datetime.now() return [ types.TextContent( type="text", text=format_time(now, time_format) ) ]
- JSON Schema for input validation of the 'get-current-time' tool, defining 'format' (required, enum) and optional 'timezone'.inputSchema={ "type": "object", "properties": { "format": { "type": "string", "enum": ["iso", "readable", "unix", "rfc3339"], "description": "Format to return the time in" }, "timezone": { "type": "string", "description": "Optional timezone (default: local system timezone)" } }, "required": ["format"] }
- Helper function to format datetime according to the specified format type: iso, readable, unix, rfc3339, or default to iso.def format_time(dt: datetime.datetime, format_type: str) -> str: """ Format a datetime object according to the specified format. Args: dt (datetime.datetime): The datetime to format. format_type (str): The format type (iso, readable, unix, rfc3339). Returns: str: The formatted datetime string. """ if format_type == "iso": return dt.isoformat() elif format_type == "readable": return dt.strftime("%Y-%m-%d %H:%M:%S") elif format_type == "unix": return str(int(dt.timestamp())) elif format_type == "rfc3339": return dt.strftime("%Y-%m-%dT%H:%M:%S%z") else: return dt.isoformat()