Files
core/tests/components/mcp_server/conftest.py
Claude 5480cf2e5e Support multiple config entries for MCP Server
Allow the Model Context Protocol Server integration to have multiple config
entries, each served on its own Streamable HTTP URL at
/api/mcp_server/<url id>.

The URL identifier is generated when the entry is created and stays stable
across the entry's lifetime. For one or two selected APIs it is derived from
the API IDs (and the entry is named after them); for more APIs a random
identifier is used and the entry is named "MCP Server" (with a counter when
that name is already taken). Colliding identifiers get a numeric suffix, so
entries exposing the same APIs are allowed and each keeps a distinct URL.

Existing config entries are migrated to minor version 2 and marked as legacy
so they keep being served on the original fixed URLs (/api/mcp and the SSE
endpoints) for backwards compatibility.

The create flow ends with a confirmation step showing the internal and
external URLs where the new server is reachable.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EaTcVgRD9o2BVwtLziWpa5
2026-07-03 21:53:35 +00:00

61 lines
1.7 KiB
Python

"""Common fixtures for the Model Context Protocol Server tests."""
from collections.abc import Generator
from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.mcp_server.const import CONF_LEGACY, CONF_URL_ID, DOMAIN
from homeassistant.const import CONF_LLM_HASS_API
from homeassistant.core import HomeAssistant
from homeassistant.helpers import llm
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
async def ensure_homeassistant_loaded(hass: HomeAssistant) -> None:
"""Ensure homeassistant component is loaded."""
assert await async_setup_component(hass, "homeassistant", {})
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.mcp_server.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture(name="llm_hass_api")
def llm_hass_api_fixture() -> list[str]:
"""Fixture for the config entry llm_hass_api."""
return [llm.LLM_API_ASSIST]
@pytest.fixture(name="legacy")
def legacy_fixture() -> bool:
"""Fixture for whether the config entry is a legacy entry."""
return True
@pytest.fixture(name="config_entry")
def mock_config_entry(
hass: HomeAssistant, llm_hass_api: str | list[str], legacy: bool
) -> MockConfigEntry:
"""Fixture to load the integration."""
data = {CONF_LLM_HASS_API: llm_hass_api}
if legacy:
data[CONF_LEGACY] = True
else:
data[CONF_URL_ID] = "assist"
config_entry = MockConfigEntry(
domain=DOMAIN,
data=data,
minor_version=2,
)
config_entry.add_to_hass(hass)
return config_entry