# flowelement-ai/m_flow architecture

> Architecture diagram of the GitHub repository flowelement-ai/m_flow, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/flowelement-ai/m_flow
- Repository: https://github.com/flowelement-ai/m_flow

## Overview

1. **Repository shape**
   - This is a **Python-first memory/RAG platform** with multiple entrypoints: the `m_flow` library/package, a **FastAPI backend**, a **CLI** (`mflow`), a **Next.js frontend** (`m_flow-frontend`), an **MCP server** (`m_flow-mcp`), and optional **distributed workers** (`mflow_workers`).
   - The backend is the system of record; the frontend and MCP server are thin clients over it.

2. **Core domain model**
   - The central knowledge structure is a **cone graph**: **Episode → Facet → FacetPoint → Entity**.
   - Episodes are the primary retrieval bundle; Facets and FacetPoints hold progressively finer evidence; Entities bridge memory across episodes.
   - There is also a separate **procedural memory** track for reusable workflows, preferences, and rules.

3. **Ingestion path**
   - Input can come from files, text, documents, web URLs, audio/image assets, or examples/scripts.
   - Ingestion flows through shared loaders and chunkers, document classification, optional **coreference resolution**, then memory-building pipelines that create graph nodes/edges and embeddings.
   - The pipeline is designed around **dataset-scoped memory**, so content is routed into the correct tenant/dataset before persistence.

4. **Memory construction**
   - Episodic memory is built in `m_flow/memory/episodic`, which handles sentence splitting, routing, normalization, entity matching, facet refinement, and writing graph objects.
   - Procedural memory is built separately in `m_flow/memory/procedural`, with governance, safety, versioning, validation, and repair steps.
   - These are not just different views of the same data; they are separate processing pipelines with different extraction logic and storage semantics.

5. **Retrieval path**
   - Queries enter through the `search`/`retrieval` layers and are routed to one of several modes: **episodic**, **procedural**, **triplet completion**, **lexical**, or **Cypher**.
   - The key architecture is **graph-routed bundle search**: vector or lexical search finds candidate anchors, then graph propagation scores the strongest evidence path through the cone graph.
   - Final answers are assembled from the returned memory bundle, typically by an LLM layer using retrieved context.

6. **Persistence and infrastructure abstraction**
   - Storage is intentionally pluggable. There are adapters for:
     - **Graph DBs**: Kùzu, Neo4j, Neptune
     - **Vector DBs**: LanceDB, ChromaDB, pgvector, Milvus, Pinecone
     - **Cache**: filesystem or Redis
     - **Relational persistence**: SQLAlchemy-backed DBs for auth, pipeline state, and metadata
   - This means the architecture is built around **backend interfaces** rather than one fixed database.

7. **Backend API surface**
   - The FastAPI backend is organized by domain routers: `add`, `memorize`, `ingest`, `search`, `graph`, `datasets`, `users/auth`, `settings`, `pipeline`, `maintenance`, `prune`, `playground`, `procedural`, `responses`, and `sync`.
   - This makes the backend a **hub** that exposes operational memory workflows, not just query endpoints.
   - The API also supports health, config, UI bootstrapping, and multi-step pipeline orchestration.

8. **Auth, tenants, and dataset boundaries**
   - `m_flow/auth` implements users, roles, tenants, permissions, and dataset-level ACLs.
   - Access control is enforced around dataset operations and search/ingest flows, so memory is partitioned by **principal + tenant + dataset** rather than being globally shared.
   - This is a major architectural boundary because it determines what data can be ingested, retrieved, or deleted by a caller.

9. **Async execution and distributed work**
   - Long-running work is handled by the pipeline system in `m_flow/pipeline`, which tracks runs, tasks, concurrency, progress, and remote dispatch.
   - `mflow_workers` provides separate worker processes for queued graph persistence / memory-node saving, with Modal support and queue-based execution.
   - This separates **request/response API handling** from **heavy ingestion and persistence workloads**.

10. **Frontend**
   - `m_flow-frontend` is a **Next.js** app that acts as the operator console: dashboard, memorize/ingest pages, retrieval views, graph visualization, settings, user management, setup wizard, and playground.
   - It uses a client API layer, React Query/provider wrappers, and websocket-driven UI pieces for live progress and task feedback.
   - The frontend does not own business logic; it visualizes and drives backend workflows.

11. **MCP server and external integrations**
   - `m_flow-mcp` exposes memory operations through the **Model Context Protocol**, so IDEs/agents can call the system as tools.
   - Core dependencies like LLM providers, embeddings, and storage backends are abstracted behind adapter layers, allowing OpenAI/Anthropic/Mistral/Ollama-style integrations without changing the core memory pipeline.
   - Face-recognition-based routing is an optional external integration used by the Playground to map conversation streams to the right dataset.

12. **Cross-cutting system services**
   - Configuration is env-driven and supported by templates, presets, migrations (`alembic`), and Docker/Helm packaging.
   - Shared utilities cover logging, tracing, rate limiting, file storage, loaders, chunking, and observability, all of which support the main ingestion/retrieval loops.
   - The repo also includes extensive tests/examples, which reflect the architecture’s emphasis on multi-backend support, retrieval correctness, and deployment portability.

