File size: 2,444 Bytes
5aa3cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Berlin Time MCP Server - A Gradio-based MCP server example
===========================================================

This is a simple MCP (Model Context Protocol) server that provides
a tool for getting the current time in Berlin timezone.

MCP servers allow AI agents to discover and use tools in a standardized way.
"""

from datetime import datetime
import pytz
import gradio as gr


def get_berlin_time():
    """
    Get the current date and time in Berlin timezone.
    
    This function will be exposed as an MCP tool that AI agents can call
    when they need to know the current time in Berlin.
    
    Returns:
        dict: A dictionary containing:
            - time: Human-readable formatted time string
            - timezone: The timezone name (Europe/Berlin)
            - timestamp: ISO 8601 formatted timestamp
            - utc_offset: Current UTC offset for Berlin
    """
    # Create Berlin timezone object
    berlin_tz = pytz.timezone('Europe/Berlin')
    
    # Get current time in Berlin
    current_time = datetime.now(berlin_tz)
    
    # Format the time in a human-readable way
    formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S %Z")
    
    # Get UTC offset (e.g., +01:00 or +02:00 depending on DST)
    utc_offset = current_time.strftime("%z")
    
    # Return structured data
    return {
        "time": formatted_time,
        "timezone": "Europe/Berlin",
        "timestamp": current_time.isoformat(),
        "utc_offset": utc_offset,
        "day_of_week": current_time.strftime("%A"),
        "is_dst": bool(current_time.dst())
    }


# Simple Interface - just like the example you showed
demo = gr.Interface(
    fn=get_berlin_time,
    inputs=[],  # Empty list = no inputs needed
    outputs=gr.JSON(label="Berlin Time Information"),
    title="πŸ• Berlin Time MCP Server",
    description="""
    This is an MCP (Model Context Protocol) server that provides the current time in Berlin.
    
    **For Testing:** Click 'Submit' below to see the current Berlin time.
    
    **For AI Agents:** This server exposes the `get_berlin_time` tool via MCP protocol.
    AI agents can connect to this server and call this tool when they need Berlin time.
    """,
    api_name="get_berlin_time"
)

if __name__ == "__main__":
    # Launch with MCP server enabled
    demo.launch(
        mcp_server=True,
        share=False,
        server_name="0.0.0.0",
        server_port=7860
    )