Spaces:
Sleeping
Sleeping
fix: app.py name change for world time change file startup
Browse files
README.md
CHANGED
|
@@ -5,7 +5,7 @@ colorFrom: purple
|
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.0.2
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
| 10 |
short_description: A minimal Gradio MCP server that provides timezone-aware dat
|
| 11 |
---
|
|
|
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.0.2
|
| 8 |
+
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
short_description: A minimal Gradio MCP server that provides timezone-aware dat
|
| 11 |
---
|
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
World Time MCP Server - Upgraded version with city parameter
|
| 3 |
+
=============================================================
|
| 4 |
+
|
| 5 |
+
This is an upgraded MCP server that allows the LLM to specify
|
| 6 |
+
which city's time to retrieve.
|
| 7 |
+
|
| 8 |
+
This demonstrates how to create MCP tools with parameters.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
import pytz
|
| 13 |
+
import gradio as gr
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_time_for_city(city: str = "Berlin"):
|
| 17 |
+
"""
|
| 18 |
+
Get current time for any major city.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
city: City name (e.g., "Berlin", "Tokyo", "New York", "London")
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
dict: Time information for the specified city, or error if city not found
|
| 25 |
+
"""
|
| 26 |
+
# Map common city names to IANA timezones
|
| 27 |
+
city_timezones = {
|
| 28 |
+
"berlin": "Europe/Berlin",
|
| 29 |
+
"london": "Europe/London",
|
| 30 |
+
"paris": "Europe/Paris",
|
| 31 |
+
"madrid": "Europe/Madrid",
|
| 32 |
+
"rome": "Europe/Rome",
|
| 33 |
+
"new york": "America/New_York",
|
| 34 |
+
"los angeles": "America/Los_Angeles",
|
| 35 |
+
"chicago": "America/Chicago",
|
| 36 |
+
"toronto": "America/Toronto",
|
| 37 |
+
"mexico city": "America/Mexico_City",
|
| 38 |
+
"tokyo": "Asia/Tokyo",
|
| 39 |
+
"beijing": "Asia/Shanghai",
|
| 40 |
+
"hong kong": "Asia/Hong_Kong",
|
| 41 |
+
"singapore": "Asia/Singapore",
|
| 42 |
+
"dubai": "Asia/Dubai",
|
| 43 |
+
"sydney": "Australia/Sydney",
|
| 44 |
+
"melbourne": "Australia/Melbourne",
|
| 45 |
+
"auckland": "Pacific/Auckland",
|
| 46 |
+
"moscow": "Europe/Moscow",
|
| 47 |
+
"istanbul": "Europe/Istanbul",
|
| 48 |
+
"cairo": "Africa/Cairo",
|
| 49 |
+
"johannesburg": "Africa/Johannesburg",
|
| 50 |
+
"sao paulo": "America/Sao_Paulo",
|
| 51 |
+
"buenos aires": "America/Argentina/Buenos_Aires",
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
# Normalize city name
|
| 55 |
+
city_lower = city.lower().strip()
|
| 56 |
+
|
| 57 |
+
# Check if city is known
|
| 58 |
+
if city_lower not in city_timezones:
|
| 59 |
+
return {
|
| 60 |
+
"error": f"Unknown city: {city}",
|
| 61 |
+
"message": "Please use one of the available cities",
|
| 62 |
+
"available_cities": sorted(city_timezones.keys())
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
# Get timezone and current time
|
| 66 |
+
timezone_name = city_timezones[city_lower]
|
| 67 |
+
tz = pytz.timezone(timezone_name)
|
| 68 |
+
current_time = datetime.now(tz)
|
| 69 |
+
|
| 70 |
+
return {
|
| 71 |
+
"city": city.title(),
|
| 72 |
+
"time": current_time.strftime("%Y-%m-%d %H:%M:%S %Z"),
|
| 73 |
+
"timezone": timezone_name,
|
| 74 |
+
"timestamp": current_time.isoformat(),
|
| 75 |
+
"utc_offset": current_time.strftime("%z"),
|
| 76 |
+
"day_of_week": current_time.strftime("%A"),
|
| 77 |
+
"is_dst": bool(current_time.dst())
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# Interface with input parameter
|
| 82 |
+
demo = gr.Interface(
|
| 83 |
+
fn=get_time_for_city,
|
| 84 |
+
inputs=gr.Textbox(
|
| 85 |
+
label="City Name",
|
| 86 |
+
placeholder="Enter city (e.g., Berlin, Tokyo, New York)",
|
| 87 |
+
value="Berlin"
|
| 88 |
+
),
|
| 89 |
+
outputs=gr.JSON(label="Time Information"),
|
| 90 |
+
title="🌍 World Time MCP Server",
|
| 91 |
+
description="""
|
| 92 |
+
This is an MCP server that provides the current time for any major city.
|
| 93 |
+
|
| 94 |
+
**For Testing:** Enter a city name and click 'Submit'.
|
| 95 |
+
|
| 96 |
+
**For AI Agents:** This server exposes the `get_time_for_city` tool via MCP protocol.
|
| 97 |
+
The LLM can specify which city's time to retrieve.
|
| 98 |
+
|
| 99 |
+
**Example cities:** Berlin, Tokyo, New York, London, Paris, Sydney
|
| 100 |
+
""",
|
| 101 |
+
examples=[
|
| 102 |
+
["Berlin"],
|
| 103 |
+
["Tokyo"],
|
| 104 |
+
["New York"],
|
| 105 |
+
["London"],
|
| 106 |
+
["Sydney"]
|
| 107 |
+
],
|
| 108 |
+
api_name="get_time_for_city"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
# Launch with MCP server enabled
|
| 113 |
+
demo.launch(
|
| 114 |
+
mcp_server=True,
|
| 115 |
+
share=False,
|
| 116 |
+
server_name="0.0.0.0",
|
| 117 |
+
server_port=7861 # Different port so both can run simultaneously
|
| 118 |
+
)
|