Skip to content

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

@bararia-swati

Description

@bararia-swati

Describe the bug
The semantic_kernel MCPStreamableHttpPlugin.aenter() method has a bug - it doesn't treat HTTP 401/403 as fatal errors. It just logs them and keeps waiting.
I think the bug is in the _inner_connect() method of the semantic kernel mcp.py file you shared above.

Python
async def _inner_connect(self, ready_event: asyncio.Event) -> None:
try:
await session.initialize()
except Exception as ex:
await self._exit_stack.aclose()
raise KernelPluginInvalidConfigurationError(
"Failed to initialize session. Please check your configuration."
) from ex

The Bug:

  1. connect() creates a background task running _inner_connect() and waits for ready_event
  2. _inner_connect() calls session.initialize(), which makes the HTTP request to the MCP server
  3. MCP server returns HTTP 401 Unauthorized
  4. session.initialize() raises an exception
  5. The exception handler in _inner_connect() raises KernelPluginInvalidConfigurationError
  6. BUG: The exception handler does NOT call ready_event.set()
  7. Meanwhile, connect() is still waiting at await ready_event.wait()
  8. Since ready_event is never set, connect() hangs forever (until PromptFlow's 30-second timeout)
  9. The exception is trapped in the background task and never propagates to our code

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Platform

  • Language: [e.g. C#, Python]
  • Source: [e.g. NuGet package version 0.1.0, pip package version 0.1.0, main branch of repository]
  • AI model: [e.g. OpenAI:GPT-4o-mini(2024-07-18)]
  • IDE: [e.g. Visual Studio, VS Code]
  • OS: [e.g. Windows, Mac]

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue or Pull requests regarding .NET codebugSomething isn't workingpythonPull requests for the Python Semantic Kerneltriage

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions