Skip to main content
Glama
khaled1174

agent-memory

by khaled1174
README.md
<div align="center">

<img src="https://raw.githubusercontent.com/khaled1174/agent-memory/main/assets/banner.svg" alt="agent-memory banner" width="900"/>

# Agent Memory System

[![Python](https://img.shields.io/badge/Python-3.8+-3776AB?style=flat-square&logo=python&logoColor=white)](https://python.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-green?style=flat-square)](LICENSE)
[![Tests](https://img.shields.io/badge/Tests-58%20passing-brightgreen?style=flat-square)](tests/)
[![Zero Dependencies](https://img.shields.io/badge/Dependencies-Zero-blue?style=flat-square)](requirements.txt)
[![MCP Tools](https://img.shields.io/badge/MCP-12%20tools-purple?style=flat-square)](memory_mcp.py)

**Persistent memory for AI agents using SQLite + FTS5.**  
Single-file core ยท Zero dependencies ยท Optional MCP server and Obsidian sync.

</div>

---

## โœจ Features

| Feature | Description |
|---------|-------------|
| ๐Ÿ—„๏ธ **Persistent** | SQLite database survives process restarts |
| ๐Ÿ” **Full-text search** | FTS5 engine with ranked results |
| ๐Ÿ”’ **Agent isolation** | Each agent gets its own namespace automatically |
| ๐Ÿ› ๏ธ **MCP Server** | 12 tools for Claude Code / Claude Desktop |
| ๐Ÿ“ **Obsidian sync** | Optional two-way Markdown bridge |
| ๐Ÿค– **CLAUDE.md generator** | Auto-inject context into every session |
| ๐ŸŒ **Cross-platform** | macOS, Linux, WSL, Windows |
| โšก **Zero dependencies** | Core uses only Python standard library |

---

## ๐Ÿ—๏ธ Architecture

```mermaid
graph TB
    subgraph Clients["๐Ÿ–ฅ๏ธ Clients"]
        CLI["๐Ÿ’ป CLI\n(memory.py)"]
        MCP["๐Ÿ”Œ MCP Server\n(memory_mcp.py)"]
        HERMES["๐ŸŒ‰ Hermes Bridge\n(hermes/bridge.py)"]
    end

    subgraph Core["โš™๏ธ Core Engine"]
        STORE["๐Ÿ“ฅ Store\nmemory_store()"]
        SEARCH["๐Ÿ” Search\nFTS5 Engine"]
        ISO["๐Ÿ”’ Isolation\nNamespace Handler"]
        CTX["๐Ÿ“‹ Context\nCLAUDE.md Gen"]
    end

    subgraph Storage["๐Ÿ’พ Storage"]
        DB[("๐Ÿ—„๏ธ SQLite DB\nmemory.db")]
        FTS[("๐Ÿ“‘ FTS5 Index")]
    end

    subgraph External["๐Ÿ”— Integrations"]
        OBS["๐Ÿ““ Obsidian Vault\n(Markdown)"]
        N8N["โš™๏ธ n8n Workflow"]
        TG["๐Ÿ“ฑ Telegram Bot"]
    end

    CLI --> STORE
    CLI --> SEARCH
    MCP --> STORE
    MCP --> SEARCH
    HERMES --> STORE
    HERMES --> SEARCH
    STORE --> ISO
    SEARCH --> ISO
    ISO --> DB
    DB --> FTS
    FTS --> SEARCH
    STORE --> OBS
    OBS --> STORE
    N8N --> HERMES
    TG --> HERMES
    CTX --> DB

    style Clients fill:#1f3558,stroke:#58a6ff,color:#cdd9e5
    style Core fill:#2d1f58,stroke:#bc8cff,color:#cdd9e5
    style Storage fill:#1f3a2d,stroke:#3fb950,color:#cdd9e5
    style External fill:#3a2d1f,stroke:#d29922,color:#cdd9e5
```

---

## ๐Ÿ”„ Memory Lifecycle

```mermaid
sequenceDiagram
    actor Agent as ๐Ÿค– AI Agent
    participant CLI as ๐Ÿ’ป CLI / MCP
    participant ISO as ๐Ÿ”’ Namespace
    participant DB as ๐Ÿ—„๏ธ SQLite
    participant FTS as ๐Ÿ” FTS5 Index
    participant OBS as ๐Ÿ““ Obsidian

    Agent->>CLI: memory store --type semantic --content "..."
    CLI->>ISO: Resolve agent namespace (AGENT_NAME)
    ISO-->>CLI: agent_id = "health" (or global)
    CLI->>DB: INSERT INTO memories (agent_id, type, content, ...)
    DB->>FTS: Auto-index content
    DB-->>CLI: memory_id = 42
    CLI-->>Agent: โœ… Stored (id=42)

    Agent->>CLI: memory search --query "rate limit"
    CLI->>ISO: Resolve agent namespace
    ISO-->>CLI: agent_id
    CLI->>FTS: SELECT * FROM memories_fts WHERE content MATCH "rate limit"
    FTS-->>CLI: Ranked results
    CLI-->>Agent: ๐Ÿ“‹ Results (ranked by relevance)

    Note over DB,OBS: Optional Obsidian Sync
    DB->>OBS: Sync to Markdown vault
    OBS->>DB: Sync from Markdown vault
```

---

## ๐Ÿง  Memory Types

```mermaid
mindmap
  root((๐Ÿง  Memory Types))
    ๐Ÿ“… episodic
      Events and decisions
      Switched to Postgres
      Deployed v2 on Monday
    ๐Ÿ“š semantic
      Facts and knowledge
      Rate limit 1000 req/min
      Max upload 50 MB
    โš™๏ธ procedural
      Workflows and how-tos
      Steps build test deploy
      Auth flow OAuth2 JWT
```

---

## ๐Ÿš€ Quick Start

### Option 1: CLI (zero dependencies)

```bash
cp memory.py /usr/local/bin/memory
chmod +x /usr/local/bin/memory

# Store a memory
memory store --type semantic --content "Max upload: 50 MB" --project api --importance 5

# Search memories
memory search --query "upload"

# List recent
memory list --project api --recent 10

# View stats
memory stats
```

### Option 2: MCP Server (Claude Code / Claude Desktop)

```bash
pip install mcp
```

Add to `~/.claude/settings.json` (Claude Code):

```json
{
  "mcpServers": {
    "agent-memory": {
      "command": "python",
      "args": ["/path/to/memory_mcp.py"]
    }
  }
}
```

> **Claude Desktop paths:**
> - macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
> - Windows: `%APPDATA%\Claude\claude_desktop_config.json`

Restart and you'll see **12 tools** available.

---

## ๐Ÿ› ๏ธ MCP Tools

```mermaid
graph LR
    subgraph Session["๐Ÿ“‹ Session"]
        T1["memory_brief\nLoad context"]
        T2["memory_who\nAgent identity"]
        T3["memory_stats\nDB statistics"]
    end

    subgraph CRUD["โœ๏ธ CRUD"]
        T4["memory_store\nSave memory"]
        T5["memory_update\nPatch memory"]
        T6["memory_delete\nRemove by ID"]
    end

    subgraph Query["๐Ÿ” Query & Export"]
        T7["memory_search\nFTS5 full-text"]
        T8["memory_list\nFilter and sort"]
        T9["memory_export\nExport to JSON"]
        T10["memory_generate_context\nBuild CLAUDE.md"]
    end

    subgraph Sync["๐Ÿ”„ Sync"]
        T11["memory_sync_to_vault\nSQLite to Obsidian"]
        T12["memory_sync_from_vault\nObsidian to SQLite"]
    end

    style Session fill:#1f3558,stroke:#58a6ff,color:#cdd9e5
    style CRUD fill:#2d1f58,stroke:#bc8cff,color:#cdd9e5
    style Query fill:#1f3a2d,stroke:#3fb950,color:#cdd9e5
    style Sync fill:#3a2d1f,stroke:#d29922,color:#cdd9e5
```

---

## ๐Ÿ”’ Agent Isolation

```mermaid
graph TD
    ENV["๐ŸŒ AGENT_NAME env var"]

    ENV -->|"= 'health'"| HEALTH["๐Ÿฅ health namespace\nWrite: own only\nRead: own + global"]
    ENV -->|"= 'orchestrator'"| ORCH["๐ŸŽฏ orchestrator\nWrite: anywhere\nRead: everything"]
    ENV -->|"not set"| GLOBAL["๐ŸŒ global\nWrite: anywhere\nRead: everything"]

    HEALTH --> DB1[("health::memories")]
    HEALTH -.->|read only| DB2[("global::memories")]
    ORCH --> DB1
    ORCH --> DB2
    ORCH --> DB3[("other::memories")]
    GLOBAL --> DB2

    style ENV fill:#21262d,stroke:#58a6ff,color:#cdd9e5
    style HEALTH fill:#1f3558,stroke:#58a6ff,color:#cdd9e5
    style ORCH fill:#2d1f58,stroke:#bc8cff,color:#cdd9e5
    style GLOBAL fill:#1f3a2d,stroke:#3fb950,color:#cdd9e5
```

```bash
export AGENT_NAME=health   # Linux / macOS / WSL
$env:AGENT_NAME = "health" # Windows PowerShell
```

| Role | Write Access | Read Access |
|------|-------------|-------------|
| Regular agent | Own namespace only | Own + global |
| `orchestrator` | Anywhere | Everything |
| Not set | Anywhere | Everything |

---

## ๐ŸŒ‰ Hermes โ€” Cross-Platform Memory

> Two agents collaborate on Slack. You ask on Telegram. The memory carries.

```mermaid
flowchart LR
    SLACK["๐Ÿ’ฌ Slack Agent"] -->|POST| N8N["โš™๏ธ n8n"]
    N8N -->|HTTP| BRIDGE["๐ŸŒ‰ Hermes Bridge\nbridge.py :8765"]
    BRIDGE <-->|SQLite| DB[("๐Ÿ—„๏ธ memory.db")]
    TG["๐Ÿ“ฑ Telegram Bot"] <-->|HTTP| BRIDGE
    MCP["๐Ÿ”Œ MCP Server"] <-->|SQLite| DB
    CLI["๐Ÿ’ป CLI"] <-->|SQLite| DB

    style SLACK fill:#1f3a2d,stroke:#3fb950,color:#cdd9e5
    style N8N fill:#3a2d1f,stroke:#d29922,color:#cdd9e5
    style BRIDGE fill:#2d1f58,stroke:#bc8cff,color:#cdd9e5
    style DB fill:#1f3558,stroke:#58a6ff,color:#cdd9e5
    style TG fill:#1f3558,stroke:#58a6ff,color:#cdd9e5
    style MCP fill:#2d1f58,stroke:#bc8cff,color:#cdd9e5
    style CLI fill:#1f3a2d,stroke:#3fb950,color:#cdd9e5
```

```bash
# 1. Start the bridge
python hermes/bridge.py

# 2. Import hermes/n8n_workflow.json into your n8n instance

# 3. Start the Telegram bot
TELEGRAM_TOKEN=xxx HERMES_URL=http://your-server:8765 python hermes/telegram_bot.py
```

> See [hermes/README.md](hermes/README.md) for full setup, API reference, and VPS deployment.

---

## ๐Ÿ“ฆ Obsidian Integration (Optional)

```bash
export OBSIDIAN_VAULT=/path/to/your/vault

# Auto-mirrors every store/update/delete to Markdown
memory store --type semantic --content "API uses JWT" --project api

# Bulk sync
memory sync-to-vault    # SQLite โ†’ Markdown
memory sync-from-vault  # Markdown โ†’ SQLite
```

---

## ๐Ÿ“‹ CLAUDE.md Generator

```bash
memory generate-context --output CLAUDE.md
```

Creates a Markdown file from your most important memories. Place it in your project root and Claude Code reads it automatically every session.

---

## ๐Ÿ“ File Structure

```
agent-memory/
โ”œโ”€โ”€ memory.py              # Core: CLI + library (1282 lines)
โ”œโ”€โ”€ memory_mcp.py          # MCP server (645 lines, requires: pip install mcp)
โ”œโ”€โ”€ assets/
โ”‚   โ””โ”€โ”€ banner.svg         # Repository banner image
โ”œโ”€โ”€ hermes/                # Cross-platform memory layer
โ”‚   โ”œโ”€โ”€ bridge.py          # HTTP gateway (zero new deps)
โ”‚   โ”œโ”€โ”€ telegram_bot.py    # Telegram recall bot (requires: requests)
โ”‚   โ”œโ”€โ”€ n8n_workflow.json  # Ready-to-import Slack โ†’ memory workflow
โ”‚   โ””โ”€โ”€ README.md          # Hermes setup guide
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ requirements.txt       # Runtime: empty (stdlib only)
โ”œโ”€โ”€ requirements-dev.txt   # Dev: pytest, black, mypy, ruff
โ””โ”€โ”€ tests/
    โ”œโ”€โ”€ __init__.py
    โ””โ”€โ”€ test_memory.py     # 58 tests across 8 classes
```

---

## ๐Ÿ—ƒ๏ธ Database Location

| Platform | Default Path |
|----------|-------------|
| macOS / Linux | `~/.claude/memory/memory.db` |
| Windows | `%APPDATA%\\claude\\memory\\memory.db` |

Override: `export AGENT_MEMORY_DIR=/custom/path`

---

## ๐Ÿงช Testing

```bash
pip install -r requirements-dev.txt
python -m pytest tests/test_memory.py -v          # 58 tests
python -m pytest tests/test_memory.py -v --cov=memory
```

---

## ๐Ÿ“„ License

[MIT](LICENSE) ยฉ khaled1174

---

<div align="center">
<sub>Built with โค๏ธ for AI agents that need to remember.</sub>
</div>