From eab11969da5320808f7beafbb41d88673879867e Mon Sep 17 00:00:00 2001 From: Grace Gettert <9805362+ggettert@users.noreply.github.com> Date: Mon, 8 Jun 2026 19:43:25 +0000 Subject: [PATCH 1/2] feat(mcp): expose list_hallways and delete_hallway tools Closes #1739. Hallways shipped in 3.3.6 (#1558) with Python API entry points `list_hallways(wing=None)` and `delete_hallway(...)` in `mempalace/hallways.py`, but neither was registered as an MCP tool in `mempalace/mcp_server.py`. Mining produces hallways visible in the mine log ("Hallways: +N within-wing entity link(s)") but they were not retrievable through MCP. This change wires both functions into the MCP tool registry mirroring the existing tunnel-tool pattern: - `mempalace_list_hallways(wing: str | None = None)` wraps `hallways.list_hallways`. Optional `wing` filter goes through `_sanitize_optional_name` just like `tool_list_tunnels`, so invalid names surface a structured error instead of crashing. - `mempalace_delete_hallway(hallway_id: str)` wraps `hallways.delete_hallway` and returns `{"deleted": bool}` so callers can distinguish a successful delete from a no-op. Tests in `tests/test_mcp_server.py` cover: - list returns all records without filter - list filters correctly by wing - list rejects invalid wing names with a structured error - delete removes the targeted record and returns `{"deleted": True}` - delete with an unknown id returns `{"deleted": False}` - delete with missing/non-string id returns a structured error - both tools are present in the public `TOOLS` registry Pure pass-throughs of the existing Python API. No behavior change in the underlying hallway storage, no schema change to the `hallways.json` file format. --- mempalace/mcp_server.py | 44 +++++++++++++++++ tests/test_mcp_server.py | 102 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) diff --git a/mempalace/mcp_server.py b/mempalace/mcp_server.py index 734ac28..6c81d15 100644 --- a/mempalace/mcp_server.py +++ b/mempalace/mcp_server.py @@ -88,6 +88,10 @@ from .palace_graph import ( # noqa: E402 delete_tunnel, follow_tunnels, ) +from .hallways import ( # noqa: E402 + list_hallways, + delete_hallway, +) from .knowledge_graph import KnowledgeGraph, DEFAULT_KG_PATH # noqa: E402 from .collision_scan import assert_no_collisions # noqa: E402 @@ -1354,6 +1358,22 @@ def tool_delete_tunnel(tunnel_id: str): return delete_tunnel(tunnel_id) +def tool_list_hallways(wing: str = None): + """List within-wing hallway records, optionally filtered by wing.""" + try: + wing = _sanitize_optional_name(wing, "wing") + except ValueError as e: + return {"error": str(e)} + return list_hallways(wing) + + +def tool_delete_hallway(hallway_id: str): + """Delete a hallway record by its ID.""" + if not hallway_id or not isinstance(hallway_id, str): + return {"error": "hallway_id is required"} + return {"deleted": delete_hallway(hallway_id)} + + def tool_follow_tunnels(wing: str, room: str): """Follow explicit tunnels from a room to see connected drawers in other wings.""" try: @@ -2477,6 +2497,30 @@ TOOLS = { }, "handler": tool_delete_tunnel, }, + "mempalace_list_hallways": { + "description": "List within-wing hallway records (entity-to-entity co-occurrence links built at mine time). Optionally filter by wing.", + "input_schema": { + "type": "object", + "properties": { + "wing": { + "type": "string", + "description": "Filter hallways by wing", + }, + }, + }, + "handler": tool_list_hallways, + }, + "mempalace_delete_hallway": { + "description": "Delete a hallway record by its ID. Returns {deleted: bool}.", + "input_schema": { + "type": "object", + "properties": { + "hallway_id": {"type": "string", "description": "Hallway ID to delete"}, + }, + "required": ["hallway_id"], + }, + "handler": tool_delete_hallway, + }, "mempalace_follow_tunnels": { "description": "Follow tunnels from a room to see what it connects to in other wings. Returns connected rooms with drawer previews.", "input_schema": { diff --git a/tests/test_mcp_server.py b/tests/test_mcp_server.py index a5e406b..a24f496 100644 --- a/tests/test_mcp_server.py +++ b/tests/test_mcp_server.py @@ -1512,6 +1512,108 @@ class TestWriteTools: assert result == {"error": msg} + # ── hallway MCP tools (mirror the tunnel pattern) ── + + def _seed_hallways(self, monkeypatch, tmp_path): + """Point hallways._HALLWAY_FILE at a tmp file and seed two records.""" + from mempalace import hallways + + hallway_file = tmp_path / "hallways.json" + monkeypatch.setattr(hallways, "_HALLWAY_FILE", str(hallway_file)) + seeded = [ + { + "id": "hallway_wing_a_X_Y_aaaa", + "wing": "wing_a", + "entity_a": "X", + "entity_b": "Y", + "co_occurrence_count": 3, + "rooms": ["room1"], + }, + { + "id": "hallway_wing_b_X_Z_bbbb", + "wing": "wing_b", + "entity_a": "X", + "entity_b": "Z", + "co_occurrence_count": 1, + "rooms": ["room2"], + }, + ] + hallways._save_hallways(seeded) + return seeded + + def test_tool_list_hallways_returns_all_without_filter(self, monkeypatch, tmp_path): + """tool_list_hallways with no wing returns every record.""" + from mempalace import mcp_server + + seeded = self._seed_hallways(monkeypatch, tmp_path) + result = mcp_server.tool_list_hallways() + assert isinstance(result, list) + assert len(result) == len(seeded) + ids = {h["id"] for h in result} + assert ids == {h["id"] for h in seeded} + + def test_tool_list_hallways_filters_by_wing(self, monkeypatch, tmp_path): + """tool_list_hallways with wing returns only that wing's records.""" + from mempalace import mcp_server + + self._seed_hallways(monkeypatch, tmp_path) + result = mcp_server.tool_list_hallways(wing="wing_a") + assert len(result) == 1 + assert result[0]["wing"] == "wing_a" + + def test_tool_list_hallways_rejects_invalid_wing_name(self, monkeypatch, tmp_path): + """Invalid wing names go through _sanitize_optional_name and return a + structured error rather than crashing — mirrors tool_list_tunnels.""" + from mempalace import mcp_server + + self._seed_hallways(monkeypatch, tmp_path) + # Forward-slash is not a valid name character per sanitize_name. + result = mcp_server.tool_list_hallways(wing="wing/with/slashes") + assert isinstance(result, dict) + assert "error" in result + + def test_tool_delete_hallway_removes_existing_record(self, monkeypatch, tmp_path): + """tool_delete_hallway removes the record and returns {deleted: True}.""" + from mempalace import mcp_server + + seeded = self._seed_hallways(monkeypatch, tmp_path) + target_id = seeded[0]["id"] + result = mcp_server.tool_delete_hallway(hallway_id=target_id) + assert result == {"deleted": True} + remaining = mcp_server.tool_list_hallways() + assert target_id not in {h["id"] for h in remaining} + + def test_tool_delete_hallway_unknown_id_returns_false(self, monkeypatch, tmp_path): + """Deleting an ID that doesn't exist returns {deleted: False} without error.""" + from mempalace import mcp_server + + self._seed_hallways(monkeypatch, tmp_path) + result = mcp_server.tool_delete_hallway(hallway_id="hallway_does_not_exist") + assert result == {"deleted": False} + + def test_tool_delete_hallway_requires_string_id(self): + """Missing or non-string hallway_id surfaces a structured error.""" + from mempalace import mcp_server + + assert mcp_server.tool_delete_hallway(hallway_id="") == {"error": "hallway_id is required"} + assert mcp_server.tool_delete_hallway(hallway_id=None) == { + "error": "hallway_id is required" + } + + def test_hallway_tools_registered_in_tools_registry(self): + """Both new tools must appear in the public TOOLS registry so MCP clients can dispatch them.""" + from mempalace import mcp_server + + assert "mempalace_list_hallways" in mcp_server.TOOLS + assert "mempalace_delete_hallway" in mcp_server.TOOLS + assert ( + mcp_server.TOOLS["mempalace_list_hallways"]["handler"] is mcp_server.tool_list_hallways + ) + assert ( + mcp_server.TOOLS["mempalace_delete_hallway"]["handler"] + is mcp_server.tool_delete_hallway + ) + def test_add_drawer_normal_content_single_drawer(self, monkeypatch, config, palace_path, kg): """Regression catch: content below CHUNK_SIZE produces exactly one drawer with ``chunks == 1``. Pre-#1539 contract preserved.""" From ef2261feb8aaab44fcc4795647051a167bcfac59 Mon Sep 17 00:00:00 2001 From: Grace Gettert <9805362+ggettert@users.noreply.github.com> Date: Mon, 8 Jun 2026 20:07:23 +0000 Subject: [PATCH 2/2] docs(mcp): document list_hallways and delete_hallway tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI's test_no_undocumented_tools enforces that every tool registered in the TOOLS dict has a corresponding section in mcp-tools.md. The two hallway tools from b866f41 were missing — adding them here. Sections mirror the format of the existing list_tunnels and delete_tunnel entries directly above. --- website/reference/mcp-tools.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/website/reference/mcp-tools.md b/website/reference/mcp-tools.md index 220b15e..85ff128 100644 --- a/website/reference/mcp-tools.md +++ b/website/reference/mcp-tools.md @@ -319,6 +319,30 @@ Delete an explicit tunnel by its ID. --- +### `mempalace_list_hallways` + +List within-wing hallway records (entity-to-entity co-occurrence links built at mine time). Optionally filter by wing. + +| Parameter | Type | Required | Description | +|-----------|------|----------|-------------| +| `wing` | string | No | Filter hallways by wing | + +**Returns:** `[ { id, wing, entity_a, entity_b, co_occurrence_count, rooms, ... }, ... ]` + +--- + +### `mempalace_delete_hallway` + +Delete a hallway record by its ID. + +| Parameter | Type | Required | Description | +|-----------|------|----------|-------------| +| `hallway_id` | string | **Yes** | Hallway ID to delete | + +**Returns:** `{ deleted: bool }` + +--- + ### `mempalace_follow_tunnels` Follow tunnels from a room to see what it connects to in other wings. Returns connected rooms with drawer previews.