Cuba-memroys

Cuba-memroys

Persistent memory for AI agents with knowledge graph, Hebbian learning, 4-signal RRF fusion search, GraphRAG enrichment, FSRS spaced repetition, and anti-hallucination grounding. 12 tools, PostgreSQL.

Category
Visit Server

README

🧠 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.3.0 β€” Modular architecture, CC-reduced (avg A), 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 βœ… ❌
GraphRAG topological enrichment βœ… ❌
REM Sleep autonomous consolidation βœ… ❌
Conditional pgvector + HNSW βœ… ❌
Modular architecture (CC avg A) βœ… ❌
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

v1.3.0 uses a modular architecture β€” the monolithic server.py (1920 LOC) was decomposed into focused modules with cyclomatic complexity reduced by 87% (avg D β†’ A).

cuba-memorys/
β”œβ”€β”€ docker-compose.yml          # Dedicated PostgreSQL (port 5488)
β”œβ”€β”€ pyproject.toml              # Package metadata + optional deps
β”œβ”€β”€ README.md
└── src/cuba_memorys/
    β”œβ”€β”€ __init__.py             # Version (1.3.0)
    β”œβ”€β”€ __main__.py             # Entry point
    β”œβ”€β”€ server.py               # Thin re-export (24 LOC)
    β”œβ”€β”€ protocol.py             # JSON-RPC transport, event loop, REM Sleep daemon
    β”œβ”€β”€ handlers.py             # 12 MCP tool handlers (CC-reduced via sub-function extraction)
    β”œβ”€β”€ constants.py            # Tool definitions, thresholds, enums
    β”œβ”€β”€ 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.3.0 results:

🟒 Normal (12/12)  β€” All 12 tools, all CRUD actions, GraphRAG enrichment,
                     Hebbian cross-reference, RRF fusion, Oja's rule,
                     dedup gate, anti-repetition guard, batch_add
🟑 Pessimist (8/8) β€” Empty strings, missing fields, invalid entity_type,
                     non-existent entities, limit=0, max_depth=999,
                     anti-repetition guard trigger, Oja negative decay
πŸ”΄ Extreme (9/9)  β€” SQL injection (parametrized queries), XSS (literal storage),
                     UNION injection (no data leak), path traversal (inert),
                     unicode bombs (emoji + special chars), oversized inputs,
                     eco correct priority semantics, Louvain communities

Previous versions: v1.1.0 (13/13), v1.0.1 (18/18).

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), McCabe (1976, CC), BAAI (2023, BGE embeddings), Malkov & Yashunin (2018, HNSW).

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured