@aeriondyseti/vector-memory-mcp 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriondyseti/vector-memory-mcp",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "A zero-configuration RAG memory server for MCP clients",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -9,6 +9,7 @@
9
9
  "vector-memory-mcp": "dist/index.js"
10
10
  },
11
11
  "files": [
12
+ "src",
12
13
  "dist",
13
14
  "README.md",
14
15
  "LICENSE"
@@ -37,7 +38,7 @@
37
38
  "@lancedb/lancedb": "^0.22.3",
38
39
  "@modelcontextprotocol/sdk": "^1.0.0",
39
40
  "@huggingface/transformers": "^3.8.0",
40
- "apache-arrow": "^21.1.0"
41
+ "apache-arrow": "^18.1.0"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@types/bun": "latest",
@@ -0,0 +1,29 @@
1
+ import { join } from "path";
2
+ import { homedir } from "os";
3
+
4
+ export interface Config {
5
+ dbPath: string;
6
+ embeddingModel: string;
7
+ embeddingDimension: number;
8
+ }
9
+
10
+ const DEFAULT_DB_PATH = join(
11
+ homedir(),
12
+ ".local",
13
+ "share",
14
+ "vector-memory-mcp",
15
+ "memories.db"
16
+ );
17
+
18
+ const DEFAULT_EMBEDDING_MODEL = "Xenova/all-MiniLM-L6-v2";
19
+ const DEFAULT_EMBEDDING_DIMENSION = 384;
20
+
21
+ export function loadConfig(): Config {
22
+ return {
23
+ dbPath: process.env.VECTOR_MEMORY_DB_PATH ?? DEFAULT_DB_PATH,
24
+ embeddingModel: process.env.VECTOR_MEMORY_MODEL ?? DEFAULT_EMBEDDING_MODEL,
25
+ embeddingDimension: DEFAULT_EMBEDDING_DIMENSION,
26
+ };
27
+ }
28
+
29
+ export const config = loadConfig();
@@ -0,0 +1,11 @@
1
+ import * as lancedb from "@lancedb/lancedb";
2
+ import { mkdirSync } from "fs";
3
+ import { dirname } from "path";
4
+
5
+ export async function connectToDatabase(dbPath: string): Promise<lancedb.Connection> {
6
+ // Ensure directory exists
7
+ mkdirSync(dirname(dbPath), { recursive: true });
8
+
9
+ const db = await lancedb.connect(dbPath);
10
+ return db;
11
+ }
@@ -0,0 +1,94 @@
1
+ import * as lancedb from "@lancedb/lancedb";
2
+ import { TABLE_NAME, memorySchema } from "./schema.js";
3
+ import {
4
+ type Memory,
5
+ type VectorRow,
6
+ DELETED_TOMBSTONE,
7
+ } from "../types/memory.js";
8
+
9
+ export class MemoryRepository {
10
+ constructor(private db: lancedb.Connection) {}
11
+
12
+ private async getTable() {
13
+ const names = await this.db.tableNames();
14
+ if (names.includes(TABLE_NAME)) {
15
+ return await this.db.openTable(TABLE_NAME);
16
+ }
17
+ // Create with empty data to initialize schema
18
+ return await this.db.createTable(TABLE_NAME, [], { schema: memorySchema });
19
+ }
20
+
21
+ async insert(memory: Memory): Promise<void> {
22
+ const table = await this.getTable();
23
+ await table.add([
24
+ {
25
+ id: memory.id,
26
+ vector: memory.embedding,
27
+ content: memory.content,
28
+ metadata: JSON.stringify(memory.metadata),
29
+ created_at: memory.createdAt.getTime(),
30
+ updated_at: memory.updatedAt.getTime(),
31
+ superseded_by: memory.supersededBy,
32
+ },
33
+ ]);
34
+ }
35
+
36
+ async findById(id: string): Promise<Memory | null> {
37
+ const table = await this.getTable();
38
+ const results = await table.query().where(`id = '${id}'`).limit(1).toArray();
39
+
40
+ if (results.length === 0) {
41
+ return null;
42
+ }
43
+
44
+ const row = results[0];
45
+
46
+ // Handle Arrow Vector type conversion
47
+ // LanceDB returns an Arrow Vector object which is iterable but not an array
48
+ const vectorData = row.vector as any;
49
+ const embedding = Array.isArray(vectorData)
50
+ ? vectorData
51
+ : Array.from(vectorData) as number[];
52
+
53
+ return {
54
+ id: row.id as string,
55
+ content: row.content as string,
56
+ embedding,
57
+ metadata: JSON.parse(row.metadata as string),
58
+ createdAt: new Date(row.created_at as number),
59
+ updatedAt: new Date(row.updated_at as number),
60
+ supersededBy: row.superseded_by as string | null,
61
+ };
62
+ }
63
+
64
+ async markDeleted(id: string): Promise<boolean> {
65
+ const table = await this.getTable();
66
+
67
+ // Verify existence first to match previous behavior (return false if not found)
68
+ const existing = await table.query().where(`id = '${id}'`).limit(1).toArray();
69
+ if (existing.length === 0) {
70
+ return false;
71
+ }
72
+
73
+ const now = Date.now();
74
+ await table.update({
75
+ where: `id = '${id}'`,
76
+ values: {
77
+ superseded_by: DELETED_TOMBSTONE,
78
+ updated_at: now,
79
+ },
80
+ });
81
+
82
+ return true;
83
+ }
84
+
85
+ async findSimilar(embedding: number[], limit: number): Promise<VectorRow[]> {
86
+ const table = await this.getTable();
87
+ const results = await table.vectorSearch(embedding).limit(limit).toArray();
88
+
89
+ return results.map((r) => ({
90
+ id: r.id as string,
91
+ distance: r._distance as number,
92
+ }));
93
+ }
94
+ }
@@ -0,0 +1,33 @@
1
+ import {
2
+ Schema,
3
+ Field,
4
+ FixedSizeList,
5
+ Float32,
6
+ Utf8,
7
+ Timestamp,
8
+ TimeUnit,
9
+ } from "apache-arrow";
10
+
11
+ export const TABLE_NAME = "memories";
12
+
13
+ export const memorySchema = new Schema([
14
+ new Field("id", new Utf8(), false),
15
+ new Field(
16
+ "vector",
17
+ new FixedSizeList(384, new Field("item", new Float32())),
18
+ false
19
+ ),
20
+ new Field("content", new Utf8(), false),
21
+ new Field("metadata", new Utf8(), false), // JSON string
22
+ new Field(
23
+ "created_at",
24
+ new Timestamp(TimeUnit.MILLISECOND),
25
+ false
26
+ ),
27
+ new Field(
28
+ "updated_at",
29
+ new Timestamp(TimeUnit.MILLISECOND),
30
+ false
31
+ ),
32
+ new Field("superseded_by", new Utf8(), true), // Nullable
33
+ ]);
package/src/index.ts ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { config } from "./config/index.js";
4
+ import { connectToDatabase } from "./db/connection.js";
5
+ import { MemoryRepository } from "./db/memory.repository.js";
6
+ import { EmbeddingsService } from "./services/embeddings.service.js";
7
+ import { MemoryService } from "./services/memory.service.js";
8
+ import { startServer } from "./mcp/server.js";
9
+
10
+ async function main(): Promise<void> {
11
+ // Initialize database
12
+ const db = await connectToDatabase(config.dbPath);
13
+
14
+ // Initialize layers
15
+ const repository = new MemoryRepository(db);
16
+ const embeddings = new EmbeddingsService(config.embeddingModel, config.embeddingDimension);
17
+ const memoryService = new MemoryService(repository, embeddings);
18
+
19
+ // Start MCP server
20
+ await startServer(memoryService);
21
+ }
22
+
23
+ main().catch(console.error);
@@ -0,0 +1,111 @@
1
+ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
+ import type { MemoryService } from "../services/memory.service.js";
3
+
4
+ export async function handleStoreMemory(
5
+ args: Record<string, unknown> | undefined,
6
+ service: MemoryService
7
+ ): Promise<CallToolResult> {
8
+ const content = args?.content as string;
9
+ const metadata = (args?.metadata as Record<string, unknown>) ?? {};
10
+ const memory = await service.store(content, metadata);
11
+
12
+ return {
13
+ content: [{ type: "text", text: `Memory stored with ID: ${memory.id}` }],
14
+ };
15
+ }
16
+
17
+ export async function handleDeleteMemory(
18
+ args: Record<string, unknown> | undefined,
19
+ service: MemoryService
20
+ ): Promise<CallToolResult> {
21
+ const id = args?.id as string;
22
+ const success = await service.delete(id);
23
+
24
+ return {
25
+ content: [
26
+ {
27
+ type: "text",
28
+ text: success
29
+ ? `Memory ${id} deleted successfully`
30
+ : `Memory ${id} not found`,
31
+ },
32
+ ],
33
+ };
34
+ }
35
+
36
+ export async function handleSearchMemories(
37
+ args: Record<string, unknown> | undefined,
38
+ service: MemoryService
39
+ ): Promise<CallToolResult> {
40
+ const query = args?.query as string;
41
+ const limit = (args?.limit as number) ?? 10;
42
+ const memories = await service.search(query, limit);
43
+
44
+ if (memories.length === 0) {
45
+ return {
46
+ content: [{ type: "text", text: "No memories found matching your query." }],
47
+ };
48
+ }
49
+
50
+ const results = memories.map((mem) => {
51
+ let result = `ID: ${mem.id}\nContent: ${mem.content}`;
52
+ if (Object.keys(mem.metadata).length > 0) {
53
+ result += `\nMetadata: ${JSON.stringify(mem.metadata)}`;
54
+ }
55
+ return result;
56
+ });
57
+
58
+ return {
59
+ content: [{ type: "text", text: results.join("\n\n---\n\n") }],
60
+ };
61
+ }
62
+
63
+ export async function handleGetMemory(
64
+ args: Record<string, unknown> | undefined,
65
+ service: MemoryService
66
+ ): Promise<CallToolResult> {
67
+ const id = args?.id as string;
68
+ const memory = await service.get(id);
69
+
70
+ if (!memory) {
71
+ return {
72
+ content: [{ type: "text", text: `Memory ${id} not found` }],
73
+ };
74
+ }
75
+
76
+ let result = `ID: ${memory.id}\nContent: ${memory.content}`;
77
+ if (Object.keys(memory.metadata).length > 0) {
78
+ result += `\nMetadata: ${JSON.stringify(memory.metadata)}`;
79
+ }
80
+ result += `\nCreated: ${memory.createdAt.toISOString()}`;
81
+ result += `\nUpdated: ${memory.updatedAt.toISOString()}`;
82
+ if (memory.supersededBy) {
83
+ result += `\nSuperseded by: ${memory.supersededBy}`;
84
+ }
85
+
86
+ return {
87
+ content: [{ type: "text", text: result }],
88
+ };
89
+ }
90
+
91
+ export async function handleToolCall(
92
+ name: string,
93
+ args: Record<string, unknown> | undefined,
94
+ service: MemoryService
95
+ ): Promise<CallToolResult> {
96
+ switch (name) {
97
+ case "store_memory":
98
+ return handleStoreMemory(args, service);
99
+ case "delete_memory":
100
+ return handleDeleteMemory(args, service);
101
+ case "search_memories":
102
+ return handleSearchMemories(args, service);
103
+ case "get_memory":
104
+ return handleGetMemory(args, service);
105
+ default:
106
+ return {
107
+ content: [{ type: "text", text: `Unknown tool: ${name}` }],
108
+ isError: true,
109
+ };
110
+ }
111
+ }
@@ -0,0 +1,34 @@
1
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3
+ import {
4
+ CallToolRequestSchema,
5
+ ListToolsRequestSchema,
6
+ } from "@modelcontextprotocol/sdk/types.js";
7
+
8
+ import { tools } from "./tools.js";
9
+ import { handleToolCall } from "./handlers.js";
10
+ import type { MemoryService } from "../services/memory.service.js";
11
+
12
+ export function createServer(memoryService: MemoryService): Server {
13
+ const server = new Server(
14
+ { name: "vector-memory-mcp", version: "0.2.0" },
15
+ { capabilities: { tools: {} } }
16
+ );
17
+
18
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
19
+ return { tools };
20
+ });
21
+
22
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
23
+ const { name, arguments: args } = request.params;
24
+ return handleToolCall(name, args, memoryService);
25
+ });
26
+
27
+ return server;
28
+ }
29
+
30
+ export async function startServer(memoryService: MemoryService): Promise<void> {
31
+ const server = createServer(memoryService);
32
+ const transport = new StdioServerTransport();
33
+ await server.connect(transport);
34
+ }
@@ -0,0 +1,80 @@
1
+ import type { Tool } from "@modelcontextprotocol/sdk/types.js";
2
+
3
+ export const storeMemoryTool: Tool = {
4
+ name: "store_memory",
5
+ description: "Store a new memory. Use this to save information for later recall.",
6
+ inputSchema: {
7
+ type: "object",
8
+ properties: {
9
+ content: {
10
+ type: "string",
11
+ description: "The text content to store as a memory",
12
+ },
13
+ metadata: {
14
+ type: "object",
15
+ description: "Optional key-value metadata to attach to the memory",
16
+ additionalProperties: true,
17
+ },
18
+ },
19
+ required: ["content"],
20
+ },
21
+ };
22
+
23
+ export const deleteMemoryTool: Tool = {
24
+ name: "delete_memory",
25
+ description:
26
+ "Delete a memory by its ID. The memory will be soft-deleted and won't appear in search results.",
27
+ inputSchema: {
28
+ type: "object",
29
+ properties: {
30
+ id: {
31
+ type: "string",
32
+ description: "The ID of the memory to delete",
33
+ },
34
+ },
35
+ required: ["id"],
36
+ },
37
+ };
38
+
39
+ export const searchMemoriesTool: Tool = {
40
+ name: "search_memories",
41
+ description:
42
+ "Search for memories using semantic similarity. Returns the most relevant memories for the given query.",
43
+ inputSchema: {
44
+ type: "object",
45
+ properties: {
46
+ query: {
47
+ type: "string",
48
+ description: "The search query to find relevant memories",
49
+ },
50
+ limit: {
51
+ type: "integer",
52
+ description: "Maximum number of results to return (default: 10)",
53
+ default: 10,
54
+ },
55
+ },
56
+ required: ["query"],
57
+ },
58
+ };
59
+
60
+ export const getMemoryTool: Tool = {
61
+ name: "get_memory",
62
+ description: "Retrieve a specific memory by its ID.",
63
+ inputSchema: {
64
+ type: "object",
65
+ properties: {
66
+ id: {
67
+ type: "string",
68
+ description: "The ID of the memory to retrieve",
69
+ },
70
+ },
71
+ required: ["id"],
72
+ },
73
+ };
74
+
75
+ export const tools: Tool[] = [
76
+ storeMemoryTool,
77
+ deleteMemoryTool,
78
+ searchMemoriesTool,
79
+ getMemoryTool,
80
+ ];
@@ -0,0 +1,66 @@
1
+ import { pipeline, type FeatureExtractionPipeline } from "@huggingface/transformers";
2
+
3
+ export class EmbeddingsService {
4
+ private modelName: string;
5
+ private extractor: FeatureExtractionPipeline | null = null;
6
+ private initPromise: Promise<void> | null = null;
7
+ private _dimension: number;
8
+
9
+ constructor(modelName: string, dimension: number) {
10
+ this.modelName = modelName;
11
+ this._dimension = dimension;
12
+ }
13
+
14
+ get dimension(): number {
15
+ return this._dimension;
16
+ }
17
+
18
+ private async init(): Promise<void> {
19
+ if (this.extractor) {
20
+ return; // Already initialized
21
+ }
22
+
23
+ if (!this.initPromise) {
24
+ this.initPromise = (async () => {
25
+ this.extractor = await pipeline("feature-extraction", this.modelName, {
26
+ dtype: "fp32",
27
+ });
28
+ })();
29
+ }
30
+ await this.initPromise;
31
+ }
32
+
33
+ async embed(text: string): Promise<number[]> {
34
+ await this.init();
35
+ if (!this.extractor) {
36
+ throw new Error("Extractor not initialized.");
37
+ }
38
+
39
+ const output = await this.extractor(text, {
40
+ pooling: "mean",
41
+ normalize: true,
42
+ });
43
+
44
+ // output.tolist() returns number[][] for single input, we want the first element
45
+ const embeddings = output.tolist() as number[][];
46
+ return embeddings[0];
47
+ }
48
+
49
+ async embedBatch(texts: string[]): Promise<number[][]> {
50
+ await this.init();
51
+ if (!this.extractor) {
52
+ throw new Error("Extractor not initialized.");
53
+ }
54
+
55
+ if (texts.length === 0) {
56
+ return [];
57
+ }
58
+
59
+ const output = await this.extractor(texts, {
60
+ pooling: "mean",
61
+ normalize: true,
62
+ });
63
+
64
+ return output.tolist() as number[][];
65
+ }
66
+ }
@@ -0,0 +1,102 @@
1
+ import { randomUUID } from "crypto";
2
+ import type { Memory } from "../types/memory.js";
3
+ import { DELETED_TOMBSTONE, isSuperseded } from "../types/memory.js";
4
+ import type { MemoryRepository } from "../db/memory.repository.js";
5
+ import type { EmbeddingsService } from "./embeddings.service.js";
6
+
7
+ export class MemoryService {
8
+ constructor(
9
+ private repository: MemoryRepository,
10
+ private embeddings: EmbeddingsService
11
+ ) {}
12
+
13
+ async store(content: string, metadata: Record<string, unknown> = {}): Promise<Memory> {
14
+ const id = randomUUID();
15
+ const now = new Date();
16
+ const embedding = await this.embeddings.embed(content);
17
+
18
+ const memory: Memory = {
19
+ id,
20
+ content,
21
+ embedding,
22
+ metadata,
23
+ createdAt: now,
24
+ updatedAt: now,
25
+ supersededBy: null,
26
+ };
27
+
28
+ await this.repository.insert(memory);
29
+ return memory;
30
+ }
31
+
32
+ async get(id: string): Promise<Memory | null> {
33
+ return await this.repository.findById(id);
34
+ }
35
+
36
+ async delete(id: string): Promise<boolean> {
37
+ return await this.repository.markDeleted(id);
38
+ }
39
+
40
+ async search(query: string, limit: number = 10): Promise<Memory[]> {
41
+ const queryEmbedding = await this.embeddings.embed(query);
42
+ const fetchLimit = limit * 3;
43
+
44
+ const rows = await this.repository.findSimilar(queryEmbedding, fetchLimit);
45
+
46
+ const results: Memory[] = [];
47
+ const seenIds = new Set<string>();
48
+
49
+ for (const row of rows) {
50
+ let memory = await this.repository.findById(row.id);
51
+
52
+ if (!memory) {
53
+ continue;
54
+ }
55
+
56
+ if (isSuperseded(memory)) {
57
+ memory = await this.followSupersessionChain(row.id);
58
+ if (!memory) {
59
+ continue;
60
+ }
61
+ }
62
+
63
+ if (seenIds.has(memory.id)) {
64
+ continue;
65
+ }
66
+ seenIds.add(memory.id);
67
+
68
+ results.push(memory);
69
+ if (results.length >= limit) {
70
+ break;
71
+ }
72
+ }
73
+
74
+ return results;
75
+ }
76
+
77
+ private async followSupersessionChain(memoryId: string): Promise<Memory | null> {
78
+ const visited = new Set<string>();
79
+ let currentId: string | null = memoryId;
80
+
81
+ while (currentId && !visited.has(currentId)) {
82
+ visited.add(currentId);
83
+ const memory = await this.repository.findById(currentId);
84
+
85
+ if (!memory) {
86
+ return null;
87
+ }
88
+
89
+ if (memory.supersededBy === null) {
90
+ return memory;
91
+ }
92
+
93
+ if (memory.supersededBy === DELETED_TOMBSTONE) {
94
+ return null;
95
+ }
96
+
97
+ currentId = memory.supersededBy;
98
+ }
99
+
100
+ return null;
101
+ }
102
+ }
@@ -0,0 +1,35 @@
1
+ export const DELETED_TOMBSTONE = "DELETED";
2
+
3
+ export interface Memory {
4
+ id: string;
5
+ content: string;
6
+ embedding: number[];
7
+ metadata: Record<string, unknown>;
8
+ createdAt: Date;
9
+ updatedAt: Date;
10
+ supersededBy: string | null;
11
+ }
12
+
13
+ export interface VectorRow {
14
+ id: string;
15
+ distance: number;
16
+ }
17
+
18
+ export function isDeleted(memory: Memory): boolean {
19
+ return memory.supersededBy === DELETED_TOMBSTONE;
20
+ }
21
+
22
+ export function isSuperseded(memory: Memory): boolean {
23
+ return memory.supersededBy !== null;
24
+ }
25
+
26
+ export function memoryToDict(memory: Memory): Record<string, unknown> {
27
+ return {
28
+ id: memory.id,
29
+ content: memory.content,
30
+ metadata: memory.metadata,
31
+ createdAt: memory.createdAt.toISOString(),
32
+ updatedAt: memory.updatedAt.toISOString(),
33
+ supersededBy: memory.supersededBy,
34
+ };
35
+ }