Skip to content

Commit 806f45a

Browse files
committed
lazy create try
1 parent bbe29ce commit 806f45a

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

src/databricks_ai_bridge/genie.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ def __init__(
182182
self.genie = workspace_client.genie
183183
self.description = self.genie.get_space(space_id).description
184184

185-
# Initialize MCP client for communication with Genie backend
186-
server_url = f"{workspace_client.config.host}/api/2.0/mcp/genie/{space_id}"
187-
self._mcp_client = DatabricksMCPClient(server_url, workspace_client)
185+
# Store workspace client for lazy MCP client initialization
186+
self._workspace_client = workspace_client
187+
self._mcp_client = None # Lazy initialization
188188
self._tool_name = f"query_space_{space_id}"
189189

190190
# Keep headers for deprecated REST methods (backwards compatibility)
@@ -393,6 +393,20 @@ def poll_result():
393393

394394
return poll_result()
395395

396+
def _get_mcp_client(self):
397+
"""Lazy initialization of MCP client.
398+
399+
Creates the MCP client on first use to avoid issues with mocked
400+
workspace clients in tests and to defer initialization until needed.
401+
402+
Returns:
403+
DatabricksMCPClient: The initialized MCP client
404+
"""
405+
if self._mcp_client is None:
406+
server_url = f"{self._workspace_client.config.host}/api/2.0/mcp/genie/{self.space_id}"
407+
self._mcp_client = DatabricksMCPClient(server_url, self._workspace_client)
408+
return self._mcp_client
409+
396410
@mlflow.trace()
397411
def ask_question(self, question, conversation_id: Optional[str] = None):
398412
"""Ask a question to the Genie space using MCP protocol.
@@ -408,7 +422,7 @@ def ask_question(self, question, conversation_id: Optional[str] = None):
408422
if conversation_id:
409423
args["conversation_id"] = conversation_id
410424

411-
mcp_result = self._mcp_client.call_tool(self._tool_name, args)
425+
mcp_result = self._get_mcp_client().call_tool(self._tool_name, args)
412426

413427
if not mcp_result.content or len(mcp_result.content) == 0:
414428
return GenieResponse(

tests/databricks_ai_bridge/test_genie.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ def test_ask_question(genie, mock_workspace_client):
131131
]
132132
)
133133

134-
with patch.object(genie._mcp_client, "call_tool", return_value=mock_mcp_result):
134+
mock_mcp_client = MagicMock()
135+
mock_mcp_client.call_tool.return_value = mock_mcp_result
136+
137+
with patch.object(genie, "_get_mcp_client", return_value=mock_mcp_client):
135138
genie_result = genie.ask_question("What is the meaning of life?")
136139
assert genie_result.result == "Answer"
137140
assert genie_result.conversation_id == "123"
@@ -147,7 +150,10 @@ def test_ask_question_continued_conversation(genie, mock_workspace_client):
147150
]
148151
)
149152

150-
with patch.object(genie._mcp_client, "call_tool", return_value=mock_mcp_result):
153+
mock_mcp_client = MagicMock()
154+
mock_mcp_client.call_tool.return_value = mock_mcp_result
155+
156+
with patch.object(genie, "_get_mcp_client", return_value=mock_mcp_client):
151157
genie_result = genie.ask_question("What is the meaning of life?", "123")
152158
assert genie_result.result == "42"
153159
assert genie_result.conversation_id == "123"
@@ -163,11 +169,14 @@ def test_ask_question_calls_mcp_without_conversation_id(genie, mock_workspace_cl
163169
]
164170
)
165171

166-
with patch.object(genie._mcp_client, "call_tool", return_value=mock_mcp_result) as mock_call:
172+
mock_mcp_client = MagicMock()
173+
mock_mcp_client.call_tool.return_value = mock_mcp_result
174+
175+
with patch.object(genie, "_get_mcp_client", return_value=mock_mcp_client):
167176
genie.ask_question("What is the meaning of life?")
168177

169178
# Verify MCP client was called with correct args (no conversation_id)
170-
mock_call.assert_called_once_with(
179+
mock_mcp_client.call_tool.assert_called_once_with(
171180
"query_space_test_space_id", {"query": "What is the meaning of life?"}
172181
)
173182

@@ -183,11 +192,14 @@ def test_ask_question_calls_mcp_with_conversation_id(genie, mock_workspace_clien
183192
]
184193
)
185194

186-
with patch.object(genie._mcp_client, "call_tool", return_value=mock_mcp_result) as mock_call:
195+
mock_mcp_client = MagicMock()
196+
mock_mcp_client.call_tool.return_value = mock_mcp_result
197+
198+
with patch.object(genie, "_get_mcp_client", return_value=mock_mcp_client):
187199
genie.ask_question("What is the meaning of life?", "123")
188200

189201
# Verify MCP client was called with conversation_id included
190-
mock_call.assert_called_once_with(
202+
mock_mcp_client.call_tool.assert_called_once_with(
191203
"query_space_test_space_id",
192204
{"query": "What is the meaning of life?", "conversation_id": "123"},
193205
)

0 commit comments

Comments
 (0)