GFiaMon commited on
Commit
5aa3cbc
Β·
1 Parent(s): be7261c

first commit

Browse files
Files changed (5) hide show
  1. .gitignore +4 -0
  2. README.md +61 -2
  3. STEP_BY_STEP_GUIDE.md +339 -0
  4. app_time_mcp_server.py +77 -0
  5. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ .env
4
+ .DS_Store
README.md CHANGED
@@ -5,9 +5,68 @@ colorFrom: purple
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
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  colorTo: blue
6
  sdk: gradio
7
  sdk_version: 6.0.2
8
+ app_file: app_time_mcp_server.py
9
  pinned: false
10
  short_description: A minimal Gradio MCP server that provides timezone-aware dat
11
  ---
12
 
13
+ # Berlin Time MCP Server
14
+
15
+ A simple MCP (Model Context Protocol) server built with Gradio that provides current Berlin time to AI agents.
16
+
17
+ ## What is this?
18
+
19
+ This is a demonstration MCP server that exposes a single tool: `get_berlin_time`. AI agents can connect to this server and call this tool when they need to know the current time in Berlin, Germany.
20
+
21
+ ## Local Testing
22
+
23
+ 1. Install dependencies:
24
+ ```bash
25
+ pip install -r requirements.txt
26
+ ```
27
+
28
+ 2. Run the server:
29
+ ```bash
30
+ python app_time_mcp_server.py
31
+ ```
32
+
33
+ 3. Open your browser to `http://localhost:7860` to test the tool manually.
34
+
35
+ ## Deploying to HuggingFace Spaces
36
+
37
+ 1. Create a new Space at [huggingface.co/spaces](https://huggingface.co/spaces)
38
+ 2. Choose "Gradio" as the SDK
39
+ 3. Upload these files:
40
+ - `app_time_mcp_server.py` (rename to `app.py` for HF Spaces)
41
+ - `requirements.txt`
42
+ 4. Your Space will automatically deploy!
43
+
44
+ ## Using with AI Agents
45
+
46
+ Once deployed, AI agents can connect to this MCP server and use the `get_berlin_time` tool.
47
+
48
+ ### Example with LangGraph
49
+
50
+ See the main guide for detailed integration instructions.
51
+
52
+ ## Tool Details
53
+
54
+ **Function:** `get_berlin_time()`
55
+
56
+ **Returns:**
57
+ ```json
58
+ {
59
+ "time": "2025-12-04 17:00:57 CET",
60
+ "timezone": "Europe/Berlin",
61
+ "timestamp": "2025-12-04T17:00:57+01:00",
62
+ "utc_offset": "+0100",
63
+ "day_of_week": "Wednesday",
64
+ "is_dst": false
65
+ }
66
+ ```
67
+
68
+ ## Future Improvements
69
+
70
+ - Add support for multiple timezones
71
+ - Add time conversion tools
72
+ - Add timezone difference calculator
STEP_BY_STEP_GUIDE.md ADDED
@@ -0,0 +1,339 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Step-by-Step Guide: Berlin Time MCP Server
2
+
3
+ ## βœ… What We've Built
4
+
5
+ You now have a complete, working MCP server in `external_mcp_servers/` with:
6
+ - `app_time_mcp_server.py` - The main server application
7
+ - `requirements.txt` - Dependencies (gradio, pytz)
8
+ - `README.md` - Documentation
9
+ - `.gitignore` - Git configuration
10
+
11
+ ## 🎯 Current Status
12
+
13
+ **Server is running at:** http://localhost:7860
14
+
15
+ The server is now:
16
+ 1. βœ… Providing a web UI for manual testing
17
+ 2. βœ… Exposing an MCP endpoint at: `http://localhost:7860/gradio_api/mcp/`
18
+ 3. βœ… Ready to be called by AI agents
19
+
20
+ ---
21
+
22
+ ## πŸ“š Understanding Each Component
23
+
24
+ ### 1. The Core Function: `get_berlin_time()`
25
+
26
+ ```python
27
+ def get_berlin_time():
28
+ berlin_tz = pytz.timezone('Europe/Berlin')
29
+ current_time = datetime.now(berlin_tz)
30
+ # ... returns structured data
31
+ ```
32
+
33
+ **What it does:**
34
+ - Creates a timezone object for Berlin
35
+ - Gets the current time in that timezone
36
+ - Returns a dictionary with multiple time formats
37
+
38
+ **Why return a dictionary?**
39
+ - AI agents can choose which format they need
40
+ - Includes both human-readable and machine-readable formats
41
+ - Provides extra context (day of week, DST status)
42
+
43
+ ### 2. The Gradio Interface
44
+
45
+ ```python
46
+ demo = gr.Interface(
47
+ fn=get_berlin_time,
48
+ inputs=None,
49
+ outputs=gr.JSON(label="Berlin Time Information"),
50
+ ...
51
+ )
52
+ ```
53
+
54
+ **What this does:**
55
+ - `fn=get_berlin_time` - The function to expose
56
+ - `inputs=None` - No parameters needed (simple tool)
57
+ - `outputs=gr.JSON()` - Returns JSON data
58
+ - Creates both a UI and an API endpoint
59
+
60
+ ### 3. The MCP Magic: `mcp_server=True`
61
+
62
+ ```python
63
+ demo.launch(mcp_server=True)
64
+ ```
65
+
66
+ **This single parameter:**
67
+ - Starts the normal Gradio UI (for testing)
68
+ - **ALSO** starts an MCP protocol server
69
+ - Automatically creates MCP tool definitions
70
+ - Handles all MCP communication
71
+
72
+ **What happens behind the scenes:**
73
+ 1. Gradio analyzes your function signature
74
+ 2. Creates an MCP tool definition with:
75
+ - Tool name: `get_berlin_time`
76
+ - Input schema: (none in this case)
77
+ - Output schema: (JSON object)
78
+ 3. Exposes an MCP endpoint that AI agents can connect to
79
+
80
+ ---
81
+
82
+ ## πŸ§ͺ Testing Locally
83
+
84
+ ### Method 1: Web UI (Manual Testing)
85
+
86
+ 1. **Server is already running** at http://localhost:7860
87
+ 2. Open your browser to that URL
88
+ 3. Click the "Submit" button
89
+ 4. You'll see JSON output like:
90
+ ```json
91
+ {
92
+ "time": "2025-12-04 17:00:57 CET",
93
+ "timezone": "Europe/Berlin",
94
+ "timestamp": "2025-12-04T17:00:57+01:00",
95
+ "utc_offset": "+0100",
96
+ "day_of_week": "Wednesday",
97
+ "is_dst": false
98
+ }
99
+ ```
100
+
101
+ ### Method 2: MCP Protocol (AI Agent Testing)
102
+
103
+ The MCP endpoint is at: `http://localhost:7860/gradio_api/mcp/`
104
+
105
+ AI agents can connect to this endpoint and:
106
+ 1. Discover available tools
107
+ 2. Call `get_berlin_time`
108
+ 3. Receive the structured response
109
+
110
+ ---
111
+
112
+ ## πŸš€ Deploying to HuggingFace Spaces
113
+
114
+ ### Step 1: Prepare Files
115
+
116
+ When deploying to HF Spaces, you need to rename `app_time_mcp_server.py` to `app.py`:
117
+
118
+ ```bash
119
+ # In the external_mcp_servers directory
120
+ cp app_time_mcp_server.py app.py
121
+ ```
122
+
123
+ ### Step 2: Create a HuggingFace Space
124
+
125
+ 1. Go to https://huggingface.co/spaces
126
+ 2. Click "Create new Space"
127
+ 3. Fill in:
128
+ - **Name:** `berlin-time-mcp` (or your choice)
129
+ - **License:** Choose one (e.g., MIT)
130
+ - **SDK:** Select **Gradio**
131
+ - **Visibility:** Public or Private
132
+
133
+ ### Step 3: Upload Files
134
+
135
+ You have two options:
136
+
137
+ **Option A: Git Push (Recommended)**
138
+ ```bash
139
+ cd external_mcp_servers
140
+ git init
141
+ git add app.py requirements.txt README.md
142
+ git commit -m "Initial MCP server"
143
+ git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/berlin-time-mcp
144
+ git push hf main
145
+ ```
146
+
147
+ **Option B: Web Upload**
148
+ 1. In your Space, click "Files" tab
149
+ 2. Upload:
150
+ - `app.py` (renamed from app_time_mcp_server.py)
151
+ - `requirements.txt`
152
+ - `README.md` (optional)
153
+
154
+ ### Step 4: Automatic Deployment
155
+
156
+ HuggingFace will:
157
+ 1. Detect it's a Gradio app (from `requirements.txt`)
158
+ 2. Install dependencies
159
+ 3. Run `app.py`
160
+ 4. Provide a public URL: `https://huggingface.co/spaces/YOUR_USERNAME/berlin-time-mcp`
161
+
162
+ ### Step 5: Verify Deployment
163
+
164
+ Once deployed, you'll see:
165
+ - The Gradio UI at the Space URL
166
+ - MCP endpoint at: `https://huggingface.co/spaces/YOUR_USERNAME/berlin-time-mcp/gradio_api/mcp/`
167
+
168
+ ---
169
+
170
+ ## πŸ”— Integrating with Your LangGraph Agent
171
+
172
+ ### Prerequisites
173
+
174
+ ```bash
175
+ pip install mcp langchain-mcp
176
+ ```
177
+
178
+ ### Step 1: Create MCP Client Connection
179
+
180
+ Create a new file in your project: `src/tools/mcp_tools.py`
181
+
182
+ ```python
183
+ from mcp import ClientSession, StdioServerParameters
184
+ from mcp.client.stdio import stdio_client
185
+ from langchain.tools import Tool
186
+ import asyncio
187
+
188
+ async def connect_to_berlin_time_mcp():
189
+ """Connect to the Berlin Time MCP server"""
190
+
191
+ # For local testing
192
+ server_params = StdioServerParameters(
193
+ command="python",
194
+ args=["external_mcp_servers/app_time_mcp_server.py"]
195
+ )
196
+
197
+ # Or for deployed version:
198
+ # server_params = HttpServerParameters(
199
+ # url="https://huggingface.co/spaces/YOUR_USERNAME/berlin-time-mcp/gradio_api/mcp/"
200
+ # )
201
+
202
+ async with stdio_client(server_params) as (read, write):
203
+ async with ClientSession(read, write) as session:
204
+ await session.initialize()
205
+ tools = await session.list_tools()
206
+ return session, tools
207
+
208
+ def create_berlin_time_tool():
209
+ """Create a LangChain tool from the MCP server"""
210
+
211
+ async def get_time():
212
+ session, _ = await connect_to_berlin_time_mcp()
213
+ result = await session.call_tool("get_berlin_time", {})
214
+ return result
215
+
216
+ return Tool(
217
+ name="get_berlin_time",
218
+ func=lambda: asyncio.run(get_time()),
219
+ description="Get the current date and time in Berlin, Germany. "
220
+ "Returns time in multiple formats including ISO 8601."
221
+ )
222
+ ```
223
+
224
+ ### Step 2: Add to Your Agent
225
+
226
+ In your main agent file (e.g., `app_v4.py`):
227
+
228
+ ```python
229
+ from src.tools.mcp_tools import create_berlin_time_tool
230
+
231
+ # Add to your existing tools
232
+ berlin_time_tool = create_berlin_time_tool()
233
+
234
+ # Include in your agent's tool list
235
+ tools = [
236
+ # ... your existing tools ...
237
+ berlin_time_tool
238
+ ]
239
+ ```
240
+
241
+ ### Step 3: Test the Integration
242
+
243
+ Ask your agent:
244
+ - "What time is it in Berlin?"
245
+ - "What's the current date and time in Berlin?"
246
+ - "Is it daytime in Berlin right now?"
247
+
248
+ The agent will:
249
+ 1. Recognize it needs Berlin time
250
+ 2. Call the MCP tool
251
+ 3. Receive the structured response
252
+ 4. Format it nicely for the user
253
+
254
+ ---
255
+
256
+ ## πŸŽ“ Teaching This to Colleagues
257
+
258
+ ### Key Points to Emphasize:
259
+
260
+ 1. **MCP is a Protocol, Not a Library**
261
+ - It's a standard way for AI to talk to tools
262
+ - Like HTTP for web, MCP for AI tools
263
+
264
+ 2. **Gradio Makes It Easy**
265
+ - One parameter: `mcp_server=True`
266
+ - Handles all the protocol complexity
267
+ - Provides both UI and API
268
+
269
+ 3. **Deployment is Simple**
270
+ - Works locally for development
271
+ - Deploy to HF Spaces for production
272
+ - No server management needed
273
+
274
+ 4. **Reusable Across AI Systems**
275
+ - Write once, use in any MCP-compatible AI
276
+ - Not locked to one framework
277
+ - Easy to share and discover
278
+
279
+ ### Demo Flow for Colleagues:
280
+
281
+ 1. **Show the code** - Point out how simple it is
282
+ 2. **Run locally** - Demonstrate the UI
283
+ 3. **Explain MCP endpoint** - Show the protocol URL
284
+ 4. **Deploy to HF** - Show the public version
285
+ 5. **Integrate with AI** - Demonstrate agent using it
286
+
287
+ ---
288
+
289
+ ## πŸ”„ Next Steps & Improvements
290
+
291
+ ### Immediate Next Steps:
292
+ 1. βœ… Test the local server (currently running)
293
+ 2. ⏭️ Deploy to HuggingFace Spaces
294
+ 3. ⏭️ Integrate with your LangGraph agent
295
+ 4. ⏭️ Test end-to-end with your agent
296
+
297
+ ### Future Enhancements:
298
+
299
+ **1. Multiple Timezones**
300
+ ```python
301
+ def get_time_for_timezone(timezone_name: str = "Europe/Berlin"):
302
+ # Allow AI to specify any timezone
303
+ ```
304
+
305
+ **2. Time Conversions**
306
+ ```python
307
+ def convert_time(time_str: str, from_tz: str, to_tz: str):
308
+ # Convert between timezones
309
+ ```
310
+
311
+ **3. Business Hours Check**
312
+ ```python
313
+ def is_business_hours(timezone: str = "Europe/Berlin"):
314
+ # Check if it's business hours (9-5)
315
+ ```
316
+
317
+ **4. Multiple Tools in One Server**
318
+ ```python
319
+ # Create multiple gr.Interface instances
320
+ # Combine them with gr.TabbedInterface
321
+ ```
322
+
323
+ ---
324
+
325
+ ## πŸ“ Summary
326
+
327
+ You now have:
328
+ - βœ… A working MCP server with Berlin time tool
329
+ - βœ… Complete understanding of how MCP works
330
+ - βœ… Local testing capability
331
+ - βœ… Deployment instructions for HF Spaces
332
+ - βœ… Integration guide for LangGraph
333
+ - βœ… Teaching materials for colleagues
334
+
335
+ **The beauty of this approach:**
336
+ - Simple Python function β†’ Gradio interface β†’ MCP server
337
+ - One line (`mcp_server=True`) enables AI integration
338
+ - Deploy anywhere Gradio runs
339
+ - Use with any MCP-compatible AI system
app_time_mcp_server.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Berlin Time MCP Server - A Gradio-based MCP server example
3
+ ===========================================================
4
+
5
+ This is a simple MCP (Model Context Protocol) server that provides
6
+ a tool for getting the current time in Berlin timezone.
7
+
8
+ MCP servers allow AI agents to discover and use tools in a standardized way.
9
+ """
10
+
11
+ from datetime import datetime
12
+ import pytz
13
+ import gradio as gr
14
+
15
+
16
+ def get_berlin_time():
17
+ """
18
+ Get the current date and time in Berlin timezone.
19
+
20
+ This function will be exposed as an MCP tool that AI agents can call
21
+ when they need to know the current time in Berlin.
22
+
23
+ Returns:
24
+ dict: A dictionary containing:
25
+ - time: Human-readable formatted time string
26
+ - timezone: The timezone name (Europe/Berlin)
27
+ - timestamp: ISO 8601 formatted timestamp
28
+ - utc_offset: Current UTC offset for Berlin
29
+ """
30
+ # Create Berlin timezone object
31
+ berlin_tz = pytz.timezone('Europe/Berlin')
32
+
33
+ # Get current time in Berlin
34
+ current_time = datetime.now(berlin_tz)
35
+
36
+ # Format the time in a human-readable way
37
+ formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S %Z")
38
+
39
+ # Get UTC offset (e.g., +01:00 or +02:00 depending on DST)
40
+ utc_offset = current_time.strftime("%z")
41
+
42
+ # Return structured data
43
+ return {
44
+ "time": formatted_time,
45
+ "timezone": "Europe/Berlin",
46
+ "timestamp": current_time.isoformat(),
47
+ "utc_offset": utc_offset,
48
+ "day_of_week": current_time.strftime("%A"),
49
+ "is_dst": bool(current_time.dst())
50
+ }
51
+
52
+
53
+ # Simple Interface - just like the example you showed
54
+ demo = gr.Interface(
55
+ fn=get_berlin_time,
56
+ inputs=[], # Empty list = no inputs needed
57
+ outputs=gr.JSON(label="Berlin Time Information"),
58
+ title="πŸ• Berlin Time MCP Server",
59
+ description="""
60
+ This is an MCP (Model Context Protocol) server that provides the current time in Berlin.
61
+
62
+ **For Testing:** Click 'Submit' below to see the current Berlin time.
63
+
64
+ **For AI Agents:** This server exposes the `get_berlin_time` tool via MCP protocol.
65
+ AI agents can connect to this server and call this tool when they need Berlin time.
66
+ """,
67
+ api_name="get_berlin_time"
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ # Launch with MCP server enabled
72
+ demo.launch(
73
+ mcp_server=True,
74
+ share=False,
75
+ server_name="0.0.0.0",
76
+ server_port=7860
77
+ )
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ pytz