Spaces:
Running
Running
Meet Patel
commited on
Commit
·
35bb43f
1
Parent(s):
0d547dd
Add periodic ping functionality to MCP server
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import os
|
|
| 6 |
import json
|
| 7 |
import asyncio
|
| 8 |
import gradio as gr
|
| 9 |
-
from typing import Optional, Dict, Any, List, Union, Tuple
|
| 10 |
import requests
|
| 11 |
import tempfile
|
| 12 |
import base64
|
|
@@ -14,6 +14,8 @@ import re
|
|
| 14 |
import networkx as nx
|
| 15 |
import matplotlib
|
| 16 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Set matplotlib to use 'Agg' backend to avoid GUI issues in Gradio
|
| 19 |
matplotlib.use('Agg')
|
|
@@ -28,6 +30,25 @@ SERVER_URL = "https://tutorx-mcp.onrender.com/sse" # Ensure this is the SSE end
|
|
| 28 |
|
| 29 |
# Utility functions
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
async def load_concept_graph(concept_id: str = None) -> Tuple[Optional[plt.Figure], Dict, List]:
|
| 33 |
"""
|
|
|
|
| 6 |
import json
|
| 7 |
import asyncio
|
| 8 |
import gradio as gr
|
| 9 |
+
from typing import Optional, Dict, Any, List, Union, Tuple, Callable
|
| 10 |
import requests
|
| 11 |
import tempfile
|
| 12 |
import base64
|
|
|
|
| 14 |
import networkx as nx
|
| 15 |
import matplotlib
|
| 16 |
import matplotlib.pyplot as plt
|
| 17 |
+
import time
|
| 18 |
+
from datetime import datetime
|
| 19 |
|
| 20 |
# Set matplotlib to use 'Agg' backend to avoid GUI issues in Gradio
|
| 21 |
matplotlib.use('Agg')
|
|
|
|
| 30 |
|
| 31 |
# Utility functions
|
| 32 |
|
| 33 |
+
async def ping_mcp_server() -> None:
|
| 34 |
+
"""Send a ping request to the MCP server"""
|
| 35 |
+
try:
|
| 36 |
+
async with sse_client(SERVER_URL) as (sse, write):
|
| 37 |
+
async with ClientSession(sse, write) as session:
|
| 38 |
+
await session.initialize()
|
| 39 |
+
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Successfully pinged MCP server")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error pinging MCP server: {str(e)}")
|
| 42 |
+
|
| 43 |
+
async def start_periodic_ping(interval_minutes: int = 10) -> None:
|
| 44 |
+
"""Start a background task to ping the MCP server periodically"""
|
| 45 |
+
while True:
|
| 46 |
+
await ping_mcp_server()
|
| 47 |
+
await asyncio.sleep(interval_minutes * 60)
|
| 48 |
+
|
| 49 |
+
# Start the periodic ping task when the module loads
|
| 50 |
+
ping_task = asyncio.create_task(start_periodic_ping())
|
| 51 |
+
|
| 52 |
|
| 53 |
async def load_concept_graph(concept_id: str = None) -> Tuple[Optional[plt.Figure], Dict, List]:
|
| 54 |
"""
|