mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add diagnostics to Model Context Protocol Client (#189159)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
8c43c16fbb
commit
39b9030d02
@@ -265,6 +265,7 @@ class ModelContextProtocolCoordinator(DataUpdateCoordinator[list[llm.Tool]]):
|
||||
update_interval=UPDATE_INTERVAL,
|
||||
)
|
||||
self.token_manager = token_manager
|
||||
self.server_info: InitializeResult | None = None
|
||||
|
||||
@override
|
||||
async def _async_update_data(self) -> list[llm.Tool]:
|
||||
@@ -277,7 +278,7 @@ class ModelContextProtocolCoordinator(DataUpdateCoordinator[list[llm.Tool]]):
|
||||
async with asyncio.timeout(TIMEOUT):
|
||||
async with mcp_client(
|
||||
self.hass, self.config_entry.data[CONF_URL], self.token_manager
|
||||
) as (session, _):
|
||||
) as (session, server_info):
|
||||
result = await session.list_tools()
|
||||
except (TimeoutError, httpx2.TimeoutException) as error:
|
||||
_LOGGER.debug("Timeout when listing tools: %s", error)
|
||||
@@ -303,6 +304,7 @@ class ModelContextProtocolCoordinator(DataUpdateCoordinator[list[llm.Tool]]):
|
||||
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
||||
|
||||
_LOGGER.debug("Received tools: %s", result.tools)
|
||||
self.server_info = server_info
|
||||
tools: list[llm.Tool] = []
|
||||
for tool in result.tools:
|
||||
try:
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Diagnostics support for the Model Context Protocol integration."""
|
||||
|
||||
from dataclasses import asdict
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
import probatio
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .types import ModelContextProtocolConfigEntry
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ModelContextProtocolConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
server_info = coordinator.server_info
|
||||
if TYPE_CHECKING:
|
||||
assert server_info is not None
|
||||
return {
|
||||
"authentication": "auth_implementation" in entry.data,
|
||||
"server": server_info.model_dump(mode="json", by_alias=True, exclude_none=True),
|
||||
"tools": [
|
||||
{
|
||||
"name": tool.name,
|
||||
"title": tool.title,
|
||||
"description": tool.description,
|
||||
"annotations": asdict(tool.annotations),
|
||||
"input_schema": probatio.to_openapi(tool.parameters),
|
||||
}
|
||||
for tool in coordinator.data
|
||||
],
|
||||
}
|
||||
@@ -57,7 +57,7 @@ rules:
|
||||
devices:
|
||||
status: exempt
|
||||
comment: Integration does not have devices.
|
||||
diagnostics: todo
|
||||
diagnostics: done
|
||||
discovery-update-info: done
|
||||
discovery: done
|
||||
docs-data-update: done
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
# serializer version: 1
|
||||
# name: test_diagnostics
|
||||
dict({
|
||||
'authentication': False,
|
||||
'server': dict({
|
||||
'capabilities': dict({
|
||||
'tools': dict({
|
||||
'listChanged': True,
|
||||
}),
|
||||
}),
|
||||
'protocolVersion': '2025-03-26',
|
||||
'serverInfo': dict({
|
||||
'name': 'Memory Server',
|
||||
'version': '1.0.0',
|
||||
}),
|
||||
}),
|
||||
'tools': list([
|
||||
dict({
|
||||
'annotations': dict({
|
||||
'destructive': True,
|
||||
'idempotent': False,
|
||||
'open_world': False,
|
||||
'read_only': True,
|
||||
}),
|
||||
'description': 'Search memory for relevant context based on a query.',
|
||||
'input_schema': dict({
|
||||
'additionalProperties': False,
|
||||
'properties': dict({
|
||||
'query': dict({
|
||||
'description': 'A free text query to search context for.',
|
||||
'type': 'string',
|
||||
}),
|
||||
}),
|
||||
'required': list([
|
||||
'query',
|
||||
]),
|
||||
'type': 'object',
|
||||
}),
|
||||
'name': 'search_memory',
|
||||
'title': 'Search memory',
|
||||
}),
|
||||
]),
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Tests for the Model Context Protocol diagnostics."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from mcp.types import (
|
||||
Implementation,
|
||||
InitializeResult,
|
||||
ListToolsResult,
|
||||
ServerCapabilities,
|
||||
Tool,
|
||||
ToolAnnotations,
|
||||
ToolsCapability,
|
||||
)
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from pytest_homeassistant.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from pytest_homeassistant.typing import ClientSessionGenerator
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .conftest import TEST_API_NAME
|
||||
|
||||
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_mcp_client: AsyncMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
session = mock_mcp_client.return_value
|
||||
session.initialize.return_value = InitializeResult(
|
||||
protocolVersion="2025-03-26",
|
||||
capabilities=ServerCapabilities(tools=ToolsCapability(listChanged=True)),
|
||||
serverInfo=Implementation(name=TEST_API_NAME, version="1.0.0"),
|
||||
)
|
||||
session.list_tools.return_value = ListToolsResult(
|
||||
tools=[
|
||||
Tool(
|
||||
name="search_memory",
|
||||
title="Search memory",
|
||||
description="Search memory for relevant context based on a query.",
|
||||
inputSchema={
|
||||
"type": "object",
|
||||
"required": ["query"],
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "A free text query to search context for.",
|
||||
}
|
||||
},
|
||||
},
|
||||
annotations=ToolAnnotations(readOnlyHint=True, openWorldHint=False),
|
||||
)
|
||||
]
|
||||
)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||
== snapshot
|
||||
)
|
||||
Reference in New Issue
Block a user