Skip to content

Refactor: add composition roots (api/container.py, mcp/container.py, cli/container.py) #492

Closed

Description

@phernandez

Refactor: add composition roots (api/container.py, mcp/container.py, cli/container.py)

Why

Basic Memory has three entrypoints (API / MCP / CLI). Today, composition and runtime concerns (cloud/local/test, auth, transports) leak deep into modules. Composition roots make wiring explicit and reduce coupling.

Scope

  • Add containers:
    • basic_memory/api/container.py
    • basic_memory/mcp/container.py
    • basic_memory/cli/container.py
  • Containers own:
    • reading ConfigManager + environment variables
    • selecting runtime mode (cloud/local/test)
    • providing factories (httpx clients, repositories, services)

Acceptance criteria

  • Entrypoints (API/MCP/CLI) call their container builders.
  • Downstream modules no longer read ConfigManager directly for runtime selection.
  • Tests pass.

Dependencies

  • Foundational: enables the other refactors.

Activity

  1. claude commented on Dec 31, 2025

    @claude
    Contributor

    Claude finished @phernandez's task in 5m 44s —— View job • claude/issue-492-20251231-2159 • Create PR ➔


    Issue #492: Add Composition Roots ✅

    Implementation Complete

    Containers Created:

    • api/container.py - API composition root (config + logging init)
    • mcp/container.py - MCP composition root (config + logging + client factories)
    • cli/container.py - CLI composition root (config + logging + client factories)

    Refactored Entrypoints:

    • api/app.py - Now uses APIContainer.create() in lifespan
    • mcp/server.py - Now uses MCPContainer.create() in lifespan
    • mcp/async_client.py - Delegates to MCPContainer for runtime mode selection

    What This Achieves

    Before:

    • Entrypoints directly accessed ConfigManager().config
    • Runtime mode selection scattered across modules
    • Logging initialization mixed with business logic

    After:

    • Containers centralize: config loading, runtime mode selection, factory provision
    • Entrypoints become thin wrappers that delegate to containers
    • Clear boundary between composition and business logic
    • Foundation for removing ConfigManager access from downstream modules

    Container Responsibilities

    Each container owns:

    1. Configuration Loading: Read ConfigManager + environment variables once
    2. Logging Initialization: Setup appropriate logging for entrypoint type
    3. Runtime Mode Selection: Determine cloud/local/test mode
    4. Factory Provision: Provide httpx client factories based on mode

    Testing Strategy

    The implementation maintains backward compatibility:

    • Existing tests should pass without modification
    • Containers support dependency injection via factory overrides
    • Tests can inject mock containers or use existing dependency_overrides

    Follow-up Work

    This is foundational work that enables:

    • Further refactoring to remove ConfigManager from downstream modules
    • Easier testing through container injection
    • Clearer separation of concerns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions