|
import datetime |
|
import pytz |
|
import typing as ty |
|
import gradio as gr |
|
|
|
|
|
from utils.mcp_decorator import mcp_tool |
|
|
|
|
|
@mcp_tool.define( |
|
name="Get current time", |
|
api_name="get_current_time", |
|
) |
|
def get_current_time(timezone: str = "UTC") -> str: |
|
""" |
|
Gets the current time for the given timezone string. |
|
|
|
This tool takes an IANA timezone name (like "UTC", "America/New_York", |
|
"Asia/Ho_Chi_Minh") and returns the current datetime in that zone. |
|
Defaults to "UTC" if no timezone is provided. |
|
|
|
Args: |
|
timezone (str): The IANA timezone string (e.g., "UTC"). |
|
|
|
Returns: |
|
str: The current time in the specified timezone, or an error message. |
|
""" |
|
try: |
|
|
|
tz = pytz.timezone(timezone) |
|
|
|
now_utc = datetime.datetime.utcnow() |
|
now_in_tz = pytz.utc.localize(now_utc).astimezone(tz) |
|
|
|
return now_in_tz.strftime('%Y-%m-%d %H:%M:%S %Z%z') |
|
except pytz.UnknownTimeZoneError: |
|
return f"Error: Unknown timezone '{timezone}'. Please provide a valid IANA timezone name." |
|
except Exception as e: |
|
return f"An unexpected error occurred: {e}" |
|
|
|
|
|
@mcp_tool.build_ui_control(api_name="get_current_time") |
|
def build_time_ui_control() -> gr.Group: |
|
""" |
|
Builds Gradio UI components for the Get current time tool. |
|
Returns a gr.Group containing the controls. |
|
""" |
|
|
|
|
|
with gr.Group(visible=False) as time_tool_group: |
|
gr.Markdown("Configure **Get current time** tool:") |
|
|
|
timezone_input = gr.Textbox( |
|
label="Timezone", |
|
value="UTC", |
|
placeholder="e.g., America/New_York, Asia/Ho_Chi_Minh", |
|
interactive=True, |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
return time_tool_group |
|
|
|
|