Skip to content

Architecture - Kagura v4.0

Universal AI Memory Platform - System Design

This document describes the architecture of Kagura v4.0 after Phase C completion.


πŸ—οΈ High-Level Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  AI Platforms (MCP Clients)                     β”‚
β”‚      Claude Desktop β€’ ChatGPT β€’ Gemini β€’ Cursor β€’ Cline         β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚ stdio (local)                    HTTP/SSE (remote)β”‚
       β”‚                                                   β”‚
β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  MCP Server        β”‚                    β”‚  MCP over HTTP/SSE    β”‚
β”‚  (Local)           β”‚                    β”‚  (/mcp endpoint)      β”‚
β”‚                    β”‚                    β”‚                       β”‚
β”‚  All 31 tools βœ…   β”‚                    β”‚  24 safe tools only   β”‚
β”‚  File ops βœ…       β”‚                    β”‚  File ops ❌          β”‚
β”‚  Shell exec βœ…     β”‚                    β”‚  Shell exec ❌        β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                                                   β”‚
       β”‚              Internal Python API                  β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚         Memory Manager                   β”‚
          β”‚   (src/kagura/core/memory/manager.py)    β”‚
          β”‚                                          β”‚
          β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
          β”‚  β”‚ Working  β”‚ Context   β”‚ Persistent  β”‚ β”‚
          β”‚  β”‚ Memory   β”‚ Memory    β”‚ Memory      β”‚ β”‚
          β”‚  β”‚(In-Mem)  β”‚(Messages) β”‚(SQLite)     β”‚ β”‚
          β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
          β”‚                                          β”‚
          β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
          β”‚  β”‚  RAG (ChromaDB)                    β”‚ β”‚
          β”‚  β”‚  β€’ Working RAG                     β”‚ β”‚
          β”‚  β”‚  β€’ Persistent RAG                  β”‚ β”‚
          β”‚  β”‚  β€’ Semantic search                 β”‚ β”‚
          β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
          β”‚                                          β”‚
          β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
          β”‚  β”‚  Graph Memory (NetworkX)           β”‚ β”‚
          β”‚  β”‚  β€’ Relationships                   β”‚ β”‚
          β”‚  β”‚  β€’ Interaction history             β”‚ β”‚
          β”‚  β”‚  β€’ User patterns                   β”‚ β”‚
          β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚    Storage      β”‚
                  β”‚  β€’ SQLite       β”‚
                  β”‚  β€’ ChromaDB     β”‚
                  β”‚  β€’ Pickle files β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ†• Phase C Architecture (Remote MCP Server)

Remote Access Flow

