cortex-mem-mcp 2.5.1

MCP server for Cortex-Mem memory management
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# Cortex Memory MCP Server

`cortex-mem-mcp` is a server implementation based on [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) that enables AI assistants to interact with the Cortex Memory system for persistent memory storage and retrieval.

## 🧠 Overview

Cortex Memory MCP Server provides six core tools for AI assistants:

- 📝 **store_memory**: Store new memories from conversations
- 🔍 **query_memory**: Semantic vector search with L0/L1/L2 layered results
- 📋 **list_memories**: Browse stored memory entries
- 📄 **get_memory**: Retrieve complete memory content
- 🗑️ **delete_memory**: Delete specific memory entries
- 📊 **get_abstract**: Get L0 abstract summary (~100 tokens)

## 🛠️ MCP Tools

### 1. `store_memory`

Store a new memory in the Cortex Memory system.

#### Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `content` | string || - | Memory content to store |
| `thread_id` | string || `"default"` | Session ID for organizing related memories |
| `role` | string || `"user"` | Message role: `"user"`, `"assistant"`, or `"system"` |

#### Example Request

```json
{
  "content": "User prefers dark theme and likes vim keybindings",
  "thread_id": "user-preferences",
  "role": "user"
}
```

#### Response

```json
{
  "success": true,
  "uri": "cortex://session/user-preferences/timeline/2024/01/15/14_30_45_abc123.md",
  "message_id": "2024-01-15T14:30:45Z-abc123"
}
```

---

### 2. `query_memory`

Search memories using semantic vector search with L0/L1/L2 layered results.

#### Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `query` | string || - | Search query string |
| `thread_id` | string || - | Limit search to this session |
| `limit` | number || `10` | Maximum number of results |
| `scope` | string || `"session"` | Search scope: `"session"`, `"user"`, or `"agent"` |

#### Scope URI Mapping

| Scope | URI Pattern |
|-------|-------------|
| `session` | `cortex://session` |
| `user` | `cortex://user` |
| `agent` | `cortex://agent` |
| (with thread_id) | `cortex://session/{thread_id}` |

#### Example Request

```json
{
  "query": "Rust OAuth implementation method",
  "thread_id": "technical-discussions",
  "limit": 5,
  "scope": "session"
}
```

#### Response

```json
{
  "success": true,
  "query": "Rust OAuth implementation method",
  "results": [
    {
      "uri": "cortex://session/tech-disc/timeline/2024/01/10/09_15_30_def456.md",
      "score": 0.92,
      "snippet": "...discussed using OAuth2 client library for authentication in Rust applications..."
    }
  ],
  "total": 1
}
```

---

### 3. `list_memories`

List memories from a specific URI path.

#### Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `uri` | string || `"cortex://session"` | URI path to list |
| `limit` | number || `50` | Maximum number of entries |
| `include_abstracts` | boolean || `false` | Include L0 abstracts |

#### Supported URI Patterns

| URI Pattern | Description |
|-------------|-------------|
| `cortex://session` | List all sessions |
| `cortex://user/{user-id}` | List user memories |
| `cortex://agent/{agent-id}` | List agent memories |
| `cortex://session/{session-id}/timeline` | List session timeline |

#### Example Request

```json
{
  "uri": "cortex://session",
  "limit": 20,
  "include_abstracts": true
}
```

#### Response

```json
{
  "success": true,
  "uri": "cortex://session",
  "entries": [
    {
      "name": "user-preferences",
      "uri": "cortex://session/user-preferences",
      "is_directory": true,
      "size": 2048,
      "abstract_text": "User preference settings and options"
    }
  ],
  "total": 1
}
```

---

### 4. `get_memory`

Retrieve complete content of a specific memory.

#### Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `uri` | string || - | Full URI of the memory |

#### Example Request

```json
{
  "uri": "cortex://session/user-preferences/timeline/2024/01/15/14_30_45_abc123.md"
}
```

#### Response

```json
{
  "success": true,
  "uri": "cortex://session/user-preferences/timeline/2024/01/15/14_30_45_abc123.md",
  "content": "# Message\n\nUser prefers dark theme and likes vim keybindings.\n\n---\n*Timestamp: 2024-01-15T14:30:45Z*\n*Role: user*"
}
```

---

### 5. `delete_memory`

Delete a specific memory entry.

#### Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `uri` | string || - | URI of the memory to delete |

#### Example Request

```json
{
  "uri": "cortex://session/user-preferences/timeline/2024/01/15/14_30_45_abc123.md"
}
```

#### Response

```json
{
  "success": true,
  "uri": "cortex://session/user-preferences/timeline/2024/01/15/14_30_45_abc123.md"
}
```

---

### 6. `get_abstract`

Get the L0 abstract summary (~100 tokens) of a memory for quick relevance checking.

#### Parameters

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `uri` | string || - | URI of the memory |

#### Example Request

```json
{
  "uri": "cortex://session/user-preferences/timeline/2024/01/15/14_30_45_abc123.md"
}
```

#### Response

```json
{
  "success": true,
  "uri": "cortex://session/user-preferences/timeline/2024/01/15/14_30_45_abc123.md",
  "abstract_text": "User preferences: dark theme, vim keybindings"
}
```

## 🚀 Installation & Configuration

### Build Requirements

- Rust 1.70 or later
- Cross-platform support: Linux, macOS, Windows

### Build

