Skip to main content
Glama

🧠 Cuba-Memorys

Python 3.11+ License: CC BY-NC 4.0 Version PostgreSQL MCP

Persistent memory for AI agents β€” A Model Context Protocol (MCP) server that gives AI coding assistants long-term memory with a knowledge graph, Hebbian learning, GraphRAG enrichment, and anti-hallucination grounding.

12 tools with Cuban soul. Zero manual setup. Mathematically rigorous. v1.1.0 β€” God-Tier: RRF fusion, REM Sleep daemon, GraphRAG, conditional pgvector.


Why Cuba-Memorys?

AI agents forget everything between conversations. Cuba-Memorys solves this by giving them:

  • A knowledge graph β€” Entities, observations, and relations that persist across sessions

  • Error memory β€” Never repeat the same mistake twice (anti-repetition guard)

  • Hebbian learning β€” Memories strengthen with use and fade adaptively (FSRS spaced repetition)

  • Anti-hallucination β€” Verify claims against stored knowledge with graduated confidence scoring

  • Semantic search β€” 4-signal RRF fusion (TF-IDF + pg_trgm + full-text + optional pgvector HNSW)

  • GraphRAG β€” Top results enriched with degree-1 graph neighbors for topological context

  • REM Sleep β€” Autonomous background consolidation (FSRS decay + prune + PageRank) after 15min idle

  • Graph intelligence β€” Personalized PageRank, Louvain community detection, betweenness centrality

Feature

Cuba-Memorys

Basic Memory MCPs

Knowledge graph with relations

βœ…

❌

