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
After installation, run the server directly:
mcp-memory
Running from a source checkout without installing uses the launcher, which creates a .venv and installs dependencies on first run:
python mcp_memory/launcher.py
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 for running from source without installingmemory.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())
Metadata
Release files for opencode-mcp-memory 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| opencode_mcp_memory-0.2.3.tar.gz | 43.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| opencode_mcp_memory-0.2.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 86.3 kB
Release files / opencode_mcp_memory-0.2.3.tar.gz
| Download URL | opencode_mcp_memory-0.2.3.tar.gz |
|---|---|
| Size | 43.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c43d54155202e22705ce8be1c4b5297cfa23fd5ef2f890332a4a4d6cecd68ce0
|
|
BLAKE2b-256 checksum How to use checksums |
8942ee40be4ca022691d3450c4c36eeda947f2d00fb99e9b2c97e5a82bd25c2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.6
|
Release files / opencode_mcp_memory-0.2.3-py3-none-any.whl
| Download URL | opencode_mcp_memory-0.2.3-py3-none-any.whl |
|---|---|
| Size | 43.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce475ecaaaafd21d968496a350dc5144dd35d767772c79d7dcfcbeda2d53448a
|
|
BLAKE2b-256 checksum How to use checksums |
cab6a4ac35fa70f8b9442b96fb8067e998b2e5c2a91ae689ca2aea6aa76df00f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.6
|