diff --git a/homeassistant/components/mcp/__init__.py b/homeassistant/components/mcp/__init__.py index 642fa400213..c4238017564 100644 --- a/homeassistant/components/mcp/__init__.py +++ b/homeassistant/components/mcp/__init__.py @@ -7,6 +7,7 @@ from typing import cast from homeassistant.components.application_credentials import AuthorizationServer from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_entry_oauth2_flow, llm from .application_credentials import authorization_server_context @@ -42,7 +43,14 @@ async def _create_token_manager( hass: HomeAssistant, entry: ModelContextProtocolConfigEntry ) -> TokenManager | None: """Create a OAuth token manager for the config entry if the server requires authentication.""" - if not (implementation := await async_get_config_entry_implementation(hass, entry)): + try: + implementation = await async_get_config_entry_implementation(hass, entry) + except config_entry_oauth2_flow.ImplementationUnavailableError as err: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="oauth2_implementation_unavailable", + ) from err + if not implementation: return None session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation) diff --git a/homeassistant/components/mcp/strings.json b/homeassistant/components/mcp/strings.json index 1dcc4400ced..c5e84505b41 100644 --- a/homeassistant/components/mcp/strings.json +++ b/homeassistant/components/mcp/strings.json @@ -56,5 +56,10 @@ } } } + }, + "exceptions": { + "oauth2_implementation_unavailable": { + "message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]" + } } } diff --git a/tests/components/mcp/test_init.py b/tests/components/mcp/test_init.py index 1f063e445ee..0a1f67e1336 100644 --- a/tests/components/mcp/test_init.py +++ b/tests/components/mcp/test_init.py @@ -13,6 +13,9 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.core import Context, HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import llm +from homeassistant.helpers.config_entry_oauth2_flow import ( + ImplementationUnavailableError, +) from .conftest import TEST_API_NAME @@ -342,3 +345,19 @@ async def test_convert_tool_schema_fails( ): await hass.config_entries.async_setup(config_entry.entry_id) assert config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_oauth_implementation_not_available( + hass: HomeAssistant, + config_entry_with_auth: MockConfigEntry, + mock_mcp_client: AsyncMock, +) -> None: + """Test that unavailable OAuth implementation raises ConfigEntryNotReady.""" + with patch( + "homeassistant.components.mcp.async_get_config_entry_implementation", + side_effect=ImplementationUnavailableError, + ): + await hass.config_entries.async_setup(config_entry_with_auth.entry_id) + await hass.async_block_till_done() + + assert config_entry_with_auth.state is ConfigEntryState.SETUP_RETRY