@cybermem/mcp 0.6.10 → 0.8.3

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/dist/index.js CHANGED
@@ -1,287 +1,259 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ /**
4
+ * CyberMem MCP Server
5
+ *
6
+ * Supports two modes:
7
+ * 1. Local/Server Mode (default): Uses openmemory-js SDK directly.
8
+ * 2. Remote Client Mode (with --url): Proxies requests to a remote CyberMem server via HTTP.
9
+ */
3
10
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
11
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
12
  };
6
13
  Object.defineProperty(exports, "__esModule", { value: true });
7
- const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js");
8
- const sse_js_1 = require("@modelcontextprotocol/sdk/server/sse.js");
14
+ const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
9
15
  const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
10
- const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
16
+ const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
11
17
  const axios_1 = __importDefault(require("axios"));
12
18
  const cors_1 = __importDefault(require("cors"));
13
19
  const dotenv_1 = __importDefault(require("dotenv"));
14
20
  const express_1 = __importDefault(require("express"));
21
+ const openmemory_js_1 = require("openmemory-js");
22
+ const zod_1 = require("zod");
23
+ const auth_js_1 = require("./auth.js");
15
24
  dotenv_1.default.config();
16
- // Parse CLI args for remote mode
25
+ // Handle CLI auth commands first
17
26
  const args = process.argv.slice(2);
18
- const getArg = (name) => {
19
- const idx = args.indexOf(name);
20
- return idx !== -1 && args[idx + 1] ? args[idx + 1] : undefined;
21
- };
22
- const cliUrl = getArg("--url");
23
- const cliApiKey = getArg("--api-key");
24
- const cliClientName = getArg("--client-name");
25
- // Use CLI args first, then env, then defaults
26
- // Default to local CyberMem backend (via Traefik on port 8626)
27
- const API_URL = cliUrl || process.env.CYBERMEM_URL || "http://localhost:8626/memory";
28
- const API_KEY = cliApiKey || process.env.OM_API_KEY || "";
29
- // Track client name per session
30
- let currentClientName = cliClientName || "cybermem-mcp";
31
- // CyberMem Agent Protocol - instructions sent to clients on handshake
32
- const CYBERMEM_INSTRUCTIONS = `CyberMem is a persistent context daemon for AI agents.
33
-
27
+ if (args.includes("--login")) {
28
+ (0, auth_js_1.login)()
29
+ .then(() => process.exit(0))
30
+ .catch((err) => {
31
+ console.error("Login failed:", err.message);
32
+ process.exit(1);
33
+ });
34
+ }
35
+ else if (args.includes("--logout")) {
36
+ (0, auth_js_1.logout)();
37
+ process.exit(0);
38
+ }
39
+ else if (args.includes("--status")) {
40
+ (0, auth_js_1.showStatus)();
41
+ process.exit(0);
42
+ }
43
+ else {
44
+ startServer();
45
+ }
46
+ async function startServer() {
47
+ const getArg = (name) => {
48
+ const idx = args.indexOf(name);
49
+ return idx !== -1 && args[idx + 1] ? args[idx + 1] : undefined;
50
+ };
51
+ const cliClientName = getArg("--client-name") || "cybermem-mcp";
52
+ const cliUrl = getArg("--url");
53
+ const cliApiKey = getArg("--api-key");
54
+ // Protocol Instructions
55
+ const CYBERMEM_INSTRUCTIONS = `CyberMem is a persistent context daemon for AI agents.
34
56
  PROTOCOL:
