Skip to content

Python: Propagate MCP initialization failures#14095

Draft
lxingy3 wants to merge 1 commit into
microsoft:mainfrom
lxingy3:fix-mcp-connect-init-errors
Draft

Python: Propagate MCP initialization failures#14095
lxingy3 wants to merge 1 commit into
microsoft:mainfrom
lxingy3:fix-mcp-connect-init-errors

Conversation

@lxingy3

@lxingy3 lxingy3 commented Jun 17, 2026

Copy link
Copy Markdown

Motivation and Context

Closes #13414.

When MCP streamable HTTP session initialization fails, for example after an authentication error from the server, the background connection task can raise before ready_event is set. connect() keeps waiting on that event, so the caller sees a hang instead of the initialization error.

Description

This change makes the connection setup fail closed:

  • _inner_connect() sets ready_event before propagating any setup failure.
  • connect() checks whether the background task already completed after the event is set, then awaits it so the original KernelPluginInvalidConfigurationError reaches the caller.
  • A unit test covers a streamable HTTP connection where transport setup succeeds but ClientSession.initialize() fails.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the SK Contribution Guidelines and the checked files pass ruff
  • All unit tests pass, and I have added new tests where possible
  • I didn't break anyone

Validation:

  • python -m pytest python\tests\unit\connectors\mcp\test_mcp.py::test_streamable_http_initialization_failure_unblocks_connect -q
  • python -m pytest python\tests\unit\connectors\mcp\test_mcp.py -q
  • python -m ruff check python\semantic_kernel\connectors\mcp.py python\tests\unit\connectors\mcp\test_mcp.py
  • git diff --check

@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label Jun 17, 2026
@lxingy3

lxingy3 commented Jun 25, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Bug: MCPStreamableHttpPlugin.__aenter__() method doesn't treat HTTP 401/403 as fatal errors

2 participants