nvidia-nat-mcp 1.3.0a20251005__py3-none-any.whl → 1.3.0a20251007__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,131 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ from datetime import timedelta
17
+ from typing import Literal
18
+
19
+ from pydantic import BaseModel
20
+ from pydantic import Field
21
+ from pydantic import HttpUrl
22
+ from pydantic import model_validator
23
+
24
+ from nat.data_models.component_ref import AuthenticationRef
25
+ from nat.data_models.function import FunctionGroupBaseConfig
26
+
27
+
28
+ class MCPToolOverrideConfig(BaseModel):
29
+ """
30
+ Configuration for overriding tool properties when exposing from MCP server.
31
+ """
32
+ alias: str | None = Field(default=None, description="Override the tool name (function name in the workflow)")
33
+ description: str | None = Field(default=None, description="Override the tool description")
34
+
35
+
36
+ class MCPServerConfig(BaseModel):
37
+ """
38
+ Server connection details for MCP client.
39
+ Supports stdio, sse, and streamable-http transports.
40
+ streamable-http is the recommended default for HTTP-based connections.
41
+ """
42
+ transport: Literal["stdio", "sse", "streamable-http"] = Field(
43
+ ..., description="Transport type to connect to the MCP server (stdio, sse, or streamable-http)")
44
+ url: HttpUrl | None = Field(default=None,
45
+ description="URL of the MCP server (for sse or streamable-http transport)")
46
+ command: str | None = Field(default=None,
47
+ description="Command to run for stdio transport (e.g. 'python' or 'docker')")
48
+ args: list[str] | None = Field(default=None, description="Arguments for the stdio command")
49
+ env: dict[str, str] | None = Field(default=None, description="Environment variables for the stdio process")
50
+
51
+ # Authentication configuration
52
+ auth_provider: str | AuthenticationRef | None = Field(default=None,
53
+ description="Reference to authentication provider")
54
+
55
+ @model_validator(mode="after")
56
+ def validate_model(self):
57
+ """Validate that stdio and SSE/Streamable HTTP properties are mutually exclusive."""
58
+ if self.transport == "stdio":
59
+ if self.url is not None:
60
+ raise ValueError("url should not be set when using stdio transport")
61
+ if not self.command:
62
+ raise ValueError("command is required when using stdio transport")
63
+ # Auth is not supported for stdio transport
64
+ if self.auth_provider is not None:
65
+ raise ValueError("Authentication is not supported for stdio transport")
66
+ elif self.transport == "sse":
67
+ if self.command is not None or self.args is not None or self.env is not None:
68
+ raise ValueError("command, args, and env should not be set when using sse transport")
69
+ if not self.url:
70
+ raise ValueError("url is required when using sse transport")
71
+ # Auth is not supported for SSE transport
72
+ if self.auth_provider is not None:
73
+ raise ValueError("Authentication is not supported for SSE transport.")
74
+ elif self.transport == "streamable-http":
75
+ if self.command is not None or self.args is not None or self.env is not None:
76
+ raise ValueError("command, args, and env should not be set when using streamable-http transport")
77
+ if not self.url:
78
+ raise ValueError("url is required when using streamable-http transport")
79
+
80
+ return self
81
+
82
+
83
+ class MCPClientConfig(FunctionGroupBaseConfig, name="mcp_client"):
84
+ """
85
+ Configuration for connecting to an MCP server as a client and exposing selected tools.
86
+ """
87
+ server: MCPServerConfig = Field(..., description="Server connection details (transport, url/command, etc.)")
88
+ tool_call_timeout: timedelta = Field(
89
+ default=timedelta(seconds=60),
90
+ description="Timeout (in seconds) for the MCP tool call. Defaults to 60 seconds.")
91
+ auth_flow_timeout: timedelta = Field(
92
+ default=timedelta(seconds=300),
93
+ description="Timeout (in seconds) for the MCP auth flow. When the tool call requires interactive \
94
+ authentication, this timeout is used. Defaults to 300 seconds.")
95
+ reconnect_enabled: bool = Field(
96
+ default=True,
97
+ description="Whether to enable reconnecting to the MCP server if the connection is lost. \
98
+ Defaults to True.")
99
+ reconnect_max_attempts: int = Field(default=2,
100
+ ge=0,
101
+ description="Maximum number of reconnect attempts. Defaults to 2.")
102
+ reconnect_initial_backoff: float = Field(
103
+ default=0.5, ge=0.0, description="Initial backoff time for reconnect attempts. Defaults to 0.5 seconds.")
104
+ reconnect_max_backoff: float = Field(
105
+ default=50.0, ge=0.0, description="Maximum backoff time for reconnect attempts. Defaults to 50 seconds.")
106
+ tool_overrides: dict[str, MCPToolOverrideConfig] | None = Field(
107
+ default=None,
108
+ description="""Optional tool name overrides and description changes.
109
+ Example:
110
+ tool_overrides:
111
+ calculator_add:
112
+ alias: "add_numbers"
113
+ description: "Add two numbers together"
114
+ calculator_multiply:
115
+ description: "Multiply two numbers" # alias defaults to original name
116
+ """)
117
+ session_aware_tools: bool = Field(default=True,
118
+ description="Session-aware tools are created if True. Defaults to True.")
119
+ max_sessions: int = Field(default=100,
120
+ ge=1,
121
+ description="Maximum number of concurrent session clients. Defaults to 100.")
122
+ session_idle_timeout: timedelta = Field(
123
+ default=timedelta(hours=1),
124
+ description="Time after which inactive sessions are cleaned up. Defaults to 1 hour.")
125
+
126
+ @model_validator(mode="after")
127
+ def _validate_reconnect_backoff(self) -> "MCPClientConfig":
128
+ """Validate reconnect backoff values."""
129
+ if self.reconnect_max_backoff < self.reconnect_initial_backoff:
130
+ raise ValueError("reconnect_max_backoff must be greater than or equal to reconnect_initial_backoff")
131
+ return self
@@ -13,29 +13,41 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
 
16
+ import asyncio
16
17
  import logging
18
+ from contextlib import asynccontextmanager
19
+ from dataclasses import dataclass
20
+ from dataclasses import field
21
+ from datetime import datetime
17
22
  from datetime import timedelta
18
- from typing import Literal
19
23
 
24
+ import aiorwlock
20
25
  from pydantic import BaseModel
21
- from pydantic import Field
22
- from pydantic import HttpUrl
23
- from pydantic import model_validator
24
26
 
27
+ from nat.authentication.interfaces import AuthProviderBase
25
28
  from nat.builder.builder import Builder
26
29
  from nat.builder.function import FunctionGroup
27
30
  from nat.cli.register_workflow import register_function_group
28
- from nat.data_models.component_ref import AuthenticationRef
29
- from nat.data_models.function import FunctionGroupBaseConfig
30
- from nat.plugins.mcp.tool import mcp_tool_function
31
+ from nat.plugins.mcp.client_base import MCPBaseClient
32
+ from nat.plugins.mcp.client_config import MCPClientConfig
33
+ from nat.plugins.mcp.client_config import MCPToolOverrideConfig
34
+ from nat.plugins.mcp.utils import truncate_session_id
31
35
 
32
36
  logger = logging.getLogger(__name__)
33
37
 
34
38
 
39
+ @dataclass
40
+ class SessionData:
41
+ """Container for all session-related data."""
42
+ client: MCPBaseClient
43
+ last_activity: datetime
44
+ ref_count: int = 0
45
+ lock: asyncio.Lock = field(default_factory=asyncio.Lock)
46
+
47
+
35
48
  class MCPFunctionGroup(FunctionGroup):
36
49
  """
37
- A specialized FunctionGroup for MCP clients that includes MCP-specific attributes
38
- with proper type safety.
50
+ A specialized FunctionGroup for MCP clients that includes MCP-specific attributes with session management.
39
51
  """
40
52
 
41
53
  def __init__(self, *args, **kwargs):
@@ -45,6 +57,20 @@ class MCPFunctionGroup(FunctionGroup):
45
57
  self._mcp_client_server_name: str | None = None
46
58
  self._mcp_client_transport: str | None = None
47
59
 
60
+ # Session management - consolidated data structure
61
+ self._sessions: dict[str, SessionData] = {}
62
+
63
+ # Use RWLock for better concurrency: multiple readers (tool calls) can access
64
+ # existing sessions simultaneously, while writers (create/delete) get exclusive access
65
+ self._session_rwlock = aiorwlock.RWLock()
66
+ # Throttled cleanup control
67
+ self._last_cleanup_check: datetime = datetime.now()
68
+ self._cleanup_check_interval: timedelta = timedelta(minutes=5)
69
+
70
+ # Shared components for session client creation
71
+ self._shared_auth_provider: AuthProviderBase | None = None
72
+ self._client_config: MCPClientConfig | None = None
73
+
48
74
  @property
49
75
  def mcp_client(self):
50
76
  """Get the MCP client instance."""
@@ -75,109 +101,253 @@ class MCPFunctionGroup(FunctionGroup):
75
101
  """Set the MCP client transport type."""
76
102
  self._mcp_client_transport = transport
77
103
 
104
+ @property
105
+ def session_count(self) -> int:
106
+ """Current number of active sessions."""
107
+ return len(self._sessions)
78
108
 
79
- class MCPToolOverrideConfig(BaseModel):
80
- """
81
- Configuration for overriding tool properties when exposing from MCP server.
82
- """
83
- alias: str | None = Field(default=None, description="Override the tool name (function name in the workflow)")
84
- description: str | None = Field(default=None, description="Override the tool description")
85
-
86
-
87
- class MCPServerConfig(BaseModel):
88
- """
89
- Server connection details for MCP client.
90
- Supports stdio, sse, and streamable-http transports.
91
- streamable-http is the recommended default for HTTP-based connections.
92
- """
93
- transport: Literal["stdio", "sse", "streamable-http"] = Field(
94
- ..., description="Transport type to connect to the MCP server (stdio, sse, or streamable-http)")
95
- url: HttpUrl | None = Field(default=None,
96
- description="URL of the MCP server (for sse or streamable-http transport)")
97
- command: str | None = Field(default=None,
98
- description="Command to run for stdio transport (e.g. 'python' or 'docker')")
99
- args: list[str] | None = Field(default=None, description="Arguments for the stdio command")
100
- env: dict[str, str] | None = Field(default=None, description="Environment variables for the stdio process")
101
-
102
- # Authentication configuration
103
- auth_provider: str | AuthenticationRef | None = Field(default=None,
104
- description="Reference to authentication provider")
105
-
106
- @model_validator(mode="after")
107
- def validate_model(self):
108
- """Validate that stdio and SSE/Streamable HTTP properties are mutually exclusive."""
109
- if self.transport == "stdio":
110
- if self.url is not None:
111
- raise ValueError("url should not be set when using stdio transport")
112
- if not self.command:
113
- raise ValueError("command is required when using stdio transport")
114
- # Auth is not supported for stdio transport
115
- if self.auth_provider is not None:
116
- raise ValueError("Authentication is not supported for stdio transport")
117
- elif self.transport == "sse":
118
- if self.command is not None or self.args is not None or self.env is not None:
119
- raise ValueError("command, args, and env should not be set when using sse transport")
120
- if not self.url:
121
- raise ValueError("url is required when using sse transport")
122
- # Auth is not supported for SSE transport
123
- if self.auth_provider is not None:
124
- raise ValueError("Authentication is not supported for SSE transport.")
125
- elif self.transport == "streamable-http":
126
- if self.command is not None or self.args is not None or self.env is not None:
127
- raise ValueError("command, args, and env should not be set when using streamable-http transport")
128
- if not self.url:
129
- raise ValueError("url is required when using streamable-http transport")
130
-
131
- return self
132
-
133
-
134
- class MCPClientConfig(FunctionGroupBaseConfig, name="mcp_client"):
135
- """
136
- Configuration for connecting to an MCP server as a client and exposing selected tools.
109
+ @property
110
+ def session_limit(self) -> int:
111
+ """Maximum allowed sessions."""
112
+ return self._client_config.max_sessions if self._client_config else 100
113
+
114
+ def _get_session_id_from_context(self) -> str | None:
115
+ """Get the session ID from the current context."""
116
+ try:
117
+ from nat.builder.context import Context as _Ctx
118
+
119
+ # Get session id from context, authentication is done per-websocket session for tool calls
120
+ session_id = None
121
+ cookies = getattr(_Ctx.get().metadata, "cookies", None)
122
+ if cookies:
123
+ session_id = cookies.get("nat-session")
124
+
125
+ if not session_id:
126
+ # use default user id if allowed
127
+ if self._shared_auth_provider and \
128
+ self._shared_auth_provider.config.allow_default_user_id_for_tool_calls:
129
+ session_id = self._shared_auth_provider.config.default_user_id
130
+ return session_id
131
+ except Exception:
132
+ return None
133
+
134
+ async def cleanup_sessions(self, max_age: timedelta | None = None) -> int:
135
+ """
136
+ Manually trigger cleanup of inactive sessions.
137
+
138
+ Args:
139
+ max_age: Maximum age for sessions before cleanup. If None, uses configured timeout.
140
+
141
+ Returns:
142
+ Number of sessions cleaned up.
143
+ """
144
+ sessions_before = len(self._sessions)
145
+ await self._cleanup_inactive_sessions(max_age)
146
+ sessions_after = len(self._sessions)
147
+ return sessions_before - sessions_after
148
+
149
+ async def _cleanup_inactive_sessions(self, max_age: timedelta | None = None):
150
+ """Remove clients for sessions inactive longer than max_age.
151
+
152
+ This method uses the RWLock writer to ensure thread-safe cleanup.
153
+ """
154
+ if max_age is None:
155
+ max_age = self._client_config.session_idle_timeout if self._client_config else timedelta(hours=1)
156
+
157
+ async with self._session_rwlock.writer:
158
+ current_time = datetime.now()
159
+ inactive_sessions = []
160
+
161
+ for session_id, session_data in self._sessions.items():
162
+ # Skip cleanup if session is actively being used
163
+ if session_data.ref_count > 0:
164
+ continue
165
+
166
+ if current_time - session_data.last_activity > max_age:
167
+ inactive_sessions.append(session_id)
168
+
169
+ for session_id in inactive_sessions:
170
+ try:
171
+ logger.info("Cleaning up inactive session client: %s", truncate_session_id(session_id))
172
+ session_data = self._sessions[session_id]
173
+ # Close the client connection
174
+ await session_data.client.__aexit__(None, None, None)
175
+ logger.info("Cleaned up inactive session client: %s", truncate_session_id(session_id))
176
+ except Exception as e:
177
+ logger.warning("Error cleaning up session client %s: %s", truncate_session_id(session_id), e)
178
+ finally:
179
+ # Always remove from tracking to prevent leaks, even if close failed
180
+ self._sessions.pop(session_id, None)
181
+ logger.info("Cleaned up session tracking for: %s", truncate_session_id(session_id))
182
+ logger.info(" Total sessions: %d", len(self._sessions))
183
+
184
+ async def _get_session_client(self, session_id: str) -> MCPBaseClient:
185
+ """Get the appropriate MCP client for the session."""
186
+ # Throttled cleanup on access
187
+ now = datetime.now()
188
+ if now - self._last_cleanup_check > self._cleanup_check_interval:
189
+ await self._cleanup_inactive_sessions()
190
+ self._last_cleanup_check = now
191
+
192
+ # If the session_id equals the configured default_user_id use the base client
193
+ # instead of creating a per-session client
194
+ if self._shared_auth_provider:
195
+ default_uid = self._shared_auth_provider.config.default_user_id
196
+ if default_uid and session_id == default_uid:
197
+ return self.mcp_client
198
+
199
+ # Fast path: check if session already exists (reader lock for concurrent access)
200
+ async with self._session_rwlock.reader:
201
+ if session_id in self._sessions:
202
+ # Update last activity for existing client
203
+ self._sessions[session_id].last_activity = datetime.now()
204
+ return self._sessions[session_id].client
205
+
206
+ # Check session limit before creating new client (outside writer lock to avoid deadlock)
207
+ if self._client_config and len(self._sessions) >= self._client_config.max_sessions:
208
+ # Try cleanup first to free up space
209
+ await self._cleanup_inactive_sessions()
210
+
211
+ # Slow path: create session with writer lock for exclusive access
212
+ async with self._session_rwlock.writer:
213
+ # Double-check after acquiring writer lock (another coroutine might have created it)
214
+ if session_id in self._sessions:
215
+ self._sessions[session_id].last_activity = datetime.now()
216
+ return self._sessions[session_id].client
217
+
218
+ # Re-check session limit inside writer lock
219
+ if self._client_config and len(self._sessions) >= self._client_config.max_sessions:
220
+ logger.warning("Session limit reached (%d), rejecting new session: %s",
221
+ self._client_config.max_sessions,
222
+ truncate_session_id(session_id))
223
+ raise RuntimeError(f"Service temporarily unavailable: Maximum concurrent sessions "
224
+ f"({self._client_config.max_sessions}) exceeded. Please try again later.")
225
+
226
+ # Create session client lazily
227
+ logger.info("Creating new MCP client for session: %s", truncate_session_id(session_id))
228
+ session_client = await self._create_session_client(session_id)
229
+
230
+ # Create session data with all components
231
+ session_data = SessionData(client=session_client, last_activity=datetime.now(), ref_count=0)
232
+
233
+ # Cache the session data
234
+ self._sessions[session_id] = session_data
235
+ logger.info(" Total sessions: %d", len(self._sessions))
236
+ return session_client
237
+
238
+ @asynccontextmanager
239
+ async def _session_usage_context(self, session_id: str):
240
+ """Context manager to track active session usage and prevent cleanup."""
241
+ # Ensure session exists - create it if it doesn't
242
+ if session_id not in self._sessions:
243
+ # Create session client first
244
+ await self._get_session_client(session_id)
245
+ # Session should now exist in _sessions
246
+
247
+ # Get session data (session must exist at this point)
248
+ session_data = self._sessions[session_id]
249
+
250
+ # Thread-safe reference counting using per-session lock
251
+ async with session_data.lock:
252
+ session_data.ref_count += 1
253
+
254
+ try:
255
+ yield
256
+ finally:
257
+ async with session_data.lock:
258
+ session_data.ref_count -= 1
259
+
260
+ async def _create_session_client(self, session_id: str) -> MCPBaseClient:
261
+ """Create a new MCP client instance for the session."""
262
+ from nat.plugins.mcp.client_base import MCPStreamableHTTPClient
263
+
264
+ config = self._client_config
265
+ if not config:
266
+ raise RuntimeError("Client config not initialized")
267
+
268
+ if config.server.transport == "streamable-http":
269
+ client = MCPStreamableHTTPClient(
270
+ str(config.server.url),
271
+ auth_provider=self._shared_auth_provider,
272
+ user_id=session_id, # Pass session_id as user_id for cache isolation
273
+ tool_call_timeout=config.tool_call_timeout,
274
+ auth_flow_timeout=config.auth_flow_timeout,
275
+ reconnect_enabled=config.reconnect_enabled,
276
+ reconnect_max_attempts=config.reconnect_max_attempts,
277
+ reconnect_initial_backoff=config.reconnect_initial_backoff,
278
+ reconnect_max_backoff=config.reconnect_max_backoff)
279
+ else:
280
+ # per-user sessions are only supported for streamable-http transport
281
+ raise ValueError(f"Unsupported transport: {config.server.transport}")
282
+
283
+ # Initialize the client
284
+ await client.__aenter__()
285
+
286
+ logger.info("Created session client for session: %s", truncate_session_id(session_id))
287
+ return client
288
+
289
+
290
+ def mcp_session_tool_function(tool, function_group: MCPFunctionGroup):
291
+ """Create a session-aware NAT function for an MCP tool.
292
+
293
+ Routes each invocation to the appropriate per-session MCP client while
294
+ preserving the original tool input schema, converters, and description.
137
295
  """
138
- server: MCPServerConfig = Field(..., description="Server connection details (transport, url/command, etc.)")
139
- tool_call_timeout: timedelta = Field(
140
- default=timedelta(seconds=60),
141
- description="Timeout (in seconds) for the MCP tool call. Defaults to 60 seconds.")
142
- auth_flow_timeout: timedelta = Field(
143
- default=timedelta(seconds=300),
144
- description="Timeout (in seconds) for the MCP auth flow. When the tool call requires interactive \
145
- authentication, this timeout is used. Defaults to 300 seconds.")
146
- reconnect_enabled: bool = Field(
147
- default=True,
148
- description="Whether to enable reconnecting to the MCP server if the connection is lost. \
149
- Defaults to True.")
150
- reconnect_max_attempts: int = Field(default=2,
151
- ge=0,
152
- description="Maximum number of reconnect attempts. Defaults to 2.")
153
- reconnect_initial_backoff: float = Field(
154
- default=0.5, ge=0.0, description="Initial backoff time for reconnect attempts. Defaults to 0.5 seconds.")
155
- reconnect_max_backoff: float = Field(
156
- default=50.0, ge=0.0, description="Maximum backoff time for reconnect attempts. Defaults to 50 seconds.")
157
- tool_overrides: dict[str, MCPToolOverrideConfig] | None = Field(
158
- default=None,
159
- description="""Optional tool name overrides and description changes.
160
- Example:
161
- tool_overrides:
162
- calculator_add:
163
- alias: "add_numbers"
164
- description: "Add two numbers together"
165
- calculator_multiply:
166
- description: "Multiply two numbers" # alias defaults to original name
167
- """)
168
-
169
- @model_validator(mode="after")
170
- def _validate_reconnect_backoff(self) -> "MCPClientConfig":
171
- """Validate reconnect backoff values."""
172
- if self.reconnect_max_backoff < self.reconnect_initial_backoff:
173
- raise ValueError("reconnect_max_backoff must be greater than or equal to reconnect_initial_backoff")
174
- return self
296
+ from nat.builder.function import FunctionInfo
297
+
298
+ def _convert_from_str(input_str: str) -> tool.input_schema:
299
+ return tool.input_schema.model_validate_json(input_str)
300
+
301
+ async def _response_fn(tool_input: BaseModel | None = None, **kwargs) -> str:
302
+ """Response function for the session-aware tool."""
303
+ try:
304
+ # Route to the appropriate session client
305
+ session_id = function_group._get_session_id_from_context()
306
+
307
+ # If no session is available and default-user fallback is disabled, deny the call
308
+ if function_group._shared_auth_provider and session_id is None:
309
+ return "User not authorized to call the tool"
310
+
311
+ # Check if this is the default user - if so, use base client directly
312
+ if (not function_group._shared_auth_provider
313
+ or session_id == function_group._shared_auth_provider.config.default_user_id):
314
+ # Use base client directly for default user
315
+ client = function_group.mcp_client
316
+ session_tool = await client.get_tool(tool.name)
317
+ else:
318
+ # Use session usage context to prevent cleanup during tool execution
319
+ async with function_group._session_usage_context(session_id):
320
+ client = await function_group._get_session_client(session_id)
321
+ session_tool = await client.get_tool(tool.name)
322
+
323
+ # Preserve original calling convention
324
+ if tool_input:
325
+ args = tool_input.model_dump()
326
+ return await session_tool.acall(args)
327
+
328
+ _ = session_tool.input_schema.model_validate(kwargs)
329
+ return await session_tool.acall(kwargs)
330
+ except Exception as e:
331
+ if tool_input:
332
+ logger.warning("Error calling tool %s with serialized input: %s",
333
+ tool.name,
334
+ tool_input.model_dump(),
335
+ exc_info=True)
336
+ else:
337
+ logger.warning("Error calling tool %s with input: %s", tool.name, kwargs, exc_info=True)
338
+ return str(e)
339
+
340
+ return FunctionInfo.create(single_fn=_response_fn,
341
+ description=tool.description,
342
+ input_schema=tool.input_schema,
343
+ converters=[_convert_from_str])
175
344
 
176
345
 
177
346
  @register_function_group(config_type=MCPClientConfig)
178
347
  async def mcp_client_function_group(config: MCPClientConfig, _builder: Builder):
179
348
  """
180
349
  Connect to an MCP server and expose tools as a function group.
350
+
181
351
  Args:
182
352
  config: The configuration for the MCP client
183
353
  _builder: The builder
@@ -215,8 +385,11 @@ async def mcp_client_function_group(config: MCPClientConfig, _builder: Builder):
215
385
  reconnect_initial_backoff=config.reconnect_initial_backoff,
216
386
  reconnect_max_backoff=config.reconnect_max_backoff)
217
387
  elif config.server.transport == "streamable-http":
388
+ # Use default_user_id for the base client
389
+ base_user_id = auth_provider.config.default_user_id if auth_provider else None
218
390
  client = MCPStreamableHTTPClient(str(config.server.url),
219
391
  auth_provider=auth_provider,
392
+ user_id=base_user_id,
220
393
  tool_call_timeout=config.tool_call_timeout,
221
394
  auth_flow_timeout=config.auth_flow_timeout,
222
395
  reconnect_enabled=config.reconnect_enabled,
@@ -231,6 +404,10 @@ async def mcp_client_function_group(config: MCPClientConfig, _builder: Builder):
231
404
  # Create the MCP function group
232
405
  group = MCPFunctionGroup(config=config)
233
406
 
407
+ # Store shared components for session client creation
408
+ group._shared_auth_provider = auth_provider
409
+ group._client_config = config
410
+
234
411
  async with client:
235
412
  # Expose the live MCP client on the function group instance so other components (e.g., HTTP endpoints)
236
413
  # can reuse the already-established session instead of creating a new client per request.
@@ -250,13 +427,17 @@ async def mcp_client_function_group(config: MCPClientConfig, _builder: Builder):
250
427
  function_name = override.alias if override and override.alias else tool_name
251
428
  description = override.description if override and override.description else tool.description
252
429
 
253
- # Create the tool function
254
- tool_fn = mcp_tool_function(tool)
430
+ # Create the tool function according to configuration
431
+ if config.session_aware_tools:
432
+ tool_fn = mcp_session_tool_function(tool, group)
433
+ else:
434
+ from nat.plugins.mcp.tool import mcp_tool_function
435
+ tool_fn = mcp_tool_function(tool)
255
436
 
256
437
  # Normalize optional typing for linter/type-checker compatibility
257
438
  single_fn = tool_fn.single_fn
258
439
  if single_fn is None:
259
- # Should not happen because mcp_tool_function always sets a single_fn
440
+ # Should not happen because FunctionInfo always sets a single_fn
260
441
  logger.warning("Skipping tool %s because single_fn is None", function_name)
261
442
  continue
262
443
 
@@ -280,6 +461,7 @@ def mcp_apply_tool_alias_and_description(
280
461
  all_tools: dict, tool_overrides: dict[str, MCPToolOverrideConfig] | None) -> dict[str, MCPToolOverrideConfig]:
281
462
  """
282
463
  Filter tool overrides to only include tools that exist in the MCP server.
464
+
283
465
  Args:
284
466
  all_tools: The tools from the MCP server
285
467
  tool_overrides: The tool overrides to apply
nat/plugins/mcp/tool.py CHANGED
@@ -26,6 +26,7 @@ from nat.builder.function_info import FunctionInfo
26
26
  from nat.cli.register_workflow import register_function
27
27
  from nat.data_models.function import FunctionBaseConfig
28
28
  from nat.plugins.mcp.client_base import MCPToolClient
29
+ from nat.utils.decorators import deprecated
29
30
 
30
31
  logger = logging.getLogger(__name__)
31
32
 
@@ -109,6 +110,10 @@ def mcp_tool_function(tool: MCPToolClient) -> FunctionInfo:
109
110
 
110
111
 
111
112
  @register_function(config_type=MCPToolConfig)
113
+ @deprecated(
114
+ reason=
115
+ "This function is being replaced with the new mcp_client function group that supports additional MCP features",
116
+ feature_name="mcp_tool_wrapper")
112
117
  async def mcp_tool(config: MCPToolConfig, builder: Builder):
113
118
  """
114
119
  Generate a NeMo Agent Toolkit Function that wraps a tool provided by the MCP server.
nat/plugins/mcp/utils.py CHANGED
@@ -21,6 +21,22 @@ from pydantic import Field
21
21
  from pydantic import create_model
22
22
 
23
23
 
24
+ def truncate_session_id(session_id: str, max_length: int = 10) -> str:
25
+ """
26
+ Truncate a session ID for logging purposes.
27
+
28
+ Args:
29
+ session_id: The session ID to truncate
30
+ max_length: Maximum length before truncation (default: 10)
31
+
32
+ Returns:
33
+ Truncated session ID with "..." if longer than max_length, otherwise full ID
34
+ """
35
+ if len(session_id) > max_length:
36
+ return session_id[:max_length] + "..."
37
+ return session_id
38
+
39
+
24
40
  def model_from_mcp_schema(name: str, mcp_input_schema: dict) -> type[BaseModel]:
25
41
  """
26
42
  Create a pydantic model from the input schema of the MCP tool