Skip to main content
Glama

Memory MCP Server

A Model Context Protocol (MCP) server providing dynamic short-term and long-term memory management with Chinese language support.

Overview

This MCP server implements a sophisticated memory system extracted from the GentianAphrodite project, offering:

  • Short-term Memory: Keyword-based, time-decayed dynamic memory with relevance scoring

  • Long-term Memory: Trigger-based permanent memories with JS code execution for flexible activation

  • Chinese Language Support: Built-in jieba segmentation for optimal Chinese text processing

  • Multiple Conversations: Isolated memory spaces per conversation ID

Features

Short-term Memory

  • ๐Ÿ” Keyword Extraction: Uses TF-IDF with jieba for Chinese text

  • โฐ Time Decay: Exponential time decay model for memory relevance

  • ๐Ÿ“Š Relevance Scoring: Dynamic scoring based on keyword matching, time, and activation history

  • ๐ŸŽฒ Smart Selection: Three-tier selection (top relevant, next relevant, random flashback)

  • ๐Ÿงน Auto Cleanup: Automatic removal of old or irrelevant memories (configurable)

  • ๐Ÿ–ผ๏ธ Image Memory: Optional image embeddings for visual similarity search

Long-term Memory

  • ๐ŸŽฏ Trigger Conditions: JavaScript code execution for flexible memory activation

  • ๐Ÿ”’ Sandboxed Execution: Using Node.js built-in vm module for secure JS code evaluation

  • ๐ŸŽฐ Random Recall: Serendipitous memory activation for context enrichment

  • ๐Ÿ“ Context Tracking: Records creation and update contexts

  • ๐Ÿ–ผ๏ธ Multimodal Support: Images, audio, and custom embeddings

Data Optimization

  • ๐Ÿ“‰ Space Saving: 30-40% reduction in storage size

  • ๐Ÿ”„ Auto Deduplication: Removes duplicate keywords and images

  • โฑ๏ธ Timestamp Normalization: Unified timestamp format (ISO 8601)

  • ๐Ÿ—œ๏ธ Smart Compression: Eliminates redundant attachments field

Performance & Reliability (NEW!)

  • โšก Query Caching: 30-50% faster searches with intelligent result caching

  • โฑ๏ธ Timeout Protection: Prevents long-running operations from blocking the server

  • ๐Ÿšฆ Rate Limiting: Protects against API abuse (100 requests/minute per conversation)

  • ๐Ÿ›ก๏ธ Input Validation: Comprehensive sanitization and validation of all inputs

  • ๐Ÿ“ Structured Logging: JSON-formatted logs for easy parsing and monitoring

  • ๐Ÿ“Š Performance Metrics: Real-time metrics collection (latency, error rates, cache hits)

  • ๐Ÿ’š Health Monitoring: Built-in health checks for proactive issue detection

  • ๐Ÿ” Audit Logging: Complete audit trail of all operations for compliance

  • ๐Ÿ”„ Graceful Shutdown: Ensures all pending writes complete before shutdown

New Features (NEW!)

  • ๐Ÿ’พ Backup & Restore: Export and import entire memory databases

    • backup_memories: Create timestamped backups with metadata

    • restore_memories: Restore from backup with merge or overwrite modes

    • list_backups: Browse available backup files

    • delete_backup: Clean up old backups

  • ๐Ÿ”Ž Advanced Search: Powerful search with flexible filtering

    • search_memories: Search with keywords, date ranges, score filters

    • analyze_memory_patterns: Statistical analysis and insights

    • Support for sorting by relevance, score, or timestamp

    • Search across short-term, long-term, or both memory types

  • ๐Ÿ“ˆ System Monitoring: Real-time server monitoring tools

    • health_check: Get server health status and diagnostics

    • get_metrics: View performance metrics (P50/P95/P99 latency, error rates)

    • get_cache_stats: Monitor query cache hit rates

Installation

# Clone or download this directory cd memory-mcp-server # Install dependencies npm install # Make the server executable (Unix/Linux/Mac) chmod +x src/index.js

Usage

With Claude Desktop

Add to your Claude Desktop configuration file:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json

{ "mcpServers": { "memory": { "command": "node", "args": ["/absolute/path/to/memory-mcp-server/src/index.js"] } } }

With Cursor or other MCP clients

Configure according to your client's MCP server setup instructions, pointing to src/index.js.

MCP Features

This server implements the full Model Context Protocol specification with:

  • Tools: 13 tools for memory management

  • Resources: 4 resources for system inspection

  • Prompts: 4 prompt templates for common memory tasks

Available Tools

Short-term Memory Tools

add_short_term_memory

Add a new short-term memory from conversation messages.

