Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import json
|
3 |
import logging
|
4 |
import anyio
|
|
|
5 |
from fastapi import FastAPI, Request
|
6 |
from fastapi.responses import Response
|
7 |
from sse_starlette import EventSourceResponse
|
@@ -53,6 +54,10 @@ async def handle_sse(request: Request):
|
|
53 |
write_streams[session_id] = write_streams.pop(placeholder_id)
|
54 |
logger.info(f"Updated placeholder {placeholder_id} to session_id {session_id}")
|
55 |
await sse_stream_writer.send({"event": event, "data": data})
|
|
|
|
|
|
|
|
|
56 |
|
57 |
sse_stream_writer, sse_stream_reader = anyio.create_memory_object_stream(0)
|
58 |
async with transport.connect_sse(request.scope, request.receive, request._send) as (read_stream, write_stream):
|
@@ -86,6 +91,31 @@ async def handle_post(request: Request):
|
|
86 |
logger.info(f"Associated placeholder {sid} with session_id {session_id}")
|
87 |
break
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
if message.get("method") == "tools/list" and write_stream:
|
90 |
response = {
|
91 |
"jsonrpc": "2.0",
|
|
|
2 |
import json
|
3 |
import logging
|
4 |
import anyio
|
5 |
+
import asyncio
|
6 |
from fastapi import FastAPI, Request
|
7 |
from fastapi.responses import Response
|
8 |
from sse_starlette import EventSourceResponse
|
|
|
54 |
write_streams[session_id] = write_streams.pop(placeholder_id)
|
55 |
logger.info(f"Updated placeholder {placeholder_id} to session_id {session_id}")
|
56 |
await sse_stream_writer.send({"event": event, "data": data})
|
57 |
+
# Keep-alive loop to maintain the SSE connection
|
58 |
+
while True:
|
59 |
+
await sse_stream_writer.send({"event": "ping", "data": "keep-alive"})
|
60 |
+
await asyncio.sleep(15) # Send keep-alive every 15 seconds
|
61 |
|
62 |
sse_stream_writer, sse_stream_reader = anyio.create_memory_object_stream(0)
|
63 |
async with transport.connect_sse(request.scope, request.receive, request._send) as (read_stream, write_stream):
|
|
|
91 |
logger.info(f"Associated placeholder {sid} with session_id {session_id}")
|
92 |
break
|
93 |
|
94 |
+
if message.get("method") == "initialize" and write_stream:
|
95 |
+
response = {
|
96 |
+
"jsonrpc": "2.0",
|
97 |
+
"id": message.get("id"),
|
98 |
+
"result": {
|
99 |
+
"protocolVersion": "2025-03-26",
|
100 |
+
"capabilities": {
|
101 |
+
"tools": {"listChanged": True},
|
102 |
+
"prompts": {"listChanged": False},
|
103 |
+
"resources": {"subscribe": False, "listChanged": False},
|
104 |
+
"logging": {},
|
105 |
+
"experimental": {}
|
106 |
+
},
|
107 |
+
"serverInfo": {
|
108 |
+
"name": "airtable-mcp",
|
109 |
+
"version": "1.0.0"
|
110 |
+
},
|
111 |
+
"instructions": "Airtable MCP server for listing and creating records."
|
112 |
+
}
|
113 |
+
}
|
114 |
+
response_data = json.dumps(response)
|
115 |
+
await write_stream.send({"event": "message", "data": response_data})
|
116 |
+
logger.info(f"Sent initialize response for session {session_id} via write_stream")
|
117 |
+
return Response(status_code=202)
|
118 |
+
|
119 |
if message.get("method") == "tools/list" and write_stream:
|
120 |
response = {
|
121 |
"jsonrpc": "2.0",
|