Spaces:
Running
Running
Fix MCP SSE implementation using raw ASGI routes
Browse files- src/combined_server.py +24 -24
src/combined_server.py
CHANGED
|
@@ -182,32 +182,32 @@ async def handle_call_tool(name: str, arguments: dict | None) -> list[types.Text
|
|
| 182 |
|
| 183 |
# --- 5. Mount MCP SSE Endpoint ---
|
| 184 |
|
| 185 |
-
# We need to manage the SSE transport manually
|
| 186 |
sse_transport = SseServerTransport("/mcp/messages")
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
|
| 212 |
from fastapi.responses import RedirectResponse
|
| 213 |
|
|
|
|
| 182 |
|
| 183 |
# --- 5. Mount MCP SSE Endpoint ---
|
| 184 |
|
| 185 |
+
# We need to manage the SSE transport manually using raw ASGI routes
|
| 186 |
sse_transport = SseServerTransport("/mcp/messages")
|
| 187 |
|
| 188 |
+
async def handle_sse(scope, receive, send):
|
| 189 |
+
"""
|
| 190 |
+
Raw ASGI handler for SSE endpoint.
|
| 191 |
+
Uses sse_transport.connect_sse to manage the connection and streams.
|
| 192 |
+
"""
|
| 193 |
+
async with sse_transport.connect_sse(scope, receive, send) as (read_stream, write_stream):
|
| 194 |
+
# Run the MCP server with the streams
|
| 195 |
+
await mcp_server.run(
|
| 196 |
+
read_stream,
|
| 197 |
+
write_stream,
|
| 198 |
+
mcp_server.create_initialization_options()
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
async def handle_messages(scope, receive, send):
|
| 202 |
+
"""
|
| 203 |
+
Raw ASGI handler for Messages endpoint.
|
| 204 |
+
Delegates to sse_transport.handle_post_message.
|
| 205 |
+
"""
|
| 206 |
+
await sse_transport.handle_post_message(scope, receive, send)
|
| 207 |
+
|
| 208 |
+
# Add routes directly to the FastAPI app (which is a Starlette app)
|
| 209 |
+
app.add_route("/mcp/sse", handle_sse, methods=["GET"])
|
| 210 |
+
app.add_route("/mcp/messages", handle_messages, methods=["POST"])
|
| 211 |
|
| 212 |
from fastapi.responses import RedirectResponse
|
| 213 |
|