datarobot-genai 0.1.75__py3-none-any.whl → 0.2.11__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.
Files changed (32) hide show
  1. datarobot_genai/core/agents/base.py +2 -1
  2. datarobot_genai/core/chat/responses.py +32 -4
  3. datarobot_genai/drmcp/core/config.py +52 -0
  4. datarobot_genai/drmcp/core/dr_mcp_server.py +45 -8
  5. datarobot_genai/drmcp/core/dynamic_prompts/dr_lib.py +22 -80
  6. datarobot_genai/drmcp/core/dynamic_prompts/register.py +4 -5
  7. datarobot_genai/drmcp/core/mcp_instance.py +41 -2
  8. datarobot_genai/drmcp/core/routes.py +4 -1
  9. datarobot_genai/drmcp/core/tool_config.py +95 -0
  10. datarobot_genai/drmcp/test_utils/mcp_utils_ete.py +29 -0
  11. datarobot_genai/drmcp/test_utils/openai_llm_mcp_client.py +6 -1
  12. datarobot_genai/drmcp/tools/clients/__init__.py +14 -0
  13. datarobot_genai/drmcp/tools/clients/atlassian.py +188 -0
  14. datarobot_genai/drmcp/tools/clients/confluence.py +196 -0
  15. datarobot_genai/drmcp/tools/clients/jira.py +147 -0
  16. datarobot_genai/drmcp/tools/clients/s3.py +28 -0
  17. datarobot_genai/drmcp/tools/confluence/__init__.py +14 -0
  18. datarobot_genai/drmcp/tools/confluence/tools.py +81 -0
  19. datarobot_genai/drmcp/tools/jira/__init__.py +14 -0
  20. datarobot_genai/drmcp/tools/jira/tools.py +52 -0
  21. datarobot_genai/drmcp/tools/predictive/predict.py +1 -1
  22. datarobot_genai/drmcp/tools/predictive/predict_realtime.py +1 -1
  23. datarobot_genai/langgraph/agent.py +143 -42
  24. datarobot_genai/nat/agent.py +4 -0
  25. datarobot_genai/nat/datarobot_auth_provider.py +110 -0
  26. datarobot_genai/nat/datarobot_mcp_client.py +234 -0
  27. {datarobot_genai-0.1.75.dist-info → datarobot_genai-0.2.11.dist-info}/METADATA +9 -2
  28. {datarobot_genai-0.1.75.dist-info → datarobot_genai-0.2.11.dist-info}/RECORD +32 -20
  29. {datarobot_genai-0.1.75.dist-info → datarobot_genai-0.2.11.dist-info}/entry_points.txt +2 -0
  30. {datarobot_genai-0.1.75.dist-info → datarobot_genai-0.2.11.dist-info}/WHEEL +0 -0
  31. {datarobot_genai-0.1.75.dist-info → datarobot_genai-0.2.11.dist-info}/licenses/AUTHORS +0 -0
  32. {datarobot_genai-0.1.75.dist-info → datarobot_genai-0.2.11.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,14 @@
1
+ # Copyright 2025 DataRobot, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
@@ -0,0 +1,52 @@
1
+ # Copyright 2025 DataRobot, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import logging
16
+ from typing import Annotated
17
+
18
+ from fastmcp.exceptions import ToolError
19
+ from fastmcp.tools.tool import ToolResult
20
+
21
+ from datarobot_genai.drmcp.core.mcp_instance import dr_mcp_tool
22
+ from datarobot_genai.drmcp.tools.clients.atlassian import get_atlassian_access_token
23
+ from datarobot_genai.drmcp.tools.clients.jira import JiraClient
24
+
25
+ logger = logging.getLogger(__name__)
26
+
27
+
28
+ @dr_mcp_tool(tags={"jira", "read", "get", "issue"})
29
+ async def jira_get_issue(
30
+ *, issue_key: Annotated[str, "The key (ID) of the Jira issue to retrieve, e.g., 'PROJ-123'."]
31
+ ) -> ToolResult:
32
+ """Retrieve all fields and details for a single Jira issue by its key."""
33
+ if not issue_key:
34
+ raise ToolError("Argument validation error: 'issue_key' cannot be empty.")
35
+
36
+ access_token = await get_atlassian_access_token()
37
+ if isinstance(access_token, ToolError):
38
+ raise access_token
39
+
40
+ try:
41
+ async with JiraClient(access_token) as client:
42
+ issue = await client.get_jira_issue(issue_key)
43
+ except Exception as e:
44
+ logger.error(f"Unexpected error getting Jira issue: {e}")
45
+ raise ToolError(
46
+ f"An unexpected error occurred while getting Jira issue '{issue_key}': {str(e)}"
47
+ )
48
+
49
+ return ToolResult(
50
+ content=f"Successfully retrieved details for issue '{issue_key}'.",
51
+ structured_content=issue.as_flat_dict(),
52
+ )
@@ -22,10 +22,10 @@ from fastmcp.resources import HttpResource
22
22
  from fastmcp.resources import ResourceManager
23
23
 
24
24
  from datarobot_genai.drmcp.core.clients import get_credentials
25
- from datarobot_genai.drmcp.core.clients import get_s3_bucket_info
26
25
  from datarobot_genai.drmcp.core.clients import get_sdk_client
27
26
  from datarobot_genai.drmcp.core.mcp_instance import dr_mcp_tool
28
27
  from datarobot_genai.drmcp.core.utils import generate_presigned_url
28
+ from datarobot_genai.drmcp.tools.clients.s3 import get_s3_bucket_info
29
29
 
30
30
  logger = logging.getLogger(__name__)
31
31
 
@@ -23,11 +23,11 @@ from datarobot_predict import TimeSeriesType
23
23
  from datarobot_predict.deployment import predict as dr_predict
24
24
  from pydantic import BaseModel
25
25
 
26
- from datarobot_genai.drmcp.core.clients import get_s3_bucket_info
27
26
  from datarobot_genai.drmcp.core.clients import get_sdk_client
28
27
  from datarobot_genai.drmcp.core.mcp_instance import dr_mcp_tool
29
28
  from datarobot_genai.drmcp.core.utils import PredictionResponse
30
29
  from datarobot_genai.drmcp.core.utils import predictions_result_response
30
+ from datarobot_genai.drmcp.tools.clients.s3 import get_s3_bucket_info
31
31
 
32
32
  logger = logging.getLogger(__name__)
33
33
 
@@ -17,6 +17,15 @@ from collections.abc import AsyncGenerator
17
17
  from typing import Any
18
18
  from typing import cast
19
19
 
20
+ from ag_ui.core import Event
21
+ from ag_ui.core import EventType
22
+ from ag_ui.core import TextMessageContentEvent
23
+ from ag_ui.core import TextMessageEndEvent
24
+ from ag_ui.core import TextMessageStartEvent
25
+ from ag_ui.core import ToolCallArgsEvent
26
+ from ag_ui.core import ToolCallEndEvent
27
+ from ag_ui.core import ToolCallResultEvent
28
+ from ag_ui.core import ToolCallStartEvent
20
29
  from langchain.tools import BaseTool
21
30
  from langchain_core.messages import AIMessageChunk
22
31
  from langchain_core.messages import ToolMessage
@@ -158,43 +167,7 @@ class LangGraphAgent(BaseAgent[BaseTool], abc.ABC):
158
167
  # The main difference is returning a generator for streaming or a final response for sync.
159
168
  if is_streaming(completion_create_params):
160
169
  # Streaming response: yield each message as it is generated
161
- async def stream_generator() -> AsyncGenerator[
162
- tuple[str, MultiTurnSample | None, UsageMetrics], None
163
- ]:
164
- # Iterate over the graph stream. For message events, yield the content.
165
- # For update events, accumulate the usage metrics.
166
- events = []
167
- async for _, mode, event in graph_stream:
168
- if mode == "messages":
169
- message_event: tuple[AIMessageChunk, dict[str, Any]] = event # type: ignore[assignment]
170
- llm_token, _ = message_event
171
- yield (
172
- str(llm_token.content),
173
- None,
174
- usage_metrics,
175
- )
176
- elif mode == "updates":
177
- update_event: dict[str, Any] = event # type: ignore[assignment]
178
- events.append(update_event)
179
- current_node = next(iter(update_event))
180
- node_data = update_event[current_node]
181
- current_usage = node_data.get("usage", {}) if node_data is not None else {}
182
- if current_usage:
183
- usage_metrics["total_tokens"] += current_usage.get("total_tokens", 0)
184
- usage_metrics["prompt_tokens"] += current_usage.get("prompt_tokens", 0)
185
- usage_metrics["completion_tokens"] += current_usage.get(
186
- "completion_tokens", 0
187
- )
188
- else:
189
- raise ValueError(f"Invalid mode: {mode}")
190
-
191
- # Create a list of events from the event listener
192
- pipeline_interactions = self.create_pipeline_interactions_from_events(events)
193
-
194
- # yield the final response indicating completion
195
- yield "", pipeline_interactions, usage_metrics
196
-
197
- return stream_generator()
170
+ return self._stream_generator(graph_stream, usage_metrics)
198
171
  else:
199
172
  # Synchronous response: collect all events and return the final message
200
173
  events: list[dict[str, Any]] = [
@@ -203,6 +176,16 @@ class LangGraphAgent(BaseAgent[BaseTool], abc.ABC):
203
176
  if mode == "updates"
204
177
  ]
205
178
 
179
+ # Accumulate the usage metrics from the updates
180
+ for update in events:
181
+ current_node = next(iter(update))
182
+ node_data = update[current_node]
183
+ current_usage = node_data.get("usage", {}) if node_data is not None else {}
184
+ if current_usage:
185
+ usage_metrics["total_tokens"] += current_usage.get("total_tokens", 0)
186
+ usage_metrics["prompt_tokens"] += current_usage.get("prompt_tokens", 0)
187
+ usage_metrics["completion_tokens"] += current_usage.get("completion_tokens", 0)
188
+
206
189
  pipeline_interactions = self.create_pipeline_interactions_from_events(events)
207
190
 
208
191
  # Extract the final event from the graph stream as the synchronous response
@@ -214,14 +197,132 @@ class LangGraphAgent(BaseAgent[BaseTool], abc.ABC):
214
197
  if node_data is not None and "messages" in node_data
215
198
  else ""
216
199
  )