ChatGPT                         Your Server
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ ChatGPT β”‚  HTTPS/SSE          β”‚    Caddy     β”‚
β”‚Connectorβ”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚ (Port 443)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β”‚
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                               β”‚  Kagura API   β”‚
                               β”‚  (Port 8080)  β”‚
                               β”‚               β”‚
                               β”‚  /mcp         │◄─ HTTP/SSE
                               β”‚  /api/v1/*    │◄─ REST
                               β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β”‚
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚ Memory Manager  β”‚
                              β”‚  + Graph        β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β”‚
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚ PostgreSQL      β”‚
                              β”‚ + pgvector      β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Security Layers

1. API Key Authentication
   β”œβ”€ SHA256 hashed storage
   β”œβ”€ Optional expiration
   └─ Audit trail (last_used_at)

2. Tool Access Control
   β”œβ”€ Local context: All 31 tools βœ…
   β”œβ”€ Remote context: 24 safe tools only
   └─ Dangerous tools filtered:
      β€’ file_read, file_write
      β€’ shell_exec
      β€’ media_open_*

3. Network Security
   β”œβ”€ Caddy reverse proxy
   β”œβ”€ Automatic HTTPS (Let's Encrypt)
   β”œβ”€ CORS configuration
   └─ Security headers (HSTS, XSS)

πŸ“¦ Component Details

1. MCP Server (src/kagura/mcp/)

stdio Transport (local): - File: src/kagura/cli/mcp.py - Command: kagura mcp serve - Context: local (all tools available) - Clients: Claude Desktop, Cursor, Cline

HTTP/SSE Transport (remote): - File: src/kagura/api/routes/mcp_transport.py - Endpoint: /mcp - Context: remote (safe tools only) - Clients: ChatGPT Connector, web browsers

Tool Permissions: - File: src/kagura/mcp/permissions.py - Logic: is_tool_allowed(tool_name, context) - Default: Deny unknown tools (fail-safe)


2. Memory Manager (src/kagura/core/memory/)

Components: - manager.py - Main coordinator - working.py - In-memory temporary storage - persistent.py - SQLite-based long-term storage - rag.py - ChromaDB vector search - export.py - JSONL export/import

Storage Scopes: - Working: Session-only, cleared after use - Persistent: Survives restarts, SQLite storage - Both: Indexed in RAG for semantic search


3. Graph Memory (src/kagura/core/graph/)

Implementation: NetworkX-based

Node Types: - user - User profiles - topic - Discussion topics - memory - Memory references - interaction - AI-User interactions

Edge Types: - related_to - Related memories - depends_on - Dependencies - learned_from - Learning relationships - works_on - User activities

Storage: Pickle files (~/.kagura/graph.pkl)


4. REST API (src/kagura/api/)

Framework: FastAPI

Endpoints: - /api/v1/memory - Memory CRUD - /api/v1/recall - Semantic search - /api/v1/search - Full-text search - /api/v1/graph/* - Graph operations - /api/v1/health - Health check - /api/v1/metrics - System metrics - /mcp - MCP over HTTP/SSE ⭐ NEW

Authentication: - File: src/kagura/api/auth.py - Method: Bearer token (API keys) - Storage: SQLite (~/.kagura/api_keys.db) - Hashing: SHA256


πŸ”„ Data Flow

Memory Store Flow

1. MCP Client (Claude/ChatGPT)
   └─► MCP Tool Call: memory_store(...)

2. MCP Server (stdio or HTTP/SSE)
   └─► Route to tool_registry

3. Built-in Tool (src/kagura/mcp/builtin/memory.py)
   └─► Call MemoryManager.store()

4. Memory Manager
   β”œβ”€β–Ί Working memory (if scope="working")
   β”œβ”€β–Ί Persistent memory (if scope="persistent")
   └─► RAG indexing (both scopes)

5. Storage
   β”œβ”€β–Ί SQLite (persistent)
   β”œβ”€β–Ί ChromaDB (vectors)
   └─► In-memory dict (working)

Memory Recall Flow

1. MCP Tool Call: memory_recall(query="Python tips", k=5)

2. Memory Manager
   └─► Query RAG (vector similarity)

3. RAG Search
   β”œβ”€β–Ί Embed query (text-embedding-3-small)
   β”œβ”€β–Ί Search ChromaDB collections
   └─► Return top-k results

4. Return to client
   └─► Formatted results with scores

πŸ” Security Architecture

Authentication Flow

1. Client Request
   └─► Authorization: Bearer kagura_abc123...

2. API Gateway (/mcp or /api/v1/*)
   └─► Extract Bearer token

3. API Key Manager (src/kagura/api/auth.py)
   β”œβ”€β–Ί Hash provided key (SHA256)
   β”œβ”€β–Ί Query api_keys.db
   β”œβ”€β–Ί Check expiration & revocation
   └─► Extract user_id

4. Request Processing
   └─► Use authenticated user_id for memory operations

Tool Filtering (Remote Context)

1. create_mcp_server(context="remote")

2. handle_list_tools()
   β”œβ”€β–Ί Get all registered tools (31 total)
   β”œβ”€β–Ί Filter by TOOL_PERMISSIONS
   └─► Return safe tools only (24)

3. Client sees:
   βœ… memory_* tools
   βœ… web_* tools
   ❌ file_* tools (blocked)
   ❌ shell_exec (blocked)

πŸ’Ύ Data Model

Memory Record

{
    "key": str,                  # Unique identifier
    "value": Any,                # Stored data (JSON serializable)
    "user_id": str,              # Owner (v4.0+)
    "agent_name": str,           # Agent scope
    "scope": "working|persistent",
    "tags": List[str],           # Categorization
    "importance": float,         # 0.0-1.0
    "created_at": datetime,
    "updated_at": datetime,
    "metadata": Dict[str, Any]   # Additional metadata
}

Graph Node

{
    "id": str,                   # Node identifier
    "type": str,                 # Node type (user, topic, memory, interaction)
    "data": Dict[str, Any],      # Node attributes
}

Graph Edge

{
    "src": str,                  # Source node ID
    "dst": str,                  # Destination node ID
    "type": str,                 # Relationship type
    "weight": float,             # 0.0-1.0
}

πŸ“Š Deployment Architecture

Local Development

Developer Machine
β”œβ”€β”€ SQLite (~/.kagura/memory.db)
β”œβ”€β”€ ChromaDB (~/.kagura/chromadb/)
β”œβ”€β”€ Graph pickle (~/.kagura/graph.pkl)
└── API Keys (~/.kagura/api_keys.db)

Production Deployment

Docker Stack (docker-compose.prod.yml)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚            Caddy (Port 443)             β”‚
β”‚     Automatic HTTPS, Reverse Proxy      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       Kagura API (Port 8080)            β”‚
β”‚    FastAPI + MCP over HTTP/SSE          β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                      β”‚
β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   PostgreSQL    β”‚   β”‚     Redis       β”‚
β”‚   + pgvector    β”‚   β”‚   (Caching)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Volumes:
β”œβ”€β”€ postgres_data  - Database persistence
β”œβ”€β”€ redis_data     - Redis persistence
β”œβ”€β”€ kagura_data    - Memory exports, etc.
└── caddy_data     - SSL certificates

πŸ”„ Export/Import System

Export Format (JSONL)

backup/
β”œβ”€β”€ memories.jsonl      # All memory records
β”œβ”€β”€ graph.jsonl         # Graph nodes & edges
└── metadata.json       # Export metadata

Example record:

{"type":"memory","scope":"persistent","key":"python_tips","value":"Use type hints","user_id":"jfk","agent_name":"global","tags":["python"],"importance":0.8,"exported_at":"2025-10-27T10:00:00Z"}


πŸ“ Design Principles

1. MCP-First

All functionality exposed via MCP tools first, then REST API.

2. Multi-User from Day 1

All operations scoped by user_id (Phase C foundation).

3. Security by Default

Remote access auto-filtered for safety.

4. Data Portability

Complete export/import in human-readable JSONL.

5. Fail-Safe

Unknown tools denied by default in remote context.



Last Updated: 2025-10-27 Version: 4.0.0 Phase: C Complete