Production-ready MCP memory server with semantic search, project conventions learning, and comprehensive memory management for OpenCode
Project description
OpenCode MCP Memory
A production-ready MCP (Model Context Protocol) memory server for OpenCode with semantic search, project conventions learning, and comprehensive memory management.
Features
- Semantic memory search via sentence-transformers with automatic text search fallback
- Multiple memory types: Working, semantic, episodic, procedural, plus domain-specific types
- Project convention learning: Auto-detect project type, tools, commands, dependencies
- Knowledge graph: Automatic relationship detection between related memories
- Duplicate detection: MD5-based content hashing to prevent redundant storage
- Configurable embeddings: Support for different sentence-transformers models
- Full-text search: SQLite FTS5 with fallback LIKE queries
- Comprehensive metrics: Track operation performance and success rates
- Input validation & security: Prevent path traversal and malicious inputs
Quick Start
The launcher automatically creates a .venv and installs dependencies on first run:
python mcp_memory/launcher.py
Or use the console entry point (after installation):
mcp-memory
Installation
From PyPI (recommended)
pip install opencode-mcp-memory
Then run directly:
mcp-memory
From Source (development)
# Clone and install in editable mode
git clone https://github.com/opencode/mcp-memory.git
cd mcp-memory
pip install -e .
# Run the server
mcp-memory
For OpenCode Integration
Add the server to your opencode config (opencode.json or opencode.jsonc):
{
"mcp": {
"opencode-memory": {
"type": "local",
"command": ["mcp-memory"]
}
}
}
Architecture
server.py- FastMCP server with 18 toolslauncher.py- Venv-aware launcher (creates.venvon first run)memory.py- Memory classification & semantic search with lazy loadingdatabase.py- SQLite3 backend with FTS5, transactions, and migrationsconventions.py- Project type detection & command learningclassification_config.py- Configurable keywords for memory classificationmetrics.py- Operation metrics and health monitoringvalidation.py- Input validation and security utilitiesexceptions.py- Custom exception typestypes.py- Response type models
Memory Types & Best Practices
Working Memory
Short-term focus and task state. Good for:
- Current task context
- Active feature being implemented
- Debugging session state
Example:
memory_manager.add_working_memory(
slot="current_feature",
value="Implementing user authentication with OAuth2"
)
Semantic Memory
Durable factual knowledge. Good for:
- Programming language features
- Framework best practices
- Architecture decisions
- Code patterns
Example:
memory_manager.add_semantic_memory(
content="Python's GIL prevents true parallelism in threads but asyncio enables concurrent I/O",
importance=0.9
)
Episodic Memory
Events that occurred. Good for:
- Bugs encountered and fixed
- Deployment events
- Meetings and decisions made
- Refactoring activities
Example:
memory_manager.add_episodic_memory(
content="Fixed critical memory leak in WebSocket handler by adding proper cleanup"
)
Procedural Memory
How to do something. Good for:
- Build and test commands
- Deployment procedures
- Development workflows
- Debugging techniques
Example:
memory_manager.add_procedural_memory(
content="To run tests: pytest --cov=src tests/; generates coverage report in htmlcov/",
importance=0.8
)
Domain-Specific Types
error: Bug reports, exceptions, failures code: Code snippets, algorithms, implementations decision: Architectural choices, approach selections pattern: Design patterns, code templates, conventions environment: OS and runtime configuration commands: Build, test, deploy scripts tools: Development tools and configurations deployment: Deployment procedures and platforms testing: Test frameworks and test procedures
Common Workflows
Initialize Project Memory
from mcp_memory.database import DatabaseManager
from mcp_memory.memory import MemoryManager
from mcp_memory.conventions import ProjectConventionLearner
db = DatabaseManager("project.db")
mem = MemoryManager(db)
conv = ProjectConventionLearner(mem, db)
# Start session for current project
mem.start_session(".")
# Learn all conventions
conventions = conv.auto_learn_project_conventions(".")
# Query context
context = mem.get_memory_context("authentication")
Search Memories
# Semantic search (with fallback to text)
results = mem.search_memories_semantic("How to handle errors", min_similarity=0.5)
# Text search
results = db.search_memories("error handling", project_id=mem.current_project_id, limit=10)
# Get memories by type
error_logs = db.get_memories(project_id=mem.current_project_id, memory_type="error", limit=20)
Store and Retrieve Context
# Add different memory types
mem.add_working_memory("current_task", "Optimize database queries")
mem.add_semantic_memory("SQL indexes improve query performance on large tables")
mem.add_episodic_memory("Performance improved 5x after adding composite index")
mem.add_procedural_memory("Run: ANALYZE queries.log to find slow queries")
# Get formatted context for AI
context = mem.get_memory_context(query="database optimization")
print(context)
Tools
Health & Monitoring (3 tools)
health_check()- Server status, database, embeddings, metricsget_database_stats()- Database statistics and sizesget_metrics()- Operation metrics and performance data
Memory Operations (5 tools)
add_memory()- Generic memory with type/importancesearch_memories()- Full-text searchsearch_semantic_memories()- Semantic search with fallbackget_memory_context()- Context for AI (includes conventions)get_project_summary()- Project statistics
Memory Helpers (4 tools)
add_working_memory()- Add working memoryadd_semantic_memory()- Add factual knowledgeadd_episodic_memory()- Add event recordadd_procedural_memory()- Add how-to knowledge
Conventions (3 tools)
auto_learn_project_conventions()- Scan project and learn conventionsget_project_conventions()- Get cached conventions for contextsuggest_correct_command()- Suggest project-specific commands
Maintenance (3 tools)
cleanup_old_data()- Remove old low-importance memoriesoptimize_memories()- Merge duplicates, clean relationshipsremember_project_pattern()- Store a reusable pattern
Configuration
Environment Variables
| Variable | Default | Description |
|---|---|---|
DATA_DIR |
~/mcp-memory |
Data and log directory |
LOG_LEVEL |
INFO |
Logging level (DEBUG, INFO, WARNING, ERROR) |
EMBEDDING_MODEL |
all-MiniLM-L6-v2 |
Sentence-transformers model name |
HF_TOKEN |
(none) | HuggingFace token for faster model downloads |
Embedding Models
The EMBEDDING_MODEL environment variable controls which sentence-transformers model to use:
# Smaller, faster (384-dim)
export EMBEDDING_MODEL=all-MiniLM-L6-v2
# Larger, more accurate (384-dim)
export EMBEDDING_MODEL=all-mpnet-base-v2
# Multilingual (384-dim)
export EMBEDDING_MODEL=sentence-transformers/multilingual-MiniLM-L6-v2
# Disable embeddings (text search only)
export EMBEDDING_MODEL=disabled
Classification Configuration
Customize memory classification in classification_config.py:
MEMORY_TYPE_KEYWORDS = {
'error': {
'keywords': ('error', 'exception', 'bug', ...),
'base_importance': 0.8,
'description': 'Error or issue'
},
# ... more types
}
IMPORTANCE_MODIFIERS = {
'critical': 0.3,
'security': 0.25,
# ... more modifiers
}
Data Storage
- Database:
~/mcp-memory/data/mcp_memory.db(SQLite3) - Logs:
~/mcp-memory/logs/mcp_memory_YYYYMMDD.log - Virtual Env:
.venv/in project directory
Performance Considerations
- Semantic search lazy loads embedding model on first use (improves startup)
- Automatic relationship detection uses similarity threshold (default 0.7, configurable)
- Full-text search fallback ensures results even if embeddings unavailable
- Duplicate detection prevents redundant storage and search clutter
- Context windows limit memory loaded per session (default: 50 memories)
Security
- Input validation: Prevents path traversal and injection attacks
- Content sanitization: Dangerous characters removed from inputs
- Transaction support: Ensures database consistency
- Error handling: Sensitive details not exposed in error messages
Development
Running Tests
pytest tests/ -v
Building & Installing
pip install -e .
mcp-memory # Run the server
Debugging
Set log level:
export LOG_LEVEL=DEBUG
python mcp_memory/launcher.py
Check metrics:
from mcp_memory.metrics import get_metrics_collector
collector = get_metrics_collector()
print(collector.get_summary())
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opencode_mcp_memory-0.2.1.tar.gz.
File metadata
- Download URL: opencode_mcp_memory-0.2.1.tar.gz
- Upload date:
- Size: 43.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
733ea9e9ca4db3e6c25504883aba2dee45cd0baf0037f3c8682a609b08606531
|
|
| MD5 |
4842e96fb60becca778e66d7f68ac925
|
|
| BLAKE2b-256 |
e6d1e0a220212084148898b5b7086c6ba849767cbb5db00203ecd56930d9bd7e
|
File details
Details for the file opencode_mcp_memory-0.2.1-py3-none-any.whl.
File metadata
- Download URL: opencode_mcp_memory-0.2.1-py3-none-any.whl
- Upload date:
- Size: 43.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e331c29b070195c17f95a1eea530137bfaa6051af894c09322e86a7b7916432
|
|
| MD5 |
8b4271dc22bfb7172713f46bc1a5d548
|
|
| BLAKE2b-256 |
5f650544a0af3d064b25b9e651db552255a331a82e7b673030af982629c5f156
|