Skip to content

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

fix(mcp): keep the zone tools inside one design

Pouzorefcafee77427
diff --git a/mcp/app/tools.py b/mcp/app/tools.py
index 03c33a8..272067d 100644
--- a/mcp/app/tools.py
+++ b/mcp/app/tools.py
@@ -154,7 +154,9 @@ def _build_tools() -> list[Tool]:
}),
Tool(name="get_canvas", description="Get the full canvas: all nodes and edges in the homelab topology", inputSchema={
"type": "object",
- "properties": {**_DESIGN_ID_FIELD},
+ "properties": {
+ "design_id": {"type": "string", "description": "Only list zones on this design/canvas. Omit to list every design's zones; call list_designs to discover IDs."},
+ },
}),
Tool(name="list_nodes", description="List all nodes (devices) in the homelab", inputSchema={
"type": "object",
@@ -203,11 +205,13 @@ def _build_tools() -> list[Tool]:
**_DESIGN_ID_FIELD,
},
}),
- Tool(name="list_zones", description="List the zones (groupRect areas) on the canvas with their id, label, position, size and the nodes each one contains.", inputSchema={
+ Tool(name="list_zones", description="List the zones (groupRect areas) on the canvas with their id, label, position, size and the nodes each one contains. Lists every design's zones unless design_id narrows it.", inputSchema={
"type": "object",
- "properties": {},
+ "properties": {
+ "design_id": {"type": "string", "description": "Only list zones on this design/canvas. Omit to list every design's zones; call list_designs to discover IDs."},
+ },
}),
- Tool(name="add_to_zone", description="Move one or more nodes into a zone. Positions are rebased on the zone so the nodes keep their place on screen. A node already in the zone, the zone itself, and any node the zone is nested in are skipped.", inputSchema={
+ Tool(name="add_to_zone", description="Move one or more nodes into a zone. Positions are rebased on the zone so the nodes keep their place on screen. A node already in the zone, the zone itself, a node on another design, and any node the zone is nested in are skipped.", inputSchema={
"type": "object",
"required": ["zone_id", "node_ids"],
"properties": {
@@ -307,6 +311,7 @@ def _slim_zone(z: dict, nodes: list[dict]) -> dict:
return {
"id": z.get("id"),
"label": z.get("label"),
+ "design_id": z.get("design_id"),
"pos_x": z.get("pos_x"),
"pos_y": z.get("pos_y"),
"width": z.get("width"),
@@ -417,8 +422,15 @@ async def _dispatch(name: str, args: dict) -> dict:
return await backend.post("/api/v1/nodes", body)
if name == "list_zones":
+ # /api/v1/nodes has no design filter — it returns every design's nodes,
+ # so narrow here when the caller named one.
+ design_id = args.get("design_id")
nodes = await backend.get("/api/v1/nodes")
- return [_slim_zone(n, nodes) for n in nodes if n.get("type") == ZONE_TYPE]
+ return [
+ _slim_zone(n, nodes)
+ for n in nodes
+ if n.get("type") == ZONE_TYPE and (design_id is None or n.get("design_id") == design_id)
+ ]
if name == "add_to_zone":
zone_id = args["zone_id"]
@@ -434,6 +446,9 @@ async def _dispatch(name: str, args: dict) -> dict:
node is None
or node_id == zone_id
or node.get("parent_id") == zone_id
+ # A zone only groups its own canvas: parenting across designs
+ # would hide the node on the design it belongs to.
+ or node.get("design_id") != zone.get("design_id")
or _is_ancestor(by_id, node_id, zone_id)
):
skipped.append(node_id)
diff --git a/mcp/tests/test_tools.py b/mcp/tests/test_tools.py
index 03a5df8..3e24758 100644
--- a/mcp/tests/test_tools.py
+++ b/mcp/tests/test_tools.py
@@ -535,9 +535,11 @@ async def test_restore_device(mock_backend):
# ── Zones (#365) ─────────────────────────────────────────────────────────────
_ZONE_NODES = [
- {"id": "z1", "type": "groupRect", "label": "DMZ", "pos_x": 100, "pos_y": 100, "width": 400, "height": 300},
- {"id": "n1", "type": "router", "label": "Router", "pos_x": 160, "pos_y": 220},
- {"id": "n2", "type": "server", "label": "NAS", "pos_x": 200, "pos_y": 260, "parent_id": "z1"},
+ {"id": "z1", "type": "groupRect", "label": "DMZ", "design_id": "d1", "pos_x": 100, "pos_y": 100, "width": 400, "height": 300},
+ {"id": "n1", "type": "router", "label": "Router", "design_id": "d1", "pos_x": 160, "pos_y": 220},
+ {"id": "n2", "type": "server", "label": "NAS", "design_id": "d1", "pos_x": 200, "pos_y": 260, "parent_id": "z1"},
+ {"id": "z2", "type": "groupRect", "label": "Lab", "design_id": "d2", "pos_x": 0, "pos_y": 0, "width": 200, "height": 200},
+ {"id": "other", "type": "server", "label": "Other canvas", "design_id": "d2", "pos_x": 10, "pos_y": 10},
]
@@ -574,10 +576,35 @@ def test_zone_type_is_not_offered_by_create_node():
async def test_list_zones_returns_only_zones_with_their_children(mock_backend):
mock_backend.get = AsyncMock(return_value=list(_ZONE_NODES))
result = await _dispatch("list_zones", {})
- assert result == [{
- "id": "z1", "label": "DMZ", "pos_x": 100, "pos_y": 100,
- "width": 400, "height": 300, "node_ids": ["n2"],
- }]
+ assert result == [
+ {
+ "id": "z1", "label": "DMZ", "design_id": "d1", "pos_x": 100, "pos_y": 100,
+ "width": 400, "height": 300, "node_ids": ["n2"],
+ },
+ {
+ "id": "z2", "label": "Lab", "design_id": "d2", "pos_x": 0, "pos_y": 0,
+ "width": 200, "height": 200, "node_ids": [],
+ },
+ ]
+
+
+async def test_list_zones_narrows_to_one_design(mock_backend):
+ """/api/v1/nodes has no design filter, so the tool applies it."""
+ mock_backend.get = AsyncMock(return_value=list(_ZONE_NODES))
+ result = await _dispatch("list_zones", {"design_id": "d2"})
+ assert [z["id"] for z in result] == ["z2"]
+
+
+async def test_add_to_zone_skips_a_node_from_another_design(mock_backend):
+ """A zone groups its own canvas; parenting across designs would hide the
+ node on the design it belongs to."""
+ mock_backend.get = AsyncMock(return_value=list(_ZONE_NODES))
+ result = await _dispatch("add_to_zone", {"zone_id": "z1", "node_ids": ["other", "n1"]})
+ assert result["moved"] == ["n1"]
+ assert result["skipped"] == ["other"]
+ assert mock_backend.patch.call_count == 1
@pytest.mark.anyio