1414from opentelemetry .sdk .trace import TracerProvider
1515from opentelemetry .sdk .trace .export import BatchSpanProcessor
1616from pysignalr .client import CompletionMessage , SignalRClient
17+ from uipath import UiPath
1718from uipath ._cli ._runtime ._contracts import (
1819 UiPathBaseRuntime ,
1920 UiPathErrorCategory ,
2526from ._context import UiPathMcpRuntimeContext , UiPathServerType
2627from ._exception import UiPathMcpRuntimeError
2728from ._session import SessionServer
28- from uipath import UiPath
2929
3030logger = logging .getLogger (__name__ )
3131tracer = trace .get_tracer (__name__ )
@@ -76,6 +76,26 @@ async def execute(self) -> Optional[UiPathRuntimeResult]:
7676 # Set up SignalR client
7777 signalr_url = f"{ os .environ .get ('UIPATH_URL' )} /agenthub_/wsstunnel?slug={ self ._server .name } &runtimeId={ self ._runtime_id } "
7878
79+ if not self .context .folder_key :
80+ folder_path = os .environ .get ("UIPATH_FOLDER_PATH" )
81+ if not folder_path :
82+ raise UiPathMcpRuntimeError (
83+ "REGISTRATION_ERROR" ,
84+ "No UIPATH_FOLDER_PATH or UIPATH_FOLDER_KEY environment variable set." ,
85+ "Please set the UIPATH_FOLDER_PATH or UIPATH_FOLDER_KEY environment variable." ,
86+ UiPathErrorCategory .USER ,
87+ )
88+ self .context .folder_key = self ._uipath .folders .retrieve_key (folder_path = folder_path )
89+ if not self .context .folder_key :
90+ raise UiPathMcpRuntimeError (
91+ "REGISTRATION_ERROR" ,
92+ "Folder NOT FOUND. Invalid UIPATH_FOLDER_PATH environment variable." ,
93+ "Please set the UIPATH_FOLDER_PATH or UIPATH_FOLDER_KEY environment variable." ,
94+ UiPathErrorCategory .USER ,
95+ )
96+
97+ logger .info (f"Folder key: { self .context .folder_key } " )
98+
7999 with tracer .start_as_current_span (self ._server .name ) as root_span :
80100 root_span .set_attribute ("runtime_id" , self ._runtime_id )
81101 root_span .set_attribute ("command" , self ._server .command )
@@ -86,6 +106,7 @@ async def execute(self) -> Optional[UiPathRuntimeResult]:
86106 headers = {
87107 "X-UiPath-Internal-TenantId" : self .context .trace_context .tenant_id ,
88108 "X-UiPath-Internal-AccountId" : self .context .trace_context .org_id ,
109+ "X-UIPATH-FolderKey" : self .context .folder_key ,
89110 },
90111 )
91112 self ._signalr_client .on ("MessageReceived" , self ._handle_signalr_message )
@@ -273,21 +294,6 @@ async def _handle_signalr_close(self) -> None:
273294 async def _register (self ) -> None :
274295 """Register the MCP server with UiPath."""
275296
276- folder_key = os .environ .get ("UIPATH_FOLDER_KEY" )
277- folder_path = os .environ .get ("UIPATH_FOLDER_PATH" )
278- if not folder_key and not folder_path :
279- raise UiPathMcpRuntimeError (
280- "REGISTRATION_ERROR" ,
281- "No UIPATH_FOLDER_PATH or UIPATH_FOLDER_KEY environment variable set." ,
282- "Please set the UIPATH_FOLDER_PATH or UIPATH_FOLDER_KEY environment variable." ,
283- UiPathErrorCategory .USER ,
284- )
285- if not folder_key :
286- uipath = UiPath ()
287- folder_key = uipath .folders .retrieve_key (folder_path = folder_path )
288-
289- logger .info (f"Folder key: { folder_key } " )
290-
291297 initialization_successful = False
292298 tools_result = None
293299 server_stderr_output = ""
@@ -386,7 +392,7 @@ async def _register(self) -> None:
386392 "POST" ,
387393 f"agenthub_/mcp/{ self ._server .name } /runtime/start?runtimeId={ self ._runtime_id } " ,
388394 json = client_info ,
389- headers = {"X-UIPATH-FolderKey" : folder_key },
395+ headers = {"X-UIPATH-FolderKey" : self . context . folder_key },
390396 )
391397 logger .info ("Registered MCP Server type successfully" )
392398 except Exception as e :
0 commit comments