from datetime import datetime
import zoneinfo
from mcp.server.fastmcp import FastMCP
# Initialize FastMCP server
mcp = FastMCP("time")
@mcp.tool()
def mcp_server_time(timezone: str = "Asia/Shanghai") -> str:
"""
Get the current time in the specified timezone.
Args:
timezone: The timezone to get the time for (default: Asia/Shanghai for UTC+8).
Examples: "UTC", "America/New_York", "Europe/London".
"""
try:
# Get current time in UTC
now_utc = datetime.now(zoneinfo.ZoneInfo("UTC"))
# Convert to target timezone
target_zone = zoneinfo.ZoneInfo(timezone)
current_time = now_utc.astimezone(target_zone)
return current_time.isoformat()
except Exception as e:
return f"Error getting time: {str(e)}"
# Expose Streamable HTTP app for uvicorn
# This allows running with: uvicorn server:app
app = mcp.streamable_http_app()
if __name__ == "__main__":
mcp.run()