@@ -110,18 +110,6 @@ async def create_mcp_tools_from_metadata(
110110 if config .is_enabled is False :
111111 return []
112112
113- sdk = UiPath ()
114- mcpServer : McpServer = await sdk .mcp .retrieve_async (
115- slug = config .slug , folder_path = config .folder_path
116- )
117-
118- default_client_kwargs = get_httpx_client_kwargs ()
119- client_kwargs = {
120- ** default_client_kwargs ,
121- "headers" : {"Authorization" : f"Bearer { sdk ._config .secret } " },
122- "timeout" : httpx .Timeout (600 ),
123- }
124-
125113 # Lazy import to improve cold start time
126114 from mcp import ClientSession
127115 from mcp .client .streamable_http import streamable_http_client
@@ -132,7 +120,7 @@ async def create_mcp_tools_from_metadata(
132120 tool_name = sanitize_tool_name (mcp_tool .name )
133121 input_model : Any = create_model (mcp_tool .input_schema )
134122
135- def get_tool_coroutine (mcp_tool : AgentMcpTool , input_model : Any ) -> Any :
123+ def build_mcp_tool (mcp_tool : AgentMcpTool , input_model : Any ) -> Any :
136124 output_schema : Any
137125 if mcp_tool .output_schema :
138126 output_schema = create_model (mcp_tool .output_schema )
@@ -148,6 +136,18 @@ def get_tool_coroutine(mcp_tool: AgentMcpTool, input_model: Any) -> Any:
148136 async def tool_fn (** kwargs : Any ) -> Any :
149137 """Execute MCP tool call with ephemeral session."""
150138 async with AsyncExitStack () as stack :
139+ sdk = UiPath ()
140+ mcpServer : McpServer = await sdk .mcp .retrieve_async (
141+ slug = config .slug , folder_path = config .folder_path
142+ )
143+
144+ default_client_kwargs = get_httpx_client_kwargs ()
145+ client_kwargs = {
146+ ** default_client_kwargs ,
147+ "headers" : {"Authorization" : f"Bearer { sdk ._config .secret } " },
148+ "timeout" : httpx .Timeout (600 ),
149+ }
150+
151151 # Create HTTP client
152152 http_client = await stack .enter_async_context (
153153 httpx .AsyncClient (** client_kwargs )
@@ -176,7 +176,7 @@ async def tool_fn(**kwargs: Any) -> Any:
176176 name = tool_name ,
177177 description = mcp_tool .description ,
178178 args_schema = input_model ,
179- coroutine = get_tool_coroutine (mcp_tool , input_model ),
179+ coroutine = build_mcp_tool (mcp_tool , input_model ),
180180 metadata = {
181181 "tool_type" : "mcp" ,
182182 "display_name" : mcp_tool .name ,
0 commit comments