@@ -363,7 +363,6 @@ async def _register(self) -> None:
363363 """Register the MCP server with UiPath."""
364364 server = cast (McpServer , self ._server )
365365
366- initialization_successful = False
367366 tools_result : Optional [ListToolsResult ] = None
368367 server_stderr_output = ""
369368 env_vars = server .env
@@ -375,6 +374,7 @@ async def _register(self) -> None:
375374 if name not in env_vars :
376375 env_vars [name ] = value
377376
377+ # Create a temporary session to get tools
378378 try :
379379 # Create a temporary session to get tools
380380 server_params = StdioServerParameters (
@@ -387,49 +387,34 @@ async def _register(self) -> None:
387387 # Use a temporary file to capture stderr
388388 with tempfile .TemporaryFile (mode = "w+b" ) as stderr_temp_binary :
389389 stderr_temp = io .TextIOWrapper (stderr_temp_binary , encoding = "utf-8" )
390- async with stdio_client (server_params , errlog = stderr_temp ) as (
391- read ,
392- write ,
393- ):
394- async with ClientSession (read , write ) as session :
395- logger .info ("Initializing client session..." )
396- # Try to initialize with timeout
397- try :
390+ try :
391+ async with stdio_client (server_params , errlog = stderr_temp ) as (
392+ read ,
393+ write ,
394+ ):
395+ async with ClientSession (read , write ) as session :
396+ logger .info ("Initializing client session..." )
398397 await asyncio .wait_for (session .initialize (), timeout = 30 )
399- initialization_successful = True
400398 logger .info ("Initialization successful" )
401-
402- # Only proceed if initialization was successful
403399 tools_result = await session .list_tools ()
404- # logger.info(tools_result)
405- except asyncio .TimeoutError :
406- logger .error ("Initialization timed out" )
407- # Capture stderr output here, after the timeout
408- stderr_temp .seek (0 )
409- server_stderr_output = stderr_temp .read ()
410-
411- except* Exception as eg :
412- for e in eg .exceptions :
413- logger .error (
414- f"Unexpected error: { e } " ,
415- exc_info = True ,
416- )
417-
418- # Now that we're outside the context managers, check if initialization succeeded
419- if not initialization_successful :
400+ except Exception :
401+ stderr_temp .seek (0 )
402+ server_stderr_output = stderr_temp .read ()
403+ raise
404+ except Exception as e :
420405 await self ._on_runtime_abort ()
421- error_message = "The server process failed to initialize. Verify environment variables are set correctly ."
406+ error_detail = "The server process failed to initialize."
422407 if server_stderr_output :
423- error_message += f"\n Server error output:\n { server_stderr_output } "
408+ error_detail += f"\n \n Server error output:\n { server_stderr_output } "
409+
424410 raise UiPathMcpRuntimeError (
425411 McpErrorCode .INITIALIZATION_ERROR ,
426412 "Server initialization failed" ,
427- error_message ,
413+ error_detail ,
428414 UiPathErrorCategory .DEPLOYMENT ,
429- )
415+ ) from e
430416
431- # If we got here, initialization was successful and we have the tools
432- # Now continue with registration
417+ # If we got here, initialization was successful - continue with registration
433418 logger .info ("Registering server runtime ..." )
434419 try :
435420 if not tools_result :
0 commit comments