@@ -56,36 +56,42 @@ async def handle_global_call_tool(name: str, arguments: dict) -> list[types.Text
5656
5757
5858sse = SseServerTransport ("/api/v1/mcp/" )
59+
60+
5961########################################################
6062# SseServerTransport.connect_sse handles the full ASGI response internally
6163# but FastAPI still expects the endpoint to return a Response,
6264# this results in ungraceful termination of the SSE connection
6365# We use this class to return a Response that FastAPI can handle gracefully
6466########################################################
6567class SSECompletedNoOp (Response ):
66- async def __call__ (self , scope , receive , send ) -> None : # noqa: ARG002
68+ async def __call__ (self , scope , receive , send ) -> None : # noqa: ARG002
6769 # connect_sse already produced the ASGI response; nothing left to send.
6870 return
6971
72+
7073# Manage state of the Streamable HTTP session manager
7174streamable_http_session_manager : StreamableHTTPSessionManager | None = None
7275
76+
7377def init_streamable_http_manager (* , stateless : bool = True ) -> StreamableHTTPSessionManager :
7478 """Create and register a Streamable HTTP session manager for the global MCP server."""
75- global streamable_http_session_manager # noqa: PLW0603
79+ global streamable_http_session_manager # noqa: PLW0603
7680 streamable_http_session_manager = StreamableHTTPSessionManager (server , stateless = stateless )
7781 return streamable_http_session_manager
7882
83+
7984def get_streamable_http_manager () -> StreamableHTTPSessionManager :
8085 """Fetch the active Streamable HTTP session manager or raise if it is unavailable."""
81- global streamable_http_session_manager # noqa: PLW0602
86+ global streamable_http_session_manager # noqa: PLW0602
8287 if streamable_http_session_manager is None :
8388 raise HTTPException (status_code = 503 , detail = "MCP Streamable HTTP transport is not initialized" )
8489 return streamable_http_session_manager
8590
91+
8692def clear_streamable_http_manager () -> None :
8793 """Clear the currently active Streamable HTTP session manager reference."""
88- global streamable_http_session_manager # noqa: PLW0603
94+ global streamable_http_session_manager # noqa: PLW0603
8995 streamable_http_session_manager = None
9096
9197
@@ -187,6 +193,8 @@ async def _dispatch_streamable_http(
187193
188194
189195streamable_http_methods = ["GET" , "POST" , "DELETE" ]
196+
197+
190198@router .api_route ("/streamable" , methods = streamable_http_methods )
191199@router .api_route ("/streamable/" , methods = streamable_http_methods )
192200async def handle_streamable_http (request : Request , current_user : CurrentActiveMCPUser ):
0 commit comments