Skip to content

kensave/agent-memory-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

59 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Agent Memory RS

Agent Memory RS Logo

Episodic memory system for AI agents with vector search, exposed via MCP server.

Verified Performance: 65.9% R@10 on LoCoMo benchmark β€’ Up to 74% on long conversations

Note: This project was developed using Kiro CLI - an AI-powered development assistant.

Rust License

Overview

Agent Memory RS stores interaction episodes with vector embeddings and retrieves them using cosine similarity search. Exposed as an MCP server with learn and search tools.

Features

  • Episode Storage β€” Events stored with vector embeddings (BGE-Small, 384 dims)
  • Vector Search β€” Cosine distance retrieval on episode embeddings
  • BM25 Search β€” Keyword search with proper IDF calculation
  • MCP Server β€” Learn and search tools via Model Context Protocol (stdio)
  • Workspace Isolation β€” Separate SQLite databases per workspace (~/.memory-rs/workspaces/)
  • Multiple Models β€” BGE-Small (default), Nomic (long context), MiniLM (fastest)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       MCP Client        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚ stdio (JSON-RPC)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      MCP Server         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  learn  β”‚ β”‚ search β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”˜
β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
β”‚   EpisodicMemoryStore   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ SQLite + vec0     β”‚   β”‚
β”‚  β”‚ (episodes table)  β”‚   β”‚
β”‚  β”‚ (vector index)    β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Installation

git clone https://github.com/yourusername/agent-memory-rs
cd agent-memory-rs
cargo build --release

MCP Server (Recommended)

# Start the server
./target/release/agent-memory-mcp my-workspace

Configure your AI assistant:

{
  "mcpServers": {
    "agent-memory": {
      "command": "/path/to/agent-memory-mcp",
      "args": ["my-workspace"],
      "env": {
        "MEMORY_MODEL": "bge"
      }
    }
  }
}

Configuration Options:

Environment Variable Values Default Description
MEMORY_MODEL bge, nomic, minilm bge Embedding model to use

Model Selection:

  • bge (BGE-Small) - Best quality/speed balance, 384 dims, ~33MB ⭐ Recommended
  • nomic (Nomic Embed) - Best for long context (8K tokens), 768 dims, ~138MB
  • minilm (MiniLM) - Fastest, 384 dims, ~23MB

Available MCP Tools:

  • @memory/learn - Store new memories
  • @memory/search - Search across all memory types

Data Storage

Workspace Isolation: Each workspace has its own isolated database. Memories are NOT shared between workspaces.

Database Location:

~/.memory-rs/workspaces/
β”œβ”€β”€ prime-sde-workspace/
β”‚   └── memory.db          # All memories for this workspace
β”œβ”€β”€ my-project/
β”‚   └── memory.db          # Separate isolated memories
└── default/
    └── memory.db          # Default workspace

Workspace Naming:

  • Specified in MCP server args: ["workspace-name"]
  • If no arg provided, auto-generates from current directory: <hash>-<dirname>
    • Example: /path/to/workspace/myproject β†’ a1b2c3d4-myproject
    • Hash ensures uniqueness across different paths with same directory name
  • Falls back to "default" if directory name unavailable

Data Persistence:

  • βœ… Survives Kiro restarts (stored in home directory)
  • βœ… Survives repo deletion (not stored in repo)
  • ❌ Deleting ~/.memory-rs/ loses all memories
  • ❌ No cross-workspace knowledge sharing (by design)

Model Cache: Models are downloaded once and cached in the standard HuggingFace cache:

~/.cache/huggingface/hub/
β”œβ”€β”€ models--BAAI--bge-small-en-v1.5/
β”œβ”€β”€ models--nomic-ai--nomic-embed-text-v1/
└── models--sentence-transformers--all-MiniLM-L6-v2/

CLI Usage

# Create workspace
cargo run --bin agent-memory-cli workspace create --name my-project --path /path/to/project

# List workspaces
cargo run --bin agent-memory-cli workspace list

