mem0-mcp-server 1.0.4 → 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.
- package/package.json +12 -8
- package/server.js +128 -50
package/package.json
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mem0-mcp-server",
|
|
3
|
-
"version": "
|
|
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
|
-
|
|
11
|
-
|
|
9
|
+
"files": ["server.js"],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "node server.js"
|
|
12
|
+
},
|
|
12
13
|
"dependencies": {
|
|
13
|
-
"@modelcontextprotocol/sdk": "^1.
|
|
14
|
+
"@modelcontextprotocol/sdk": "^1.26.0"
|
|
14
15
|
},
|
|
15
|
-
"keywords": ["mcp", "mem0", "memory"],
|
|
16
|
-
"
|
|
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,75 +3,153 @@ 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 MEM0_API_URL = "http://
|
|
7
|
-
const MEM0_API_TOKEN = "
|
|
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
|
+
|
|
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
|
+
}
|
|
8
21
|
|
|
9
22
|
const server = new Server(
|
|
10
|
-
{ name: "mem0-mcp-server", version: "
|
|
23
|
+
{ name: "mem0-mcp-server", version: "2.0.0" },
|
|
11
24
|
{ capabilities: { tools: {} } }
|
|
12
25
|
);
|
|
13
26
|
|
|
14
27
|
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
15
28
|
tools: [
|
|
16
|
-
{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
+
],
|
|
24
113
|
}));
|
|
25
114
|
|
|
26
115
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
27
116
|
const { name, arguments: args } = request.params;
|
|
28
|
-
const
|
|
117
|
+
const uid = args.user_id || DEFAULT_USER_ID;
|
|
29
118
|
try {
|
|
30
|
-
let
|
|
119
|
+
let result;
|
|
31
120
|
switch (name) {
|
|
32
|
-
case "
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
case "delete_memory":
|
|
57
|
-
res = await fetch(`${MEM0_API_URL}/memory/delete`, { method: "DELETE", headers, body: JSON.stringify(args) });
|
|
58
|
-
data = await res.json();
|
|
59
|
-
return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
|
|
60
|
-
case "delete_all_memories":
|
|
61
|
-
const params2 = new URLSearchParams();
|
|
62
|
-
if (args.user_id) params2.append("user_id", args.user_id);
|
|
63
|
-
if (args.agent_id) params2.append("agent_id", args.agent_id);
|
|
64
|
-
if (args.run_id) params2.append("run_id", args.run_id);
|
|
65
|
-
res = await fetch(`${MEM0_API_URL}/memory/delete_all?${params2}`, { method: "DELETE", headers });
|
|
66
|
-
data = await res.json();
|
|
67
|
-
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;
|
|
68
145
|
default:
|
|
69
146
|
throw new Error(`Unknown tool: ${name}`);
|
|
70
147
|
}
|
|
148
|
+
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
71
149
|
} catch (error) {
|
|
72
150
|
return { content: [{ type: "text", text: `Error: ${error.message}` }], isError: true };
|
|
73
151
|
}
|
|
74
152
|
});
|
|
75
153
|
|
|
76
154
|
const transport = new StdioServerTransport();
|
|
77
|
-
await server.connect(transport);
|
|
155
|
+
await server.connect(transport);
|