|
| 1 | +"""Default POSIX discovery and navigation through the MCP client transport.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import pytest |
| 6 | +from fastmcp import Client |
| 7 | + |
| 8 | +from basic_memory.mcp.server import set_posix_tools_visibility |
| 9 | + |
| 10 | + |
| 11 | +POSIX_TOOLS = {"cat", "grep", "ls", "find", "tail", "man"} |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.asyncio |
| 15 | +@pytest.mark.parametrize("setting", [None, False, True]) |
| 16 | +@pytest.mark.parametrize("mode", ["auto", "legacy"]) |
| 17 | +async def test_posix_registration_over_mcp(mcp_server, config_manager, setting, mode) -> None: |
| 18 | + # Omit the key entirely to exercise existing configs that inherit new defaults. |
| 19 | + saved = json.loads(config_manager.config_file.read_text()) |
| 20 | + saved.pop("enable_posix_tools", None) |
| 21 | + if setting is not None: |
| 22 | + saved["enable_posix_tools"] = setting |
| 23 | + config_manager.config_file.write_text(json.dumps(saved)) |
| 24 | + |
| 25 | + try: |
| 26 | + async with Client(mcp_server, mode=mode) as client: |
| 27 | + tools = {tool.name: tool for tool in await client.list_tools()} |
| 28 | + assert {"read_note", "search_notes", "write_note", "build_context"} <= tools.keys() |
| 29 | + instructions = client.instructions |
| 30 | + assert instructions is not None |
| 31 | + assert "When available in your tool list" in instructions |
| 32 | + assert "If they are absent, use the existing rich tools instead" in instructions |
| 33 | + if setting is False: |
| 34 | + assert POSIX_TOOLS.isdisjoint(tools) |
| 35 | + hidden = await client.call_tool("man", {}, raise_on_error=False) |
| 36 | + assert hidden.is_error |
| 37 | + else: |
| 38 | + assert POSIX_TOOLS <= tools.keys() |
| 39 | + for name in POSIX_TOOLS: |
| 40 | + annotations = tools[name].annotations |
| 41 | + assert annotations is not None |
| 42 | + assert annotations.read_only_hint is True |
| 43 | + assert annotations.destructive_hint is False |
| 44 | + assert "Projects are mount points" in instructions |
| 45 | + assert 'cat(identifier="research/notes/topic.md")' in instructions |
| 46 | + finally: |
| 47 | + set_posix_tools_visibility(mcp_server, False) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.asyncio |
| 51 | +async def test_posix_default_navigation_workflow(mcp_server, app, test_project) -> None: |
| 52 | + """Follow advertised mount and result paths, then scan a bounded note window.""" |
| 53 | + try: |
| 54 | + async with Client(mcp_server) as client: |
| 55 | + await client.call_tool( |
| 56 | + "write_note", |
| 57 | + { |
| 58 | + "title": "POSIX Runbook", |
| 59 | + "directory": "notes", |
| 60 | + "content": "# Recovery\n\nbefore\nretry the task\nafter\nremaining", |
| 61 | + "metadata": {"status": "active", "priority": 3}, |
| 62 | + "project": test_project.name, |
| 63 | + }, |
| 64 | + ) |
| 65 | + mounts = await client.call_tool("ls", {"path": "/"}) |
| 66 | + mount = next(row for row in mounts.data["nodes"] if row["name"] == test_project.name) |
| 67 | + note_dir = f"{mount['directory_path']}/notes" |
| 68 | + listing = await client.call_tool("ls", {"path": note_dir}) |
| 69 | + note_path = listing.data["nodes"][0]["file_path"] |
| 70 | + full = await client.call_tool("cat", {"identifier": note_path}) |
| 71 | + assert "retry the task" in full.data["content"] |
| 72 | + |
| 73 | + found = await client.call_tool("find", {"path": note_dir, "name": "*.md"}) |
| 74 | + assert found.data["nodes"][0]["file_path"] == note_path |
| 75 | + projected = await client.call_tool( |
| 76 | + "find", |
| 77 | + {"path": note_dir, "meta": ["status=active"], "fields": ["priority"]}, |
| 78 | + ) |
| 79 | + row = projected.data["results"][0] |
| 80 | + assert row["file_path"] == note_path |
| 81 | + assert row["fields"] == {"priority": 3} |
| 82 | + assert "content" not in row |
| 83 | + |
| 84 | + matches = await client.call_tool( |
| 85 | + "grep", |
| 86 | + { |
| 87 | + "pattern": "retry", |
| 88 | + "literal": True, |
| 89 | + "context_lines": 1, |
| 90 | + "project": test_project.name, |
| 91 | + }, |
| 92 | + ) |
| 93 | + window = matches.data["results"][0]["windows"][0] |
| 94 | + sliced = await client.call_tool( |
| 95 | + "cat", |
| 96 | + { |
| 97 | + "identifier": note_path, |
| 98 | + "start_line": window["start_line"], |
| 99 | + "end_line": window["end_line"], |
| 100 | + }, |
| 101 | + ) |
| 102 | + assert sliced.data["content"] == window["content"] == "before\nretry the task\nafter" |
| 103 | + assert len(sliced.data["content"]) < len(full.data["content"]) |
| 104 | + |
| 105 | + recent = await client.call_tool("tail", {"project": test_project.name, "lines": 1}) |
| 106 | + assert recent.data[0]["title"] == "POSIX Runbook" |
| 107 | + manual = await client.call_tool("man", {"page": "cat(1)"}) |
| 108 | + assert "--lines" in manual.data |
| 109 | + finally: |
| 110 | + set_posix_tools_visibility(mcp_server, False) |
0 commit comments