MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / build_sse_app

Function build_sse_app

tests/interaction/_connect.py:323–346  ·  view source on GitHub ↗

Mount a server on a Starlette app exposing the legacy SSE transport at /sse and /messages/. `MCPServer.sse_app()` exists but does not expose the underlying `SseServerTransport`, which the SSE-specific tests need; building the app explicitly here gives both server flavours the same routi

(server: Server | MCPServer)

Source from the content-addressed store, hash-verified

321
322
323def build_sse_app(server: Server | MCPServer) -> tuple[Starlette, SseServerTransport]:
324 """Mount a server on a Starlette app exposing the legacy SSE transport at /sse and /messages/.
325
326 `MCPServer.sse_app()` exists but does not expose the underlying `SseServerTransport`, which
327 the SSE-specific tests need; building the app explicitly here gives both server flavours the
328 same routing while keeping that handle.
329 """
330 sse = SseServerTransport(
331 "/messages/", security_settings=TransportSecuritySettings(enable_dns_rebinding_protection=False)
332 )
333 lowlevel = server._lowlevel_server if isinstance(server, MCPServer) else server
334
335 async def handle_sse(request: Request) -> Response:
336 async with sse.connect_sse(request.scope, request.receive, request._send) as (read, write):
337 await lowlevel.run(read, write, lowlevel.create_initialization_options())
338 return Response()
339
340 app = Starlette(
341 routes=[
342 Route("/sse", endpoint=handle_sse, methods=["GET"]),
343 Mount("/messages/", app=sse.handle_post_message),
344 ],
345 )
346 return app, sse
347
348
349@asynccontextmanager

Calls 2

SseServerTransportClass · 0.90