Memory MCP Server
# Memory MCP Server
A Model Context Protocol (MCP) server providing dynamic short-term and long-term memory management with Chinese language support.
<a href="https://glama.ai/mcp/servers/@win10ogod/memory-mcp-server">
<img width="380" height="200" src="https://glama.ai/mcp/servers/@win10ogod/memory-mcp-server/badge" alt="Memory Server MCP server" />
</a>
## 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
```bash
# 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`
```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:**
```json
{
"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:**
```javascript
// 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
```bash
# 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:**
```javascript
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](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](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:
```bash
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](https://github.com/steve02081504/GentianAphrodite) project.
TDQS
Scored across 21 tools
Most tools have distinct purposes targeting specific memory types (long-term vs. short-term) or operations (add, delete, search, backup), but some overlap exists: 'search_memories' and 'search_short_term_memories' could be confused, and 'get_metrics' and 'health_check' both relate to performance monitoring. Descriptions help clarify, but boundaries are not perfectly clear.
The naming is mixed with no consistent pattern: some use verb_noun (e.g., 'add_long_term_memory', 'delete_short_term_memories'), others use noun_verb (e.g., 'health_check'), and there's a mix of English and Chinese names. While readable, the conventions vary significantly across the tool set.
With 21 tools, the count is slightly high but reasonable for a memory management server covering operations like CRUD, search, backup, and analytics. It feels comprehensive without being overly bloated, though it could be streamlined by merging some overlapping tools.
The tool set provides complete coverage for memory management: CRUD operations for both long-term and short-term memories, search functionalities, backup/restore, cleanup, and analytics (e.g., stats, patterns). No obvious gaps exist; agents can handle the full lifecycle of memories effectively.