Skip to content

Commit 000a3b0

Browse files
committed
Preserve configuration errors for supplied MCP sessions
1 parent 5ee6eee commit 000a3b0

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎python/semantic_kernel/connectors/mcp.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,12 @@ async def _inner_connect(self, ready_event: asyncio.Event) -> None:
359359
self.session = session
360360
elif self.session._request_id == 0:
361361
# If the session is not initialized, we need to reinitialize it
362-
await self.session.initialize()
362+
try:
363+
await self.session.initialize()
364+
except Exception as ex:
365+
raise KernelPluginInvalidConfigurationError(
366+
"Failed to initialize session. Please check your configuration."
367+
) from ex
363368
logger.debug("Connected to MCP server: %s", self.session)
364369
if self.load_tools_flag:
365370
await self.load_tools()

‎python/tests/unit/connectors/mcp/test_mcp.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ async def test_mcp_plugin_failed_get_session():
256256
pass
257257

258258

259+
@pytest.mark.parametrize("supplied_session", [False, True])
259260
@patch("semantic_kernel.connectors.mcp.streamablehttp_client")
260261
@patch("semantic_kernel.connectors.mcp.ClientSession")
261-
async def test_streamable_http_initialization_failure_unblocks_connect(mock_session, mock_client):
262+
async def test_streamable_http_initialization_failure_unblocks_connect(mock_session, mock_client, supplied_session):
262263
mock_read = MagicMock()
263264
mock_write = MagicMock()
264265
mock_callback = MagicMock()
@@ -268,18 +269,25 @@ async def test_streamable_http_initialization_failure_unblocks_connect(mock_sess
268269
mock_generator.__aexit__.return_value = (mock_read, mock_write, mock_callback)
269270
mock_client.return_value = mock_generator
270271

271-
mock_session.return_value.__aenter__.return_value.initialize.side_effect = RuntimeError("Unauthorized")
272+
session = mock_session.return_value.__aenter__.return_value
273+
session._request_id = 0
274+
initialization_error = RuntimeError("Unauthorized")
275+
session.initialize.side_effect = initialization_error
272276

273277
plugin = MCPStreamableHttpPlugin(
274278
name="test",
275279
url="http://localhost:8080/mcp",
280+
session=session if supplied_session else None,
276281
load_tools=False,
277282
load_prompts=False,
278283
)
279284

280-
with pytest.raises(KernelPluginInvalidConfigurationError, match="Failed to initialize session"):
285+
with pytest.raises(KernelPluginInvalidConfigurationError, match="Failed to initialize session") as exc_info:
281286
await asyncio.wait_for(plugin.connect(), timeout=1)
282287

288+
assert exc_info.value.__cause__ is initialization_error
289+
assert plugin.session is None
290+
283291

284292
@pytest.mark.parametrize("failed_loader", ["load_tools", "load_prompts"])
285293
@patch("semantic_kernel.connectors.mcp.streamablehttp_client")

0 commit comments

Comments
 (0)