# Store episode
cargo run --bin agent-memory-cli store --workspace 1 --type user_query --context "How do I use Rust?" --outcome "Provided tutorial" --valence 0.8

# Query memories
cargo run --bin agent-memory-cli query --workspace 1 "rust programming" --limit 10

# Check system health
cargo run --bin agent-memory-cli stats --workspace 1

Programmatic Usage

use agent_memory_rs::services::MemoryManager;
use agent_memory_rs::storage::Database;

// Initialize
let db = Database::new("memory.db")?;
let manager = MemoryManager::new(db.clone());

// Store episode
manager.store_episode(
    1, // workspace_id
    "user_query",
    serde_json::json!({"query": "How do I use Rust?"}),
    Some("Provided Rust tutorial"),
    Some(0.8), // positive valence
)?;

// Search memories
let results = manager.retrieve("rust programming", 1, 10)?;

πŸ“š Documentation

πŸŽ“ Agent Skill

The repository includes a skill for AI agents using Kiro CLI:

Location: skill/agent-memory/SKILL.md

Add to your agent configuration:

{
  "resources": [
    "skill:///path/to/agent-memory-rs/skill/agent-memory/SKILL.md"
  ]
}

What the skill provides:

  • When to use @memory/learn vs @memory/search
  • Best practices for memory management
  • Importance scoring and tagging strategies
  • Workflow patterns for common scenarios
  • Configuration options and troubleshooting

The skill is loaded on-demand, providing guidance only when needed without consuming context at startup.

πŸ—οΈ Architecture

MemoryManager (Facade)
    β”œβ”€β”€ EpisodicMemoryStore      - Raw interaction events
    └── HybridRetrievalEngine    - BM25 + Vector search

Built with SOLID principles:

  • Core traits (MemoryStore, MemoryRetriever, EmbeddingService)
  • Dependency injection throughout
  • Thread-safe Database pattern: Arc<Mutex<Connection>>

πŸ§ͺ Testing

# Run all tests
cargo test

# Run integration tests only
cargo test --test '*'

# Run with output
cargo test -- --nocapture

Test Coverage: 29 tests covering full lifecycle

πŸ“Š Performance

  • Episode Storage: ~5ms
  • Hybrid Search: ~20ms (10k memories)

πŸ”¬ Research Foundation

Based on modern AI agent memory research:

  1. Memory Management for Long-Running Agents (2025, arXiv:2509.25250v1)
  2. Episodic Memory for RAG (2024, arXiv:2511.07587v1)
  3. MIRIX Multi-Agent Memory (2024)
  4. Episodic Memory Properties (2025, arXiv:2502.06975v1)
  5. Procedural Memory Is Not All You Need (2025, arXiv:2505.03434v1)

See Design Rationale for complete references.

πŸ› οΈ Technology Stack

  • Language: Rust 1.70+
  • Database: SQLite with sqlite-vec extension
  • Embeddings: BERT MiniLM (384 dimensions) via Candle
  • Vector Search: Cosine distance with HNSW-like indexing
  • Interface: MCP (Model Context Protocol)

πŸ“ License

Licensed under either of:

at your option.

🀝 Contributing

Contributions welcome! Please read our contributing guidelines first.

πŸ™ Acknowledgments

Inspired by cognitive science research on human memory systems and modern AI agent architectures.

Prune old memories

memory-cli prune --workspace 1 --dry-run


## πŸ§ͺ Testing

