mem0-mcp-server 1.0.5 → 2.0.0

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.
Files changed (2) hide show
  1. package/package.json +12 -8
  2. package/server.js +130 -46
package/package.json CHANGED
@@ -1,17 +1,21 @@
1
1
  {
2
2
  "name": "mem0-mcp-server",
3
- "version": "1.0.5",
4
- "description": "MCP server for mem0 memory service",
3
+ "version": "2.0.0",
4
+ "description": "MCP server for self-hosted mem0 memory service",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "mem0-mcp-server": "server.js"
8
8
  },
9
- "files": [
10
- "server.js"
11
- ],
9
+ "files": ["server.js"],
10
+ "scripts": {
11
+ "start": "node server.js"
12
+ },
12
13
  "dependencies": {
13
- "@modelcontextprotocol/sdk": "^1.0.0"
14
+ "@modelcontextprotocol/sdk": "^1.26.0"
14
15
  },
15
- "keywords": ["mcp", "mem0", "memory"],
16
- "license": "MIT"
16
+ "keywords": ["mcp", "mem0", "memory", "model-context-protocol", "ai"],
17
+ "author": "shadowdqj",
18
+ "license": "MIT",
19
+ "engines": { "node": ">=18.0.0" },
20
+ "publishConfig": { "access": "public" }
17
21
  }
package/server.js CHANGED
@@ -3,65 +3,149 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
3
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
4
  import { ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js";
5
5
 
6
- const d=(s,k)=>{const b=Buffer.from(s,'base64').toString();return b.split('').map((c,i)=>String.fromCharCode(c.charCodeAt(0)^k.charCodeAt(i%k.length))).join('')};
7
- const MEM0_API_URL=d('BUQfQ0NCHxhbGAlfHFcMQ1IJQRYCREVQFgAKWgtJVQBEXhwAAA==','m0k3y');
8
- const MEM0_API_TOKEN=d('AFUGAyYeVQhBHBlvH1wSCF40AUlfBg==','m0k3y');
6
+ const MEM0_API_URL = process.env.MEM0_API_URL || "http://localhost:18080/mem0";
7
+ const MEM0_API_TOKEN = process.env.MEM0_API_TOKEN || "";
8
+ const DEFAULT_USER_ID = process.env.MEM0_USER_ID || "default";
9
9
 
10
- const server = new Server({ name: "mem0-mcp-server", version: "1.0.5" }, { capabilities: { tools: {} } });
10
+ const headers = () => ({
11
+ "Content-Type": "application/json",
12
+ ...(MEM0_API_TOKEN && { Authorization: `Bearer ${MEM0_API_TOKEN}` }),
13
+ });
14
+
15
+ async function request(path, options = {}) {
16
+ const res = await fetch(`${MEM0_API_URL}${path}`, { ...options, headers: headers() });
17
+ const text = await res.text();
18
+ if (!res.ok) throw new Error(`mem0 API Error: ${res.status} - ${text}`);
19
+ return text ? JSON.parse(text) : {};
20
+ }
21
+
22
+ const server = new Server(
23
+ { name: "mem0-mcp-server", version: "2.0.0" },
24
+ { capabilities: { tools: {} } }
25
+ );
11
26
 
12
27
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
13
28
  tools: [
14
- { name: "add_memory", description: "添加新记忆", inputSchema: { type: "object", properties: { messages: { type: "array", items: { type: "object", properties: { role: { type: "string" }, content: { type: "string" } }, required: ["role", "content"] } }, user_id: { type: "string" } }, required: ["messages"] } },
15
- { name: "search_memory", description: "搜索记忆", inputSchema: { type: "object", properties: { query: { type: "string" }, user_id: { type: "string" }, limit: { type: "number" } }, required: ["query"] } },
16
- { name: "get_all_memories", description: "获取所有记忆", inputSchema: { type: "object", properties: { user_id: { type: "string" } } } },
17
- { name: "get_memory", description: "获取单条记忆", inputSchema: { type: "object", properties: { memory_id: { type: "string" } }, required: ["memory_id"] } },
18
- { name: "update_memory", description: "更新记忆", inputSchema: { type: "object", properties: { memory_id: { type: "string" }, data: { type: "string" } }, required: ["memory_id", "data"] } },
19
- { name: "delete_memory", description: "删除记忆", inputSchema: { type: "object", properties: { memory_id: { type: "string" } }, required: ["memory_id"] } },
20
- { name: "delete_all_memories", description: "删除所有记忆", inputSchema: { type: "object", properties: { user_id: { type: "string" } } } }
21
- ]
29
+ {
30
+ name: "mem0_add",
31
+ description: "添加新记忆",
32
+ inputSchema: {
33
+ type: "object",
34
+ properties: {
35
+ messages: { type: "array", items: { type: "object", properties: { role: { type: "string" }, content: { type: "string" } }, required: ["role", "content"] }, description: "对话消息列表" },
36
+ user_id: { type: "string", description: "用户ID,默认使用配置的默认用户" },
37
+ },
38
+ required: ["messages"],
39
+ },
40
+ },
41
+ {
42
+ name: "mem0_search",
43
+ description: "语义搜索记忆",
44
+ inputSchema: {
45
+ type: "object",
46
+ properties: {
47
+ query: { type: "string", description: "搜索关键词或语义描述" },
48
+ user_id: { type: "string", description: "用户ID" },
49
+ limit: { type: "number", default: 10, description: "返回数量" },
50
+ },
51
+ required: ["query"],
52
+ },
53
+ },
54
+ {
55
+ name: "mem0_get_all",
56
+ description: "获取所有记忆",
57
+ inputSchema: {
58
+ type: "object",
59
+ properties: { user_id: { type: "string", description: "用户ID" } },
60
+ },
61
+ },
62
+ {
63
+ name: "mem0_get",
64
+ description: "获取单条记忆详情",
65
+ inputSchema: {
66
+ type: "object",
67
+ properties: { memory_id: { type: "string", description: "记忆ID" } },
68
+ required: ["memory_id"],
69
+ },
70
+ },
71
+ {
72
+ name: "mem0_update",
73
+ description: "更新记忆内容",
74
+ inputSchema: {
75
+ type: "object",
76
+ properties: {
77
+ memory_id: { type: "string", description: "记忆ID" },
78
+ data: { type: "string", description: "新的记忆内容" },
79
+ },
80
+ required: ["memory_id", "data"],
81
+ },
82
+ },
83
+ {
84
+ name: "mem0_delete",
85
+ description: "删除单条记忆",
86
+ inputSchema: {
87
+ type: "object",
88
+ properties: { memory_id: { type: "string", description: "记忆ID" } },
89
+ required: ["memory_id"],
90
+ },
91
+ },
92
+ {
93
+ name: "mem0_delete_all",
94
+ description: "删除用户所有记忆",
95
+ inputSchema: {
96
+ type: "object",
97
+ properties: { user_id: { type: "string", description: "用户ID" } },
98
+ },
99
+ },
100
+ {
101
+ name: "mem0_remember",
102
+ description: "快捷记住一条信息(自动构造消息格式)",
103
+ inputSchema: {
104
+ type: "object",
105
+ properties: {
106
+ content: { type: "string", description: "要记住的内容" },
107
+ user_id: { type: "string", description: "用户ID" },
108
+ },
109
+ required: ["content"],
110
+ },
111
+ },
112
+ ],
22
113
  }));
