Skip to main content
Glama
README.md
# CML v3 — Cognitive Memory Layer for LLM Agents

A production-ready [MCP](https://modelcontextprotocol.io/) memory server that productionizes the mathematical foundations introduced in [SLM-V3](https://arxiv.org/abs/2603.14588) (Bhardwaj, 2026), delivering them with a novel **Memory Feed** presentation format and comprehensive benchmark evaluation.

CML v3 builds on three mathematical frameworks from SLM-V3:

- **Fisher-Rao geodesic distance** for embedding similarity (replacing cosine similarity)
- **Sheaf cohomology** for topical coherence detection
- **Langevin dynamics on Poincare ball** for memory lifecycle (natural reinforcement and forgetting)

## Features

- **4-channel retrieval**: Semantic + BM25 + Entity Graph + Temporal, fused with Reciprocal Rank Fusion
- **Memory Feed**: SNS-inspired presentation with trajectory tracking, causal chains, and retrieval reasons
- **Bayesian trust scoring**: Beta distribution (alpha/beta) for memory reliability
- **Fully local**: SQLite storage, no cloud dependencies required
- **MCP protocol**: Works with Claude Code, Claude Desktop, and any MCP-compatible client

## Quick Start

### Install

```bash
pip install cml-v3

# For Gemini embeddings (recommended, requires API key):
pip install cml-v3[gemini]

# For fully offline operation (sentence-transformers):
pip install cml-v3[local]

# Everything:
pip install cml-v3[all]
```

### Run the MCP Server

```bash
# With Gemini embeddings (set API key first):
export GEMINI_API_KEY="your-key-here"
cml-v3-server

# With local embeddings (no API key needed):
CML_V3_EMBEDDING_PROVIDER=local cml-v3-server
```

### Configure in Claude Code

Add to `~/.claude/settings.json`:

```json
{
  "mcpServers": {
    "cml-v3-memory": {
      "command": "cml-v3-server",
      "env": {
        "GEMINI_API_KEY": "your-key-here"
      }
    }
  }
}
```

### Configure in Claude Desktop

Add to `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "cml-v3-memory": {
      "command": "python",
      "args": ["-m", "cml_v3.server"],
      "env": {
        "GEMINI_API_KEY": "your-key-here"
      }
    }
  }
}
```

## MCP Tools

CML v3 provides 4 consolidated tools (optimized for minimal context token usage):

### `memory_search`
Search long-term memory with multi-channel retrieval.

| Action | Description |
|--------|-------------|
| `search` | Multi-channel search with Memory Feed format |
| `prime` | Session-start briefing (formatted context) |
| `recall` | Deep search with entity graph expansion |
| `timeline` | Chronological memory listing |

### `memory_store`
Write to long-term memory with contradiction detection.

| Action | Description |
|--------|-------------|
| `store` | Save new fact (auto-embeds, checks contradictions) |
| `reinforce` | Strengthen existing memory (trust alpha += 1) |
| `forget` | Archive a memory (move to ARCHIVED zone) |

### `memory_manage`
Memory system administration.

| Action | Description |
|--------|-------------|
| `stats` | Zone/project statistics |
| `contradictions` | List detected contradictions |
| `graph` | Explore entity knowledge graph |

### `memory_system`
Infrastructure health and configuration.

| Action | Description |
|--------|-------------|
| `health` | Component status check |
| `evolve` | Run Langevin dynamics evolution step |
| `config` | View/update configuration |

## Memory Feed Format

Search results are returned as enriched feeds (not flat lists):

```json
{
  "id": "sm-abc123",
  "content": "The project deadline was moved to April.",
  "trajectory": "strengthening",
  "confidence_rank": "high",
  "trust_score": 0.85,
  "shared_entities": ["Project X", "April deadline"],
  "related_to": ["sm-def456", "sm-ghi789"],
  "thread": [{"from": "sm-def456", "type": "led_to"}],
  "retrieval_reason": "semantic similarity + entity(Project X)",
  "contradictions": []
}
```

## Mathematical Foundations

The following mathematical modules are ported from [SLM-V3](https://arxiv.org/abs/2603.14588) (MIT License).

### Fisher-Rao Geodesic Distance

Instead of cosine similarity, CML v3 uses the Fisher-Rao geodesic distance on diagonal Gaussian manifolds, respecting per-dimension confidence:

$$d_{FR}(\mathbf{u}, \mathbf{v}) = \sqrt{\sum_i \frac{(u_i - v_i)^2}{\frac{1}{2}(\sigma_{u,i}^2 + \sigma_{v,i}^2)}}$$

### Sheaf Cohomology for Topical Coherence

Memories connected by shared entities form a cellular sheaf. The coboundary operator detects topical anomalies with mathematical guarantees (note: effective for topical coherence, not factual contradiction — see our [paper](https://arxiv.org/abs/XXXX.XXXXX) for details):

$$(\delta_0 \mathbf{x})(e_{uv}) = \mathcal{F}_{u \trianglelefteq e}(\mathbf{x}_u) - \mathcal{F}_{v \trianglelefteq e}(\mathbf{x}_v)$$

### Langevin Dynamics on Poincare Ball

Memory lifecycle is modeled as a stochastic process on the Poincare ball, where the origin represents high importance and the boundary represents forgetting:

| Zone | Radius | Semantics |
|------|--------|-----------|
| ACTIVE | [0, 0.3) | Core memories |
| WARM | [0.3, 0.55) | Recently relevant |
| COLD | [0.55, 0.8) | Fading |
| ARCHIVED | [0.8, 1.0) | Effectively forgotten |

## Data Storage

All data is stored locally in `~/.cml-v3/memory.db` (SQLite). Override with:

```bash
export CML_V3_DATA_DIR=/path/to/custom/dir
```

## Development

```bash
git clone https://github.com/Haratakeshi/cml-v3.git
cd cml-v3
pip install -e ".[dev,all]"
pytest tests/
```

## Citation

If you use CML v3 in your research, please cite:

```bibtex
@article{fujiwara2026cmlv3,
  title={Memory Feed: Productionizing Mathematical Memory Foundations
         for LLM Agents via MCP},
  author={Fujiwara, Takeshi},
  journal={arXiv preprint},
  year={2026}
}
```

## License

MIT License. See [LICENSE](LICENSE).

## Acknowledgments

The mathematical foundations (Fisher-Rao distance, sheaf cohomology, Langevin dynamics on Poincare ball) are ported from [SLM-V3](https://github.com/qualixar/superlocalmemory) by Varun Pratap Bhardwaj under the MIT license. We thank the author for releasing these foundational models.

CML v3 was co-developed with Claude (Anthropic).