Spaces:
Sleeping
Sleeping
endpoint changed
Browse files
app.py
CHANGED
|
@@ -13,6 +13,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 13 |
from fastapi.responses import StreamingResponse
|
| 14 |
from pydantic import BaseModel
|
| 15 |
import uvicorn
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Configure logging
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -319,5 +321,28 @@ async def list_docs():
|
|
| 319 |
|
| 320 |
return {"documents": docs_data}
|
| 321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
if __name__ == "__main__":
|
| 323 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 13 |
from fastapi.responses import StreamingResponse
|
| 14 |
from pydantic import BaseModel
|
| 15 |
import uvicorn
|
| 16 |
+
import asyncio
|
| 17 |
+
import json
|
| 18 |
|
| 19 |
# Configure logging
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 321 |
|
| 322 |
return {"documents": docs_data}
|
| 323 |
|
| 324 |
+
@app.get("/mcp/sse")
|
| 325 |
+
async def mcp_sse():
|
| 326 |
+
"""SSE endpoint for MCP communication"""
|
| 327 |
+
async def event_generator():
|
| 328 |
+
# Send initial connection event
|
| 329 |
+
yield f"data: {json.dumps({'type': 'connection', 'status': 'connected'})}\n\n"
|
| 330 |
+
|
| 331 |
+
# Keep connection alive
|
| 332 |
+
while True:
|
| 333 |
+
await asyncio.sleep(30) # Send heartbeat every 30 seconds
|
| 334 |
+
yield f"data: {json.dumps({'type': 'heartbeat', 'timestamp': asyncio.get_event_loop().time()})}\n\n"
|
| 335 |
+
|
| 336 |
+
return StreamingResponse(
|
| 337 |
+
event_generator(),
|
| 338 |
+
media_type="text/event-stream",
|
| 339 |
+
headers={
|
| 340 |
+
"Cache-Control": "no-cache",
|
| 341 |
+
"Connection": "keep-alive",
|
| 342 |
+
"Access-Control-Allow-Origin": "*",
|
| 343 |
+
"Access-Control-Allow-Headers": "*",
|
| 344 |
+
}
|
| 345 |
+
)
|
| 346 |
+
|
| 347 |
if __name__ == "__main__":
|
| 348 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|