using System.ComponentModel; using System.Text.Json; using HomeAssistant.McpServer.Services; using ModelContextProtocol.Server; namespace HomeAssistant.McpServer.Tools; [McpServerToolType] public sealed class SystemTools { private readonly IHomeAssistantApiClient _client; public SystemTools(IHomeAssistantApiClient client) { _client = client; } [McpServerTool, Description( "Get Home Assistant configuration including version, location, components, and unit system. " + "Useful for understanding the HA environment.")] public async Task GetHaConfig(CancellationToken cancellationToken = default) { var result = await _client.GetConfigAsync(cancellationToken).ConfigureAwait(false); return JsonSerializer.Serialize(result, SerializerOptions); } [McpServerTool, Description( "Check if the Home Assistant API is reachable and responding. " + "Returns the API status message or an error description.")] public async Task CheckApi(CancellationToken cancellationToken = default) { try { var result = await _client.GetApiStatusAsync(cancellationToken).ConfigureAwait(false); return JsonSerializer.Serialize(result, SerializerOptions); } catch (HttpRequestException ex) { return JsonSerializer.Serialize(new { status = "error", message = ex.Message }, SerializerOptions); } catch (TaskCanceledException) { return JsonSerializer.Serialize( new { status = "error", message = "Request timed out - HA may be unreachable" }, SerializerOptions); } } private static readonly JsonSerializerOptions SerializerOptions = new() { WriteIndented = true }; }