Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,110 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
| 3 |
from datetime import datetime, timezone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def now_utc():
|
| 6 |
"""
|
| 7 |
-
Return the current time in
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
| 13 |
demo = gr.Interface(
|
| 14 |
fn=now_utc,
|
| 15 |
inputs=None,
|
| 16 |
-
outputs=gr.
|
| 17 |
title="Datetime MCP Server",
|
| 18 |
-
description="Returns the current UTC datetime."
|
| 19 |
)
|
| 20 |
|
| 21 |
if __name__ == "__main__":
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import time
|
| 5 |
+
import uuid
|
| 6 |
from datetime import datetime, timezone
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# ---------- Logging (JSON, UTC) ----------
|
| 10 |
+
class JsonFormatter(logging.Formatter):
|
| 11 |
+
# Ensure all timestamps are UTC and RFC3339-like
|
| 12 |
+
converter = time.gmtime
|
| 13 |
+
|
| 14 |
+
def format(self, record: logging.LogRecord) -> str:
|
| 15 |
+
base = {
|
| 16 |
+
"ts": self.formatTime(record, "%Y-%m-%dT%H:%M:%S.%fZ"),
|
| 17 |
+
"level": record.levelname,
|
| 18 |
+
"logger": record.name,
|
| 19 |
+
"message": record.getMessage(),
|
| 20 |
+
}
|
| 21 |
+
# Include extras if provided
|
| 22 |
+
for k in ("request_id", "duration_ms", "event", "path", "client"):
|
| 23 |
+
if hasattr(record, k):
|
| 24 |
+
base[k] = getattr(record, k)
|
| 25 |
+
return json.dumps(base, ensure_ascii=False)
|
| 26 |
+
|
| 27 |
+
def _setup_logging():
|
| 28 |
+
handler = logging.StreamHandler()
|
| 29 |
+
handler.setFormatter(JsonFormatter())
|
| 30 |
+
root = logging.getLogger()
|
| 31 |
+
root.handlers.clear()
|
| 32 |
+
root.addHandler(handler)
|
| 33 |
+
root.setLevel(logging.INFO)
|
| 34 |
+
|
| 35 |
+
_setup_logging()
|
| 36 |
+
log = logging.getLogger("datetime_mcp")
|
| 37 |
+
|
| 38 |
+
# ---------- Time payload builder ----------
|
| 39 |
+
def build_time_payload(dt: datetime) -> dict:
|
| 40 |
+
"""
|
| 41 |
+
Return a compact, LLM-friendly bundle with multiple canonical formats
|
| 42 |
+
and useful calendar context. Always UTC.
|
| 43 |
+
"""
|
| 44 |
+
# Basic forms
|
| 45 |
+
iso_utc = dt.replace(tzinfo=timezone.utc).isoformat().replace("+00:00", "Z")
|
| 46 |
+
unix_ms = int(dt.timestamp() * 1000)
|
| 47 |
+
unix_s = unix_ms // 1000
|
| 48 |
|
| 49 |
+
# Calendar context
|
| 50 |
+
iso_week_year, iso_week, iso_weekday = dt.isocalendar()
|
| 51 |
+
day_of_year = int(dt.strftime("%j"))
|
| 52 |
+
|
| 53 |
+
payload = {
|
| 54 |
+
# Canonical machine-readable
|
| 55 |
+
"iso_utc": iso_utc, # RFC3339/ISO-8601 with Z
|
| 56 |
+
"unix_s": unix_s, # seconds since epoch
|
| 57 |
+
"unix_ms": unix_ms, # milliseconds since epoch
|
| 58 |
+
|
| 59 |
+
# Human-readable disambiguated strings (still UTC)
|
| 60 |
+
"date_utc": dt.strftime("%Y-%m-%d"),
|
| 61 |
+
"time_utc": dt.strftime("%H:%M:%S"),
|
| 62 |
+
"weekday_utc": dt.strftime("%A"),
|
| 63 |
+
|
| 64 |
+
# Calendar context (helps reasoning about “week/day”)
|
| 65 |
+
"iso_week": f"{iso_week_year}-W{iso_week:02d}",
|
| 66 |
+
"iso_weekday": iso_weekday, # 1=Mon .. 7=Sun
|
| 67 |
+
"day_of_year": day_of_year,
|
| 68 |
+
"quarter": (dt.month - 1) // 3 + 1,
|
| 69 |
+
|
| 70 |
+
# Explicit timezone grounding
|
| 71 |
+
"timezone": "UTC",
|
| 72 |
+
"offset": "+00:00",
|
| 73 |
+
|
| 74 |
+
# Friendly summary (for quick reading / prompts)
|
| 75 |
+
"summary": dt.strftime("%A, %d %B %Y %H:%M:%S UTC"),
|
| 76 |
+
}
|
| 77 |
+
return payload
|
| 78 |
+
|
| 79 |
+
# ---------- Tool function ----------
|
| 80 |
def now_utc():
|
| 81 |
"""
|
| 82 |
+
Return the current time bundle in JSON (UTC).
|
| 83 |
+
Generator -> streamable over HTTP/SSE.
|
| 84 |
"""
|
| 85 |
+
req_id = str(uuid.uuid4())
|
| 86 |
+
t0 = time.perf_counter()
|
| 87 |
+
|
| 88 |
+
log.info("request_start", extra={"request_id": req_id, "event": "now_utc_start"})
|
| 89 |
+
dt = datetime.now(timezone.utc)
|
| 90 |
+
payload = build_time_payload(dt)
|
| 91 |
+
|
| 92 |
+
# Stream one JSON event (LLM-friendly)
|
| 93 |
+
yield json.dumps(payload, ensure_ascii=False)
|
| 94 |
+
|
| 95 |
+
dur_ms = int((time.perf_counter() - t0) * 1000)
|
| 96 |
+
log.info(
|
| 97 |
+
"request_end",
|
| 98 |
+
extra={"request_id": req_id, "event": "now_utc_end", "duration_ms": dur_ms},
|
| 99 |
+
)
|
| 100 |
|
| 101 |
+
# ---------- Gradio app ----------
|
| 102 |
demo = gr.Interface(
|
| 103 |
fn=now_utc,
|
| 104 |
inputs=None,
|
| 105 |
+
outputs=gr.JSON(label="UTC time bundle (LLM-friendly)"),
|
| 106 |
title="Datetime MCP Server",
|
| 107 |
+
description="Returns the current UTC datetime as a structured JSON bundle for LLMs.",
|
| 108 |
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|