perf: cache ChromaDB PersistentClient instead of re-instantiating per call

The MCP server previously created a new PersistentClient on every tool
call via _get_collection(). This incurs HNSW index loading overhead
on each request.

Cache the client and collection at module level. The cache resets
naturally on process restart (MCP runs as a subprocess).

Also adds a _reset_mcp_cache fixture to conftest.py for test isolation.

Includes test infrastructure from PR #131.
92 tests pass.
This commit is contained in:
Igor Lins e Silva
2026-04-07 17:19:53 -03:00
parent 71736a3f4f
commit a67b00d7c7
2 changed files with 25 additions and 4 deletions
+12 -4
View File
@@ -39,13 +39,21 @@ logger = logging.getLogger("mempalace_mcp")
_config = MempalaceConfig()
_client_cache = None
_collection_cache = None
def _get_collection(create=False):
"""Return the ChromaDB collection, or None on failure."""
"""Return the ChromaDB collection, caching the client between calls."""
global _client_cache, _collection_cache
try:
client = chromadb.PersistentClient(path=_config.palace_path)
if _client_cache is None:
_client_cache = chromadb.PersistentClient(path=_config.palace_path)
if create:
return client.get_or_create_collection(_config.collection_name)
return client.get_collection(_config.collection_name)
_collection_cache = _client_cache.get_or_create_collection(_config.collection_name)
elif _collection_cache is None:
_collection_cache = _client_cache.get_collection(_config.collection_name)
return _collection_cache
except Exception:
return None