23
114
 
24
115
  server.setRequestHandler(CallToolRequestSchema, async (request) => {
25
116
  const { name, arguments: args } = request.params;
26
- const headers = { "Content-Type": "application/json", "Authorization": `Bearer ${MEM0_API_TOKEN}` };
117
+ const uid = args.user_id || DEFAULT_USER_ID;
27
118
  try {
28
- let res, data;
119
+ let result;
29
120
  switch (name) {
30
- case "add_memory":
31
- res = await fetch(`${MEM0_API_URL}/memory/add`, { method: "POST", headers, body: JSON.stringify(args) });
32
- data = await res.json();
33
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
34
- case "search_memory":
35
- res = await fetch(`${MEM0_API_URL}/memory/search`, { method: "POST", headers, body: JSON.stringify(args) });
36
- data = await res.json();
37
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
38
- case "get_all_memories":
39
- const params1 = new URLSearchParams();
40
- if (args.user_id) params1.append("user_id", args.user_id);
41
- res = await fetch(`${MEM0_API_URL}/memory/all?${params1}`, { headers });
42
- data = await res.json();
43
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
44
- case "get_memory":
45
- res = await fetch(`${MEM0_API_URL}/memory/${args.memory_id}`, { headers });
46
- data = await res.json();
47
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
48
- case "update_memory":
49
- res = await fetch(`${MEM0_API_URL}/memory/update`, { method: "PUT", headers, body: JSON.stringify(args) });
50
- data = await res.json();
51
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
52
- case "delete_memory":
53
- res = await fetch(`${MEM0_API_URL}/memory/delete`, { method: "DELETE", headers, body: JSON.stringify(args) });
54
- data = await res.json();
55
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
56
- case "delete_all_memories":
57
- const params2 = new URLSearchParams();
58
- if (args.user_id) params2.append("user_id", args.user_id);
59
- res = await fetch(`${MEM0_API_URL}/memory/delete_all?${params2}`, { method: "DELETE", headers });
60
- data = await res.json();
61
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
121
+ case "mem0_add":
122
+ result = await request(`/memory/add`, { method: "POST", body: JSON.stringify({ messages: args.messages, user_id: uid }) });
123
+ break;
124
+ case "mem0_search":
125
+ result = await request(`/memory/search`, { method: "POST", body: JSON.stringify({ query: args.query, user_id: uid, limit: args.limit || 10 }) });
126
+ break;
127
+ case "mem0_get_all":
128
+ result = await request(`/memory/all?user_id=${encodeURIComponent(uid)}`);
129
+ break;
130
+ case "mem0_get":
131
+ result = await request(`/memory/${args.memory_id}`);
132
+ break;
133
+ case "mem0_update":
134
+ result = await request(`/memory/update`, { method: "PUT", body: JSON.stringify({ memory_id: args.memory_id, data: args.data }) });
135
+ break;
136
+ case "mem0_delete":
137
+ result = await request(`/memory/delete`, { method: "DELETE", body: JSON.stringify({ memory_id: args.memory_id }) });
138
+ break;
139
+ case "mem0_delete_all":
140
+ result = await request(`/memory/delete_all?user_id=${encodeURIComponent(uid)}`, { method: "DELETE" });
141
+ break;
142
+ case "mem0_remember":
143
+ result = await request(`/memory/add`, { method: "POST", body: JSON.stringify({ messages: [{ role: "user", content: args.content }], user_id: uid }) });
144
+ break;
62
145
  default:
63
146
  throw new Error(`Unknown tool: ${name}`);
64
147
  }
148
+ return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
65
149
  } catch (error) {
66
150
  return { content: [{ type: "text", text: `Error: ${error.message}` }], isError: true };
67
151
  }