Parameters:

  • messages (array): Recent conversation messages with role and content

  • conversation_id (string): Unique conversation identifier

  • roleWeights (object, optional): Custom weights for different roles

Example:

{ "messages": [ {"role": "user", "content": "My birthday is July 17, 1990"}, {"role": "assistant", "content": "I'll remember that!"} ], "conversation_id": "user_123", "roleWeights": { "user": 2.7, "assistant": 2.0, "system": 1.0 } }

search_short_term_memories

Search for relevant memories based on current context.

Parameters:

  • recentMessages (array): Recent messages to search against

  • conversation_id (string): Current conversation ID

  • roleWeights (object, optional): Role weights

Returns: Top relevant, next relevant, and random flashback memories

delete_short_term_memories

Delete memories matching a pattern.

Parameters:

  • pattern (string): Keyword or regex pattern (e.g., "/pattern/i")

  • conversation_id (string): Conversation ID

get_memory_stats

Get statistics about short-term memories.

cleanup_memories

Manually trigger memory cleanup (removes old/low-relevance memories).

get_frequent_conversation

Get the most frequently mentioned conversation ID.

Long-term Memory Tools

add_long_term_memory

Add a permanent memory with a trigger condition.

Parameters:

  • name (string): Unique memory name

  • prompt (string): Memory content

  • trigger (string): JavaScript code for activation condition

  • conversation_id (string, optional): Conversation ID to store the memory under (defaults to "default")

  • createdContext (string, optional): Context description

  • recentMessages (array, optional): Auto-generate context from messages

Trigger Examples:

// Activate when "birthday" is mentioned "match_keys(context.messages, ['birthday', '็”Ÿๆ—ฅ'], 'any')" // Activate on specific date or when mentioned "match_keys(context.messages, ['anniversary'], 'any') || (new Date().getMonth() === 6 && new Date().getDate() === 17)" // Multiple keywords required "match_keys_all(context.messages, ['project', 'deadline'], 'user')"

Available in trigger context:

  • context.messages: Recent message array

  • context.conversation_id: Current conversation ID

  • context.participants: Participant information

  • match_keys(messages, keywords, scope, depth): Match any keyword

  • match_keys_all(messages, keywords, scope, depth): Match all keywords

  • Date, Math, RegExp, JSON: Safe built-in objects

update_long_term_memory

Update an existing long-term memory.

Parameters:

  • name (string): Memory name to update

  • trigger (string, optional): New trigger condition

  • prompt (string, optional): New content

  • conversation_id (string, optional): Conversation ID that owns the memory

  • updatedContext (string, optional): Update context

delete_long_term_memory

Delete a long-term memory by name.

Parameters:

  • name (string): Memory name to delete

  • conversation_id (string, optional): Conversation ID that owns the memory

list_long_term_memories

List all long-term memories with basic info.

Parameters:

  • conversation_id (string, optional): Conversation ID to inspect (defaults to "default")

search_long_term_memories

Search and activate memories based on current context.

Parameters:

  • messages (array): Recent conversation messages

  • conversation_id (string): Current conversation ID

  • participants (object, optional): Participant info

Returns: Activated memories (triggered) and random memories

get_memory_context

Get creation and update context of a specific memory.

Parameters:

  • name (string): Memory name to inspect

  • conversation_id (string, optional): Conversation ID that owns the memory

Available Resources

MCP resources allow AI to inspect the memory system state:

memory://stats/overview

System-wide overview and health status.

Returns:

  • Total conversation count

  • System health status

  • Available features

memory://conversations/list

List all conversations with memory statistics.

Returns:

  • Conversation IDs

  • Short-term memory counts

  • Long-term memory counts

memory://stats/conversation/{id}

Detailed statistics for a specific conversation.

Parameters:

  • {id}: Conversation ID to inspect

Returns:

  • Short-term memory: total, scores, age ranges

  • Long-term memory: total, update counts, timestamps

memory://guide/best-practices

Comprehensive guide on using the memory system effectively.

Returns:

  • Best practices for short-term and long-term memory

  • Trigger condition examples

  • Multimodal support guidelines

  • Common usage patterns

Available Prompts

MCP prompts provide guided workflows for common tasks:

remember-user-info

Store important user information in long-term memory.

Arguments:

  • info_type (required): Type of information (preference, birthday, fact, etc.)

  • information (required): The information to remember

  • conversation_id (optional): Target conversation ID

Guides AI to:

  1. Create appropriate memory name

  2. Generate relevant trigger conditions

  3. Use add_long_term_memory tool

recall-context

Search for relevant memories based on current conversation.

Arguments:

  • current_topic (required): Current topic or question

  • conversation_id (optional): Conversation to search

Guides AI to:

  1. Search short-term memories for recent context

  2. Search long-term memories for permanent facts

  3. Consider keyword relevance and time decay

