get_current_datetime
Retrieve the current date and time for any specified timezone using IANA timezone names like America/New_York or Europe/London.
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
- Executes the core logic of the get_current_datetime tool: validates input, retrieves timezone info, computes current datetime, formats as JSON using TimeResult model, or returns error.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)}" ) ]
- Defines the Tool description and inputSchema for get_current_datetime, requiring 'timezone_name' as string (IANA timezone).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:90-93 (registration)Registers the GetCurrentDateTimeToolHandler instance in the server's tool_handlers registry via add_tool_handler.# Time tools add_tool_handler(GetCurrentDateTimeToolHandler()) add_tool_handler(GetTimeZoneInfoToolHandler()) add_tool_handler(ConvertTimeToolHandler())