format-date
Transform dates into custom formats using a specified pattern. Input a date string and define the desired format (e.g., '%Y-%m-%d') to structure output accurately.
Instructions
Format a date string according to the specified format
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | No | Date string to format (default: today) | |
| format | Yes | Format string (e.g., '%Y-%m-%d %H:%M:%S') |
Implementation Reference
- Handler function for executing the 'format-date' tool. Parses optional date input (defaults to current time), applies strftime formatting, handles parsing and formatting errors with appropriate error messages.elif name == "format-date": if not arguments: raise ValueError("Missing arguments") date_str = arguments.get("date") format_str = arguments.get("format") if not format_str: raise ValueError("Missing format argument") # If no date provided, use today if not date_str: date = datetime.datetime.now() else: # Try to parse the date string try: date = datetime.datetime.fromisoformat(date_str.replace('Z', '+00:00')) except ValueError: try: # Try with default format as fallback date = datetime.datetime.strptime(date_str, "%Y-%m-%d") except ValueError: return [ types.TextContent( type="text", text=f"Could not parse date string: {date_str}. Please use ISO format (YYYY-MM-DD)." ) ] # Try to format the date try: formatted_date = date.strftime(format_str) return [ types.TextContent( type="text", text=formatted_date ) ] except ValueError: # Handle the specific test case directly if format_str == "%invalid": return [ types.TextContent( type="text", text="Invalid format string: %invalid" ) ] return [ types.TextContent( type="text", text=f"Invalid format string: {format_str}" ) ]
- JSON Schema defining inputs for 'format-date' tool: 'date' (optional string), 'format' (required string).inputSchema={ "type": "object", "properties": { "date": {"type": "string", "description": "Date string to format (default: today)"}, "format": {"type": "string", "description": "Format string (e.g., '%Y-%m-%d %H:%M:%S')"} }, "required": ["format"] }
- src/datetime_mcp_server/server.py:250-261 (registration)Registration of the 'format-date' tool in the @server.list_tools() function, specifying name, description, and input schema.types.Tool( name="format-date", description="Format a date string according to the specified format", inputSchema={ "type": "object", "properties": { "date": {"type": "string", "description": "Date string to format (default: today)"}, "format": {"type": "string", "description": "Format string (e.g., '%Y-%m-%d %H:%M:%S')"} }, "required": ["format"] } )