get_current_datetime
Retrieve the current date and time for any IANA timezone. Simply provide the timezone name to get accurate local time.
Instructions
Get current time in specified timezone.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| timezone_name | Yes | IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Use UTC timezone if no timezone provided by the user. |
Implementation Reference
- The main handler class for the 'get_current_datetime' tool. It extends ToolHandler, registers the tool name, provides schema via get_tool_description(), and executes the core logic in run_tool() by resolving the timezone via utils.get_zoneinfo() and returning the current datetime formatted as JSON.
class GetCurrentDateTimeToolHandler(ToolHandler): """ Tool handler for getting current date and time in a specified timezone. """ def __init__(self): super().__init__("get_current_datetime") def get_tool_description(self) -> Tool: """ Return the tool description for current datetime lookup. """ return Tool( name=self.name, description="""Get current time in specified timezone.""", inputSchema={ "type": "object", "properties": { "timezone_name": { "type": "string", "description": "IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Use UTC timezone if no timezone provided by the user." } }, "required": ["timezone_name"] } ) async def run_tool(self, args: dict) -> Sequence[TextContent | ImageContent | EmbeddedResource]: """ Execute the current datetime tool. """ try: self.validate_required_args(args, ["timezone_name"]) timezone_name = args["timezone_name"] logger.info(f"Getting current time for timezone: {timezone_name}") # Get timezone info timezone = utils.get_zoneinfo(timezone_name) current_time = datetime.now(timezone) # Create time result time_result = utils.TimeResult( timezone=timezone_name, datetime=current_time.isoformat(timespec="seconds"), ) return [ TextContent( type="text", text=json.dumps(time_result.model_dump(), indent=2) ) ] except Exception as e: logger.exception(f"Error in get_current_datetime: {str(e)}") return [ TextContent( type="text", text=f"Error getting current time: {str(e)}" ) ] - Input/output schema for the tool. Requires a 'timezone_name' string parameter (IANA timezone name). Returns JSON with 'timezone' and 'datetime' fields via the TimeResult model.
def get_tool_description(self) -> Tool: """ Return the tool description for current datetime lookup. """ return Tool( name=self.name, description="""Get current time in specified timezone.""", inputSchema={ "type": "object", "properties": { "timezone_name": { "type": "string", "description": "IANA timezone name (e.g., 'America/New_York', 'Europe/London'). Use UTC timezone if no timezone provided by the user." } }, "required": ["timezone_name"] } ) - src/mcp_weather_server/server.py:91-92 (registration)Registration of the GetCurrentDateTimeToolHandler in the central register_all_tools() function, which calls add_tool_handler() to store it in the global tool_handlers dictionary.
add_tool_handler(GetCurrentDateTimeToolHandler()) add_tool_handler(GetTimeZoneInfoToolHandler()) - Helper utilities: TimeResult Pydantic model defines the output schema (timezone string + datetime string), and get_zoneinfo() resolves an IANA timezone name to a ZoneInfo object, raising McpError on invalid timezone names.
class TimeResult(BaseModel): timezone: str datetime: str def get_zoneinfo(timezone_name: str) -> ZoneInfo: try: return ZoneInfo(timezone_name) except Exception as e: error_data = ErrorData(code=-1, message=f"Invalid timezone: {str(e)}") raise McpError(error_data)