Entry point for the SSE connection.
(request: Request)
| 78 | |
| 79 | |
| 80 | async def handle_sse(request: Request): |
| 81 | """Entry point for the SSE connection.""" |
| 82 | logger.info("SSE client connected") |
| 83 | sender = _SendTracker(request._send) |
| 84 | try: |
| 85 | async with sse.connect_sse(request.scope, request.receive, sender) as (read_stream, write_stream): |
| 86 | await mcp_server.run( |
| 87 | read_stream, |
| 88 | write_stream, |
| 89 | InitializationOptions( |
| 90 | server_name="CodeGraphContext", |
| 91 | server_version="0.1.0", |
| 92 | capabilities=ServerCapabilities( |
| 93 | tools=ToolsCapability(listChanged=False) |
| 94 | ) |
| 95 | ) |
| 96 | ) |
| 97 | except anyio.EndOfStream: |
| 98 | logger.debug("SSE client disconnected cleanly (stream ended)") |
| 99 | except Exception as exc: |
| 100 | logger.debug("SSE connection closed: %s", type(exc).__name__) |
| 101 | finally: |
| 102 | logger.info("SSE client disconnected — handler exited, resources freed") |
| 103 | if sender.response_started: |
| 104 | return _AlreadySentResponse() |
| 105 | # connect_sse bailed out before writing anything (e.g. rejected origin). |
| 106 | return Response(status_code=500, content="SSE connection could not be established") |
| 107 | |
| 108 | |
| 109 | async def handle_messages(request: Request): |