```bash
# Run all tests
cargo test

# Run integration tests only
cargo test --test '*'

# Run with output
cargo test -- --nocapture

Test Coverage: 29 tests covering full lifecycle

πŸ”§ Development

Project Structure

src/
β”œβ”€β”€ services/          # 6 core services
β”œβ”€β”€ storage/           # Database and memory store
β”œβ”€β”€ traits/            # 5 SOLID traits
β”œβ”€β”€ models/            # DTOs and types
β”œβ”€β”€ cli/               # CLI commands
└── mcp/               # MCP server

tests/                 # 16 integration test files
docs/                  # 5 documentation files

Building

# Development build
cargo build

# Release build (optimized)
cargo build --release

# Build MCP server only
cargo build --bin memory-rs-mcp --release

πŸ“Š Performance

  • Episode storage: ~5ms
  • Hybrid search: ~20ms (1000 memories)
  • All operations: Non-blocking

🀝 Contributing

  1. Follow SOLID principles
  2. Write minimal, focused code
  3. Add tests for new features
  4. Update documentation
  5. Run cargo test before committing

πŸ“ License

MIT OR Apache-2.0

πŸ™ Acknowledgments

Built with:

  • Rust πŸ¦€
  • SQLite + sqlite-vec
  • Candle (ML framework)
  • MCP Protocol

Status: Production-ready βœ… Tests: 44 passing βœ… Documentation: Complete βœ… }


#### Search (Query Memories)

```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "search",
    "arguments": {
      "query": "programming languages",
      "workspace_id": 1,
      "limit": 5
    }
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "results": [
      {
        "memory_id": 42,
        "text": "Rust is a systems programming language...",
        "similarity_score": 0.92,
        "combined_score": 0.88,
        "importance_score": 0.8,
        "tags": "rust,programming",
        "created_at": "2026-01-30T22:00:00Z"
      }
    ],
    "count": 1
  }
}

πŸ“š Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         CLI Tool                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚ stdio (JSON-RPC 2.0)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      MCP Server                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                     β”‚
β”‚  β”‚  Learn Tool    β”‚  β”‚  Search Tool   β”‚                     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Memory System                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”‚
β”‚  β”‚  FastEmbedder    β”‚  β”‚  Memory Store    β”‚                β”‚
β”‚  β”‚  (MiniLM/Nomic)  β”‚  β”‚  (SQLite+vec)    β”‚                β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Workspace Manager                               β”‚
β”‚  ~/.memory-rs/workspaces/                                    β”‚
β”‚    β”œβ”€β”€ project-a/memory.db                                   β”‚
β”‚    β”œβ”€β”€ project-b/memory.db                                   β”‚
β”‚    └── project-c/memory.db                                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Components

  1. Storage Layer (src/storage/)

    • schema.rs: Database schema with sqlite-vec integration
    • memory_store.rs: CRUD operations and vector search
  2. Memory System (src/memory_system.rs)

    • High-level API combining embedder and storage
    • Atomic learn and search operations
  3. MCP Server (src/mcp/)

    • server.rs: JSON-RPC 2.0 stdio transport
    • tools.rs: Learn and Search tool implementations
  4. Workspace Manager (src/workspace.rs)

    • Multi-database support
    • Workspace isolation and management
  5. Embedder (src/embedder.rs)

    • FastEmbedder with multiple model support
    • Mock fallback for testing

πŸ”§ Configuration

Embedding Models

Choose your embedding model based on your needs:

Model Dimensions Speed Quality
MiniLM 384 Fast Good
BGE-small 384 Medium Better
Nomic 768 Slower Best

Configure in code:

use memory_rs::{WorkspaceManager, ModelType};

let manager = WorkspaceManager::new(ModelType::BgeSmall)?;

Workspace Management

Workspaces are stored in ~/.memory-rs/workspaces/ by default:

use memory_rs::WorkspaceManager;

let manager = WorkspaceManager::new(ModelType::MiniLM)?;

// Create or get workspace
let system = manager.get_or_create_workspace("my-project")?;

// List all workspaces
let workspaces = manager.list_workspaces()?;

// Delete workspace
manager.delete_workspace("old-project")?;

πŸ§ͺ Testing

Run all tests:

cargo test

Run specific test suites:

# Storage tests
cargo test --lib storage

# MCP server tests
cargo test --lib mcp

# Workspace tests
cargo test --lib workspace

πŸ“Š Database Schema

Tables

workspaces

  • id: Primary key
  • name: Workspace name (unique)
  • path: Filesystem path
  • created_at: Timestamp

agents

  • id: Primary key
  • workspace_id: Foreign key to workspaces
  • name: Agent name
  • created_at: Timestamp

