@@ -101,6 +101,48 @@ async def running_manager():
101101 yield manager , app
102102
103103
104+ @pytest .mark .anyio
105+ async def test_streamable_http_post_sse_cleans_up_streams_when_response_returns (monkeypatch : pytest .MonkeyPatch ):
106+ transport = StreamableHTTPServerTransport (mcp_session_id = None )
107+ sent_messages : list [Message ] = []
108+ body = json .dumps ({"jsonrpc" : "2.0" , "id" : 1 , "method" : "tools/list" , "params" : {}}).encode ()
109+
110+ class DisconnectingEventSourceResponse :
111+ def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
112+ pass
113+
114+ async def __call__ (self , scope : Scope , receive : Any , send : Any ) -> None :
115+ await send ({"type" : "http.response.start" , "status" : 200 , "headers" : []})
116+
117+ async def send (message : Message ) -> None :
118+ sent_messages .append (message )
119+
120+ async def receive () -> Message :
121+ return {"type" : "http.request" , "body" : body , "more_body" : False }
122+
123+ scope : Scope = {
124+ "type" : "http" ,
125+ "method" : "POST" ,
126+ "path" : "/mcp" ,
127+ "headers" : [
128+ (b"accept" , b"application/json, text/event-stream" ),
129+ (b"content-type" , b"application/json" ),
130+ ],
131+ }
132+
133+ monkeypatch .setattr ("mcp.server.streamable_http.EventSourceResponse" , DisconnectingEventSourceResponse )
134+
135+ async with transport .connect () as (read_stream , _write_stream ):
136+ async with anyio .create_task_group () as tg :
137+ tg .start_soon (transport .handle_request , scope , receive , send )
138+ session_message = await read_stream .receive ()
139+ assert session_message .message .method == "tools/list"
140+
141+ assert transport ._request_streams == {}
142+ assert transport ._sse_stream_writers == {}
143+ assert any (message ["type" ] == "http.response.start" for message in sent_messages )
144+
145+
104146@pytest .mark .anyio
105147async def test_stateful_session_cleanup_on_graceful_exit (running_manager : tuple [StreamableHTTPSessionManager , Server ]):
106148 manager , _app = running_manager
0 commit comments