create-reminder

Create a conditional reminder that activates based on context or date.

Arguments:

  • reminder_content (required): What to remind about

  • trigger_condition (required): When to trigger (keywords or date)

  • conversation_id (optional): Target conversation

Guides AI to:

  1. Convert natural language conditions to JavaScript

  2. Create date-based or keyword-based triggers

  3. Use add_long_term_memory with proper trigger

analyze-conversation

Analyze conversation history and suggest what should be remembered.

Arguments:

  • conversation_id (required): Conversation to analyze

Guides AI to:

  1. Get current memory statistics

  2. Identify important information types

  3. Categorize for short-term vs long-term storage

  4. Create appropriate memory entries

Architecture

memory-mcp-server/ โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ index.js # MCP server entry point โ”‚ โ”œโ”€โ”€ memory/ โ”‚ โ”‚ โ”œโ”€โ”€ short-term.js # Short-term memory logic โ”‚ โ”‚ โ”œโ”€โ”€ long-term.js # Long-term memory logic โ”‚ โ”‚ โ”œโ”€โ”€ storage.js # JSON file storage with caching โ”‚ โ”‚ โ””โ”€โ”€ modalities.js # Multimodal attachment handling โ”‚ โ”œโ”€โ”€ nlp/ โ”‚ โ”‚ โ”œโ”€โ”€ jieba.js # Chinese segmentation โ”‚ โ”‚ โ””โ”€โ”€ keywords.js # Keyword matching โ”‚ โ”œโ”€โ”€ triggers/ โ”‚ โ”‚ โ””โ”€โ”€ matcher.js # JS code execution sandbox (Node.js vm) โ”‚ โ”œโ”€โ”€ tools/ โ”‚ โ”‚ โ”œโ”€โ”€ short-term-tools.js # Short-term MCP tools โ”‚ โ”‚ โ””โ”€โ”€ long-term-tools.js # Long-term MCP tools โ”‚ โ”œโ”€โ”€ resources/ โ”‚ โ”‚ โ””โ”€โ”€ index.js # MCP resources (stats, guides) โ”‚ โ”œโ”€โ”€ prompts/ โ”‚ โ”‚ โ””โ”€โ”€ index.js # MCP prompts (workflows) โ”‚ โ””โ”€โ”€ utils/ โ”‚ โ”œโ”€โ”€ lru-cache.js # LRU cache for managers โ”‚ โ””โ”€โ”€ zod-to-json-schema.js โ””โ”€โ”€ data/ # Memory storage (auto-created) โ””โ”€โ”€ {conversation_id}/ โ”œโ”€โ”€ short-term-memory.json โ””โ”€โ”€ long-term-memory.json

Memory Algorithms

Short-term Memory Relevance

relevance = keyword_match_score - time_penalty + memory_score where: keyword_match_score = ฮฃ(current_kw.weight + memory_kw.weight) time_penalty = 15 * (1 - e^(-time_diff * 2e-9)) memory_score = accumulated score from past activations

Selection Strategy

  1. Top Relevant (max 2): Highest relevance scores

  2. Next Relevant (max 1): Next highest scores

  3. Random Flashback (max 2): Weighted random from remaining memories

Filtering:

  • Excludes same-conversation memories from last 20 minutes

  • Excludes memories within 10 minutes of any selected memory

  • Ensures diversity in recalled memories

Cleanup Policy

  • Triggers every 24 hours

  • Removes memories older than 1 year

  • Removes low-relevance memories (score < -5)

  • Always keeps at least 512 memories

Development

# Run in development mode with auto-reload npm run dev # Run normally npm start

Security

  • Sandboxed Execution: Long-term memory triggers run in Node.js built-in vm module sandbox with timeout protection

  • No File System Access: Trigger code cannot access filesystem (sandboxed)

  • No Network Access: Trigger code cannot make network requests

  • Timeout Protection: 1-second execution timeout prevents infinite loops

  • Secure Context: Only safe built-in objects are exposed to trigger code

Note: The built-in vm module provides good isolation for most use cases. For maximum security in production environments, consider running the MCP server in a containerized environment with additional restrictions.

Limitations

  • Memory storage is file-based (JSON), suitable for moderate usage

  • Trigger execution has 1-second timeout

  • Manager instances cached with LRU (max 100 conversations, 30-min idle timeout)

  • Chinese text processing optimized (may be less optimal for other languages)

