Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

fix: sync MCP node type enum with frontend NodeType

Nebesza88688170a4a
diff --git a/mcp/app/tools.py b/mcp/app/tools.py
index 03fc69e..5140d98 100644
--- a/mcp/app/tools.py
+++ b/mcp/app/tools.py
@@ -4,7 +4,17 @@ from mcp.types import Tool, TextContent
from .backend_client import backend
-NODE_TYPES = ["isp", "router", "switch", "server", "proxmox", "vm", "lxc", "nas", "iot", "ap", "generic"]
+# Kept in sync manually with frontend/src/types/index.ts NodeType (device types only —
+# group/groupRect/text are canvas annotations created via dedicated UI actions, not create_node).
+# Sync is enforced automatically by test_node_types_in_sync_with_frontend (mcp/tests/test_node_types_sync.py).
+NODE_TYPES = [
+ "isp", "router", "firewall", "switch", "server", "proxmox", "vm", "lxc", "nas", "iot", "ap",
+ "camera", "printer", "computer", "laptop", "mobile", "cpl", "docker_host", "docker_container",
+ "generic", "zigbee_coordinator", "zigbee_router", "zigbee_enddevice",
+ "zwave_coordinator", "zwave_router", "zwave_enddevice", "grid", "ups", "battery", "generator",
+ "solar_panel", "inverter", "circuit_breaker", "contactor", "electrical_switch", "socket",
+ "light", "meter", "transformer", "load",
+]
# Shared field schemas mirroring backend NodeBase / NodeUpdate (backend/app/schemas/nodes.py).
# create_node and update_node both expose these so the MCP is symmetric with what the
diff --git a/mcp/tests/test_node_types_sync.py b/mcp/tests/test_node_types_sync.py
new file mode 100644
index 0000000..d4e1bc5
--- /dev/null
+++ b/mcp/tests/test_node_types_sync.py
@@ -0,0 +1,30 @@
+import re
+from pathlib import Path
+
+from app.tools import NODE_TYPES
+
+FRONTEND_TYPES_FILE = Path(__file__).resolve().parents[2] / "frontend" / "src" / "types" / "index.ts"
+
+# Canvas annotations, not devices — created via dedicated UI actions (grouping a
+# selection, "Add Zone", "Add Text"), never through create_node/approve_device.
+ANNOTATION_TYPES = {"group", "groupRect", "text"}
+
+
+def _frontend_node_types() -> set[str]:
+ src = FRONTEND_TYPES_FILE.read_text()
+ match = re.search(r"export type NodeType =\n((?:\s*\|\s*'[^']+'\n)+)", src)
+ assert match, (
+ "Could not locate the NodeType union in frontend/src/types/index.ts — "
+ "update this parser if the file's shape changed."
+ )
+ return set(re.findall(r"'([^']+)'", match.group(1)))
+
+
+def test_node_types_in_sync_with_frontend():
+ frontend_types = _frontend_node_types() - ANNOTATION_TYPES
+ mcp_types = set(NODE_TYPES)
+ assert mcp_types == frontend_types, (
+ "mcp/app/tools.py NODE_TYPES is out of sync with frontend/src/types/index.ts NodeType.\n"
+ f"Missing from NODE_TYPES: {sorted(frontend_types - mcp_types)}\n"
+ f"Extra in NODE_TYPES (no longer a device type?): {sorted(mcp_types - frontend_types)}"
+ )
diff --git a/mcp/tests/test_tools.py b/mcp/tests/test_tools.py
index 009ebdd..12f7ca8 100644
--- a/mcp/tests/test_tools.py
+++ b/mcp/tests/test_tools.py
@@ -65,6 +65,16 @@ async def test_update_node_properties(mock_backend):
})
[email protected]("node_type", ["firewall", "docker_container", "ups", "camera", "zigbee_router"])
+async def test_create_node_accepts_previously_rejected_types(mock_backend, node_type):
+ # These types were missing from the old, stale NODE_TYPES enum and were
+ # rejected by the MCP schema even though the backend always accepted them.
+ args = {"type": node_type, "label": "Device"}
+ await _dispatch("create_node", dict(args))
+ mock_backend.post.assert_called_once_with("/api/v1/nodes", args)
+
+
@pytest.mark.anyio
async def test_delete_node(mock_backend):
await _dispatch("delete_node", {"id": "42"})
@@ -205,6 +215,16 @@ def test_create_node_schema_exposes_full_node_fields():
assert "enum" in props["type"]
[email protected]("tool_name", ["create_node", "update_node", "approve_device"])
+def test_node_type_enum_excludes_canvas_annotations(tool_name):
+ # group/groupRect/text are canvas annotations created via dedicated UI actions
+ # (grouping a selection, "Add Zone", "Add Text") — not real devices, so they
+ # must never be creatable/settable through the device-oriented node tools.
+ type_enum = set(_tool_schema(tool_name)["type"]["enum"])
+ assert type_enum.isdisjoint({"group", "groupRect", "text"})
+ assert "firewall" in type_enum
+
+
def test_update_node_schema_exposes_full_node_fields():
props = _tool_schema("update_node")
for field in ("os", "notes", "services", "cpu_count", "ram_gb", "disk_gb", "properties", "mac"):