|
| 1 | +"""Date and time operations MCP server running over streamable HTTP.""" |
| 2 | + |
| 3 | +from datetime import datetime, timedelta, timezone |
| 4 | +from typing import Dict |
| 5 | + |
| 6 | +from mcp.server.fastmcp import FastMCP |
| 7 | + |
| 8 | +mcp = FastMCP("DateTime Operations Server", host="127.0.0.1", port=8080) |
| 9 | + |
| 10 | + |
| 11 | +@mcp.tool() |
| 12 | +def current_utc_time() -> str: |
| 13 | + """Get the current UTC date and time in ISO 8601 format. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + Current UTC datetime as an ISO 8601 string |
| 17 | + """ |
| 18 | + return datetime.now(timezone.utc).isoformat() |
| 19 | + |
| 20 | + |
| 21 | +@mcp.tool() |
| 22 | +def days_between(date1: str, date2: str) -> int: |
| 23 | + """Calculate the number of days between two dates. |
| 24 | +
|
| 25 | + Args: |
| 26 | + date1: First date in ISO 8601 format |
| 27 | + date2: Second date in ISO 8601 format |
| 28 | +
|
| 29 | + Returns: |
| 30 | + Absolute number of days between the two dates |
| 31 | + """ |
| 32 | + dt1 = datetime.fromisoformat(date1) |
| 33 | + dt2 = datetime.fromisoformat(date2) |
| 34 | + return abs((dt2 - dt1).days) |
| 35 | + |
| 36 | + |
| 37 | +@mcp.tool() |
| 38 | +def add_days(date_string: str, days: int) -> str: |
| 39 | + """Add or subtract days from a date. |
| 40 | +
|
| 41 | + Args: |
| 42 | + date_string: Starting date in ISO 8601 format |
| 43 | + days: Number of days to add (negative to subtract) |
| 44 | +
|
| 45 | + Returns: |
| 46 | + The resulting date in ISO 8601 format |
| 47 | + """ |
| 48 | + dt = datetime.fromisoformat(date_string) |
| 49 | + result = dt + timedelta(days=days) |
| 50 | + return result.isoformat() |
| 51 | + |
| 52 | + |
| 53 | +@mcp.tool() |
| 54 | +def day_of_week(date_string: str) -> str: |
| 55 | + """Get the day of the week for a given date. |
| 56 | +
|
| 57 | + Args: |
| 58 | + date_string: Date in ISO 8601 format |
| 59 | +
|
| 60 | + Returns: |
| 61 | + Name of the day of the week (e.g. "Monday") |
| 62 | + """ |
| 63 | + dt = datetime.fromisoformat(date_string) |
| 64 | + return dt.strftime("%A") |
| 65 | + |
| 66 | + |
| 67 | +@mcp.tool() |
| 68 | +def is_leap_year(year: int) -> bool: |
| 69 | + """Check if a given year is a leap year. |
| 70 | +
|
| 71 | + Args: |
| 72 | + year: The year to check |
| 73 | +
|
| 74 | + Returns: |
| 75 | + True if the year is a leap year |
| 76 | + """ |
| 77 | + return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) |
| 78 | + |
| 79 | + |
| 80 | +@mcp.tool() |
| 81 | +def days_in_month(year: int, month: int) -> int: |
| 82 | + """Get the number of days in a given month. |
| 83 | +
|
| 84 | + Args: |
| 85 | + year: The year |
| 86 | + month: The month (1-12) |
| 87 | +
|
| 88 | + Returns: |
| 89 | + Number of days in the month |
| 90 | +
|
| 91 | + Raises: |
| 92 | + ValueError: If month is not between 1 and 12 |
| 93 | + """ |
| 94 | + if month < 1 or month > 12: |
| 95 | + raise ValueError("Month must be between 1 and 12") |
| 96 | + days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| 97 | + if month == 2 and is_leap_year(year): |
| 98 | + return 29 |
| 99 | + return days[month - 1] |
| 100 | + |
| 101 | + |
| 102 | +@mcp.tool() |
| 103 | +def time_difference(datetime1: str, datetime2: str) -> Dict[str, int]: |
| 104 | + """Calculate the difference between two datetimes. |
| 105 | +
|
| 106 | + Args: |
| 107 | + datetime1: First datetime in ISO 8601 format |
| 108 | + datetime2: Second datetime in ISO 8601 format |
| 109 | +
|
| 110 | + Returns: |
| 111 | + Dictionary with days, hours, minutes, and seconds of difference |
| 112 | + """ |
| 113 | + dt1 = datetime.fromisoformat(datetime1) |
| 114 | + dt2 = datetime.fromisoformat(datetime2) |
| 115 | + diff = abs(dt2 - dt1) |
| 116 | + total_seconds = int(diff.total_seconds()) |
| 117 | + days = diff.days |
| 118 | + hours = (total_seconds % 86400) // 3600 |
| 119 | + minutes = (total_seconds % 3600) // 60 |
| 120 | + seconds = total_seconds % 60 |
| 121 | + return {"days": days, "hours": hours, "minutes": minutes, "seconds": seconds} |
| 122 | + |
| 123 | + |
| 124 | +@mcp.tool() |
| 125 | +def is_weekend(date_string: str) -> bool: |
| 126 | + """Check if a given date falls on a weekend. |
| 127 | +
|
| 128 | + Args: |
| 129 | + date_string: Date in ISO 8601 format |
| 130 | +
|
| 131 | + Returns: |
| 132 | + True if the date is Saturday or Sunday |
| 133 | + """ |
| 134 | + dt = datetime.fromisoformat(date_string) |
| 135 | + return dt.weekday() >= 5 |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + mcp.run(transport="streamable-http") |
0 commit comments