Main entry points: [`m_flow/api`](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/api) (Backend API), [`m_flow/memory/episodic`](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/memory/episodic) (Episodic Memory), [`m_flow/memory/procedural`](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/memory/procedural) (Procedural Memory), [`m_flow/retrieval`](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/retrieval) (Retrieval)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_app["App Surfaces"]
  node_m_flow["m_flow<br/>python package"]
  node_frontend["Frontend<br/>next.js app"]
  node_mcp["MCP Server<br/>protocol server"]
  node_workers["Workers<br/>distributed jobs"]
end

subgraph group_g_api["Backend API"]
  node_api["Backend API<br/>fastapi app"]
  node_auth["Auth<br/>tenant ACL"]
  node_pipeline["Pipeline<br/>async orchestration"]
  node_search["Search API<br/>query facade"]
end

subgraph group_g_core["Core Memory"]
  node_memory_episodic["Episodic Memory<br/>ingest pipeline"]
  node_memory_procedural["Procedural Memory<br/>extract pipeline"]
  node_domain_models["Domain Model<br/>cone graph"]
  node_ingestion["Ingestion<br/>load and chunk"]
  node_preprocessing["Coreference<br/>preprocess"]
end

subgraph group_g_retrieval["Retrieval"]
  node_retrieval["Retrieval<br/>query engine"]
end

subgraph group_g_infra["Infrastructure"]
  node_storage["Storage<br/>persistence"]
  node_adapters["Adapters<br/>backend interfaces"]
  node_llm["LLM Layer<br/>model gateway"]
  node_shared["Shared Infra<br/>platform utils"]
end

node_api -->|"enforces"| node_auth
node_api -->|"orchestrates"| node_pipeline
node_api -->|"ingests via"| node_ingestion
node_api -->|"serves"| node_search
node_api -->|"builds"| node_memory_episodic
node_api -->|"extracts"| node_memory_procedural
node_search -->|"uses"| node_retrieval
node_retrieval -->|"queries"| node_memory_episodic
node_retrieval -->|"queries"| node_memory_procedural
node_memory_episodic -->|"writes"| node_domain_models
node_memory_procedural -->|"writes"| node_domain_models
node_ingestion -->|"optionally uses"| node_preprocessing
node_ingestion -->|"loads with"| node_shared
node_memory_episodic -->|"persists through"| node_storage
node_memory_procedural -->|"persists through"| node_storage
node_storage -->|"abstracts over"| node_adapters
node_retrieval -->|"searches via"| node_adapters
node_llm -->|"extracts for"| node_memory_episodic
node_llm -->|"extracts for"| node_memory_procedural
node_frontend -->|"calls"| node_api
node_mcp -->|"proxies"| node_api
node_workers -->|"persists"| node_storage
node_workers -->|"executes"| node_pipeline
node_m_flow -->|"contains"| node_api
node_m_flow -->|"contains"| node_retrieval
node_m_flow -->|"contains"| node_memory_episodic
node_m_flow -->|"contains"| node_memory_procedural