Hebbian learning (Oja's rule)

βœ…

❌

FSRS adaptive spaced repetition

βœ…

❌

4-signal RRF fusion search (v1.1)

βœ…

❌

GraphRAG topological enrichment (v1.1)

βœ…

❌

REM Sleep autonomous consolidation (v1.1)

βœ…

❌

Conditional pgvector + HNSW (v1.1)

βœ…

❌

Optional BGE embeddings (ONNX)

βœ…

❌

Contradiction detection

βœ…

❌

Graduated confidence scoring

βœ…

❌

Personalized PageRank

βœ…

❌

Louvain community detection

βœ…

❌

Betweenness centrality (bridges)

βœ…

❌

Shannon entropy (knowledge diversity)

βœ…

❌

Chi-squared concept drift detection

βœ…

❌

Error pattern detection + MTTR

βœ…

❌

Entity duplicate detection (SQL similarity)

βœ…

❌

Observation versioning (audit trail)

βœ…

❌

Temporal validity (valid_from/valid_until)

βœ…

❌

Write-time dedup gate

βœ…

❌

Auto-supersede contradictions

βœ…

❌

Full JSON export/backup (bounded)

βœ…

❌

Fuzzy search (typo-tolerant)

βœ…

❌

Spreading activation

βœ…

❌

Batch observations (10x fewer calls)

βœ…

❌

Entity type validation

βœ…

❌

Graceful shutdown (SIGTERM/SIGINT)

βœ…

❌

Auto-provisions its own DB

βœ…

❌


Quick Start

1. Prerequisites

  • Python 3.11+

  • Docker (for PostgreSQL)

2. Install

git clone https://github.com/lENADRO1910/cuba-memorys.git
cd cuba-memorys

docker compose up -d

pip install -e .

# Optional: BGE embeddings for semantic search (~130MB model)
pip install -e ".[embeddings]"

3. Configure your AI editor

Add to your MCP configuration:

{
  "mcpServers": {
    "cuba-memorys": {
      "command": "/path/to/cuba_memorys_launcher.sh",
      "disabled": false
    }
  }
}

Or run directly:

DATABASE_URL="postgresql://cuba:memorys2026@127.0.0.1:5488/brain" python -m cuba_memorys

The server auto-creates the brain database and all tables on first run.


πŸ‡¨πŸ‡Ί Las 12 Herramientas

Every tool is named after Cuban culture β€” memorable, professional, and meaningful.

Knowledge Graph

Tool

Meaning

Description

cuba_alma

Alma β€” soul, essence

CRUD knowledge entities. Types: concept, project, technology, person, pattern, config. Triggers spreading activation on neighbors.

cuba_cronica

CrΓ³nica β€” chronicle

Attach observations to entities with contradiction detection and dedup gate. Supports batch_add. Types: fact, decision, lesson, preference, context, tool_usage.

cuba_puente

Puente β€” bridge

Connect entities with typed relations (uses, causes, implements, depends_on, related_to). Traverse walks the graph. Infer discovers transitive connections (A→B→C).

Search & Verification

Tool

Meaning

Description

cuba_faro

Faro β€” lighthouse

Search with 4-signal RRF fusion (TF-IDF + full-text + trigrams + pgvector). verify mode for anti-hallucination (returns verified / partial / weak / unknown). Top-3 results enriched with GraphRAG neighbors. Session-aware: boosts results matching active goals.

Error Memory

Tool

Meaning

Description

cuba_alarma

Alarma β€” alarm

Report errors immediately. Auto-detects patterns (β‰₯3 similar = warning). Hebbian boosting for retrieval.

cuba_remedio

Remedio β€” remedy

Mark an error as resolved. Cross-references similar unresolved errors.

cuba_expediente

Expediente β€” case file

Search past errors/solutions. Anti-repetition guard: warns if a similar approach previously failed.

Sessions & Decisions

Tool

Meaning

Description

cuba_jornada

Jornada β€” workday

Track working sessions with goals and outcomes.

cuba_decreto

Decreto β€” decree

Record architecture decisions with context, alternatives, and rationale.

Memory Maintenance

Tool

Meaning

Description

cuba_zafra

Zafra β€” sugar harvest 🎯

Memory consolidation: decay (FSRS adaptive), prune, merge, summarize, pagerank (personalized), find_duplicates (SQL similarity), export (bounded JSON backup), stats.

cuba_eco

Eco β€” echo

RLHF feedback: positive (Oja's rule boost), negative (decrease), correct (update with versioning).

cuba_vigia

VigΓ­a β€” watchman

Graph analytics: summary (counts + token estimate), health (staleness, Shannon entropy, DB size), drift (chi-squared), communities (Louvain), bridges (betweenness centrality).


Mathematical Foundations

Cuba-Memorys is built on peer-reviewed algorithms, not ad-hoc heuristics:

FSRS Adaptive Decay β€” Wozniak (1987) / Ye (2022)

stability = fsrs_stability Β· (1 + decay_factor)^rating
R(t) = (1 + t/(9Β·S))^(-1)

FSRS (Free Spaced Repetition Scheduler) provides adaptive memory decay. Stability grows with successful reviews β€” memories that are reinforced survive longer.

Oja's Rule (1982) β€” Hebbian Learning

Positive: Ξ”w = Ξ· Β· (1 - wΒ²)     β†’ converges to 1.0, cannot explode
Negative: Ξ”w = Ξ· Β· (1 + wΒ²)     β†’ converges to 0.01 (floor)

Where Ξ· = 0.05. The wΒ² term provides natural saturation β€” self-normalizing without explicit clipping.

TF-IDF + RRF Fusion β€” Salton (1975) / Cormack (2009)

tfidf(t, d) = tf(t, d) Β· log(N / df(t))
RRF(d) = Ξ£ 1/(k + rank_i(d))     where k = 60

Reciprocal Rank Fusion combines multiple ranked lists from independent signals into a single robust ranking. In v1.1.0, up to 4 signals are fused: entities (full-text + trigrams), observations (full-text + trigrams + TF-IDF), errors (full-text + trigrams), and optional pgvector (cosine distance on embeddings).

Optional BGE Embeddings β€” BAAI (2023)

model: Qdrant/bge-small-en-v1.5-onnx-Q (quantized, ~130MB)
runtime: ONNX (no PyTorch dependency)
similarity: cosine(embed(query), embed(observation))

Auto-downloads on first use. Falls back to TF-IDF if not installed.

GraphRAG Enrichment β€” v1.1.0

Top-3 search results are enriched with degree-1 graph neighbors via a single batched SQL query. Each result gets a graph_context array containing neighbor name, entity type, relation type, and Hebbian strength. Provides topological context without N+1 queries.

REM Sleep Daemon β€” v1.1.0

After 15 minutes of user inactivity, an autonomous consolidation coroutine runs:

  1. FSRS Decay β€” Applies memory decay using Ye (2022) algorithm

  2. Prune β€” Removes low-importance (< 0.1), rarely-accessed observations

  3. PageRank β€” Recalculates personalized importance scores

Cancels immediately on new user activity. Prevents concurrent runs.

Conditional pgvector β€” v1.1.0

IF pgvector extension detected:
  β†’ Migrate embedding column: float4[] β†’ vector(384)
  β†’ Create HNSW index (m=16, ef_construction=64, vector_cosine_ops)
  β†’ Add vector cosine distance as 4th RRF signal
  β†’ Persist embeddings on observation insert
ELSE:
  β†’ Graceful degradation: TF-IDF + trigrams (unchanged)

Zero-downtime: auto-detects at startup, no configuration needed.

Personalized PageRank β€” Brin & Page (1998)

PR(v) = (1-Ξ±)/N + Ξ± Β· Ξ£ PR(u)/deg(u)     where Ξ± = 0.85
personalization: biased toward recently active entities
final_importance = 0.6Β·PR + 0.4Β·current_importance

Graph-structural importance via networkx. Personalized bias prevents cold-start domination.

Shannon Entropy β€” Knowledge Diversity

H = -Ξ£ pΒ·logβ‚‚(p)
diversity_score = H / H_max

Measures how evenly distributed your knowledge is across entity/observation types. Score 0–1.

Spreading Activation β€” Collins & Loftus (1975)

When entity X is accessed, its graph neighbors receive a small importance boost (0.6% per hop, decaying 30% per level).

Chi-Squared Concept Drift β€” Pearson (1900)

χ² = Ξ£ (observed - expected)Β² / expected
p-value via scipy.stats.chi2.sf()

Detects when the distribution of error types changes significantly β€” signals that something new is breaking.

Contradiction Detection

conflict = tfidf_similarity(new, existing) > 0.7 AND has_negation(new, existing)

Checks for semantic overlap + negation patterns (not, never, instead of, replaced, deprecated) in both English and Spanish.


Architecture

cuba-memorys/
β”œβ”€β”€ docker-compose.yml          # Dedicated PostgreSQL (port 5488)
β”œβ”€β”€ pyproject.toml              # Package metadata + optional deps
β”œβ”€β”€ README.md
└── src/cuba_memorys/
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ __main__.py             # Entry point
    β”œβ”€β”€ server.py               # 12 MCP tools + REM Sleep daemon (~1850 LOC)
    β”œβ”€β”€ db.py                   # asyncpg pool + orjson + pgvector detection + Decimal
    β”œβ”€β”€ schema.sql              # 5 tables, 15+ indexes, pg_trgm, versioning
    β”œβ”€β”€ hebbian.py              # FSRS, Oja's rule, spreading activation
    β”œβ”€β”€ search.py               # LRU cache, RRF fusion, NEIGHBORS_SQL, SEARCH_VECTOR_SQL
    β”œβ”€β”€ tfidf.py                # TF-IDF semantic search (scikit-learn)
    └── embeddings.py           # Optional BGE embeddings (ONNX Runtime)

Database Schema

Table

Purpose

Key Features

brain_entities

Knowledge graph nodes

tsvector + pg_trgm indexes, importance ∈ [0,1], FSRS stability/difficulty

brain_observations

Facts attached to entities

9 types, provenance, versioning, temporal validity, vector(384) embedding (if pgvector)

brain_relations

Graph edges

5 types, bidirectional delete, Hebbian strength

brain_errors

Error memory

JSONB context, synapse weight, MTTR tracking

brain_sessions

Working sessions

Goals (JSONB), outcome tracking

Search Pipeline (v1.1.0)

Cuba-Memorys uses Reciprocal Rank Fusion (RRF, k=60) to combine up to 4 independent ranked signals:

#

Signal

Source

Condition

1

Entities (ts_rank + trigrams + importance + freshness)

brain_entities

Always

2

Observations (ts_rank + trigrams + TF-IDF + importance)

brain_observations

Always

3

Errors (ts_rank + trigrams + synapse_weight)

brain_errors

Always

4

Vector cosine distance (HNSW)

brain_observations.embedding

pgvector installed

Each signal produces an independent ranking. RRF fuses them: score(d) = Ξ£ 1/(60 + rank_i(d)).

Post-fusion enrichment:

  • Top-3 results receive GraphRAG context (degree-1 neighbors)

  • Active session goals boost matching results by 15%

Dependencies

Core:

  • asyncpg β€” PostgreSQL async driver

  • orjson β€” Fast JSON serialization (handles UUID/datetime)

  • scikit-learn β€” TF-IDF vectorization

  • networkx β€” PageRank + Louvain + betweenness centrality

  • scipy β€” Chi-squared statistical tests

  • rapidfuzz β€” Entity duplicate detection

  • numpy β€” Numerical operations

Optional (pip install -e ".[embeddings]"):

  • onnxruntime β€” ONNX model inference

  • huggingface-hub β€” Auto-download BGE model

  • tokenizers β€” Fast tokenization


Configuration

Environment Variables

Variable

Default

Description

DATABASE_URL

β€”

PostgreSQL connection string (required)

Docker Compose

Runs a dedicated PostgreSQL 18 Alpine instance:

  • Port: 5488 (avoids conflicts with 5432/5433)

  • Resources: 256MB RAM, 0.5 CPU

  • Restart: always (auto-starts on boot)

  • Healthcheck: pg_isready every 10s


How It Works in Practice

1. The agent learns from your project

Agent: I learned that FastAPI endpoints must use async def with response_model.
β†’ cuba_alma(create, "FastAPI", technology)
β†’ cuba_cronica(add, "FastAPI", "All endpoints must be async def with response_model")

2. Error memory prevents repeated mistakes

Agent: I got IntegrityError: duplicate key on numero_parte.
β†’ cuba_alarma("IntegrityError", "duplicate key on numero_parte")
β†’ Similar error found! Solution: "Add SELECT EXISTS before INSERT with FOR UPDATE"

3. Anti-hallucination grounding

Agent: Let me verify this claim before responding...
β†’ cuba_faro("FastAPI uses Django ORM", mode="verify")
β†’ confidence: 0.0, level: "unknown"
β†’ recommendation: "No supporting evidence found. High hallucination risk."

4. Memories adapt with FSRS

Initial stability:    S = 1.0 (decays in ~9 days)
After 5 reviews:      S = 8.2 (decays in ~74 days)
After 20 reviews:     S = 45.0 (survives ~13 months)

5. Contradiction detection

Existing:  "Project uses PostgreSQL 15"
New:       "Project does NOT use PostgreSQL, it uses MySQL instead"
β†’ ⚠️ CONFLICT detected (similarity=0.82, negation pattern: "does NOT")

6. Graph intelligence

β†’ cuba_zafra(action="pagerank")
β†’ Top entities: FastAPI (0.42), SQLAlchemy (0.38), PostgreSQL (0.35)

β†’ cuba_vigia(metric="communities")
β†’ Community 1: [FastAPI, Pydantic, SQLAlchemy] β€” Backend cluster
β†’ Community 2: [React, Next.js, TypeScript] β€” Frontend cluster

Verification

Tested with NEMESIS protocol (3-tier) β€” v1.1.0 results:

🟒 Normal (4/4)   β€” RRF fusion (rrf_score on all results), GraphRAG (graph_context with
                     neighbors), scope=observations with grounding, verify mode (0.7093)
🟑 Pessimist (4/4) β€” Empty queries, whitespace, dedup gate (similarity=1.0),
                     graph traversal depth=2 with strength decay
πŸ”΄ Extreme (5/5)  β€” SQL injection, XSS, path traversal, min-length content,
                     transitive inference depth=5

Previous v1.0.1 tests (18/18) also verified: Entity CRUD, observe, search, relate, PageRank, communities, decay, find_duplicates, export, health, entropy, Unicode, bidirectional delete.

Performance

Operation

Avg latency

RRF hybrid search

< 5ms

Analytics

< 2.5ms

Entity CRUD

< 1ms

PageRank (100 entities)

< 50ms

GraphRAG enrichment

< 2ms


License

CC BY-NC 4.0 β€” Free to use and modify, not for commercial use.


Author

Leandro PΓ©rez G.

Credits

Mathematical foundations: Wozniak (1987), Ye (2022, FSRS), Oja (1982), Salton (1975, TF-IDF), Cormack (2009, RRF), Brin & Page (1998, PageRank), Collins & Loftus (1975), Shannon (1948), Pearson (1900, χ²), Blondel et al. (2008, Louvain), BAAI (2023, BGE embeddings), Malkov & Yashunin (2018, HNSW).

-
security - not tested
F
license - not found
-
quality - not tested

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/LeandroPG19/cuba-memroys'

If you have feedback or need assistance with the MCP directory API, please join our Discord server