```bash
# Clone repository
git clone https://github.com/sopaco/cortex-mem.git
cd cortex-mem

# Build the server
cargo build --release --bin cortex-mem-mcp

# Binary location
./target/release/cortex-mem-mcp
```

### Command-line Arguments

| Argument | Default | Description |
|----------|---------|-------------|
| `--config` / `-c` | `config.toml` | Path to configuration file |
| `--tenant` | `default` | Tenant ID for memory isolation |

### Configure Claude Desktop

Edit Claude Desktop configuration file:

**macOS**:
```bash
open ~/Library/Application\ Support/Claude/claude_desktop_config.json
```

**Windows**:
```bash
notepad %APPDATA%\Claude\claude_desktop_config.json
```

Add configuration:

```json
{
  "mcpServers": {
    "cortex-memory": {
      "command": "/path/to/cortex-mem-mcp",
      "args": [
        "--config", "/path/to/config.toml",
        "--tenant", "default"
      ],
      "env": {
        "RUST_LOG": "info"
      }
    }
  }
}
```

### Configuration File (config.toml)

```toml
[cortex]
# Data directory (optional, has smart defaults)
data_dir = "/path/to/cortex-data"

[llm]
# LLM API configuration
api_base_url = "https://api.openai.com/v1"
api_key = "your-api-key"
model_efficient = "gpt-4o-mini"
temperature = 0.1
max_tokens = 4096

[embedding]
# Embedding configuration
api_base_url = "https://api.openai.com/v1"
api_key = "your-embedding-api-key"
model_name = "text-embedding-3-small"
batch_size = 10
timeout_secs = 30

[qdrant]
# Vector database configuration
url = "http://localhost:6333"
collection_name = "cortex_memories"
embedding_dim = 1536
timeout_secs = 30
```

### Data Directory Resolution

Priority order:
1. `cortex.data_dir` config value
2. `CORTEX_DATA_DIR` environment variable
3. System app data directory (e.g., `%APPDATA%/tars/cortex` on Windows)
4. Fallback: `./.cortex` in current directory

## 🔄 MCP Workflow

### Typical Memory Workflow

```javascript
// 1. Start of conversation: Query relevant memories
await query_memory({
  query: "user preferences",
  scope: "user",
  limit: 5
});

// 2. During conversation: Store new information
await store_memory({
  content: "User mentioned they are learning Rust async programming",
  thread_id: "learning-journey",
  role: "user"
});

// 3. End of conversation: Store summary
await store_memory({
  content: "Discussed Rust async/await, Pin, and Future. User understood the basics.",
  thread_id: "rust-async-discussion",
  role: "assistant"
});
```

### Advanced Search Strategy

```javascript
// 1. Search in sessions first
const sessionResults = await query_memory({
  query: "Rust error handling",
  scope: "session",
  limit: 5
});

// 2. If more context needed, search user memories
if (sessionResults.results.length < 3) {
  const userResults = await query_memory({
    query: "Rust error handling",
    scope: "user",
    limit: 5
  });
  // Merge results
  sessionResults.results.push(...userResults.results);
}

// 3. Get full content
const fullContent = await get_memory({
  uri: sessionResults.results[0].uri
});

// 4. Or get abstract for quick preview
const abstract = await get_abstract({
  uri: sessionResults.results[0].uri
});
```

## 🔧 Troubleshooting

### Common Issues

#### 1. Connection Failed

**Error**: `Failed to connect to MCP server`

**Solution**:
1. Check Claude Desktop configuration file path
2. Verify binary file path and permissions
3. View log output

```bash
# Test run
RUST_LOG=debug ./cortex-mem-mcp --config config.toml --tenant default
```

#### 2. Memory Storage Failed

**Error**: `Failed to store memory`

**Solution**:
1. Check data directory permissions
2. Verify LLM API configuration
3. Confirm Qdrant service is running
4. Check embedding configuration

```bash
# Check directory permissions
ls -la ./cortex-data
chmod 755 ./cortex-data

# Check Qdrant connection
curl http://localhost:6333/collections
```

#### 3. Empty Search Results

**Error**: `Search returned empty results`

**Solution**:
1. Check if memories exist
2. Verify search query format
3. Confirm search scope

```javascript
// Test listing
await list_memories({
  uri: "cortex://session",
  limit: 50
});
```

#### 4. Qdrant Connection Failed

**Error**: `Failed to connect to Qdrant`

**Solution**:
1. Ensure Qdrant service is running
2. Check URL configuration
3. Verify collection name exists

```bash
# Start Qdrant (Docker)
docker run -p 6333:6333 qdrant/qdrant

# Check connection
curl http://localhost:6333
```

### Debug Mode

```bash
# Enable verbose logging
RUST_LOG=debug ./cortex-mem-mcp --config config.toml --tenant default
```

## 🔗 Related Resources

- [Cortex Memory Main Documentation]../README.md
- [Cortex Memory Core]../cortex-mem-core/README.md
- [Cortex Memory Tools]../cortex-mem-tools/README.md
- [Model Context Protocol]https://modelcontextprotocol.io/
- [Claude Desktop MCP Documentation]https://docs.anthropic.com/claude/docs/mcp

## 🤝 Contributing

Contributions are welcome! Please follow these steps:

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Create a Pull Request

## 📄 License

MIT License - see the [LICENSE](../../LICENSE) file for details.

---

**Built with ❤️ using Rust and Model Context Protocol**