Performance Optimizations

  • Write Caching: Delayed writes with 1-second batching to reduce disk I/O

  • Directory Caching: Directory existence checks are cached to avoid repeated file system calls

  • LRU Manager Cache: Automatic cleanup of inactive conversation managers prevents memory leaks

  • Retry Logic: File operations automatically retry with exponential backoff on transient errors

  • Graceful Shutdown: Pending writes are flushed and resources cleaned up on shutdown signals

  • Data Deduplication: Automatic removal of duplicate images and keywords (30-40% space savings)

  • Timestamp Normalization: Unified timestamp format eliminates redundancy

Image Memory Features

The server includes optional image memory capabilities:

  • Image Modalities: Store images with memories using embeddings, tags, and descriptions

  • Similarity Search: Find visually similar memories using cosine similarity on embeddings

  • Auto Deduplication: Automatically detect and remove duplicate images (URL or content hash)

  • Flexible Embeddings: Support for CLIP, ResNet, or custom image embeddings

  • Base64 Support: Handle both URLs and data URI images

Example:

import { createImageModality } from './src/utils/image-processor.js'; const imageMemory = createImageModality({ uri: 'https://example.com/photo.jpg', embedding: [0.1, 0.2, 0.3, ...], // 512-d vector from CLIP/etc tags: ['vacation', 'beach'], description: 'Sunset at the beach' }); // Use in memory creation await addShortTermMemory(messages, conversationId, { modalities: [imageMemory] });

See docs/IMAGE_MEMORY.md for detailed guide.

Data Optimization

Built-in data optimization reduces storage by 30-40%:

  • Timestamp Normalization: time_stamp/timeStamp/timestamp โ†’ timestamp (ISO 8601)

  • Remove Redundancy: attachments field removed (use modalities only)

  • Keyword Deduplication: Case-insensitive merge with max weight retention

  • Image Deduplication: Remove duplicate images based on URI or content hash

All optimizations are applied automatically during storage. See docs/DATA_OPTIMIZATION.md for details.

Performance Benchmarks

Performance improvements from the latest enhancements:

Feature

Improvement

Details

Query Caching

30-50% faster

Caches 50 most recent queries for 5 minutes

Vector Similarity

40-60% faster

Pre-computed magnitude caching

Rate Limiting

Protection

100 requests/minute per conversation

Timeout Protection

Reliability

All operations timeout after 5-30s

Data Storage

30-40% smaller

Deduplication and normalization

Health Checks

Proactive

Memory, error rate, cache monitoring

Audit Logging

Complete

All operations logged with timestamps

Test Results

Run the comprehensive test suite:

node test-improvements.js

Expected output: 36 tests pass covering:

  • Query caching (4 tests)

  • Timeout handling (3 tests)

  • Rate limiting (5 tests)

  • Input validation (8 tests)

  • Structured logging (3 tests)

  • Performance metrics (6 tests)

  • Health checks (4 tests)

  • Audit logging (3 tests)

Architecture

src/ โ”œโ”€โ”€ memory/ # Core memory management โ”‚ โ”œโ”€โ”€ short-term.js # Dynamic keyword-based memory โ”‚ โ”œโ”€โ”€ long-term.js # Trigger-based permanent memory โ”‚ โ””โ”€โ”€ storage.js # JSON file persistence โ”œโ”€โ”€ tools/ # MCP tool implementations โ”‚ โ”œโ”€โ”€ short-term-tools.js โ”‚ โ”œโ”€โ”€ long-term-tools.js โ”‚ โ”œโ”€โ”€ backup-tools.js # NEW: Backup/restore โ”‚ โ””โ”€โ”€ search-tools.js # NEW: Advanced search โ”œโ”€โ”€ utils/ # Utility modules โ”‚ โ”œโ”€โ”€ query-cache.js # NEW: Query result caching โ”‚ โ”œโ”€โ”€ timeout.js # NEW: Timeout handling โ”‚ โ”œโ”€โ”€ logger.js # NEW: Structured logging โ”‚ โ””โ”€โ”€ lru-cache.js # LRU cache implementation โ”œโ”€โ”€ security/ # NEW: Security features โ”‚ โ”œโ”€โ”€ rate-limiter.js # API rate limiting โ”‚ โ”œโ”€โ”€ input-validator.js # Input sanitization โ”‚ โ””โ”€โ”€ audit-log.js # Audit trail logging โ”œโ”€โ”€ monitoring/ # NEW: Monitoring features โ”‚ โ””โ”€โ”€ metrics.js # Performance metrics โ”œโ”€โ”€ health/ # NEW: Health checks โ”‚ โ””โ”€โ”€ index.js # Health monitoring โ”œโ”€โ”€ resources/ # MCP resources โ”‚ โ””โ”€โ”€ index.js โ””โ”€โ”€ prompts/ # MCP prompts โ””โ”€โ”€ index.js

License

BSD-3-Clause license

Credits

Extracted and generalized from the GentianAphrodite project.

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/win10ogod/memory-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server