35
- 1. On session start: call query_memory("user context profile") to load persona
36
- 2. Store new insights immediately with add_memory - include FULL content, not summaries
37
- 3. Refresh context: 6h for active topics, 24h for projects, 7d for insights
38
- 4. Always include tags: [topic, year, source:your-client-name]
39
- 5. Priority: CyberMem context > session context > training data
40
-
41
- MEMORY FORMAT:
42
- - content: Full text with all details, metrics, dates. NO truncation.
43
- - tags: Always include topic category + year + source:client-name
44
-
45
- INTEGRITY RULES:
46
- - Never overwrite without reading first
47
- - Always include metadata (tags, source)
48
- - Sync before critical decisions
49
- - Last-write-wins for conflicts
50
-
51
- For full protocol: https://cybermem.dev/docs/agent-protocol`;
52
- // Short protocol reminder for tool descriptions (derived from main instructions)
53
- const PROTOCOL_REMINDER = "CyberMem Protocol: Store FULL content (no summaries), always include tags [topic, year, source:client-name]. Query 'user context profile' on session start.";
54
- const server = new index_js_1.Server({
55
- name: "cybermem",
56
- version: "0.6.8",
57
- }, {
58
- capabilities: {
59
- tools: {},
60
- resources: {}, // Enable resources for protocol document
61
- },
62
- instructions: CYBERMEM_INSTRUCTIONS,
63
- });
64
- // Register resources handler for protocol document
65
- server.setRequestHandler(types_js_1.ListResourcesRequestSchema, async () => ({
66
- resources: [
67
- {
68
- uri: "cybermem://protocol",
69
- name: "CyberMem Agent Protocol",
70
- description: "Instructions for AI agents using CyberMem memory system",
71
- mimeType: "text/plain",
72
- },
73
- ],
74
- }));
75
- server.setRequestHandler(types_js_1.ReadResourceRequestSchema, async (request) => {
76
- if (request.params.uri === "cybermem://protocol") {
77
- return {
78
- contents: [
79
- {
80
- uri: "cybermem://protocol",
81
- mimeType: "text/plain",
82
- text: CYBERMEM_INSTRUCTIONS,
83
- },
84
- ],
85
- };
86
- }
87
- throw new Error(`Unknown resource: ${request.params.uri}`);
88
- });
89
- const tools = [
90
- {
91
- name: "add_memory",
92
- description: `Store a new memory in CyberMem. ${PROTOCOL_REMINDER}`,
93
- inputSchema: {
94
- type: "object",
95
- properties: {
96
- content: {
97
- type: "string",
98
- description: "Full content with all details - NO truncation or summarization",
99
- },
100
- user_id: { type: "string" },
101
- tags: {
102
- type: "array",
103
- items: { type: "string" },
104
- description: "Always include [topic, year, source:your-client-name]",
105
- },
106
- },
107
- required: ["content"],
108
- },
109
- },
110
- {
111
- name: "query_memory",
112
- description: `Search for relevant memories. On session start, call query_memory("user context profile") first.`,
113
- inputSchema: {
114
- type: "object",
115
- properties: {
116
- query: { type: "string" },
117
- k: { type: "number", default: 5 },
118
- },
119
- required: ["query"],
120
- },
121
- },
122
- {
123
- name: "list_memories",
124
- description: "List recent memories",
125
- inputSchema: {
126
- type: "object",
127
- properties: {
128
- limit: { type: "number", default: 10 },
129
- },
130
- },
131
- },
132
- {
133
- name: "delete_memory",
134
- description: "Delete a memory by ID",
135
- inputSchema: {
136
- type: "object",
137
- properties: {
138
- id: { type: "string" },
57
+ 1. On session start: call query_memory("user context profile")
58
+ 2. Store new insights immediately with add_memory (FULL content)
59
+ 3. Always include tags: [topic, year, source:your-client-name]
60
+ For full protocol: https://docs.cybermem.dev/agent-protocol`;
61
+ const server = new mcp_js_1.McpServer({ name: "cybermem", version: "0.8.2" }, {
62
+ instructions: CYBERMEM_INSTRUCTIONS,
63
+ });
64
+ server.registerResource("CyberMem Agent Protocol", "cybermem://protocol", { description: "Instructions for AI agents", mimeType: "text/plain" }, async () => ({
65
+ contents: [
66
+ {
67
+ uri: "cybermem://protocol",
68
+ mimeType: "text/plain",
69
+ text: CYBERMEM_INSTRUCTIONS,
139
70
  },
140
- required: ["id"],
141
- },
142
- },
143
- {
144
- name: "update_memory",
145
- description: "Update a memory by ID",
146
- inputSchema: {
147
- type: "object",
148
- properties: {
149
- id: { type: "string" },
150
- content: { type: "string" },
151
- tags: { type: "array", items: { type: "string" } },
152
- metadata: { type: "object" },
71
+ ],
72
+ }));
73
+ // --- IMPLEMENTATION LOGIC ---
74
+ let memory = null;
75
+ let apiClient = null;
76
+ if (cliUrl) {
77
+ // REMOTE CLIENT MODE
78
+ console.error(`Connecting to remote CyberMem at ${cliUrl}`);
79
+ apiClient = axios_1.default.create({
80
+ baseURL: cliUrl,
81
+ headers: {
82
+ Authorization: `Bearer ${cliApiKey}`,
83
+ "X-Client-Name": cliClientName,
153
84
  },
154
- required: ["id"],
155
- },
156
- },
157
- ];
158
- server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => ({
159
- tools,
160
- }));
161
- // Create axios instance
162
- const apiClient = axios_1.default.create({
163
- baseURL: API_URL,
164
- headers: {
165
- Authorization: `Bearer ${API_KEY}`,
166
- },
167
- });
168
- // Helper to get client with context
169
- function getClient(customHeaders = {}) {
170
- // Get client name from MCP protocol (sent during initialize) or fallback to CLI arg
171
- const clientVersion = server.getClientVersion();
172
- const clientName = customHeaders["X-Client-Name"] || clientVersion?.name || currentClientName;
173
- return {
174
- ...apiClient,
175
- get: (url, config) => apiClient.get(url, {
176
- ...config,
177
- headers: { "X-Client-Name": clientName, ...config?.headers },
178
- }),
179
- post: (url, data, config) => apiClient.post(url, data, {
180
- ...config,
181
- headers: { "X-Client-Name": clientName, ...config?.headers },
182
- }),
183
- put: (url, data, config) => apiClient.put(url, data, {
184
- ...config,
185
- headers: { "X-Client-Name": clientName, ...config?.headers },
186
- }),
187
- patch: (url, data, config) => apiClient.patch(url, data, {
188
- ...config,
189
- headers: { "X-Client-Name": clientName, ...config?.headers },
190
- }),
191
- delete: (url, config) => apiClient.delete(url, {
192
- ...config,
193
- headers: { "X-Client-Name": clientName, ...config?.headers },
194
- }),
85
+ });
86
+ }
87
+ else {
88
+ // LOCAL SDK MODE
89
+ const homedir = process.env.HOME || process.env.USERPROFILE || "";
90
+ // Default to ~/.cybermem/data if OM_DB_PATH not set
91
+ if (!process.env.OM_DB_PATH) {
92
+ process.env.OM_DB_PATH = `${homedir}/.cybermem/data/openmemory.sqlite`;
93
+ }
94
+ // Ensure directory exists
95
+ const fs = require("fs");
96
+ try {
97
+ const dbPath = process.env.OM_DB_PATH;
98
+ const dir = dbPath.substring(0, dbPath.lastIndexOf("/"));
99
+ if (dir)
100
+ fs.mkdirSync(dir, { recursive: true });
101
+ }
102
+ catch { }
103
+ memory = new openmemory_js_1.Memory();
104
+ }
105
+ // Helper to add source tag
106
+ const addSourceTag = (tags = []) => {
107
+ if (!tags.some((t) => t.startsWith("source:")))
108
+ tags.push(`source:${cliClientName}`);
109
+ return tags;
195
110
  };
196
- }
197
- server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
198
- const { name, arguments: args } = request.params;
199
- try {
200
- switch (name) {
201
- case "add_memory": {
202
- const response = await getClient().post("/add", args);
203
- return {
204
- content: [{ type: "text", text: JSON.stringify(response.data) }],
205
- };
206
- }
207
- case "query_memory": {
208
- const response = await getClient().post("/query", args);
209
- return {
210
- content: [{ type: "text", text: JSON.stringify(response.data) }],
211
- };
212
- }
213
- case "list_memories": {
214
- const limit = args?.limit || 10;
215
- const response = await getClient().get(`/all?l=${limit}`);
111
+ // --- TOOLS ---
112
+ server.registerTool("add_memory", {
113
+ description: "Store a new memory. " + CYBERMEM_INSTRUCTIONS,
114
+ inputSchema: zod_1.z.object({
115
+ content: zod_1.z.string(),
116
+ user_id: zod_1.z.string().optional(),
117
+ tags: zod_1.z.array(zod_1.z.string()).optional(),
118
+ }),
119
+ }, async (args) => {
120
+ const tags = addSourceTag(args.tags);
121
+ if (cliUrl) {
122
+ const res = await apiClient.post("/add", { ...args, tags });
123
+ return { content: [{ type: "text", text: JSON.stringify(res.data) }] };
124
+ }
125
+ else {
126
+ const res = await memory.add(args.content, {
127
+ user_id: args.user_id,
128
+ tags,
129
+ });
130
+ return { content: [{ type: "text", text: JSON.stringify(res) }] };
131
+ }
132
+ });
133
+ server.registerTool("query_memory", {
134
+ description: "Search memories.",
135
+ inputSchema: zod_1.z.object({ query: zod_1.z.string(), k: zod_1.z.number().default(5) }),
136
+ }, async (args) => {
137
+ if (cliUrl) {
138
+ const res = await apiClient.post("/query", args);
139
+ return { content: [{ type: "text", text: JSON.stringify(res.data) }] };
140
+ }
141
+ else {
142
+ const res = await memory.search(args.query, { limit: args.k });
143
+ return { content: [{ type: "text", text: JSON.stringify(res) }] };
144
+ }
145
+ });
146
+ server.registerTool("list_memories", {
147
+ description: "List recent memories",
148
+ inputSchema: zod_1.z.object({ limit: zod_1.z.number().default(10) }),
149
+ }, async (args) => {
150
+ if (cliUrl) {
151
+ // Fallback to /query with empty string if /list not available, or use /all
152
+ // Old API had /all
153
+ try {
154
+ const res = await apiClient.get(`/all?limit=${args.limit}`);
216
155
  return {
217
- content: [{ type: "text", text: JSON.stringify(response.data) }],
156
+ content: [{ type: "text", text: JSON.stringify(res.data) }],
218
157
  };
219
158
  }
220
- case "delete_memory": {
221
- const { id } = args;
222
- await getClient().delete(`/${id}`);
223
- return { content: [{ type: "text", text: `Memory ${id} deleted` }] };
224
- }
225
- case "update_memory": {
226
- const { id, ...updates } = args;
227
- const response = await getClient().patch(`/${id}`, updates);
159
+ catch {
160
+ const res = await apiClient.post("/query", {
161
+ query: "",
162
+ k: args.limit,
163
+ });
228
164
  return {
229
- content: [{ type: "text", text: JSON.stringify(response.data) }],
165
+ content: [{ type: "text", text: JSON.stringify(res.data) }],
230
166
  };
231
167
  }
232
- default:
233
- throw new Error(`Unknown tool: ${name}`);
234
168
  }
235
- }
236
- catch (error) {
237
- return {
238
- content: [{ type: "text", text: `Error: ${error.message}` }],
239
- isError: true,
240
- };
241
- }
242
- });
243
- async function run() {
244
- const isSse = process.argv.includes("--sse") || !!process.env.PORT;
245
- if (isSse) {
169
+ else {
170
+ const res = await memory.search("", { limit: args.limit });
171
+ return { content: [{ type: "text", text: JSON.stringify(res) }] };
172
+ }
173
+ });
174
+ server.registerTool("delete_memory", {
175
+ description: "Delete memory by ID",
176
+ inputSchema: zod_1.z.object({ id: zod_1.z.string() }),
177
+ }, async (args) => {
178
+ if (cliUrl) {
179
+ const res = await apiClient.delete(`/${args.id}`);
180
+ return { content: [{ type: "text", text: JSON.stringify(res.data) }] };
181
+ }
182
+ else {
183
+ return {
184
+ content: [
185
+ { type: "text", text: "Delete not implemented in SDK yet" },
186
+ ],
187
+ };
188
+ }
189
+ });
190
+ server.registerTool("update_memory", {
191
+ description: "Update memory",
192
+ inputSchema: zod_1.z.object({ id: zod_1.z.string(), content: zod_1.z.string().optional() }),
193
+ }, async (args) => {
194
+ return { content: [{ type: "text", text: "Update not implemented" }] };
195
+ });
196
+ // --- TRANSPORT ---
197
+ const useHttp = args.includes("--http") || args.includes("--port");
198
+ if (useHttp) {
199
+ const port = parseInt(getArg("--port") || "3100", 10);
246
200
  const app = (0, express_1.default)();
247
201
  app.use((0, cors_1.default)());
248
- const port = process.env.PORT || 8627;
249
- let transport = null;
250
- app.get("/sse", async (req, res) => {
251
- // Extract client name from header
252
- const clientName = req.headers["x-client-name"];
253
- if (clientName) {
254
- currentClientName = clientName;
255
- }
256
- transport = new sse_js_1.SSEServerTransport("/messages", res);
257
- await server.connect(transport);
258
- });
259
- app.post("/messages", async (req, res) => {
260
- // Also check headers on messages
261
- const clientName = req.headers["x-client-name"];
262
- if (clientName) {
263
- currentClientName = clientName;
264
- }
265
- if (transport) {
266
- await transport.handlePostMessage(req, res);
267
- }
268
- else {
269
- res.status(400).send("Session not established");
270
- }
202
+ app.use(express_1.default.json());
203
+ app.get("/health", (req, res) => res.json({ ok: true, version: "0.8.2", mode: cliUrl ? "proxy" : "sdk" }));
204
+ // REST API Compatibility (for Remote Clients)
205
+ // Only enable if in SDK mode (Server)
206
+ if (!cliUrl && memory) {
207
+ app.post("/add", async (req, res) => {
208
+ try {
209
+ const { content, user_id, tags } = req.body;
210
+ const finalTags = addSourceTag(tags);
211
+ const result = await memory.add(content, {
212
+ user_id,
213
+ tags: finalTags,
214
+ });
215
+ res.json(result);
216
+ }
217
+ catch (e) {
218
+ res.status(500).json({ error: e.message });
219
+ }
220
+ });
221
+ app.post("/query", async (req, res) => {
222
+ try {
223
+ const { query, k } = req.body;
224
+ const result = await memory.search(query || "", { limit: k || 5 });
225
+ res.json(result);
226
+ }
227
+ catch (e) {
228
+ res.status(500).json({ error: e.message });
229
+ }
230
+ });
231
+ app.get("/all", async (req, res) => {
232
+ try {
233
+ const limit = parseInt(req.query.limit) || 10;
234
+ const result = await memory.search("", { limit });
235
+ res.json(result);
236
+ }
237
+ catch (e) {
238
+ res.status(500).json({ error: e.message });
239
+ }
240
+ });
241
+ }
242
+ const transport = new streamableHttp_js_1.StreamableHTTPServerTransport({
243
+ sessionIdGenerator: () => crypto.randomUUID(),
271
244
  });
272
- app.listen(port, () => {
273
- console.error(`CyberMem MCP Server running on SSE at http://localhost:${port}`);
274
- console.error(` - SSE endpoint: http://localhost:${port}/sse`);
275
- console.error(` - Message endpoint: http://localhost:${port}/messages`);
245
+ app.all("/mcp", async (req, res) => await transport.handleRequest(req, res, req.body));
246
+ app.all("/sse", async (req, res) => await transport.handleRequest(req, res, req.body));
247
+ server.connect(transport).then(() => {
248
+ app.listen(port, () => {
249
+ console.log(`CyberMem MCP running on http://localhost:${port}`);
250
+ });
276
251
  });
277
252
  }
278
253
  else {
279
254
  const transport = new stdio_js_1.StdioServerTransport();
280
- await server.connect(transport);
281
- console.error("CyberMem MCP Server running on stdio");
255
+ server
256
+ .connect(transport)
257
+ .then(() => console.error("CyberMem MCP connected via STDIO"));
282
258
  }
283
259
  }
284
- run().catch((error) => {
285
- console.error("Fatal error running server:", error);
286
- process.exit(1);
287
- });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cybermem/mcp",
3
- "version": "0.6.10",
4
- "description": "CyberMem MCP Server (TypeScript)",
3
+ "version": "0.8.3",
4
+ "description": "CyberMem MCP Server - AI Memory with openmemory-js SDK",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
7
7
  "cybermem-mcp": "./dist/index.js"
@@ -39,7 +39,9 @@
39
39
  "axios": "^1.13.2",
40
40
  "cors": "^2.8.5",
41
41
  "dotenv": "^16.0.0",
42
- "express": "^5.2.1"
42
+ "express": "^5.2.1",
43
+ "openmemory-js": "^1.3.2",
44
+ "zod": "^3.25.76"
43
45
  },
44
46
  "devDependencies": {
45
47
  "@types/cors": "^2.8.19",