get_current_datetime
Retrieve current time for any IANA timezone to coordinate weather data with local time.
Instructions
Get current time in specified timezone.
Input Schema
TableJSON 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 GetCurrentDateTimeToolHandler class provides the full implementation of the 'get_current_datetime' tool, including initialization with the tool name, schema definition via get_tool_description, and execution logic in run_tool which retrieves the current datetime for the given timezone using zoneinfo and returns it 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)}" ) ]
- src/mcp_weather_server/server.py:91-91 (registration)Registration of the GetCurrentDateTimeToolHandler instance in the server's tool registry via add_tool_handler during initialization in register_all_tools().add_tool_handler(GetCurrentDateTimeToolHandler())
- Input schema definition for the 'get_current_datetime' tool, specifying the required 'timezone_name' parameter as a string.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"] } )
- Helper function get_zoneinfo that resolves the IANA timezone name to a zoneinfo.ZoneInfo object, used in the tool handler.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)
- Pydantic model TimeResult used to structure the tool's output data containing timezone and datetime.class TimeResult(BaseModel): timezone: str datetime: str