217
- current_usage = node_data.get("usage", {}) if node_data is not None else {}
218
- if current_usage:
219
- usage_metrics["total_tokens"] += current_usage.get("total_tokens", 0)
220
- usage_metrics["prompt_tokens"] += current_usage.get("prompt_tokens", 0)
221
- usage_metrics["completion_tokens"] += current_usage.get("completion_tokens", 0)
222
200
 
223
201
  return response_text, pipeline_interactions, usage_metrics
224
202
 
203
+ async def _stream_generator(
204
+ self, graph_stream: AsyncGenerator[tuple[Any, str, Any], None], usage_metrics: UsageMetrics
205
+ ) -> AsyncGenerator[tuple[str | Event, MultiTurnSample | None, UsageMetrics], None]:
206
+ # Iterate over the graph stream. For message events, yield the content.
207
+ # For update events, accumulate the usage metrics.
208
+ events = []
209
+ current_message_id = None
210
+ tool_call_id = ""
211
+ async for _, mode, event in graph_stream:
212
+ if mode == "messages":
213
+ message_event: tuple[AIMessageChunk | ToolMessage, dict[str, Any]] = event # type: ignore[assignment]
214
+ message = message_event[0]
215
+ if isinstance(message, ToolMessage):
216
+ yield (
217
+ ToolCallEndEvent(
218
+ type=EventType.TOOL_CALL_END, tool_call_id=message.tool_call_id
219
+ ),
220
+ None,
221
+ usage_metrics,
222
+ )
223
+ yield (
224
+ ToolCallResultEvent(
225
+ type=EventType.TOOL_CALL_RESULT,
226
+ message_id=message.id,
227
+ tool_call_id=message.tool_call_id,
228
+ content=message.content,
229
+ role="tool",
230
+ ),
231
+ None,
232
+ usage_metrics,
233
+ )
234
+ tool_call_id = ""
235
+ elif isinstance(message, AIMessageChunk):
236
+ if message.tool_call_chunks:
237
+ # This is a tool call message
238
+ for tool_call_chunk in message.tool_call_chunks:
239
+ if name := tool_call_chunk.get("name"):
240
+ # Its a tool call start message
241
+ tool_call_id = tool_call_chunk["id"]
242
+ yield (
243
+ ToolCallStartEvent(
244
+ type=EventType.TOOL_CALL_START,
245
+ tool_call_id=tool_call_id,
246
+ tool_call_name=name,
247
+ parent_message_id=message.id,
248
+ ),
249
+ None,
250
+ usage_metrics,
251
+ )
252
+ elif args := tool_call_chunk.get("args"):
253
+ # Its a tool call args message
254
+ yield (
255
+ ToolCallArgsEvent(
256
+ type=EventType.TOOL_CALL_ARGS,
257
+ # Its empty when the tool chunk is not a start message
258
+ # So we use the tool call id from a previous start message
259
+ tool_call_id=tool_call_id,
260
+ delta=args,
261
+ ),
262
+ None,
263
+ usage_metrics,
264
+ )
265
+ elif message.content:
266
+ # Its a text message
267
+ # Handle the start and end of the text message
268
+ if message.id != current_message_id:
269
+ if current_message_id:
270
+ yield (
271
+ TextMessageEndEvent(
272
+ type=EventType.TEXT_MESSAGE_END,
273
+ message_id=current_message_id,
274
+ ),
275
+ None,
276
+ usage_metrics,
277
+ )
278
+ current_message_id = message.id
279
+ yield (
280
+ TextMessageStartEvent(
281
+ type=EventType.TEXT_MESSAGE_START,
282
+ message_id=message.id,
283
+ role="assistant",
284
+ ),
285
+ None,
286
+ usage_metrics,
287
+ )
288
+ yield (
289
+ TextMessageContentEvent(
290
+ type=EventType.TEXT_MESSAGE_CONTENT,
291
+ message_id=message.id,
292
+ delta=message.content,
293
+ ),
294
+ None,
295
+ usage_metrics,
296
+ )
297
+ else:
298
+ raise ValueError(f"Invalid message event: {message_event}")
299
+ elif mode == "updates":
300
+ update_event: dict[str, Any] = event # type: ignore[assignment]
301
+ events.append(update_event)
302
+ current_node = next(iter(update_event))
303
+ node_data = update_event[current_node]
304
+ current_usage = node_data.get("usage", {}) if node_data is not None else {}
305
+ if current_usage:
306
+ usage_metrics["total_tokens"] += current_usage.get("total_tokens", 0)
307
+ usage_metrics["prompt_tokens"] += current_usage.get("prompt_tokens", 0)
308
+ usage_metrics["completion_tokens"] += current_usage.get("completion_tokens", 0)
309
+ if current_message_id:
310
+ yield (
311
+ TextMessageEndEvent(
312
+ type=EventType.TEXT_MESSAGE_END,
313
+ message_id=current_message_id,
314
+ ),
315
+ None,
316
+ usage_metrics,
317
+ )
318
+ current_message_id = None
319
+
320
+ # Create a list of events from the event listener
321
+ pipeline_interactions = self.create_pipeline_interactions_from_events(events)
322
+
323
+ # yield the final response indicating completion
324
+ yield "", pipeline_interactions, usage_metrics
325
+
225
326
  @classmethod
