Skip to content

[BUG] Broken file sync when using mcp #481

@krksgbr

Description

@krksgbr

Bug Description

File sync is broken when using the MCP server.

alembic/env.py unconditionally sets BASIC_MEMORY_ENV="test" at module import time, which causes is_test_env to always return True, so the MCP server skips starting the watch service.

Steps To Reproduce

  1. Install basic-memory 0.17.0 via uv tool install basic-memory
  2. Start the mcp server: bm mcp
  3. Check logs: grep "Test environment" ~/.basic-memory/basic-memory.log
  4. Observe: "Test environment detected - skipping local file sync"

Expected Behavior

The MCP server should start the watch service and sync files created/modified externally.

Actual Behavior

MCP server startup logs:

Test environment detected - skipping local file sync
  • Files modified outside of MCP tools are never indexed.
  • recent_activity returns empty results even when notes exist on disk.

Environment

  • OS: macOS 15.1 (Darwin 25.1.0)
  • Python version: 3.12.10
  • Basic Memory version: 0.17.0
  • Installation method: uv tool install

Additional Context

I think the issue is in basic_memory/alembic/env.py:

os.environ["BASIC_MEMORY_ENV"] = "test"

This runs whenever alembic migrations are imported (which happens during MCP server startup via run_migrations). The is_test_env property in config.py then checks:

os.getenv("BASIC_MEMORY_ENV", "").lower() == "test"  # Always True!

To debug, add this to basic_memory/config.py in the is_test_env property:

@property
def is_test_env(self) -> bool:
    logger.info(f"DEBUG is_test_env: self.env={self.env!r}, BASIC_MEMORY_ENV={os.getenv('BASIC_MEMORY_ENV')!r}, PYTEST_CURRENT_TEST={os.getenv('PYTEST_CURRENT_TEST')!r}")
    result = (
        self.env == "test"
        or os.getenv("BASIC_MEMORY_ENV", "").lower() == "test"
        or os.getenv("PYTEST_CURRENT_TEST") is not None
    )
    logger.info(f"DEBUG is_test_env result: {result}")
    return result

Then restart the MCP server and check ~/.basic-memory/basic-memory.log.

DEBUG is_test_env: self.env='user', BASIC_MEMORY_ENV='test', PYTEST_CURRENT_TEST=None
DEBUG is_test_env result: True

Note: self.env='user' (correctly set via pydantic-settings), but os.getenv('BASIC_MEMORY_ENV') returns 'test' because alembic/env.py overwrote it.

Possible Solution

Only set the env var when actually running under pytest:

if os.getenv("PYTEST_CURRENT_TEST") is not None:
    os.environ["BASIC_MEMORY_ENV"] = "test"

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions