@@ -207,6 +207,9 @@ async def _register(self) -> None:
207207 """Register the MCP server type with UiPath."""
208208 logger .info (f"Registering MCP server type: { self .server .name } " )
209209
210+ initialization_successful = False
211+ tools_result = None
212+
210213 try :
211214 # Create a temporary session to get tools
212215 server_params = StdioServerParameters (
@@ -219,50 +222,79 @@ async def _register(self) -> None:
219222 async with stdio_client (server_params ) as (read , write ):
220223 async with ClientSession (read , write ) as session :
221224 logger .info ("Initializing client session..." )
222- await asyncio .wait_for (
223- session .initialize (),
224- timeout = 30
225- )
226- tools_result = await session .list_tools ()
227- logger .info (tools_result )
228- client_info = {
229- "server" : {
230- "Name" : self .server .name ,
231- "Slug" : self .server .name ,
232- "Version" : "1.0.0" ,
233- "Type" : 1 ,
234- },
235- "tools" : [],
236- }
237-
238- for tool in tools_result .tools :
239- tool_info = {
240- "Type" : 1 ,
241- "Name" : tool .name ,
242- "ProcessType" : "Tool" ,
243- "Description" : tool .description ,
244- }
245- client_info ["tools" ].append (tool_info )
246-
247- # Register with UiPath MCP Server
248- uipath = UiPath ()
249- uipath .api_client .request (
250- "POST" ,
251- f"mcp_/api/servers-with-tools/{ self .server .name } " ,
252- json = client_info ,
253- )
254- logger .info ("Registered MCP Server type successfully" )
255- except asyncio .TimeoutError as e :
225+
226+ # Try to initialize with timeout
227+ try :
228+ await asyncio .wait_for (
229+ session .initialize (),
230+ timeout = 30
231+ )
232+ initialization_successful = True
233+ logger .info ("Initialization successful" )
234+
235+ # Only proceed if initialization was successful
236+ tools_result = await session .list_tools ()
237+ logger .info (tools_result )
238+ except asyncio .TimeoutError :
239+ logger .error ("Initialization timed out" )
240+ # We'll handle this after exiting the context managers
241+
242+ # We don't continue with registration here - we'll do it after the context managers
243+
244+ except Exception as e :
245+ # Handle any other exceptions that occur
246+ logger .error (f"Error during server initialization: { e } " )
256247 raise UiPathMcpRuntimeError (
257- "TIMEOUT_ERROR " ,
248+ "SERVER_ERROR " ,
258249 "Server initialization failed" ,
259250 str (e ),
260251 UiPathErrorCategory .DEPLOYMENT ,
261252 ) from e
253+
254+ # Now that we're outside the context managers, check if initialization succeeded
255+ if not initialization_successful :
256+ raise UiPathMcpRuntimeError (
257+ "TIMEOUT_ERROR" ,
258+ "Server initialization timed out" ,
259+ "The server process did not respond in time. Verify environment variables are set correctly." ,
260+ UiPathErrorCategory .DEPLOYMENT ,
261+ )
262+
263+ # If we got here, initialization was successful and we have the tools
264+ # Now continue with registration
265+ try :
266+ client_info = {
267+ "server" : {
268+ "Name" : self .server .name ,
269+ "Slug" : self .server .name ,
270+ "Version" : "1.0.0" ,
271+ "Type" : 1 ,
272+ },
273+ "tools" : [],
274+ }
275+
276+ for tool in tools_result .tools :
277+ tool_info = {
278+ "Type" : 1 ,
279+ "Name" : tool .name ,
280+ "ProcessType" : "Tool" ,
281+ "Description" : tool .description ,
282+ }
283+ client_info ["tools" ].append (tool_info )
284+
285+ # Register with UiPath MCP Server
286+ uipath = UiPath ()
287+ uipath .api_client .request (
288+ "POST" ,
289+ f"mcp_/api/servers-with-tools/{ self .server .name } " ,
290+ json = client_info ,
291+ )
292+ logger .info ("Registered MCP Server type successfully" )
262293 except Exception as e :
294+ logger .error (f"Error during registration: { e } " )
263295 raise UiPathMcpRuntimeError (
264- "NETWORK_ERROR " ,
265- "Failed to register with UiPath MCP Server" ,
296+ "REGISTRATION_ERROR " ,
297+ "Failed to register MCP Server" ,
266298 str (e ),
267299 UiPathErrorCategory .SYSTEM ,
268300 ) from e
0 commit comments