226
327
  def create_pipeline_interactions_from_events(
227
328
  cls,
@@ -123,6 +123,8 @@ class NatAgent(BaseAgent[None]):
123
123
  model: str | None = None,
124
124
  verbose: bool | str | None = True,
125
125
  timeout: int | None = 90,
126
+ authorization_context: dict[str, Any] | None = None,
127
+ forwarded_headers: dict[str, str] | None = None,
126
128
  **kwargs: Any,
127
129
  ) -> None:
128
130
  super().__init__(
@@ -131,6 +133,8 @@ class NatAgent(BaseAgent[None]):
131
133
  model=model,
132
134
  verbose=verbose,
133
135
  timeout=timeout,
136
+ authorization_context=authorization_context,
137
+ forwarded_headers=forwarded_headers,
134
138
  **kwargs,
135
139
  )
136
140
  self.workflow_path = workflow_path
@@ -0,0 +1,110 @@
1
+ # Copyright 2025 DataRobot, Inc. and its affiliates.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from collections.abc import AsyncGenerator
15
+ from typing import Any
16
+
17
+ from datarobot.core.config import DataRobotAppFrameworkBaseSettings
18
+ from nat.authentication.api_key.api_key_auth_provider import APIKeyAuthProvider
19
+ from nat.authentication.api_key.api_key_auth_provider_config import APIKeyAuthProviderConfig
20
+ from nat.authentication.interfaces import AuthProviderBase
21
+ from nat.builder.builder import Builder
22
+ from nat.cli.register_workflow import register_auth_provider
23
+ from nat.data_models.authentication import AuthProviderBaseConfig
24
+ from nat.data_models.authentication import AuthResult
25
+ from nat.data_models.authentication import HeaderCred
26
+ from pydantic import Field
27
+
28
+ from datarobot_genai.core.mcp.common import MCPConfig
29
+
30
+
31
+ class Config(DataRobotAppFrameworkBaseSettings):
32
+ """
33
+ Finds variables in the priority order of: env
34
+ variables (including Runtime Parameters), .env, file_secrets, then
35
+ Pulumi output variables.
36
+ """
37
+
38
+ datarobot_api_token: str | None = None
39
+
40
+
41
+ config = Config()
42
+
43
+
44
+ class DataRobotAPIKeyAuthProviderConfig(APIKeyAuthProviderConfig, name="datarobot_api_key"): # type: ignore[call-arg]
45
+ raw_key: str = Field(
46
+ description=(
47
+ "Raw API token or credential to be injected into the request parameter. "
48
+ "Used for 'bearer','x-api-key','custom', and other schemes. "
49
+ ),
50
+ default=config.datarobot_api_token,
51
+ )
52
+ default_user_id: str | None = Field(default="default-user", description="Default user ID")
53
+ allow_default_user_id_for_tool_calls: bool = Field(
54
+ default=True, description="Allow default user ID for tool calls"
55
+ )
56
+
57
+
58
+ @register_auth_provider(config_type=DataRobotAPIKeyAuthProviderConfig)
59
+ async def datarobot_api_key_client(
60
+ config: DataRobotAPIKeyAuthProviderConfig, builder: Builder
61
+ ) -> AsyncGenerator[APIKeyAuthProvider]:
62
+ yield APIKeyAuthProvider(config=config)
63
+
64
+
65
+ mcp_config = MCPConfig().server_config
66
+
67
+
68
+ class DataRobotMCPAuthProviderConfig(AuthProviderBaseConfig, name="datarobot_mcp_auth"): # type: ignore[call-arg]
69
+ headers: dict[str, str] | None = Field(
70
+ description=("Headers to be used for authentication. "),
71
+ default=mcp_config["headers"] if mcp_config else None,
72
+ )
73
+ default_user_id: str | None = Field(default="default-user", description="Default user ID")
74
+ allow_default_user_id_for_tool_calls: bool = Field(
75
+ default=True, description="Allow default user ID for tool calls"
76
+ )
77
+
78
+
79
+ class DataRobotMCPAuthProvider(AuthProviderBase[DataRobotMCPAuthProviderConfig]):
80
+ def __init__(
81
+ self, config: DataRobotMCPAuthProviderConfig, config_name: str | None = None
82
+ ) -> None:
83
+ assert isinstance(config, DataRobotMCPAuthProviderConfig), (
84
+ "Config is not DataRobotMCPAuthProviderConfig"
85
+ )
86
+ super().__init__(config)
87
+
88
+ async def authenticate(self, user_id: str | None = None, **kwargs: Any) -> AuthResult | None:
89
+ """
90
+ Authenticate the user using the API key credentials.
91
+
92
+ Args:
93
+ user_id (str): The user ID to authenticate.
94
+
95
+ Returns
96
+ -------
97
+ AuthenticatedContext: The authenticated context containing headers
98
+ """
99
+ return AuthResult(
100
+ credentials=[
101
+ HeaderCred(name=name, value=value) for name, value in self.config.headers.items()
102
+ ]
103
+ )
104
+
105
+
106
+ @register_auth_provider(config_type=DataRobotMCPAuthProviderConfig)
107
+ async def datarobot_mcp_auth_provider(
108
+ config: DataRobotMCPAuthProviderConfig, builder: Builder
109
+ ) -> AsyncGenerator[DataRobotMCPAuthProvider]:
110
+ yield DataRobotMCPAuthProvider(config=config)
@@ -0,0 +1,234 @@
1
+ # Copyright 2025 DataRobot, Inc. and its affiliates.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import logging
16
+ from datetime import timedelta
17
+ from typing import Literal
18
+
19
+ import httpx
20
+ from nat.authentication.interfaces import AuthProviderBase
21
+ from nat.builder.builder import Builder
22
+ from nat.cli.register_workflow import register_function_group
23
+ from nat.data_models.component_ref import AuthenticationRef
24
+ from nat.plugins.mcp.client_base import AuthAdapter
25
+ from nat.plugins.mcp.client_base import MCPSSEClient
26
+ from nat.plugins.mcp.client_base import MCPStdioClient
27
+ from nat.plugins.mcp.client_base import MCPStreamableHTTPClient
28
+ from nat.plugins.mcp.client_config import MCPServerConfig
29
+ from nat.plugins.mcp.client_impl import MCPClientConfig
30
+ from nat.plugins.mcp.client_impl import MCPFunctionGroup
31
+ from nat.plugins.mcp.client_impl import mcp_apply_tool_alias_and_description
32
+ from nat.plugins.mcp.client_impl import mcp_session_tool_function
33
+ from pydantic import Field
34
+ from pydantic import HttpUrl
35
+
36
+ from datarobot_genai.core.mcp.common import MCPConfig
37
+
38
+ logger = logging.getLogger(__name__)
39
+
40
+ config = MCPConfig().server_config
41
+
42
+
43
+ class DataRobotMCPServerConfig(MCPServerConfig):
44
+ transport: Literal["streamable-http", "sse", "stdio"] = Field(
45
+ default=config["transport"] if config else "stdio",
46
+ description="Transport type to connect to the MCP server (sse or streamable-http)",
47
+ )
48
+ url: HttpUrl | None = Field(
49
+ default=config["url"] if config else None,
50
+ description="URL of the MCP server (for sse or streamable-http transport)",
51
+ )
52
+ # Authentication configuration
53
+ auth_provider: str | AuthenticationRef | None = Field(
54
+ default="datarobot_mcp_auth" if config else None,
55
+ description="Reference to authentication provider",
56
+ )
57
+ command: str | None = Field(
58
+ default=None if config else "docker",
59
+ description="Command to run for stdio transport (e.g. 'python' or 'docker')",
60
+ )
61
+
62
+
63
+ class DataRobotMCPClientConfig(MCPClientConfig, name="datarobot_mcp_client"): # type: ignore[call-arg]
64
+ server: DataRobotMCPServerConfig = Field(
65
+ default=DataRobotMCPServerConfig(), description="DataRobot MCP Server configuration"
66
+ )
67
+
68
+
69
+ class DataRobotAuthAdapter(AuthAdapter):
70
+ async def _get_auth_headers(
71
+ self, request: httpx.Request | None = None, response: httpx.Response | None = None
72
+ ) -> dict[str, str]:
73
+ """Get authentication headers from the NAT auth provider."""
74
+ try:
75
+ # Use the user_id passed to this AuthAdapter instance
76
+ auth_result = await self.auth_provider.authenticate(
77
+ user_id=self.user_id, response=response
78
+ )
79
+ as_kwargs = auth_result.as_requests_kwargs()
80
+ return as_kwargs["headers"]
81
+ except Exception as e:
82
+ logger.warning("Failed to get auth token: %s", e)
83
+ return {}
84
+
85
+
86
+ class DataRobotMCPStreamableHTTPClient(MCPStreamableHTTPClient):
87
+ def __init__(
88
+ self,
89
+ url: str,
90
+ auth_provider: AuthProviderBase | None = None,
91
+ user_id: str | None = None,
92
+ tool_call_timeout: timedelta = timedelta(seconds=60),
93
+ auth_flow_timeout: timedelta = timedelta(seconds=300),
94
+ reconnect_enabled: bool = True,
95
+ reconnect_max_attempts: int = 2,
96
+ reconnect_initial_backoff: float = 0.5,
97
+ reconnect_max_backoff: float = 50.0,
98
+ ):
99
+ super().__init__(
100
+ url=url,
101
+ auth_provider=auth_provider,
102
+ user_id=user_id,
103
+ tool_call_timeout=tool_call_timeout,
104
+ auth_flow_timeout=auth_flow_timeout,
105
+ reconnect_enabled=reconnect_enabled,
106
+ reconnect_max_attempts=reconnect_max_attempts,
107
+ reconnect_initial_backoff=reconnect_initial_backoff,
108
+ reconnect_max_backoff=reconnect_max_backoff,
109
+ )
110
+ effective_user_id = user_id or (
111
+ auth_provider.config.default_user_id if auth_provider else None
112
+ )
113
+ self._httpx_auth = (
114
+ DataRobotAuthAdapter(auth_provider, effective_user_id) if auth_provider else None
115
+ )
116
+
117
+
118
+ @register_function_group(config_type=DataRobotMCPClientConfig)
119
+ async def datarobot_mcp_client_function_group(
120
+ config: DataRobotMCPClientConfig, _builder: Builder
121
+ ) -> MCPFunctionGroup:
122
+ """
123
+ Connect to an MCP server and expose tools as a function group.
124
+
125
+ Args:
126
+ config: The configuration for the MCP client
127
+ _builder: The builder
128
+ Returns:
129
+ The function group
130
+ """
131
+ # Resolve auth provider if specified
132
+ auth_provider = None
133
+ if config.server.auth_provider:
134
+ auth_provider = await _builder.get_auth_provider(config.server.auth_provider)
135
+
136
+ # Build the appropriate client
137
+ if config.server.transport == "stdio":
138
+ if not config.server.command:
139
+ raise ValueError("command is required for stdio transport")
140
+ client = MCPStdioClient(
141
+ config.server.command,
142
+ config.server.args,
143
+ config.server.env,
144
+ tool_call_timeout=config.tool_call_timeout,
145
+ auth_flow_timeout=config.auth_flow_timeout,
146
+ reconnect_enabled=config.reconnect_enabled,
147
+ reconnect_max_attempts=config.reconnect_max_attempts,
148
+ reconnect_initial_backoff=config.reconnect_initial_backoff,
149
+ reconnect_max_backoff=config.reconnect_max_backoff,
150
+ )
151
+ elif config.server.transport == "sse":
152
+ client = MCPSSEClient(
153
+ str(config.server.url),
154
+ tool_call_timeout=config.tool_call_timeout,
155
+ auth_flow_timeout=config.auth_flow_timeout,
156
+ reconnect_enabled=config.reconnect_enabled,
157
+ reconnect_max_attempts=config.reconnect_max_attempts,
158
+ reconnect_initial_backoff=config.reconnect_initial_backoff,
159
+ reconnect_max_backoff=config.reconnect_max_backoff,
160
+ )
161
+ elif config.server.transport == "streamable-http":
162
+ # Use default_user_id for the base client
163
+ base_user_id = auth_provider.config.default_user_id if auth_provider else None
164
+ client = DataRobotMCPStreamableHTTPClient(
165
+ str(config.server.url),
166
+ auth_provider=auth_provider,
167
+ user_id=base_user_id,
168
+ tool_call_timeout=config.tool_call_timeout,
169
+ auth_flow_timeout=config.auth_flow_timeout,
170
+ reconnect_enabled=config.reconnect_enabled,
171
+ reconnect_max_attempts=config.reconnect_max_attempts,
172
+ reconnect_initial_backoff=config.reconnect_initial_backoff,
173
+ reconnect_max_backoff=config.reconnect_max_backoff,
174
+ )
175
+ else:
176
+ raise ValueError(f"Unsupported transport: {config.server.transport}")
177
+
178
+ logger.info("Configured to use MCP server at %s", client.server_name)
179
+
180
+ # Create the MCP function group
181
+ group = MCPFunctionGroup(config=config)
182
+
183
+ # Store shared components for session client creation
184
+ group._shared_auth_provider = auth_provider
185
+ group._client_config = config
186
+
187
+ async with client:
188
+ # Expose the live MCP client on the function group instance so other components
189
+ # (e.g., HTTP endpoints) can reuse the already-established session instead of creating a
190
+ # new client per request.
191
+ group.mcp_client = client
192
+ group.mcp_client_server_name = client.server_name
193
+ group.mcp_client_transport = client.transport
194
+
195
+ all_tools = await client.get_tools()
196
+ tool_overrides = mcp_apply_tool_alias_and_description(all_tools, config.tool_overrides)
197
+
198
+ # Add each tool as a function to the group
199
+ for tool_name, tool in all_tools.items():
200
+ # Get override if it exists
201
+ override = tool_overrides.get(tool_name)
202
+
203
+ # Use override values or defaults
204
+ function_name = override.alias if override and override.alias else tool_name
205
+ description = (
206
+ override.description if override and override.description else tool.description
207
+ )
208
+
209
+ # Create the tool function according to configuration
210
+ tool_fn = mcp_session_tool_function(tool, group)
211
+
212
+ # Normalize optional typing for linter/type-checker compatibility
213
+ single_fn = tool_fn.single_fn
214
+ if single_fn is None:
215
+ # Should not happen because FunctionInfo always sets a single_fn
216
+ logger.warning("Skipping tool %s because single_fn is None", function_name)
217
+ continue
218
+
219
+ input_schema = tool_fn.input_schema
220
+ # Convert NoneType sentinel to None for FunctionGroup.add_function signature
221
+ if input_schema is type(None): # noqa: E721
222
+ input_schema = None
223
+
224
+ # Add to group
225
+ logger.info("Adding tool %s to group", function_name)
226
+ group.add_function(
227
+ name=function_name,
228
+ description=description,
229
+ fn=single_fn,
230
+ input_schema=input_schema,
231
+ converters=tool_fn.converters,
232
+ )
233
+
234
+ yield group