Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,28 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# generator -> streamable over HTTP/SSE (client will see a streamed event)
|
| 11 |
-
yield datetime.now(timezone.utc).isoformat()
|
| 12 |
-
|
| 13 |
-
demo = gr.Interface(
|
| 14 |
-
fn=now_utc,
|
| 15 |
-
inputs=None,
|
| 16 |
-
outputs=gr.Textbox(label="UTC datetime (ISO 8601)"),
|
| 17 |
-
title="Datetime MCP Server",
|
| 18 |
-
description="Returns the current UTC datetime."
|
| 19 |
)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from mcp.server.fastmcp import FastMCP
|
| 4 |
+
import zoneinfo
|
| 5 |
|
| 6 |
+
# --- Minimal MCP server that returns current datetime ---
|
| 7 |
+
mcp = FastMCP(
|
| 8 |
+
name="datetime",
|
| 9 |
+
stateless_http=True, # no auth/state, HTTP transport
|
| 10 |
+
# json_response=True, # optional: force JSON-only if your client prefers it
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
+
@mcp.tool(description="Return current datetime. tz is an IANA TZ name (e.g. 'UTC', 'Asia/Kolkata').")
|
| 14 |
+
def get_datetime(tz: str = "UTC", iso: bool = True) -> str:
|
| 15 |
+
z = zoneinfo.ZoneInfo(tz)
|
| 16 |
+
now = datetime.now(tz=z)
|
| 17 |
+
return now.isoformat() if iso else now.strftime("%Y-%m-%d %H:%M:%S %Z")
|
| 18 |
+
|
| 19 |
+
# --- Host app ---
|
| 20 |
+
app = FastAPI()
|
| 21 |
+
|
| 22 |
+
# Mount the Streamable HTTP MCP server at /mcp with NO extra middleware around it
|
| 23 |
+
app.mount("/mcp", mcp.streamable_http_app())
|
| 24 |
+
|
| 25 |
+
# (Optional) tiny root page so Spaces shows something at "/"
|
| 26 |
+
@app.get("/")
|
| 27 |
+
def root():
|
| 28 |
+
return {"ok": True, "mcp": "/mcp"}
|