[MLsys2026 Best Paper]: https://arxiv.org/abs/2506.08276. RAG on Everything with LEANN. Enjoy 97% storage savings while running a fast, accurate, and 100% private RAG application on your personal device.
Python aifaissgpt-osslangchainllama-indexllmlocalstorageoffline-firstollamaprivacypythonragretrieval-augmented-generationvector-databasevector-searchvectors
fix(mcp): honor --base-dir across tools (#421)
dafyy321-pixel6f8e6415ad82
diff --git a/packages/leann-core/src/leann/mcp.py b/packages/leann-core/src/leann/mcp.pyindex 4818e54..4051b0f 100755--- a/packages/leann-core/src/leann/mcp.py+++ b/packages/leann-core/src/leann/mcp.py@@ -4,10 +4,15 @@ import argparseimport jsonimport subprocessimport sys+from pathlib import Path_base_dir: str | None = None+def _working_dir() -> Path:+ return Path(_base_dir) if _base_dir else Path.cwd()++def _leann_cmd() -> list[str]:"""Build the base command for invoking ``leann`` CLI.@@ -30,6 +35,7 @@ def _run_leann(*args, timeout=120):encoding="utf-8",errors="replace",timeout=timeout,+ cwd=_working_dir(),)return result.returncode, result.stdout, result.stderr@@ -230,9 +236,8 @@ def handle_build(request_id, args):# updates use the same model (avoids mismatch with CLI default).if index_name:import json as _json- from pathlib import Path- meta_path = Path.cwd() / ".leann" / "indexes" / index_name / "documents.leann.meta.json"+ meta_path = _working_dir() / ".leann" / "indexes" / index_name / "documents.leann.meta.json"if meta_path.exists():try:with open(meta_path, encoding="utf-8") as f:@@ -259,10 +264,8 @@ def handle_status(request_id, args):if not index_name:return _make_result(request_id, "Error: index_name is required.")- from pathlib import Path-# Check standard location- leann_dir = Path.cwd() / ".leann" / "indexes" / index_name+ leann_dir = _working_dir() / ".leann" / "indexes" / index_namemeta_path = leann_dir / "documents.leann.meta.json"passages_path = leann_dir / "documents.leann.passages.jsonl"diff --git a/tests/test_mcp_base_dir.py b/tests/test_mcp_base_dir.pynew file mode 100644index 0000000..701a900--- /dev/null+++ b/tests/test_mcp_base_dir.py@@ -0,0 +1,51 @@+"""MCP tools should use the directory selected at server startup."""++import json+from importlib.machinery import SourceFileLoader+from pathlib import Path+from types import ModuleType, SimpleNamespace++import pytest++module_path = Path(__file__).resolve().parents[1] / "packages/leann-core/src/leann/mcp.py"+mcp = ModuleType("leann_mcp_base_dir_test")+SourceFileLoader(mcp.__name__, str(module_path)).exec_module(mcp)++[email protected]("relative_base_dir", [False, True])+def test_base_dir_applies_to_cli_and_local_index_reads(tmp_path, monkeypatch, relative_base_dir):+ project_dir = tmp_path / "project"+ launch_dir = tmp_path / "launcher"+ index_dir = project_dir / ".leann" / "indexes" / "docs"+ index_dir.mkdir(parents=True)+ launch_dir.mkdir()+ (index_dir / "documents.leann.meta.json").write_text(+ json.dumps(+ {"backend_name": "ivf", "embedding_model": "existing-model", "embedding_mode": "local"}+ ),+ encoding="utf-8",+ )+ monkeypatch.chdir(launch_dir)+ base_dir = Path("..") / "project" if relative_base_dir else project_dir+ monkeypatch.setattr(mcp, "_base_dir", str(base_dir))++ calls = []++ def fake_run(command, **kwargs):+ calls.append((command, kwargs))+ output = "[]" if command[3] == "search" else "ok"+ return SimpleNamespace(returncode=0, stdout=output, stderr="")++ monkeypatch.setattr(mcp.subprocess, "run", fake_run)++ status = mcp.handle_status(1, {"index_name": "docs"})+ expected_index_dir = base_dir / ".leann" / "indexes" / "docs"+ assert f"Location: {expected_index_dir}" in status["result"]["content"][0]["text"]++ mcp.handle_build(2, {"index_name": "docs", "docs": ["source.txt"]})+ mcp.handle_list(3)+ mcp.handle_search(4, {"index_name": "docs", "query": "example"})++ assert all(kwargs["cwd"] == base_dir for _, kwargs in calls)+ assert "--embedding-model=existing-model" in calls[0][0]+ assert "--embedding-mode=local" in calls[0][0]