nimo007 commited on
Commit
d478b93
·
verified ·
1 Parent(s): 0ad0bcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,23 +1,28 @@
1
- # app.py
2
- import gradio as gr
3
- from datetime import datetime, timezone
 
4
 
5
- def now_utc():
6
- """
7
- Return the current time in ISO 8601 (UTC).
8
- No inputs. Useful as a 'time' tool for MCP clients.
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
- if __name__ == "__main__":
22
- # Starts the web UI and an MCP SSE endpoint; no authentication configured.
23
- demo.launch(mcp_server=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"}