@@ -155,25 +155,24 @@ async def handle_streamable_http(scope, receive, send):
155155 mcp_session_id = new_session_id ,
156156 is_json_response_enabled = json_response ,
157157 )
158- async with http_transport .connect () as streams :
159- read_stream , write_stream = streams
158+ server_instances [http_transport .mcp_session_id ] = http_transport
159+ async with http_transport .connect () as streams :
160+ read_stream , write_stream = streams
160161
161- async def run_server ():
162- await app .run (
163- read_stream ,
164- write_stream ,
165- app .create_initialization_options (),
166- )
162+ async def run_server ():
163+ await app .run (
164+ read_stream ,
165+ write_stream ,
166+ app .create_initialization_options (),
167+ )
167168
168- if not task_group :
169- raise RuntimeError ("Task group is not initialized" )
169+ if not task_group :
170+ raise RuntimeError ("Task group is not initialized" )
170171
171- # Store the instance before starting the task to prevent races
172- server_instances [http_transport .mcp_session_id ] = http_transport
173- task_group .start_soon (run_server )
172+ task_group .start_soon (run_server )
174173
175- # Handle the HTTP request and return the response
176- await http_transport .handle_request (scope , receive , send )
174+ # Handle the HTTP request and return the response
175+ await http_transport .handle_request (scope , receive , send )
177176 else :
178177 response = Response (
179178 "Bad Request: No valid session ID provided" ,
0 commit comments