-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
370 lines (293 loc) · 11.4 KB
/
cli.py
File metadata and controls
370 lines (293 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python3
"""
Mnemosyne CLI - Command line interface for memory layer MCP server
"""
import argparse
import json
import os
import subprocess
import sys
from pathlib import Path
from typing import Any, Dict
from config import ensure_directories, load_config
def get_cursor_config_paths() -> list[Path]:
"""Get all possible paths to Cursor/Claude MCP configuration files"""
paths = []
# Cursor MCP config (primary for Cursor)
paths.append(Path.home() / ".cursor" / "mcp.json")
# Claude Desktop config (standard location)
if sys.platform == "darwin": # macOS
paths.append(
Path.home()
/ "Library"
/ "Application Support"
/ "Claude"
/ "claude_desktop_config.json"
)
elif sys.platform == "win32": # Windows
paths.append(Path.home() / "AppData" / "Roaming" / "Claude" / "claude_desktop_config.json")
else: # Linux
paths.append(Path.home() / ".config" / "Claude" / "claude_desktop_config.json")
# Cursor Cline MCP settings
if sys.platform == "darwin": # macOS
paths.append(
Path.home()
/ "Library"
/ "Application Support"
/ "Cursor"
/ "User"
/ "globalStorage"
/ "rooveterinaryinc.roo-cline"
/ "settings"
/ "cline_mcp_settings.json"
)
elif sys.platform == "win32": # Windows
paths.append(
Path.home()
/ "AppData"
/ "Roaming"
/ "Cursor"
/ "User"
/ "globalStorage"
/ "rooveterinaryinc.roo-cline"
/ "settings"
/ "cline_mcp_settings.json"
)
else: # Linux
paths.append(
Path.home()
/ ".config"
/ "Cursor"
/ "User"
/ "globalStorage"
/ "rooveterinaryinc.roo-cline"
/ "settings"
/ "cline_mcp_settings.json"
)
return paths
def get_cursor_config_path() -> Path:
"""Get the primary path to use for Cursor MCP configuration (backward compatibility)"""
return get_cursor_config_paths()[0] # Prefer .cursor/mcp.json
def get_mnemosyne_server_path() -> str:
"""Get the path to the Mnemosyne server script"""
return str(Path(__file__).parent / "server.py")
def init_config():
"""Initialize Mnemosyne configuration"""
print("🔧 Initializing Mnemosyne configuration...")
try:
config = load_config()
ensure_directories(config)
print("✅ Configuration initialized successfully")
print(f"📁 Storage path: {config.storage.vector_db_path}")
print(f"📝 Logs path: {config.logging.path}")
# Test connections
print("\n🧪 Testing connections...")
# Test Neo4j
try:
from memory.graph import KnowledgeGraph
kg = KnowledgeGraph(config)
if kg.driver:
print("✅ Neo4j: Connected")
kg.close()
else:
print("⚠️ Neo4j: Not available (optional)")
except Exception as e:
print(f"⚠️ Neo4j: Not available ({e})")
# Test embeddings
try:
from memory.embeddings import EmbeddingGenerator
embedder = EmbeddingGenerator(config)
if embedder.embedding_type == "sentence_transformers":
print("✅ Embeddings: sentence-transformers available")
else:
print(
"⚠️ Embeddings: Using dummy embeddings (install sentence-transformers for better performance)"
)
except Exception as e:
print(f"⚠️ Embeddings: Error ({e})")
print("\n🚀 Mnemosyne is ready!")
except Exception as e:
print(f"❌ Configuration failed: {e}")
sys.exit(1)
def configure_cursor():
"""Configure Cursor to use Mnemosyne MCP server"""
print("🔧 Configuring Cursor for Mnemosyne integration...")
cursor_config_path = get_cursor_config_path() # Use Claude Desktop config as primary
server_path = get_mnemosyne_server_path()
# Get full Python path
python_path = sys.executable
# Create the MCP configuration
mcp_config = {"mcpServers": {"mnemosyne": {"command": python_path, "args": [server_path]}}}
# Ensure the directory exists
cursor_config_path.parent.mkdir(parents=True, exist_ok=True)
# Read existing config if it exists
existing_config = {}
if cursor_config_path.exists():
try:
with open(cursor_config_path, "r") as f:
existing_config = json.load(f)
except Exception as e:
print(f"⚠️ Could not read existing Cursor config: {e}")
# Merge configurations
if "mcpServers" not in existing_config:
existing_config["mcpServers"] = {}
existing_config["mcpServers"]["mnemosyne"] = mcp_config["mcpServers"]["mnemosyne"]
# Write the updated configuration
try:
with open(cursor_config_path, "w") as f:
json.dump(existing_config, f, indent=2)
print("✅ Cursor configuration updated successfully")
print(f"📁 Config file: {cursor_config_path}")
print("\n🔄 Please restart Cursor to apply the changes")
print("\n💡 To test the integration:")
print(" 1. Open a project in Cursor")
print(" 2. Start a chat with Claude")
print(" 3. Try: 'store_decision' or 'search_memory' tools")
except Exception as e:
print(f"❌ Failed to update Cursor configuration: {e}")
print(f"\n📝 Manual configuration required:")
print(f" Add this to {cursor_config_path}:")
print(json.dumps(mcp_config, indent=2))
sys.exit(1)
def start_server():
"""Start the Mnemosyne MCP server"""
print("🚀 Starting Mnemosyne MCP server...")
try:
# Ensure configuration is valid
config = load_config()
ensure_directories(config)
# Start the server
server_path = get_mnemosyne_server_path()
subprocess.run([sys.executable, server_path], check=True)
except KeyboardInterrupt:
print("\n⏹️ Server stopped by user")
except Exception as e:
print(f"❌ Server failed to start: {e}")
sys.exit(1)
def check_status():
"""Check the status of Mnemosyne installation and configuration"""
print("🔍 Checking Mnemosyne status...\n")
# Check configuration
try:
config = load_config()
print("✅ Configuration: Valid")
except Exception as e:
print(f"❌ Configuration: Invalid ({e})")
return
# Check storage directories
storage_dir = Path(config.storage.vector_db_path).parent
if storage_dir.exists():
print(f"✅ Storage directory: {storage_dir}")
else:
print(f"❌ Storage directory: Missing ({storage_dir})")
# Check Cursor configuration
configured = False
for config_path in get_cursor_config_paths():
if config_path.exists():
try:
with open(config_path, "r") as f:
cursor_config = json.load(f)
if "mcpServers" in cursor_config and "mnemosyne" in cursor_config["mcpServers"]:
print(f"✅ Cursor integration: Configured ({config_path.name})")
configured = True
break
except Exception as e:
print(f"❌ Error reading {config_path.name}: {e}")
if not configured:
print("⚠️ Cursor integration: Not configured in any location")
# Check dependencies
print("\n📦 Dependencies:")
dependencies = [
("mcp", "MCP Protocol"),
("chromadb", "Vector Database"),
("sentence_transformers", "Embeddings"),
("neo4j", "Knowledge Graph"),
("pyyaml", "Configuration"),
]
for module, description in dependencies:
try:
__import__(module)
print(f" ✅ {description}: Available")
except ImportError:
print(f" ❌ {description}: Missing (pip install {module})")
# Check for stored memories
try:
from memory.storage import MemoryStorage
storage = MemoryStorage(config)
if storage.storage_type == "file":
memories_file = (
Path(config.storage.vector_db_path).parent / "file_storage" / "memories.jsonl"
)
if memories_file.exists():
with open(memories_file, "r") as f:
memory_count = sum(1 for _ in f)
print(f"\n💾 Stored memories: {memory_count}")
else:
print("\n💾 Stored memories: 0 (no memories file)")
else:
print(f"\n💾 Storage backend: {storage.storage_type}")
except Exception as e:
print(f"\n❌ Storage check failed: {e}")
def uninstall():
"""Remove Mnemosyne configuration from Cursor"""
print("🗑️ Removing Mnemosyne from Cursor configuration...")
cursor_config_path = get_cursor_config_path()
if not cursor_config_path.exists():
print("⚠️ Cursor configuration file not found - nothing to remove")
return
try:
with open(cursor_config_path, "r") as f:
config = json.load(f)
if "mcpServers" in config and "mnemosyne" in config["mcpServers"]:
del config["mcpServers"]["mnemosyne"]
with open(cursor_config_path, "w") as f:
json.dump(config, f, indent=2)
print("✅ Mnemosyne removed from Cursor configuration")
print("🔄 Please restart Cursor to apply the changes")
else:
print("⚠️ Mnemosyne not found in Cursor configuration")
except Exception as e:
print(f"❌ Failed to remove configuration: {e}")
def main():
"""Main CLI entry point"""
parser = argparse.ArgumentParser(
description="Mnemosyne - Memory Layer MCP Server for AI Coding Sessions",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
mnemosyne init # Initialize configuration
mnemosyne configure-cursor # Configure Cursor integration
mnemosyne start # Start the MCP server
mnemosyne status # Check installation status
mnemosyne uninstall # Remove from Cursor
""",
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# Init command
subparsers.add_parser("init", help="Initialize Mnemosyne configuration")
# Configure Cursor command
subparsers.add_parser("configure-cursor", help="Configure Cursor to use Mnemosyne")
# Start server command
subparsers.add_parser("start", help="Start the Mnemosyne MCP server")
# Status command
subparsers.add_parser("status", help="Check Mnemosyne status and configuration")
# Uninstall command
subparsers.add_parser("uninstall", help="Remove Mnemosyne from Cursor configuration")
args = parser.parse_args()
if not args.command:
parser.print_help()
return
print("🧠 Mnemosyne - Memory Layer MCP Server")
print("=" * 50)
if args.command == "init":
init_config()
elif args.command == "configure-cursor":
configure_cursor()
elif args.command == "start":
start_server()
elif args.command == "status":
check_status()
elif args.command == "uninstall":
uninstall()
if __name__ == "__main__":
main()