click node_m_flow "https://github.com/flowelement-ai/m_flow/tree/main/m_flow"
click node_api "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/api"
click node_auth "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/auth"
click node_pipeline "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/pipeline"
click node_memory_episodic "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/memory/episodic"
click node_memory_procedural "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/memory/procedural"
click node_domain_models "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/core/domain/models"
click node_ingestion "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/ingestion"
click node_preprocessing "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/preprocessing/coreference"
click node_storage "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/storage"
click node_adapters "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/adapters"
click node_llm "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/llm"
click node_shared "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/shared"
click node_retrieval "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/retrieval"
click node_search "https://github.com/flowelement-ai/m_flow/tree/main/m_flow/search"
click node_frontend "https://github.com/flowelement-ai/m_flow/tree/main/m_flow-frontend"
click node_mcp "https://github.com/flowelement-ai/m_flow/tree/main/m_flow-mcp"
click node_workers "https://github.com/flowelement-ai/m_flow/tree/main/mflow_workers"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_m_flow,node_frontend,node_mcp,node_workers toneBlue
class node_api,node_auth,node_pipeline,node_search toneAmber
class node_memory_episodic,node_memory_procedural,node_domain_models,node_ingestion,node_preprocessing toneMint
class node_retrieval toneRose
class node_storage,node_adapters,node_llm,node_shared toneIndigo
```

## Components

### App Surfaces

User-facing and integration entrypoints.

- [m_flow](https://github.com/flowelement-ai/m_flow/tree/main/m_flow): python package · Main library and system entrypoint for memory, retrieval, and platform services. · `m_flow`
- [Frontend](https://github.com/flowelement-ai/m_flow/tree/main/m_flow-frontend): next.js app · Operator console for ingest, search, graph visualization, settings, setup, and user management. · `m_flow-frontend`
- [MCP Server](https://github.com/flowelement-ai/m_flow/tree/main/m_flow-mcp): protocol server · Model Context Protocol surface for agent and IDE integrations. · `m_flow-mcp`
- [Workers](https://github.com/flowelement-ai/m_flow/tree/main/mflow_workers): distributed jobs · Queued persistence workers for graph and memory-node saving. · `mflow_workers`

### Backend API

FastAPI routers and operational service endpoints.

- [Backend API](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/api): fastapi app · Operational hub exposing ingest, search, graph, datasets, users, and maintenance workflows. · `m_flow/api`
- [Auth](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/auth): tenant ACL · Users, roles, tenants, permissions, and dataset-scoped access control. · `m_flow/auth`
- [Pipeline](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/pipeline): async orchestration · Run/task tracking, concurrency, progress, and remote dispatch for long-running work. · `m_flow/pipeline`
- [Search API](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/search): query facade · Search methods, access-control-aware query handling, and result logging. · `m_flow/search`

### Core Memory

Domain models and memory construction pipelines.

- [Episodic Memory](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/memory/episodic): ingest pipeline · Builds cone-graph memory from text and documents: routing, normalization, entities, facets, and writes. · `m_flow/memory/episodic`
- [Procedural Memory](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/memory/procedural): extract pipeline · Separately governs reusable procedures with safety, validation, versioning, and repair. · `m_flow/memory/procedural`
- [Domain Model](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/core/domain/models): cone graph · Episode, Facet, FacetPoint, Entity, Procedure, and related memory entities. · `m_flow/core/domain/models`
- [Ingestion](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/ingestion): load and chunk · Shared loaders, document classification, chunking, web scraping, and dataset routing. · `m_flow/ingestion`
- [Coreference](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/preprocessing/coreference): preprocess · Optional coreference resolution and normalization before memory building. · `m_flow/preprocessing/coreference`

### Retrieval

Query routing, ranking, and answer assembly.

- [Retrieval](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/retrieval): query engine · Search orchestration across episodic, procedural, lexical, triplet, and Cypher modes. · `m_flow/retrieval`

### Infrastructure

Pluggable storage, LLM, loaders, and shared services.

- [Storage](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/storage): persistence · Graph persistence, episode metadata, node indexing, pruning, and data deletion. · `m_flow/storage`
- [Adapters](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/adapters): backend interfaces · Pluggable graph, vector, cache, relational, and dataset database adapters. · `m_flow/adapters`
- [LLM Layer](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/llm): model gateway · Provider abstractions, prompts, tokenizers, extraction, and rate limiting for model calls. · `m_flow/llm`
- [Shared Infra](https://github.com/flowelement-ai/m_flow/tree/main/m_flow/shared): platform utils · Loaders, file storage, chunking engines, tracing, logging, cache, and observability. · `m_flow/shared`

## Connections

- Backend API → Auth: enforces · Applies principal, tenant, role, and dataset boundaries.
- Backend API → Pipeline: orchestrates · Starts and monitors long-running ingestion and maintenance runs.
- Backend API → Ingestion: ingests via · Accepts files, text, URLs, and media for dataset-scoped processing.
- Backend API → Search API: serves · Exposes query and retrieval endpoints to clients.
- Backend API → Episodic Memory: builds · Routes memorization requests into episodic memory construction.
- Backend API → Procedural Memory: extracts · Supports procedural extraction and upkeep workflows.
- Search API → Retrieval: uses · Delegates ranking and bundle assembly to retrieval engine.
- Retrieval → Episodic Memory: queries · Performs graph-routed bundle search over episodic memory.
- Retrieval → Procedural Memory: queries · Handles procedural recall and trigger-based injection.
- Episodic Memory → Domain Model: writes · Creates Episode, Facet, FacetPoint, Entity, and edges.
- Procedural Memory → Domain Model: writes · Stores procedures, steps, context packs, and versions.
- Ingestion → Coreference: optionally uses · Runs coreference and normalization before memory extraction.
- Ingestion → Shared Infra: loads with · Uses shared loaders, chunkers, file storage, and scraping utilities.
- Episodic Memory → Storage: persists through · Writes graph objects, metadata, and indices.
- Procedural Memory → Storage: persists through · Writes validated procedural artifacts and state.
- Storage → Adapters: abstracts over · Uses pluggable graph, vector, cache, and relational backends.
- Retrieval → Adapters: searches via · Depends on vector and graph backends for candidate discovery and traversal.
- LLM Layer → Episodic Memory: extracts for · Supports sentence splitting, entity matching, facet refinement, and summarization.
- LLM Layer → Procedural Memory: extracts for · Supports governance, validation, repair, and procedure compilation.
- Frontend → Backend API: calls · Uses backend APIs and websocket feedback for operator workflows.
- MCP Server → Backend API: proxies · Wraps backend memory operations as MCP tools.
- Workers → Storage: persists · Handles queued graph persistence and memory-node saving.
- Workers → Pipeline: executes · Consumes remote or queued tasks from pipeline orchestration.
- m_flow → Backend API: contains · Library package includes the backend application surface.
- m_flow → Retrieval: contains · Core query and answer assembly logic lives in the package.
- m_flow → Episodic Memory: contains · Episodic memory pipeline is part of the core package.
- m_flow → Procedural Memory: contains · Procedural memory pipeline is part of the core package.
