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
|
---
description: Initialize Memory MCP Server project from scratch
argument-hint: "[quick, full, or database]"
allowed-tools: Write, MultiEdit, Bash, Task, TodoWrite
---
# Memory MCP Server Setup
Initialize and configure the Memory MCP Server project based on $ARGUMENTS:
## Quick Setup
Initialize minimal working MCP server with memory capabilities:
```bash
# Initialize project
npm init -y
npm install @modelcontextprotocol/sdk zod dotenv
npm install -D typescript @types/node tsx nodemon
npm install @neondatabase/serverless drizzle-orm@^0.44.4
npm install openai pgvector
# Create TypeScript config
npx tsc --init
```
## Full Setup
Complete project initialization with all features:
### 1. Project Structure
```text
memory-mcp-server/
├── src/
│ ├── index.ts # MCP server entry point
│ ├── server.ts # Server initialization
│ ├── tools/ # MCP tool implementations
│ │ ├── createMemory.ts
│ │ ├── searchMemories.ts
│ │ ├── getMemory.ts
│ │ ├── updateMemory.ts
│ │ └── deleteMemory.ts
│ ├── db/
│ │ ├── client.ts # Database connection
│ │ ├── schema.ts # Drizzle schema
│ │ └── migrations/ # Database migrations
│ ├── services/
│ │ ├── embeddings.ts # OpenAI embeddings
│ │ ├── vectorSearch.ts # pgvector operations
│ │ └── memoryLifecycle.ts # Memory management
│ ├── types/
│ │ └── index.ts # TypeScript types
│ └── utils/
│ ├── logger.ts # Structured logging
│ └── errors.ts # Error handling
├── tests/
│ ├── unit/
│ ├── integration/
│ └── fixtures/
├── .env.example
├── .mcp.json # MCP manifest
├── tsconfig.json
├── package.json
├── drizzle.config.ts
└── README.md
```
### 2. Package Dependencies
```json
{
"name": "memory-mcp-server",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc",
"start": "node dist/index.js",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"lint": "eslint . --ext .ts",
"typecheck": "tsc --noEmit",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio"
},
"dependencies": {
"@modelcontextprotocol/sdk": "^1.0.0",
"@neondatabase/serverless": "^1.0.1",
"drizzle-orm": "^0.44.4",
"zod": "^4.0.17",
"openai": "^4.0.0",
"pgvector": "^0.2.0",
"dotenv": "^16.0.0",
"winston": "^3.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"typescript": "^5.0.0",
"tsx": "^4.0.0",
"nodemon": "^3.0.0",
"jest": "^29.0.0",
"@types/jest": "^29.0.0",
"ts-jest": "^29.0.0",
"eslint": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"drizzle-kit": "^0.32.0"
}
}
```
### 3. TypeScript Configuration
```json
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["ES2022"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "tests"]
}
```
### 4. Environment Variables
```bash
# .env
DATABASE_URL="postgresql://user:pass@host/dbname?sslmode=require"
OPENAI_API_KEY="sk-..."
MCP_SERVER_PORT=3000
LOG_LEVEL=info
NODE_ENV=development
# Vector search settings
VECTOR_SEARCH_LIMIT=10
SIMILARITY_THRESHOLD=0.7
# Memory lifecycle
MEMORY_EXPIRATION_DAYS=90
MAX_MEMORIES_PER_USER=10000
IMPORTANCE_DECAY_RATE=0.1
```
### 5. MCP Manifest
```json
{
"name": "memory-mcp-server",
"version": "1.0.0",
"description": "Persistent memory management for AI assistants",
"author": "Your Name",
"license": "MIT",
"server": {
"command": "node",
"args": ["dist/index.js"],
"transport": "stdio"
},
"tools": {
"create_memory": {
"description": "Create a new memory with vector embedding",
"inputSchema": {
"type": "object",
"properties": {
"content": { "type": "string" },
"type": { "type": "string" },
"importance": { "type": "number" },
"expires_at": { "type": "string" }
},
"required": ["content", "type"]
}
},
"search_memories": {
"description": "Search memories using semantic similarity",
"inputSchema": {
"type": "object",
"properties": {
"query": { "type": "string" },
"limit": { "type": "number" },
"threshold": { "type": "number" }
},
"required": ["query"]
}
}
}
}
```
## Database Setup
Initialize Neon PostgreSQL with pgvector:
### 1. Create Database
```sql
-- Enable pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;
-- Create database schema
CREATE SCHEMA IF NOT EXISTS memory_mcp;
```
### 2. Drizzle Schema
```typescript
// src/db/schema.ts
import { pgTable, uuid, text, timestamp, boolean, real, vector, index, jsonb } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: uuid('id').primaryKey().defaultRandom(),
external_id: text('external_id').notNull().unique(),
created_at: timestamp('created_at').defaultNow().notNull(),
metadata: jsonb('metadata')
});
export const companions = pgTable('companions', {
id: uuid('id').primaryKey().defaultRandom(),
name: text('name').notNull(),
user_id: uuid('user_id').references(() => users.id),
created_at: timestamp('created_at').defaultNow().notNull(),
is_active: boolean('is_active').default(true)
});
export const memories = pgTable('memories', {
id: uuid('id').primaryKey().defaultRandom(),
companion_id: uuid('companion_id').references(() => companions.id),
user_id: uuid('user_id').references(() => users.id),
content: text('content').notNull(),
type: text('type').notNull(),
embedding: vector('embedding', { dimensions: 1536 }),
importance: real('importance').default(0.5),
access_count: integer('access_count').default(0),
last_accessed: timestamp('last_accessed'),
expires_at: timestamp('expires_at'),
is_archived: boolean('is_archived').default(false),
created_at: timestamp('created_at').defaultNow().notNull(),
updated_at: timestamp('updated_at').defaultNow().notNull()
}, (table) => ({
embeddingIdx: index('memories_embedding_idx')
.using('ivfflat', table.embedding.op('vector_cosine_ops'))
.with({ lists: 100 }),
userIdx: index('memories_user_idx').on(table.user_id),
companionIdx: index('memories_companion_idx').on(table.companion_id),
typeIdx: index('memories_type_idx').on(table.type)
}));
```
### 3. Migration Commands
```bash
# Generate migration
npx drizzle-kit generate
# Run migrations
npx drizzle-kit migrate
# Open Drizzle Studio
npx drizzle-kit studio
```
## Initial Server Implementation
```typescript
// src/index.ts
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
import { createMemoryTool } from './tools/createMemory.js';
import { searchMemoriesTool } from './tools/searchMemories.js';
const server = new Server({
name: 'memory-mcp-server',
version: '1.0.0'
}, {
capabilities: {
tools: {}
}
});
// Register tools
server.setRequestHandler('tools/list', async () => ({
tools: [
createMemoryTool.definition,
searchMemoriesTool.definition
]
}));
server.setRequestHandler('tools/call', async (request) => {
const { name, arguments: args } = request.params;
switch (name) {
case 'create_memory':
return await createMemoryTool.handler(args);
case 'search_memories':
return await searchMemoriesTool.handler(args);
default:
throw new Error(`Unknown tool: ${name}`);
}
});
// Start server
const transport = new StdioServerTransport();
await server.connect(transport);
console.log('Memory MCP Server started');
```
## Testing Setup
```bash
# Install test dependencies
npm install -D jest @types/jest ts-jest
# Create Jest config
npx ts-jest config:init
# Run tests
npm test
```
## Development Workflow
```bash
# Start development server
npm run dev
# In another terminal, test MCP connection
npx @modelcontextprotocol/cli connect stdio "npm run start"
# Test tool execution
npx @modelcontextprotocol/cli call create_memory '{"content": "Test memory"}'
```
## Production Deployment
```dockerfile
# Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
CMD ["node", "dist/index.js"]
```
## Monitoring & Observability
```typescript
// src/utils/logger.ts
import winston from 'winston';
export const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.json(),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
```
This setup provides a complete foundation for the Memory MCP Server with all necessary configurations and best practices.
|