Skip to content

fix(memory): defer store loading to prevent MCP timeout#39

Merged
Joao208 merged 1 commit into
mainfrom
joao-barros-/-fix-memory-mcp-embedding-cache
Apr 14, 2026
Merged

fix(memory): defer store loading to prevent MCP timeout#39
Joao208 merged 1 commit into
mainfrom
joao-barros-/-fix-memory-mcp-embedding-cache

Conversation

@Joao208

@Joao208 Joao208 commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Problema

O memory-mcp server dava timeout (-32001) durante a inicialização porque o store.load() (que baixa/carrega o modelo de embedding de ~113MB) rodava antes do server.connect(). O MCP protocol não conseguia responder tools/list dentro do timeout de 60s.

O modelo era cacheado dentro do node_modules do diretório npx, que é efêmero — qualquer limpeza de cache ou bump de versão forçava um novo download.

Solução

1. Deferred loading

O transport MCP agora conecta antes do store carregar. O store.load() roda em background e as tools aguardam a conclusão via loadingPromise quando chamadas.

2. Cache dir configurável

Suporte à env var TRANSFORMERS_CACHE_DIR no EmbeddingEngine, permitindo cachear o modelo ONNX num diretório fixo que sobrevive a limpezas de npx cache.

3. Async store API

store.get() e store.list() agora são async e aguardam o loading completar ao invés de dar throw.

Mudanças

  • embeddings.ts: seta env.cacheDir via TRANSFORMERS_CACHE_DIR
  • server.ts: conecta transport antes, carrega store em background
  • store.ts: ensureLoaded() aguarda loadingPromise; get()/list() agora async
  • tools.ts: await nas chamadas de store.get() e store.list()
  • store.test.ts: ajustado pra async
  • package.json: bump 1.1.1 → 1.2.0

Summary by CodeRabbit

  • New Features

    • Added support for the TRANSFORMERS_CACHE_DIR environment variable to customize embeddings cache location.
  • Improvements

    • Server startup is now faster, as it connects immediately without waiting for data loading to complete.

…model download

- Connect MCP transport before loading store, so tools/list responds immediately
- Store loading (embedding model init + memory indexing) runs in background
- Tools await store readiness via loadingPromise instead of throwing
- Support TRANSFORMERS_CACHE_DIR env var for persistent model cache across npx runs
- Bump version to 1.2.0
@coderabbitai

coderabbitai Bot commented Apr 14, 2026

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

The pull request converts MemoryStore from synchronous to asynchronous operations, enabling non-blocking data loading during server startup. It refactors core store methods (list, get) to async, introduces proper loading promise tracking, and updates all dependent code and tests to handle the new async API. Environment-based cache directory configuration is also added.

Changes

Cohort / File(s) Summary
Store Async Migration
packages/memory/src/store.ts, packages/memory/src/store.test.ts
Core store refactoring: list() and get() converted to async with Promise<...> return types; load() now returns a tracked promise via private doLoad(); ensureLoaded() becomes async; all call sites updated to await async methods.
Integration Updates
packages/memory/src/server.ts, packages/memory/src/tools.ts
Server startup and tool methods adapted for async store: server now connects transport before store load completes (background error handling); tool methods (getMemory, listMemories) add await to store calls.
Configuration
packages/memory/src/embeddings.ts
Environment variable handling: TRANSFORMERS_CACHE_DIR env var now sets pipeline cache directory if present.
Package Management
packages/memory/package.json
Version bump from 1.1.1 to 1.2.0.

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 hops with delight
The store now dances without chains,
Async paths through data lanes,
While servers bloom and caches sing,
Non-blocking joy's the gift we bring! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: deferring store loading to prevent MCP timeout, which is the core issue addressed across all modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch joao-barros-/-fix-memory-mcp-embedding-cache

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Joao208 Joao208 merged commit 07ab848 into main Apr 14, 2026
3 of 4 checks passed
@Joao208 Joao208 deleted the joao-barros-/-fix-memory-mcp-embedding-cache branch April 14, 2026 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant