Skip to content

Commit 4c51e47

Browse files
committed
fix: runtime error handliing
1 parent b8a1501 commit 4c51e47

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

src/uipath_mcp/_cli/_runtime/_runtime.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -387,49 +386,34 @@ async def _register(self) -> None:
387386
# Use a temporary file to capture stderr
388387
with tempfile.TemporaryFile(mode="w+b") as stderr_temp_binary:
389388
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:
389+
try:
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...")
398396
await asyncio.wait_for(session.initialize(), timeout=30)
399-
initialization_successful = True
400397
logger.info("Initialization successful")
401-
402-
# Only proceed if initialization was successful
403398
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:
399+
except Exception:
400+
stderr_temp.seek(0)
401+
server_stderr_output = stderr_temp.read()
402+
raise
403+
except Exception as e:
420404
await self._on_runtime_abort()
421-
error_message = "The server process failed to initialize. Verify environment variables are set correctly."
405+
error_detail = "The server process failed to initialize."
422406
if server_stderr_output:
423-
error_message += f"\nServer error output:\n{server_stderr_output}"
407+
error_detail += f"\n\nServer error output:\n{server_stderr_output}"
408+
424409
raise UiPathMcpRuntimeError(
425410
McpErrorCode.INITIALIZATION_ERROR,
426411
"Server initialization failed",
427-
error_message,
412+
error_detail,
428413
UiPathErrorCategory.DEPLOYMENT,
429-
)
414+
) from e
430415

431-
# If we got here, initialization was successful and we have the tools
432-
# Now continue with registration
416+
# If we got here, initialization was successful - continue with registration
433417
logger.info("Registering server runtime ...")
434418
try:
435419
if not tools_result:

0 commit comments

Comments
 (0)