Add multiple LLM API support for MCP Server (#147785)

* Add multiple LLM API support for MCP Server

* Update homeassistant/components/mcp_server/config_flow.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* ruff

* Update tests/components/mcp_server/conftest.py

Co-authored-by: Allen Porter <allen.porter@gmail.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Allen Porter <allen.porter@gmail.com>
This commit is contained in:
Denis Shulyaka 2025-07-01 16:14:03 +03:00 committed by GitHub
parent 073a467fb2
commit 7deca35172
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 55 additions and 12 deletions

View File

@ -32,10 +32,17 @@ class ModelContextServerProtocolConfigFlow(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult: ) -> ConfigFlowResult:
"""Handle the initial step.""" """Handle the initial step."""
errors: dict[str, str] = {}
llm_apis = {api.id: api.name for api in llm.async_get_apis(self.hass)} llm_apis = {api.id: api.name for api in llm.async_get_apis(self.hass)}
if user_input is not None: if user_input is not None:
if not user_input[CONF_LLM_HASS_API]:
errors[CONF_LLM_HASS_API] = "llm_api_required"
else:
return self.async_create_entry( return self.async_create_entry(
title=llm_apis[user_input[CONF_LLM_HASS_API]], data=user_input title=", ".join(
llm_apis[api_id] for api_id in user_input[CONF_LLM_HASS_API]
),
data=user_input,
) )
return self.async_show_form( return self.async_show_form(
@ -44,7 +51,7 @@ class ModelContextServerProtocolConfigFlow(ConfigFlow, domain=DOMAIN):
{ {
vol.Optional( vol.Optional(
CONF_LLM_HASS_API, CONF_LLM_HASS_API,
default=llm.LLM_API_ASSIST, default=[llm.LLM_API_ASSIST],
): SelectSelector( ): SelectSelector(
SelectSelectorConfig( SelectSelectorConfig(
options=[ options=[
@ -53,10 +60,12 @@ class ModelContextServerProtocolConfigFlow(ConfigFlow, domain=DOMAIN):
value=llm_api_id, value=llm_api_id,
) )
for llm_api_id, name in llm_apis.items() for llm_api_id, name in llm_apis.items()
] ],
multiple=True,
) )
), ),
} }
), ),
description_placeholders={"more_info_url": MORE_INFO_URL}, description_placeholders={"more_info_url": MORE_INFO_URL},
errors=errors,
) )

View File

@ -42,7 +42,7 @@ def _format_tool(
async def create_server( async def create_server(
hass: HomeAssistant, llm_api_id: str, llm_context: llm.LLMContext hass: HomeAssistant, llm_api_id: str | list[str], llm_context: llm.LLMContext
) -> Server: ) -> Server:
"""Create a new Model Context Protocol Server. """Create a new Model Context Protocol Server.

View File

@ -11,6 +11,9 @@
} }
} }
}, },
"error": {
"llm_api_required": "At least one LLM API must be configured."
},
"abort": { "abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]" "already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
} }

View File

@ -23,13 +23,15 @@ def mock_setup_entry() -> Generator[AsyncMock]:
@pytest.fixture(name="llm_hass_api") @pytest.fixture(name="llm_hass_api")
def llm_hass_api_fixture() -> str: def llm_hass_api_fixture() -> list[str]:
"""Fixture for the config entry llm_hass_api.""" """Fixture for the config entry llm_hass_api."""
return llm.LLM_API_ASSIST return [llm.LLM_API_ASSIST]
@pytest.fixture(name="config_entry") @pytest.fixture(name="config_entry")
def mock_config_entry(hass: HomeAssistant, llm_hass_api: str) -> MockConfigEntry: def mock_config_entry(
hass: HomeAssistant, llm_hass_api: str | list[str]
) -> MockConfigEntry:
"""Fixture to load the integration.""" """Fixture to load the integration."""
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,

View File

@ -16,7 +16,7 @@ from homeassistant.data_entry_flow import FlowResultType
"params", "params",
[ [
{}, {},
{CONF_LLM_HASS_API: "assist"}, {CONF_LLM_HASS_API: ["assist"]},
], ],
) )
async def test_form( async def test_form(
@ -38,4 +38,33 @@ async def test_form(
assert result["type"] == FlowResultType.CREATE_ENTRY assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Assist" assert result["title"] == "Assist"
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
assert result["data"] == {CONF_LLM_HASS_API: "assist"} assert result["data"] == {CONF_LLM_HASS_API: ["assist"]}
@pytest.mark.parametrize(
("params", "errors"),
[
({CONF_LLM_HASS_API: []}, {CONF_LLM_HASS_API: "llm_api_required"}),
],
)
async def test_form_errors(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
params: dict[str, Any],
errors: dict[str, str],
) -> None:
"""Test we get the errors on invalid user input."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.FORM
assert not result["errors"]
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
params,
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["errors"] == errors

View File

@ -194,7 +194,7 @@ async def test_http_sse_multiple_config_entries(
""" """
config_entry = MockConfigEntry( config_entry = MockConfigEntry(
domain="mcp_server", data={CONF_LLM_HASS_API: "llm-api-id"} domain="mcp_server", data={CONF_LLM_HASS_API: ["llm-api-id"]}
) )
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id) await hass.config_entries.async_setup(config_entry.entry_id)