memories

  • id: Primary key
  • workspace_id: Foreign key to workspaces
  • agent_id: Optional foreign key to agents
  • text: Memory content
  • tags: Comma-separated tags
  • importance_score: Float 0-1
  • access_count: Usage tracking
  • last_accessed: Timestamp
  • conversation_id: Optional conversation grouping
  • parent_memory_id: Optional memory hierarchy
  • user_feedback: Optional feedback text
  • created_at, updated_at: Timestamps

vec0 (virtual table)

  • memory_id: Foreign key to memories
  • embedding: Float vector (384 or 768 dimensions)

Indexes

  • idx_memories_workspace: Fast workspace filtering
  • idx_memories_agent: Fast agent filtering
  • idx_memories_importance: Importance-based queries
  • idx_memories_created: Temporal queries
  • idx_memories_conversation: Conversation grouping

πŸ” Search Capabilities

Hybrid Search

Combines semantic similarity (70%) with importance score (30%):

use memory_rs::storage::SearchFilters;

let filters = SearchFilters {
    workspace_id: Some(1),
    agent_id: Some(5),
    min_importance: Some(0.5),
    max_importance: Some(1.0),
    conversation_id: Some("conv-123".to_string()),
    ..Default::default()
};

let results = system.search("query text", &filters, 10)?;

Filtering Options

  • workspace_id: Limit to specific workspace
  • agent_id: Limit to specific agent
  • min_importance / max_importance: Importance range
  • created_after / created_before: Date range
  • conversation_id: Conversation grouping
  • tags: Tag-based filtering (future)

🚦 MCP Protocol

Available Methods

  1. initialize: Server initialization
  2. tools/list: List available tools
  3. tools/call: Execute a tool
  4. learn: Store a memory (via tools/call)
  5. search: Query memories (via tools/call)

Tool Schemas

Learn Tool

Input Schema:

{
  "type": "object",
  "properties": {
    "text": {"type": "string", "description": "The text to remember"},
    "workspace_id": {"type": "integer", "description": "Workspace ID"},
    "agent_id": {"type": "integer", "description": "Optional agent ID"},
    "tags": {"type": "string", "description": "Optional comma-separated tags"},
    "importance_score": {"type": "number", "description": "Importance score 0-1"},
    "conversation_id": {"type": "string", "description": "Optional conversation ID"}
  },
  "required": ["text", "workspace_id"]
}

Search Tool

Input Schema:

{
  "type": "object",
  "properties": {
    "query": {"type": "string", "description": "Search query"},
    "workspace_id": {"type": "integer", "description": "Optional workspace ID filter"},
    "agent_id": {"type": "integer", "description": "Optional agent ID filter"},
    "min_importance": {"type": "number", "description": "Minimum importance score"},
    "max_importance": {"type": "number", "description": "Maximum importance score"},
    "conversation_id": {"type": "string", "description": "Optional conversation ID filter"},
    "limit": {"type": "integer", "description": "Maximum results (default 10, max 100)"}
  },
  "required": ["query"]
}

πŸŽ“ Examples

See examples/ directory for complete examples:

  • mcp_server.rs: Full MCP server implementation
  • More examples coming soon!

πŸ“ˆ Performance

  • Storage: SQLite with sqlite-vec for efficient vector operations
  • Embedding: ~300ms per embedding with real models, ~20ΞΌs with mock
  • Search: Sub-second for <10K memories, optimized for 100K+ scale
  • Memory: Efficient storage with optional quantization support

🀝 Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass: cargo test
  5. Submit a pull request

πŸ“ License

MIT OR Apache-2.0

πŸ™ Acknowledgments

  • sqlite-vec for vector search in SQLite
  • Candle for ML inference
  • Model Context Protocol by Anthropic

πŸ“ž Support


Built with ❀️ in Rust

About

Comprehensive memory management system for LLM agents implementing episodic, semantic, and procedural memory with automatic consolidation, intelligent decay, and hierarchical retrieval. Built in Rust with MCP server support for Kiro CLI

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors