nvidia-nat 1.3.0a20250904__py3-none-any.whl → 1.3.0a20250909__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/builder/component_utils.py +1 -1
- nat/cli/commands/info/list_mcp.py +29 -7
- nat/cli/commands/workflow/templates/pyproject.toml.j2 +1 -1
- nat/data_models/common.py +1 -1
- nat/data_models/thinking_mixin.py +2 -3
- nat/eval/utils/weave_eval.py +6 -4
- nat/front_ends/fastapi/fastapi_front_end_config.py +18 -2
- nat/observability/exporter/processing_exporter.py +294 -39
- nat/observability/mixin/redaction_config_mixin.py +41 -0
- nat/observability/mixin/tagging_config_mixin.py +50 -0
- nat/observability/processor/header_redaction_processor.py +123 -0
- nat/observability/processor/redaction_processor.py +77 -0
- nat/observability/processor/span_tagging_processor.py +61 -0
- nat/tool/register.py +0 -2
- {nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/METADATA +7 -2
- {nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/RECORD +21 -22
- nat/tool/mcp/__init__.py +0 -14
- nat/tool/mcp/exceptions.py +0 -142
- nat/tool/mcp/mcp_client_base.py +0 -406
- nat/tool/mcp/mcp_client_impl.py +0 -229
- nat/tool/mcp/mcp_tool.py +0 -133
- nat/utils/exception_handlers/mcp.py +0 -211
- {nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/WHEEL +0 -0
- {nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/entry_points.txt +0 -0
- {nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
- {nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/licenses/LICENSE.md +0 -0
- {nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/top_level.txt +0 -0
nat/tool/mcp/mcp_tool.py
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
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
|
-
import logging
|
|
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.builder.builder import Builder
|
|
25
|
-
from nat.builder.function_info import FunctionInfo
|
|
26
|
-
from nat.cli.register_workflow import register_function
|
|
27
|
-
from nat.data_models.function import FunctionBaseConfig
|
|
28
|
-
|
|
29
|
-
logger = logging.getLogger(__name__)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
class MCPToolConfig(FunctionBaseConfig, name="mcp_tool_wrapper"):
|
|
33
|
-
"""
|
|
34
|
-
Function which connects to a Model Context Protocol (MCP) server and wraps the selected tool as a NeMo Agent toolkit
|
|
35
|
-
function.
|
|
36
|
-
"""
|
|
37
|
-
# Add your custom configuration parameters here
|
|
38
|
-
url: HttpUrl | None = Field(default=None,
|
|
39
|
-
description="The URL of the MCP server (for streamable-http or sse modes)")
|
|
40
|
-
mcp_tool_name: str = Field(description="The name of the tool served by the MCP Server that you want to use")
|
|
41
|
-
transport: Literal["sse", "stdio", "streamable-http"] = Field(
|
|
42
|
-
default="streamable-http",
|
|
43
|
-
description="The type of transport to use (default: streamable-http, backwards compatible with sse)")
|
|
44
|
-
command: str | None = Field(default=None,
|
|
45
|
-
description="The command to run for stdio mode (e.g. 'docker' or 'python')")
|
|
46
|
-
args: list[str] | None = Field(default=None, description="Additional arguments for the stdio command")
|
|
47
|
-
env: dict[str, str] | None = Field(default=None, description="Environment variables to set for the stdio process")
|
|
48
|
-
description: str | None = Field(default=None,
|
|
49
|
-
description="""
|
|
50
|
-
Description for the tool that will override the description provided by the MCP server. Should only be used if
|
|
51
|
-
the description provided by the server is poor or nonexistent
|
|
52
|
-
""")
|
|
53
|
-
return_exception: bool = Field(default=True,
|
|
54
|
-
description="""
|
|
55
|
-
If true, the tool will return the exception message if the tool call fails.
|
|
56
|
-
If false, raise the exception.
|
|
57
|
-
""")
|
|
58
|
-
|
|
59
|
-
@model_validator(mode="after")
|
|
60
|
-
def validate_model(self):
|
|
61
|
-
"""Validate that stdio and SSE/Streamable HTTP properties are mutually exclusive."""
|
|
62
|
-
if self.transport == 'stdio':
|
|
63
|
-
if self.url is not None:
|
|
64
|
-
raise ValueError("url should not be set when using stdio client type")
|
|
65
|
-
if not self.command:
|
|
66
|
-
raise ValueError("command is required when using stdio client type")
|
|
67
|
-
elif self.transport in ['streamable-http', 'sse']:
|
|
68
|
-
if self.command is not None or self.args is not None or self.env is not None:
|
|
69
|
-
raise ValueError(
|
|
70
|
-
"command, args, and env should not be set when using streamable-http or sse client type")
|
|
71
|
-
if not self.url:
|
|
72
|
-
raise ValueError("url is required when using streamable-http or sse client type")
|
|
73
|
-
return self
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
@register_function(config_type=MCPToolConfig)
|
|
77
|
-
async def mcp_tool(config: MCPToolConfig, builder: Builder):
|
|
78
|
-
"""
|
|
79
|
-
Generate a NeMo Agent Toolkit Function that wraps a tool provided by the MCP server.
|
|
80
|
-
"""
|
|
81
|
-
|
|
82
|
-
from nat.tool.mcp.mcp_client_base import MCPSSEClient
|
|
83
|
-
from nat.tool.mcp.mcp_client_base import MCPStdioClient
|
|
84
|
-
from nat.tool.mcp.mcp_client_base import MCPStreamableHTTPClient
|
|
85
|
-
from nat.tool.mcp.mcp_client_base import MCPToolClient
|
|
86
|
-
|
|
87
|
-
# Initialize the client
|
|
88
|
-
if config.transport == 'stdio':
|
|
89
|
-
client = MCPStdioClient(command=config.command, args=config.args, env=config.env)
|
|
90
|
-
elif config.transport == 'streamable-http':
|
|
91
|
-
client = MCPStreamableHTTPClient(url=str(config.url))
|
|
92
|
-
elif config.transport == 'sse':
|
|
93
|
-
client = MCPSSEClient(url=str(config.url))
|
|
94
|
-
else:
|
|
95
|
-
raise ValueError(f"Invalid transport type: {config.transport}")
|
|
96
|
-
|
|
97
|
-
async with client:
|
|
98
|
-
# If the tool is found create a MCPToolClient object and set the description if provided
|
|
99
|
-
tool: MCPToolClient = await client.get_tool(config.mcp_tool_name)
|
|
100
|
-
if config.description:
|
|
101
|
-
tool.set_description(description=config.description)
|
|
102
|
-
|
|
103
|
-
logger.info("Configured to use tool: %s from MCP server at %s", tool.name, client.server_name)
|
|
104
|
-
|
|
105
|
-
def _convert_from_str(input_str: str) -> tool.input_schema:
|
|
106
|
-
return tool.input_schema.model_validate_json(input_str)
|
|
107
|
-
|
|
108
|
-
async def _response_fn(tool_input: BaseModel | None = None, **kwargs) -> str:
|
|
109
|
-
# Run the tool, catching any errors and sending to agent for correction
|
|
110
|
-
try:
|
|
111
|
-
if tool_input:
|
|
112
|
-
args = tool_input.model_dump()
|
|
113
|
-
return await tool.acall(args)
|
|
114
|
-
|
|
115
|
-
_ = tool.input_schema.model_validate(kwargs)
|
|
116
|
-
return await tool.acall(kwargs)
|
|
117
|
-
except Exception as e:
|
|
118
|
-
if config.return_exception:
|
|
119
|
-
if tool_input:
|
|
120
|
-
logger.warning("Error calling tool %s with serialized input: %s",
|
|
121
|
-
tool.name,
|
|
122
|
-
tool_input.model_dump(),
|
|
123
|
-
exc_info=True)
|
|
124
|
-
else:
|
|
125
|
-
logger.warning("Error calling tool %s with input: %s", tool.name, kwargs, exc_info=True)
|
|
126
|
-
return str(e)
|
|
127
|
-
# If the tool call fails, raise the exception.
|
|
128
|
-
raise
|
|
129
|
-
|
|
130
|
-
yield FunctionInfo.create(single_fn=_response_fn,
|
|
131
|
-
description=tool.description,
|
|
132
|
-
input_schema=tool.input_schema,
|
|
133
|
-
converters=[_convert_from_str])
|
|
@@ -1,211 +0,0 @@
|
|
|
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
|
-
import logging
|
|
17
|
-
import ssl
|
|
18
|
-
import sys
|
|
19
|
-
from collections.abc import Callable
|
|
20
|
-
from functools import wraps
|
|
21
|
-
from typing import Any
|
|
22
|
-
|
|
23
|
-
import httpx
|
|
24
|
-
|
|
25
|
-
from nat.tool.mcp.exceptions import MCPAuthenticationError
|
|
26
|
-
from nat.tool.mcp.exceptions import MCPConnectionError
|
|
27
|
-
from nat.tool.mcp.exceptions import MCPError
|
|
28
|
-
from nat.tool.mcp.exceptions import MCPProtocolError
|
|
29
|
-
from nat.tool.mcp.exceptions import MCPRequestError
|
|
30
|
-
from nat.tool.mcp.exceptions import MCPSSLError
|
|
31
|
-
from nat.tool.mcp.exceptions import MCPTimeoutError
|
|
32
|
-
from nat.tool.mcp.exceptions import MCPToolNotFoundError
|
|
33
|
-
|
|
34
|
-
logger = logging.getLogger(__name__)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def format_mcp_error(error: MCPError, include_traceback: bool = False) -> None:
|
|
38
|
-
"""Format MCP errors for CLI display with structured logging and user guidance.
|
|
39
|
-
|
|
40
|
-
Logs structured error information for debugging and displays user-friendly
|
|
41
|
-
error messages with actionable suggestions to stderr.
|
|
42
|
-
|
|
43
|
-
Args:
|
|
44
|
-
error (MCPError): MCPError instance containing message, url, category, suggestions, and original_exception
|
|
45
|
-
include_traceback (bool, optional): Whether to include the traceback in the error message. Defaults to False.
|
|
46
|
-
"""
|
|
47
|
-
# Log structured error information for debugging
|
|
48
|
-
logger.error("MCP operation failed: %s", error, exc_info=include_traceback)
|
|
49
|
-
|
|
50
|
-
# Display user-friendly suggestions
|
|
51
|
-
for suggestion in error.suggestions:
|
|
52
|
-
print(f" → {suggestion}", file=sys.stderr)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def _extract_url(args: tuple, kwargs: dict[str, Any], url_param: str, func_name: str) -> str:
|
|
56
|
-
"""Extract URL from function arguments using clean fallback chain.
|
|
57
|
-
|
|
58
|
-
Args:
|
|
59
|
-
args: Function positional arguments
|
|
60
|
-
kwargs: Function keyword arguments
|
|
61
|
-
url_param (str): Parameter name containing the URL
|
|
62
|
-
func_name (str): Function name for logging
|
|
63
|
-
|
|
64
|
-
Returns:
|
|
65
|
-
str: URL string or "unknown" if extraction fails
|
|
66
|
-
"""
|
|
67
|
-
# Try keyword arguments first
|
|
68
|
-
if url_param in kwargs:
|
|
69
|
-
return kwargs[url_param]
|
|
70
|
-
|
|
71
|
-
# Try self attribute (e.g., self.url)
|
|
72
|
-
if args and hasattr(args[0], url_param):
|
|
73
|
-
return getattr(args[0], url_param)
|
|
74
|
-
|
|
75
|
-
# Try common case: url as second parameter after self
|
|
76
|
-
if len(args) > 1 and url_param == "url":
|
|
77
|
-
return args[1]
|
|
78
|
-
|
|
79
|
-
# Fallback with warning
|
|
80
|
-
logger.warning("Could not extract URL for error handling in %s", func_name)
|
|
81
|
-
return "unknown"
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
def extract_primary_exception(exceptions: list[Exception]) -> Exception:
|
|
85
|
-
"""Extract the most relevant exception from a group.
|
|
86
|
-
|
|
87
|
-
Prioritizes connection errors over others for better user experience.
|
|
88
|
-
|
|
89
|
-
Args:
|
|
90
|
-
exceptions (list[Exception]): List of exceptions from ExceptionGroup
|
|
91
|
-
|
|
92
|
-
Returns:
|
|
93
|
-
Exception: Most relevant exception for user feedback
|
|
94
|
-
"""
|
|
95
|
-
# Prioritize connection errors
|
|
96
|
-
for exc in exceptions:
|
|
97
|
-
if isinstance(exc, (httpx.ConnectError, ConnectionError)):
|
|
98
|
-
return exc
|
|
99
|
-
|
|
100
|
-
# Then timeout errors
|
|
101
|
-
for exc in exceptions:
|
|
102
|
-
if isinstance(exc, httpx.TimeoutException):
|
|
103
|
-
return exc
|
|
104
|
-
|
|
105
|
-
# Then SSL errors
|
|
106
|
-
for exc in exceptions:
|
|
107
|
-
if isinstance(exc, ssl.SSLError):
|
|
108
|
-
return exc
|
|
109
|
-
|
|
110
|
-
# Fall back to first exception
|
|
111
|
-
return exceptions[0]
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def convert_to_mcp_error(exception: Exception, url: str) -> MCPError:
|
|
115
|
-
"""Convert single exception to appropriate MCPError.
|
|
116
|
-
|
|
117
|
-
Args:
|
|
118
|
-
exception (Exception): Single exception to convert
|
|
119
|
-
url (str): MCP server URL for context
|
|
120
|
-
|
|
121
|
-
Returns:
|
|
122
|
-
MCPError: Appropriate MCPError subclass
|
|
123
|
-
"""
|
|
124
|
-
match exception:
|
|
125
|
-
case httpx.ConnectError() | ConnectionError():
|
|
126
|
-
return MCPConnectionError(url, exception)
|
|
127
|
-
case httpx.TimeoutException():
|
|
128
|
-
return MCPTimeoutError(url, exception)
|
|
129
|
-
case ssl.SSLError():
|
|
130
|
-
return MCPSSLError(url, exception)
|
|
131
|
-
case httpx.RequestError():
|
|
132
|
-
return MCPRequestError(url, exception)
|
|
133
|
-
case ValueError() if "Tool" in str(exception) and "not available" in str(exception):
|
|
134
|
-
# Extract tool name from error message if possible
|
|
135
|
-
tool_name = str(exception).split("Tool ")[1].split(" not available")[0] if "Tool " in str(
|
|
136
|
-
exception) else "unknown"
|
|
137
|
-
return MCPToolNotFoundError(tool_name, url, exception)
|
|
138
|
-
case _:
|
|
139
|
-
# Handle TaskGroup error message specifically
|
|
140
|
-
if "unhandled errors in a TaskGroup" in str(exception):
|
|
141
|
-
return MCPProtocolError(url, "Failed to connect to MCP server", exception)
|
|
142
|
-
if "unauthorized" in str(exception).lower() or "forbidden" in str(exception).lower():
|
|
143
|
-
return MCPAuthenticationError(url, exception)
|
|
144
|
-
return MCPError(f"Unexpected error: {exception}", url, original_exception=exception)
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
def handle_mcp_exceptions(url_param: str = "url") -> Callable[..., Any]:
|
|
148
|
-
"""Decorator that handles exceptions and converts them to MCPErrors.
|
|
149
|
-
|
|
150
|
-
This decorator wraps MCP client methods and converts low-level exceptions
|
|
151
|
-
to structured MCPError instances with helpful user guidance.
|
|
152
|
-
|
|
153
|
-
Args:
|
|
154
|
-
url_param (str): Name of the parameter or attribute containing the MCP server URL
|
|
155
|
-
|
|
156
|
-
Returns:
|
|
157
|
-
Callable[..., Any]: Decorated function
|
|
158
|
-
|
|
159
|
-
Example:
|
|
160
|
-
.. code-block:: python
|
|
161
|
-
|
|
162
|
-
@handle_mcp_exceptions("url")
|
|
163
|
-
async def get_tools(self, url: str):
|
|
164
|
-
# Method implementation
|
|
165
|
-
pass
|
|
166
|
-
|
|
167
|
-
@handle_mcp_exceptions("url") # Uses self.url
|
|
168
|
-
async def get_tool(self):
|
|
169
|
-
# Method implementation
|
|
170
|
-
pass
|
|
171
|
-
"""
|
|
172
|
-
|
|
173
|
-
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
|
|
174
|
-
|
|
175
|
-
@wraps(func)
|
|
176
|
-
async def wrapper(*args, **kwargs):
|
|
177
|
-
try:
|
|
178
|
-
return await func(*args, **kwargs)
|
|
179
|
-
except MCPError:
|
|
180
|
-
# Re-raise MCPErrors as-is
|
|
181
|
-
raise
|
|
182
|
-
except Exception as e:
|
|
183
|
-
url = _extract_url(args, kwargs, url_param, func.__name__)
|
|
184
|
-
|
|
185
|
-
# Handle ExceptionGroup by extracting most relevant exception
|
|
186
|
-
if isinstance(e, ExceptionGroup): # noqa: F821
|
|
187
|
-
primary_exception = extract_primary_exception(list(e.exceptions))
|
|
188
|
-
mcp_error = convert_to_mcp_error(primary_exception, url)
|
|
189
|
-
else:
|
|
190
|
-
mcp_error = convert_to_mcp_error(e, url)
|
|
191
|
-
|
|
192
|
-
raise mcp_error from e
|
|
193
|
-
|
|
194
|
-
return wrapper
|
|
195
|
-
|
|
196
|
-
return decorator
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
def mcp_exception_handler(func: Callable[..., Any]) -> Callable[..., Any]:
|
|
200
|
-
"""Simplified decorator for methods that have self.url attribute.
|
|
201
|
-
|
|
202
|
-
This is a convenience decorator that assumes the URL is available as self.url.
|
|
203
|
-
Follows the same pattern as schema_exception_handler in this directory.
|
|
204
|
-
|
|
205
|
-
Args:
|
|
206
|
-
func (Callable[..., Any]): The function to decorate
|
|
207
|
-
|
|
208
|
-
Returns:
|
|
209
|
-
Callable[..., Any]: Decorated function
|
|
210
|
-
"""
|
|
211
|
-
return handle_mcp_exceptions("url")(func)
|
|
File without changes
|
{nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
|
File without changes
|
{nvidia_nat-1.3.0a20250904.dist-info → nvidia_nat-1.3.0a20250909.dist-info}/licenses/LICENSE.md
RENAMED
|
File without changes
|
|
File without changes
|