Files
python-sdk/pkg/hanzo-mcp/demo_memory_system.py
zooqueenandHanzo Dev 920f767417 style: auto-fix ruff lint and format for Python 3.12
Run ruff check --fix and ruff format across the repo to fix ~495
auto-fixable lint errors (mostly I001 import sorting) and reformat
225 files to match the py312 target.

Co-authored-by: Hanzo Dev <[email protected]>
2026-02-20 21:29:31 -08:00

128 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""
Demonstration of the Hanzo MCP Memory System with Modular Plugin Architecture.
This script shows how users can:
1. Install hanzo-mcp with memory support
2. Configure memory backends
3. Use the memory system
"""
import asyncio
import json
from pathlib import Path
from hanzo_mcp.config import get_global_config_path, load_config, save_global_config
from hanzo_mcp.memory_service import PluginMemoryService
async def demo_memory_system():
"""Demonstrate the memory system usage."""
print("๐Ÿš€ Hanzo MCP Memory System Demo")
print("=" * 50)
# Step 1: Show configuration file location
config_path = get_global_config_path()
print(f"๐Ÿ“ Configuration file location: {config_path}")
# Step 2: Create a sample configuration
print("\n๐Ÿ”ง Creating sample configuration...")
sample_config = {
"enabled_backends": ["sqlite"], # Enable SQLite backend
"backend_configs": {
"sqlite": {
"enabled": True,
"path": str(Path.home() / ".hanzo" / "memory.db"),
"settings": {"auto_migrate": True, "connection_timeout": 30},
}
},
"default_user_id": "demo-user",
"default_project_id": "demo-project",
}
print("๐Ÿ“‹ Sample configuration:")
print(json.dumps(sample_config, indent=2))
# Step 3: Save and load configuration
print(f"\n๐Ÿ’พ Saving configuration to {config_path}")
with open(config_path, "w") as f:
json.dump(sample_config, f, indent=2)
print("๐Ÿ”„ Loading configuration...")
config = load_config()
print(f" Enabled backends: {config.enabled_backends}")
# Step 4: Initialize memory service
print("\nโšก Initializing memory service...")
service = PluginMemoryService()
await service.initialize(enabled_backends=config.enabled_backends)
print(f" Active backends: {service.get_active_backends()}")
# Step 5: Demonstrate memory operations
print("\n๐Ÿง  Demonstrating memory operations...")
# Store a memory
memory_id = await service.store_memory(
content="The Hanzo MCP modular architecture enables flexible backend selection",
metadata={"type": "architecture", "domain": "mcp", "importance": 0.9},
user_id=config.default_user_id,
project_id=config.default_project_id,
)
print(f" Stored memory with ID: {memory_id[:8]}...")
# Store another memory
memory_id2 = await service.store_memory(
content="SQLite backend provides lightweight persistence with vector search",
metadata={"type": "backend", "domain": "storage", "importance": 0.8},
user_id=config.default_user_id,
project_id=config.default_project_id,
)
print(f" Stored memory with ID: {memory_id2[:8]}...")
# Retrieve memories
results = await service.retrieve_memory(
query="architecture",
user_id=config.default_user_id,
project_id=config.default_project_id,
limit=5,
)
print(f" Retrieved {len(results)} memories for 'architecture'")
# Step 6: Show available backends and capabilities
print("\n๐Ÿงฉ Available backends and capabilities:")
print(f" Available: {service.get_available_backends()}")
print(f" With persistence: {service.has_capability('persistence')}")
print(f" With vector search: {service.has_capability('vector_search')}")
print(f" With embeddings: {service.has_capability('embeddings')}")
# Step 7: Demonstrate backend management
print("\nโš™๏ธ Backend management:")
print(f" Current active: {service.get_active_backends()}")
# Disable and re-enable
service.disable_backend("sqlite")
print(f" After disabling SQLite: {service.get_active_backends()}")
service.enable_backend("sqlite")
print(f" After re-enabling SQLite: {service.get_active_backends()}")
# Step 8: Cleanup
print("\n๐Ÿงน Cleaning up...")
await service.shutdown()
# Remove demo config
if config_path.exists():
config_path.unlink()
print(f" Removed config file: {config_path}")
print("\nโœจ Demo completed successfully!")
print("\n๐Ÿ’ก Usage:")
print(" 1. Install with memory support: pip install hanzo-mcp[memory]")
print(" 2. Configure backends in ~/.config/hanzo/mcp-settings.json")
print(" 3. Use the memory system with modular backend support")
print(" 4. Enable/disable backends as needed")
if __name__ == "__main__":
asyncio.run(demo_memory_system())