nvidia-nat-mcp 1.3.0a20250926__py3-none-any.whl → 1.3.0a20251111__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.
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
 
@@ -93,13 +94,7 @@ def mcp_tool_function(tool: MCPToolClient) -> FunctionInfo:
93
94
  _ = tool.input_schema.model_validate(kwargs)
94
95
  return await tool.acall(kwargs)
95
96
  except Exception as e:
96
- if tool_input:
97
- logger.warning("Error calling tool %s with serialized input: %s",
98
- tool.name,
99
- tool_input.model_dump(),
100
- exc_info=True)
101
- else:
102
- logger.warning("Error calling tool %s with input: %s", tool.name, kwargs, exc_info=True)
97
+ logger.warning("Error calling tool %s", tool.name, exc_info=True)
103
98
  return str(e)
104
99
 
105
100
  return FunctionInfo.create(single_fn=_response_fn,
@@ -109,6 +104,10 @@ def mcp_tool_function(tool: MCPToolClient) -> FunctionInfo:
109
104
 
110
105
 
111
106
  @register_function(config_type=MCPToolConfig)
107
+ @deprecated(
108
+ reason=
109
+ "This function is being replaced with the new mcp_client function group that supports additional MCP features",
110
+ feature_name="mcp_tool_wrapper")
112
111
  async def mcp_tool(config: MCPToolConfig, builder: Builder):
113
112
  """
114
113
  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
@@ -31,7 +47,7 @@ def model_from_mcp_schema(name: str, mcp_input_schema: dict) -> type[BaseModel]:
31
47
  "integer": int,
32
48
  "boolean": bool,
33
49
  "array": list,
34
- "null": None,
50
+ "null": type(None),
35
51
  "object": dict,
36
52
  }
37
53
 
@@ -42,51 +58,168 @@ def model_from_mcp_schema(name: str, mcp_input_schema: dict) -> type[BaseModel]:
42
58
  def _generate_valid_classname(class_name: str):
43
59
  return class_name.replace('_', ' ').replace('-', ' ').title().replace(' ', '')
44
60
 
45
- def _generate_field(field_name: str, field_properties: dict[str, Any]) -> tuple:
46
- json_type = field_properties.get("type", "string")
47
- enum_vals = field_properties.get("enum")
61
+ def _resolve_schema_type(schema: dict[str, Any], name: str) -> Any:
62
+ """
63
+ Recursively resolve a JSON schema to a Python type.
64
+ Handles nested anyOf/oneOf, arrays, objects, enums, and primitive types.
65
+ """
66
+ # Check for anyOf/oneOf first
67
+ any_of = schema.get("anyOf")
68
+ one_of = schema.get("oneOf")
69
+
70
+ if any_of or one_of:
71
+ union_schemas = any_of if any_of else one_of
72
+ resolved_type: Any = None
48
73
 
74
+ if union_schemas:
75
+ for sub_schema in union_schemas:
76
+ mapped = _resolve_schema_type(sub_schema, name)
77
+ if resolved_type is None:
78
+ resolved_type = mapped
79
+ elif mapped is not type(None):
80
+ # Don't add None here, handle separately
81
+ resolved_type = resolved_type | mapped
82
+ else:
83
+ # If we encounter null, combine with None at the end
84
+ resolved_type = resolved_type | None if resolved_type else type(None)
85
+
86
+ return resolved_type if resolved_type is not None else Any
87
+
88
+ # Handle enum values
89
+ enum_vals = schema.get("enum")
49
90
  if enum_vals:
50
- enum_name = f"{field_name.capitalize()}Enum"
51
- field_type = Enum(enum_name, {item: item for item in enum_vals})
52
-
53
- elif json_type == "object" and "properties" in field_properties:
54
- field_type = model_from_mcp_schema(name=field_name, mcp_input_schema=field_properties)
55
- elif json_type == "array" and "items" in field_properties:
56
- item_properties = field_properties.get("items", {})
57
- if item_properties.get("type") == "object":
58
- item_type = model_from_mcp_schema(name=field_name, mcp_input_schema=item_properties)
91
+ # Check if enum contains null
92
+ has_null = any(val is None or val == "null" for val in enum_vals)
93
+ # Filter out None/null values from enum
94
+ non_null_vals = [v for v in enum_vals if v is not None and v != "null"]
95
+
96
+ if non_null_vals:
97
+ enum_name = f"{name.capitalize()}Enum"
98
+ enum_type: Any = Enum(enum_name, {item: item for item in non_null_vals})
99
+ # If enum had null, make it a union with None
100
+ return enum_type | None if has_null else enum_type
101
+ elif has_null:
102
+ # Enum only contains null
103
+ return type(None)
59
104
  else:
60
- item_type = _type_map.get(item_properties.get("type", "string"), Any)
61
- field_type = list[item_type]
62
- elif isinstance(json_type, list):
63
- field_type = None
64
- for t in json_type:
65
- mapped = _type_map.get(t, Any)
66
- field_type = mapped if field_type is None else field_type | mapped
67
-
68
- return field_type, Field(
69
- default=field_properties.get("default", None if "null" in json_type else ...),
70
- description=field_properties.get("description", "")
71
- )
72
- else:
73
- field_type = _type_map.get(json_type, Any)
105
+ # Empty enum (shouldn't happen but handle gracefully)
106
+ return Any
107
+
108
+ schema_type = schema.get("type")
109
+
110
+ # Handle type as list (e.g., ["string", "integer", "null"])
111
+ if isinstance(schema_type, list):
112
+ list_type: Any = None
113
+ for t in schema_type:
114
+ if t == "array":
115
+ # Incorporate the mapped type of items
116
+ item_schema = schema.get("items", {})
117
+ if item_schema:
118
+ item_type = _resolve_schema_type(item_schema, name)
119
+ mapped = list[item_type]
120
+ else:
121
+ mapped = _type_map.get(t, Any)
122
+ elif t == "object":
123
+ # Incorporate the mapped type from properties
124
+ if "properties" in schema:
125
+ mapped = model_from_mcp_schema(name=name, mcp_input_schema=schema)
126
+ else:
127
+ mapped = _type_map.get(t, Any)
128
+ else:
129
+ mapped = _type_map.get(t, Any)
130
+
131
+ list_type = mapped if list_type is None else list_type | mapped
132
+ return list_type if list_type is not None else Any
133
+
134
+ # Handle null type
135
+ if schema_type == "null":
136
+ return type(None)
137
+
138
+ # Handle object type
139
+ if schema_type == "object" and "properties" in schema:
140
+ return model_from_mcp_schema(name=name, mcp_input_schema=schema)
141
+
142
+ # Handle array type
143
+ if schema_type == "array" and "items" in schema:
144
+ item_schema = schema.get("items", {})
145
+ # Recursively resolve item type (handles nested anyOf/oneOf)
146
+ item_type = _resolve_schema_type(item_schema, name)
147
+ return list[item_type]
148
+
149
+ # Handle primitive types
150
+ if schema_type is not None:
151
+ return _type_map.get(schema_type, Any)
152
+
153
+ return Any
154
+
155
+ def _has_null_in_type(field_properties: dict[str, Any]) -> bool:
156
+ """Check if a schema contains null as a valid type."""
157
+ # Check anyOf/oneOf for null
158
+ any_of = field_properties.get("anyOf")
159
+ one_of = field_properties.get("oneOf")
160
+ if any_of or one_of:
161
+ union_schemas = any_of if any_of else one_of
162
+ if union_schemas:
163
+ for schema in union_schemas:
164
+ if schema.get("type") == "null":
165
+ return True
166
+
167
+ # Check type list for null
168
+ json_type = field_properties.get("type")
169
+ if isinstance(json_type, list) and "null" in json_type:
170
+ return True
171
+
172
+ # Check enum for null (Python None or string "null")
173
+ enum_vals = field_properties.get("enum")
174
+ if enum_vals:
175
+ for val in enum_vals:
176
+ if val is None or val == "null":
177
+ return True
178
+
179
+ # Check const for null (Python None or string "null")
180
+ if "const" in field_properties:
181
+ const_val = field_properties.get("const")
182
+ if const_val is None or const_val == "null":
183
+ return True
184
+
185
+ return False
186
+
187
+ def _generate_field(field_name: str, field_properties: dict[str, Any]) -> tuple:
188
+ """
189
+ Generate a Pydantic field from JSON schema properties.
190
+ Uses _resolve_schema_type for type resolution and handles field-specific logic.
191
+ """
192
+ # Resolve the field type using the unified resolver
193
+ field_type = _resolve_schema_type(field_properties, field_name)
194
+
195
+ # Check if the type includes null
196
+ has_null = _has_null_in_type(field_properties)
74
197
 
75
198
  # Determine the default value based on whether the field is required
199
+ default_value = field_properties.get("default")
200
+
76
201
  if field_name in required_fields:
77
- # Field is required - use explicit default if provided, otherwise make it required
78
- default_value = field_properties.get("default", ...)
202
+ # Field is required - use explicit default if provided, otherwise use ... to enforce presence
203
+ if default_value is None and "default" not in field_properties:
204
+ # Required field without explicit default: always use ... even if nullable
205
+ default_value = ...
206
+ # Make the field type nullable if it allows null
207
+ if has_null:
208
+ field_type = field_type | None
79
209
  else:
80
210
  # Field is optional - use explicit default if provided, otherwise None
81
- default_value = field_properties.get("default", None)
82
- # Make the type optional if no default was provided
83
- if "default" not in field_properties:
211
+ if default_value is None:
212
+ default_value = None
213
+ # Make the type optional if no default was provided and not already nullable
214
+ if "default" not in field_properties and not has_null:
84
215
  field_type = field_type | None
85
216
 
217
+ # Handle nullable property (less common, but still supported)
86
218
  nullable = field_properties.get("nullable", False)
87
- description = field_properties.get("description", "")
219
+ if nullable and not has_null:
220
+ field_type = field_type | None
88
221
 
89
- field_type = field_type | None if nullable else field_type
222
+ description = field_properties.get("description", "")
90
223
 
91
224
  return field_type, Field(default=default_value, description=description)
92
225
 
@@ -1,7 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nvidia-nat-mcp
3
- Version: 1.3.0a20250926
3
+ Version: 1.3.0a20251111
4
4
  Summary: Subpackage for MCP client integration in NeMo Agent toolkit
5
+ Author: NVIDIA Corporation
6
+ Maintainer: NVIDIA Corporation
7
+ License: Apache-2.0
8
+ Project-URL: documentation, https://docs.nvidia.com/nemo/agent-toolkit/latest/
9
+ Project-URL: source, https://github.com/NVIDIA/NeMo-Agent-Toolkit
5
10
  Keywords: ai,rag,agents,mcp
6
11
  Classifier: Programming Language :: Python
7
12
  Classifier: Programming Language :: Python :: 3.11
@@ -9,8 +14,12 @@ Classifier: Programming Language :: Python :: 3.12
9
14
  Classifier: Programming Language :: Python :: 3.13
10
15
  Requires-Python: <3.14,>=3.11
11
16
  Description-Content-Type: text/markdown
12
- Requires-Dist: nvidia-nat==v1.3.0a20250926
17
+ License-File: LICENSE-3rd-party.txt
18
+ License-File: LICENSE.md
19
+ Requires-Dist: nvidia-nat==v1.3.0a20251111
20
+ Requires-Dist: aiorwlock~=1.5
13
21
  Requires-Dist: mcp~=1.14
22
+ Dynamic: license-file
14
23
 
15
24
  <!--
16
25
  SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
@@ -33,9 +42,9 @@ limitations under the License.
33
42
 
34
43
 
35
44
  # NVIDIA NeMo Agent Toolkit MCP Subpackage
36
- Subpackage for MCP client integration in NeMo Agent toolkit.
45
+ Subpackage for MCP integration in NeMo Agent toolkit.
37
46
 
38
- This package provides MCP (Model Context Protocol) client functionality, allowing NeMo Agent toolkit workflows to connect to external MCP servers and use their tools as functions.
47
+ This package provides MCP (Model Context Protocol) functionality, allowing NeMo Agent toolkit workflows to connect to external MCP servers and use their tools as functions.
39
48
 
40
49
  ## Features
41
50
 
@@ -0,0 +1,23 @@
1
+ nat/meta/pypi.md,sha256=EYyJTCCEOWzuuz-uNaYJ_WBk55Jiig87wcUr9E4g0yw,1484
2
+ nat/plugins/mcp/__init__.py,sha256=GUJrgGtpvyMUCjUBvR3faAdv-tZzbU9W-izgx9aMEQg,680
3
+ nat/plugins/mcp/client_base.py,sha256=JIyO2ZJsVkQ1g5BOU2zKXGHg_0yxv16g7_YJAqdCXTA,26504
4
+ nat/plugins/mcp/client_config.py,sha256=l9tVUHe8WdFPJ9rXDg8dZkQi1dvHGYwoqQ8Glqg2LGs,6783
5
+ nat/plugins/mcp/client_impl.py,sha256=j7cKAUBKtZAY3mt5Mm8VqgqMhRZk7kzvUd1nwMU_h0o,27072
6
+ nat/plugins/mcp/exception_handler.py,sha256=4JVdZDJL4LyumZEcMIEBK2LYC6djuSMzqUhQDZZ6dUo,7648
7
+ nat/plugins/mcp/exceptions.py,sha256=EGVOnYlui8xufm8dhJyPL1SUqBLnCGOTvRoeyNcmcWE,5980
8
+ nat/plugins/mcp/register.py,sha256=HOT2Wl2isGuyFc7BUTi58-BbjI5-EtZMZo7stsv5pN4,831
9
+ nat/plugins/mcp/tool.py,sha256=xNfBIF__ugJKFEjkYEM417wWM1PpuTaCMGtSFmxHSuA,6089
10
+ nat/plugins/mcp/utils.py,sha256=dUIig7jeKz0ctb4o38jFGbe2uvM3DMR3PSJjfN_Lr5M,9111
11
+ nat/plugins/mcp/auth/__init__.py,sha256=GUJrgGtpvyMUCjUBvR3faAdv-tZzbU9W-izgx9aMEQg,680
12
+ nat/plugins/mcp/auth/auth_flow_handler.py,sha256=v21IK3IKZ2TLEP6wO9r-sJQiilWPq7Ry40M96SAxQFA,9125
13
+ nat/plugins/mcp/auth/auth_provider.py,sha256=BgH66DlZgzhLDLO4cBERpHvNAmli5fMo_SCy11W9aBU,21251
14
+ nat/plugins/mcp/auth/auth_provider_config.py,sha256=b1AaXzOuAkygKXAWSxMKWg8wfW8k33tmUUq6Dk5Mmwk,4038
15
+ nat/plugins/mcp/auth/register.py,sha256=L2x69NjJPS4s6CCE5myzWVrWn3e_ttHyojmGXvBipMg,1228
16
+ nat/plugins/mcp/auth/token_storage.py,sha256=aS13ZvEJXcYzkZ0GSbrSor4i5bpjD5BkXHQw1iywC9k,9240
17
+ nvidia_nat_mcp-1.3.0a20251111.dist-info/licenses/LICENSE-3rd-party.txt,sha256=fOk5jMmCX9YoKWyYzTtfgl-SUy477audFC5hNY4oP7Q,284609
18
+ nvidia_nat_mcp-1.3.0a20251111.dist-info/licenses/LICENSE.md,sha256=QwcOLU5TJoTeUhuIXzhdCEEDDvorGiC6-3YTOl4TecE,11356
19
+ nvidia_nat_mcp-1.3.0a20251111.dist-info/METADATA,sha256=_xxkOAPgqKhQZd0MiZe1NCGmgmPaqBT-pWxFA7esvSY,2319
20
+ nvidia_nat_mcp-1.3.0a20251111.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
+ nvidia_nat_mcp-1.3.0a20251111.dist-info/entry_points.txt,sha256=rYvUp4i-klBr3bVNh7zYOPXret704vTjvCk1qd7FooI,97
22
+ nvidia_nat_mcp-1.3.0a20251111.dist-info/top_level.txt,sha256=8-CJ2cP6-f0ZReXe5Hzqp-5pvzzHz-5Ds5H2bGqh1-U,4
23
+ nvidia_nat_mcp-1.3.0a20251111.dist-info/RECORD,,