2525
2626import  mcp .types  as  types 
2727from  mcp .client .session  import  ClientSession 
28- from  mcp .client .streamable_http  import  streamablehttp_client 
28+ from  mcp .client .streamable_http  import  HEADER_CAPTURE ,  streamablehttp_client 
2929from  mcp .server  import  Server 
3030from  mcp .server .streamable_http  import  (
3131    MCP_SESSION_ID_HEADER ,
@@ -262,7 +262,7 @@ async def header_capture_wrapper(scope, receive, send):
262262
263263        # Return error response with headers in body 
264264        response  =  Response (
265-             "[TESTING_HEADER_CAPTURE]:"  +  json .dumps ({"headers" : headers }),
265+             HEADER_CAPTURE  +  json .dumps ({"headers" : headers }),
266266            status_code = 418 ,
267267        )
268268        await  response (scope , receive , send )
@@ -279,7 +279,7 @@ async def header_capture_wrapper(scope, receive, send):
279279
280280
281281def  _get_captured_headrs (str ) ->  dict [str , str ]:
282-     return  json .loads (str .split ("[TESTING_HEADER_CAPTURE]:" )[1 ])["headers" ]
282+     return  json .loads (str .split (HEADER_CAPTURE )[1 ])["headers" ]
283283
284284
285285def  run_server (
@@ -356,21 +356,23 @@ def _start_basic_server(
356356
357357    # Wait for server to be running 
358358    max_attempts  =  20 
359-     for  attempt  in  range (max_attempts ):
359+     attempt  =  0 
360+     while  attempt  <  max_attempts :
360361        try :
361362            with  socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as  s :
362363                s .connect (("127.0.0.1" , basic_server_port ))
363364                break 
364365        except  ConnectionRefusedError :
365366            time .sleep (0.1 )
367+             attempt  +=  1 
366368    else :
367369        raise  RuntimeError (f"Server failed to start after { max_attempts }  )
368370
369-     try : 
370-          yield 
371-     finally : 
372-          proc .kill ()
373-          proc .join (timeout = 2 )
371+     yield 
372+ 
373+     # Clean up 
374+     proc .kill ()
375+     proc .join (timeout = 2 )
374376
375377
376378@pytest .fixture  
0 commit comments