memax-cli 0.1.0-alpha.9 → 0.1.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 (197) hide show
  1. package/assets/skills/memax-memory/SKILL.md +173 -0
  2. package/dist/commands/agent-configs.d.ts +41 -0
  3. package/dist/commands/agent-configs.d.ts.map +1 -0
  4. package/dist/commands/agent-configs.js +1290 -0
  5. package/dist/commands/agent-configs.js.map +1 -0
  6. package/dist/commands/agent-configs.test.d.ts +2 -0
  7. package/dist/commands/agent-configs.test.d.ts.map +1 -0
  8. package/dist/commands/agent-configs.test.js +122 -0
  9. package/dist/commands/agent-configs.test.js.map +1 -0
  10. package/dist/commands/agent-sessions.d.ts +74 -0
  11. package/dist/commands/agent-sessions.d.ts.map +1 -0
  12. package/dist/commands/agent-sessions.js +1513 -0
  13. package/dist/commands/agent-sessions.js.map +1 -0
  14. package/dist/commands/agent-sessions.test.d.ts +2 -0
  15. package/dist/commands/agent-sessions.test.d.ts.map +1 -0
  16. package/dist/commands/agent-sessions.test.js +255 -0
  17. package/dist/commands/agent-sessions.test.js.map +1 -0
  18. package/dist/commands/agents.d.ts +3 -0
  19. package/dist/commands/agents.d.ts.map +1 -0
  20. package/dist/commands/agents.js +36 -0
  21. package/dist/commands/agents.js.map +1 -0
  22. package/dist/commands/ask.d.ts +15 -0
  23. package/dist/commands/ask.d.ts.map +1 -0
  24. package/dist/commands/ask.js +483 -0
  25. package/dist/commands/ask.js.map +1 -0
  26. package/dist/commands/auth.d.ts +7 -0
  27. package/dist/commands/auth.d.ts.map +1 -1
  28. package/dist/commands/auth.js +103 -8
  29. package/dist/commands/auth.js.map +1 -1
  30. package/dist/commands/capture.d.ts +19 -0
  31. package/dist/commands/capture.d.ts.map +1 -0
  32. package/dist/commands/capture.js +69 -0
  33. package/dist/commands/capture.js.map +1 -0
  34. package/dist/commands/config.d.ts +2 -0
  35. package/dist/commands/config.d.ts.map +1 -1
  36. package/dist/commands/config.js +13 -0
  37. package/dist/commands/config.js.map +1 -1
  38. package/dist/commands/delete.d.ts +2 -0
  39. package/dist/commands/delete.d.ts.map +1 -1
  40. package/dist/commands/delete.js +15 -18
  41. package/dist/commands/delete.js.map +1 -1
  42. package/dist/commands/dreams.d.ts +22 -0
  43. package/dist/commands/dreams.d.ts.map +1 -0
  44. package/dist/commands/dreams.js +251 -0
  45. package/dist/commands/dreams.js.map +1 -0
  46. package/dist/commands/dreams.test.d.ts +2 -0
  47. package/dist/commands/dreams.test.d.ts.map +1 -0
  48. package/dist/commands/dreams.test.js +39 -0
  49. package/dist/commands/dreams.test.js.map +1 -0
  50. package/dist/commands/hook.d.ts +2 -0
  51. package/dist/commands/hook.d.ts.map +1 -1
  52. package/dist/commands/hook.js +25 -103
  53. package/dist/commands/hook.js.map +1 -1
  54. package/dist/commands/hub.d.ts +37 -0
  55. package/dist/commands/hub.d.ts.map +1 -0
  56. package/dist/commands/hub.js +347 -0
  57. package/dist/commands/hub.js.map +1 -0
  58. package/dist/commands/hub.test.d.ts +2 -0
  59. package/dist/commands/hub.test.d.ts.map +1 -0
  60. package/dist/commands/hub.test.js +62 -0
  61. package/dist/commands/hub.test.js.map +1 -0
  62. package/dist/commands/import.d.ts +13 -0
  63. package/dist/commands/import.d.ts.map +1 -0
  64. package/dist/commands/import.js +257 -0
  65. package/dist/commands/import.js.map +1 -0
  66. package/dist/commands/import.test.d.ts +2 -0
  67. package/dist/commands/import.test.d.ts.map +1 -0
  68. package/dist/commands/import.test.js +11 -0
  69. package/dist/commands/import.test.js.map +1 -0
  70. package/dist/commands/list.d.ts +9 -2
  71. package/dist/commands/list.d.ts.map +1 -1
  72. package/dist/commands/list.js +118 -9
  73. package/dist/commands/list.js.map +1 -1
  74. package/dist/commands/list.test.d.ts +2 -0
  75. package/dist/commands/list.test.d.ts.map +1 -0
  76. package/dist/commands/list.test.js +20 -0
  77. package/dist/commands/list.test.js.map +1 -0
  78. package/dist/commands/login.d.ts +7 -1
  79. package/dist/commands/login.d.ts.map +1 -1
  80. package/dist/commands/login.js +81 -20
  81. package/dist/commands/login.js.map +1 -1
  82. package/dist/commands/mcp.d.ts.map +1 -1
  83. package/dist/commands/mcp.js +386 -65
  84. package/dist/commands/mcp.js.map +1 -1
  85. package/dist/commands/push.d.ts +6 -1
  86. package/dist/commands/push.d.ts.map +1 -1
  87. package/dist/commands/push.js +42 -8
  88. package/dist/commands/push.js.map +1 -1
  89. package/dist/commands/recall.d.ts +11 -1
  90. package/dist/commands/recall.d.ts.map +1 -1
  91. package/dist/commands/recall.js +228 -41
  92. package/dist/commands/recall.js.map +1 -1
  93. package/dist/commands/recall.test.d.ts +2 -0
  94. package/dist/commands/recall.test.d.ts.map +1 -0
  95. package/dist/commands/recall.test.js +31 -0
  96. package/dist/commands/recall.test.js.map +1 -0
  97. package/dist/commands/setup-hooks.d.ts +13 -0
  98. package/dist/commands/setup-hooks.d.ts.map +1 -0
  99. package/dist/commands/setup-hooks.js +193 -0
  100. package/dist/commands/setup-hooks.js.map +1 -0
  101. package/dist/commands/setup-instructions.d.ts +21 -0
  102. package/dist/commands/setup-instructions.d.ts.map +1 -0
  103. package/dist/commands/setup-instructions.js +172 -0
  104. package/dist/commands/setup-instructions.js.map +1 -0
  105. package/dist/commands/setup-mcp.d.ts +40 -0
  106. package/dist/commands/setup-mcp.d.ts.map +1 -0
  107. package/dist/commands/setup-mcp.js +414 -0
  108. package/dist/commands/setup-mcp.js.map +1 -0
  109. package/dist/commands/setup-types.d.ts +33 -0
  110. package/dist/commands/setup-types.d.ts.map +1 -0
  111. package/dist/commands/setup-types.js +60 -0
  112. package/dist/commands/setup-types.js.map +1 -0
  113. package/dist/commands/setup.d.ts +10 -1
  114. package/dist/commands/setup.d.ts.map +1 -1
  115. package/dist/commands/setup.js +216 -534
  116. package/dist/commands/setup.js.map +1 -1
  117. package/dist/commands/show.d.ts +5 -1
  118. package/dist/commands/show.d.ts.map +1 -1
  119. package/dist/commands/show.js +36 -14
  120. package/dist/commands/show.js.map +1 -1
  121. package/dist/commands/topic.d.ts +32 -0
  122. package/dist/commands/topic.d.ts.map +1 -0
  123. package/dist/commands/topic.js +265 -0
  124. package/dist/commands/topic.js.map +1 -0
  125. package/dist/commands/topic.test.d.ts +2 -0
  126. package/dist/commands/topic.test.d.ts.map +1 -0
  127. package/dist/commands/topic.test.js +114 -0
  128. package/dist/commands/topic.test.js.map +1 -0
  129. package/dist/index.js +35 -144
  130. package/dist/index.js.map +1 -1
  131. package/dist/lib/client.d.ts +10 -0
  132. package/dist/lib/client.d.ts.map +1 -0
  133. package/dist/lib/client.js +104 -0
  134. package/dist/lib/client.js.map +1 -0
  135. package/dist/lib/client.test.d.ts +2 -0
  136. package/dist/lib/client.test.d.ts.map +1 -0
  137. package/dist/lib/client.test.js +44 -0
  138. package/dist/lib/client.test.js.map +1 -0
  139. package/dist/lib/config.d.ts +43 -0
  140. package/dist/lib/config.d.ts.map +1 -1
  141. package/dist/lib/config.js +72 -1
  142. package/dist/lib/config.js.map +1 -1
  143. package/dist/lib/credentials.d.ts +3 -0
  144. package/dist/lib/credentials.d.ts.map +1 -1
  145. package/dist/lib/credentials.js +24 -2
  146. package/dist/lib/credentials.js.map +1 -1
  147. package/dist/lib/hubs.d.ts +7 -0
  148. package/dist/lib/hubs.d.ts.map +1 -0
  149. package/dist/lib/hubs.js +33 -0
  150. package/dist/lib/hubs.js.map +1 -0
  151. package/dist/lib/hubs.test.d.ts +2 -0
  152. package/dist/lib/hubs.test.d.ts.map +1 -0
  153. package/dist/lib/hubs.test.js +58 -0
  154. package/dist/lib/hubs.test.js.map +1 -0
  155. package/dist/lib/project-context.d.ts +56 -0
  156. package/dist/lib/project-context.d.ts.map +1 -0
  157. package/dist/lib/project-context.js +225 -0
  158. package/dist/lib/project-context.js.map +1 -0
  159. package/dist/lib/project-context.test.d.ts +2 -0
  160. package/dist/lib/project-context.test.d.ts.map +1 -0
  161. package/dist/lib/project-context.test.js +75 -0
  162. package/dist/lib/project-context.test.js.map +1 -0
  163. package/dist/lib/prompt.d.ts +7 -0
  164. package/dist/lib/prompt.d.ts.map +1 -0
  165. package/dist/lib/prompt.js +41 -0
  166. package/dist/lib/prompt.js.map +1 -0
  167. package/dist/lib/trash.d.ts +6 -0
  168. package/dist/lib/trash.d.ts.map +1 -0
  169. package/dist/lib/trash.js +28 -0
  170. package/dist/lib/trash.js.map +1 -0
  171. package/package.json +17 -13
  172. package/.vscode/mcp.json +0 -8
  173. package/dist/commands/sync.d.ts +0 -12
  174. package/dist/commands/sync.d.ts.map +0 -1
  175. package/dist/commands/sync.js +0 -414
  176. package/dist/commands/sync.js.map +0 -1
  177. package/dist/lib/api.d.ts +0 -4
  178. package/dist/lib/api.d.ts.map +0 -1
  179. package/dist/lib/api.js +0 -95
  180. package/dist/lib/api.js.map +0 -1
  181. package/src/commands/auth.ts +0 -92
  182. package/src/commands/config.ts +0 -27
  183. package/src/commands/delete.ts +0 -58
  184. package/src/commands/hook.ts +0 -243
  185. package/src/commands/list.ts +0 -38
  186. package/src/commands/login.ts +0 -164
  187. package/src/commands/mcp.ts +0 -405
  188. package/src/commands/push.ts +0 -137
  189. package/src/commands/recall.ts +0 -163
  190. package/src/commands/setup.ts +0 -1077
  191. package/src/commands/show.ts +0 -35
  192. package/src/commands/sync.ts +0 -506
  193. package/src/index.ts +0 -213
  194. package/src/lib/api.ts +0 -110
  195. package/src/lib/config.ts +0 -61
  196. package/src/lib/credentials.ts +0 -42
  197. package/tsconfig.json +0 -9
@@ -1,164 +0,0 @@
1
- import { createServer } from "node:http";
2
- import { loadConfig } from "../lib/config.js";
3
- import { saveCredentials } from "../lib/credentials.js";
4
-
5
- interface TokenPair {
6
- access_token: string;
7
- refresh_token: string;
8
- expires_in: number;
9
- }
10
-
11
- export async function loginCommand(): Promise<void> {
12
- const config = loadConfig();
13
- const apiUrl = config.api_url;
14
-
15
- // Start a temporary local server to receive the OAuth callback
16
- const port = await findFreePort();
17
- const callbackUrl = `http://localhost:${port}/callback`;
18
-
19
- const tokenPromise = new Promise<TokenPair>((resolve, reject) => {
20
- const server = createServer(async (req, res) => {
21
- const url = new URL(req.url!, `http://localhost:${port}`);
22
-
23
- if (url.pathname === "/callback") {
24
- const code = url.searchParams.get("code");
25
-
26
- if (code) {
27
- // Exchange the one-time code for tokens via POST
28
- try {
29
- const exchangeRes = await fetch(`${apiUrl}/v1/auth/exchange`, {
30
- method: "POST",
31
- headers: { "Content-Type": "application/json" },
32
- body: JSON.stringify({ code }),
33
- });
34
- const json = (await exchangeRes.json()) as {
35
- data?: TokenPair;
36
- error?: { message: string };
37
- };
38
-
39
- if (json.data) {
40
- res.writeHead(200, { "Content-Type": "text/html" });
41
- res.end(`
42
- <html><body style="font-family: system-ui; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0;">
43
- <div style="text-align: center;">
44
- <h2 style="font-weight: 600;">Logged in to Memax</h2>
45
- <p style="color: #64748b;">You can close this tab and return to your terminal.</p>
46
- </div>
47
- </body></html>
48
- `);
49
- resolve(json.data);
50
- } else {
51
- res.writeHead(400, { "Content-Type": "text/html" });
52
- res.end(
53
- `<html><body><h2>Login failed</h2><p>${json.error?.message ?? "Token exchange failed."}</p></body></html>`,
54
- );
55
- reject(
56
- new Error(json.error?.message ?? "Token exchange failed."),
57
- );
58
- }
59
- } catch (err) {
60
- res.writeHead(500, { "Content-Type": "text/html" });
61
- res.end(
62
- "<html><body><h2>Login failed</h2><p>Could not reach Memax API.</p></body></html>",
63
- );
64
- reject(new Error("Could not reach Memax API for token exchange."));
65
- }
66
- } else {
67
- res.writeHead(400, { "Content-Type": "text/html" });
68
- res.end(
69
- "<html><body><h2>Login failed</h2><p>No authorization code received.</p></body></html>",
70
- );
71
- reject(new Error("No authorization code received from callback."));
72
- }
73
-
74
- // Close the server and force-kill connections so the process exits
75
- setTimeout(() => {
76
- server.close();
77
- server.closeAllConnections();
78
- }, 500);
79
- }
80
- });
81
-
82
- server.listen(port);
83
-
84
- // Timeout after 2 minutes — use unref() so it doesn't keep the process alive
85
- const timeout = setTimeout(() => {
86
- server.close();
87
- server.closeAllConnections();
88
- reject(
89
- new Error("Login timed out — no callback received within 2 minutes."),
90
- );
91
- }, 120_000);
92
- timeout.unref();
93
- });
94
-
95
- // Build the GitHub OAuth URL with our local callback as the redirect
96
- const authUrl = `${apiUrl}/v1/auth/github?redirect_uri=${encodeURIComponent(callbackUrl)}`;
97
-
98
- console.log("\n Opening browser for GitHub login...\n");
99
- console.log(` If the browser doesn't open, visit:\n ${authUrl}\n`);
100
-
101
- // Try to open the browser
102
- try {
103
- const { exec } = await import("node:child_process");
104
- const cmd =
105
- process.platform === "darwin"
106
- ? `open "${authUrl}"`
107
- : process.platform === "win32"
108
- ? `start "${authUrl}"`
109
- : `xdg-open "${authUrl}"`;
110
- exec(cmd);
111
- } catch {
112
- // Browser open failed — user can copy the URL
113
- }
114
-
115
- try {
116
- const tokens = await tokenPromise;
117
- saveCredentials({
118
- access_token: tokens.access_token,
119
- refresh_token: tokens.refresh_token,
120
- expires_at: Date.now() + tokens.expires_in * 1000,
121
- });
122
- console.log(
123
- " Logged in successfully. Credentials saved to ~/.memax/credentials.json\n",
124
- );
125
- } catch (err) {
126
- console.error(` Login failed: ${(err as Error).message}\n`);
127
- process.exit(1);
128
- }
129
- }
130
-
131
- export async function logoutCommand(): Promise<void> {
132
- const { clearCredentials } = await import("../lib/credentials.js");
133
- clearCredentials();
134
- console.log(" Logged out. Credentials cleared.\n");
135
- }
136
-
137
- export async function whoamiCommand(): Promise<void> {
138
- const { loadCredentials } = await import("../lib/credentials.js");
139
- const { apiGet } = await import("../lib/api.js");
140
-
141
- const creds = loadCredentials();
142
- if (!creds?.access_token) {
143
- console.log(" Not logged in. Run: memax login\n");
144
- return;
145
- }
146
-
147
- try {
148
- const user = await apiGet<{ name: string; email: string }>("/v1/auth/me");
149
- console.log(` ${user.name} (${user.email})\n`);
150
- } catch {
151
- console.log(" Session expired or invalid. Run: memax login\n");
152
- }
153
- }
154
-
155
- async function findFreePort(): Promise<number> {
156
- return new Promise((resolve) => {
157
- const server = createServer();
158
- server.listen(0, () => {
159
- const addr = server.address();
160
- const port = typeof addr === "object" && addr ? addr.port : 0;
161
- server.close(() => resolve(port));
162
- });
163
- });
164
- }
@@ -1,405 +0,0 @@
1
- import { Command } from "commander";
2
- import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
- import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
- import {
5
- CallToolRequestSchema,
6
- ListToolsRequestSchema,
7
- } from "@modelcontextprotocol/sdk/types.js";
8
- import { apiGet, apiPost, apiDelete } from "../lib/api.js";
9
-
10
- interface RecallResult {
11
- notes: Array<{
12
- id: string;
13
- title: string;
14
- chunk_content: string;
15
- heading_chain: string;
16
- relevance_score: number;
17
- category: string;
18
- source: string;
19
- age: string;
20
- }>;
21
- query_metadata: {
22
- intent: string;
23
- total_candidates: number;
24
- latency_ms: number;
25
- };
26
- }
27
-
28
- interface NoteResult {
29
- id: string;
30
- title: string;
31
- category: string;
32
- source: string;
33
- tags: string[];
34
- summary: string;
35
- created_at: string;
36
- }
37
-
38
- function createServer(): Server {
39
- const server = new Server(
40
- { name: "memax", version: "0.0.1" },
41
- { capabilities: { tools: {} } },
42
- );
43
-
44
- server.setRequestHandler(ListToolsRequestSchema, async () => ({
45
- tools: [
46
- {
47
- name: "memax_recall",
48
- description:
49
- "Search the user's Memax knowledge base with a natural language query. " +
50
- "Returns relevant memories ranked by relevance. Use this when you need " +
51
- "background information about the project, team processes, or past decisions.",
52
- inputSchema: {
53
- type: "object" as const,
54
- properties: {
55
- query: {
56
- type: "string",
57
- description: "Natural language search query",
58
- },
59
- limit: {
60
- type: "number",
61
- description: "Max results to return (default 5)",
62
- },
63
- category: {
64
- type: "string",
65
- description:
66
- "Filter by category prefix (e.g. 'core', 'decisions', 'process')",
67
- },
68
- },
69
- required: ["query"],
70
- },
71
- },
72
- {
73
- name: "memax_push",
74
- description:
75
- "Save a piece of knowledge to the user's Memax knowledge base. " +
76
- "Auto-categorized by AI if no category is provided.",
77
- inputSchema: {
78
- type: "object" as const,
79
- properties: {
80
- content: {
81
- type: "string",
82
- description: "The knowledge content to save",
83
- },
84
- title: {
85
- type: "string",
86
- description: "Optional title (auto-generated if omitted)",
87
- },
88
- category: {
89
- type: "string",
90
- description:
91
- "Category (e.g. 'decisions/adr', 'core/architecture'). Auto-detected if omitted.",
92
- },
93
- tags: {
94
- type: "array",
95
- items: { type: "string" },
96
- description: "Tags for the memory",
97
- },
98
- },
99
- required: ["content"],
100
- },
101
- },
102
- {
103
- name: "memax_get",
104
- description:
105
- "Get the full content of a specific note by ID. " +
106
- "Use this after memax_recall to read the complete content of a relevant memory.",
107
- inputSchema: {
108
- type: "object" as const,
109
- properties: {
110
- id: {
111
- type: "string",
112
- description:
113
- "The note ID (from memax_recall or memax_search results)",
114
- },
115
- },
116
- required: ["id"],
117
- },
118
- },
119
- {
120
- name: "memax_search",
121
- description:
122
- "List notes filtered by category or tags. " +
123
- "For browsing and structured lookups, not semantic search. " +
124
- "Use memax_recall for natural language queries instead.",
125
- inputSchema: {
126
- type: "object" as const,
127
- properties: {
128
- category: {
129
- type: "string",
130
- description: "Filter by category prefix",
131
- },
132
- limit: {
133
- type: "number",
134
- description: "Max results to return (default 10)",
135
- },
136
- },
137
- },
138
- },
139
- {
140
- name: "memax_forget",
141
- description:
142
- "Delete a note from the user's Memax knowledge base by ID. " +
143
- "Use this to remove outdated, incorrect, or duplicate memories.",
144
- inputSchema: {
145
- type: "object" as const,
146
- properties: {
147
- id: {
148
- type: "string",
149
- description:
150
- "The note ID to delete (from memax_recall or memax_search results)",
151
- },
152
- },
153
- required: ["id"],
154
- },
155
- },
156
- ],
157
- }));
158
-
159
- server.setRequestHandler(CallToolRequestSchema, async (request) => {
160
- const { name, arguments: args } = request.params;
161
-
162
- switch (name) {
163
- case "memax_recall": {
164
- const typedArgs = args as {
165
- query: string;
166
- limit?: number;
167
- category?: string;
168
- };
169
- try {
170
- const result = await apiPost<RecallResult>("/v1/recall", {
171
- query: typedArgs.query,
172
- limit: typedArgs.limit ?? 5,
173
- category: typedArgs.category ?? "",
174
- source: "mcp",
175
- working_dir: process.cwd(),
176
- });
177
-
178
- const notes = result.notes ?? [];
179
- if (notes.length === 0) {
180
- return {
181
- content: [{ type: "text" as const, text: "No results found." }],
182
- };
183
- }
184
-
185
- const formatted = notes
186
- .map((n, i) => {
187
- const score = (n.relevance_score * 100).toFixed(0);
188
- const heading = n.heading_chain ? ` — ${n.heading_chain}` : "";
189
- return (
190
- `[${i + 1}] ${n.title} [${n.category}, ${score}%, ${n.age}] (id: ${n.id})${heading}\n` +
191
- n.chunk_content
192
- );
193
- })
194
- .join("\n\n");
195
-
196
- return { content: [{ type: "text" as const, text: formatted }] };
197
- } catch (err) {
198
- return {
199
- content: [
200
- {
201
- type: "text" as const,
202
- text: `Recall failed: ${(err as Error).message}`,
203
- },
204
- ],
205
- isError: true,
206
- };
207
- }
208
- }
209
-
210
- case "memax_push": {
211
- const typedArgs = args as {
212
- content: string;
213
- title?: string;
214
- category?: string;
215
- tags?: string[];
216
- };
217
- try {
218
- const note = await apiPost<NoteResult>("/v1/notes", {
219
- content: typedArgs.content,
220
- title: typedArgs.title ?? "",
221
- category: typedArgs.category ?? "",
222
- tags: typedArgs.tags ?? [],
223
- source: "mcp",
224
- });
225
-
226
- return {
227
- content: [
228
- {
229
- type: "text" as const,
230
- text: `Saved: ${note.title} (id: ${note.id})`,
231
- },
232
- ],
233
- };
234
- } catch (err) {
235
- return {
236
- content: [
237
- {
238
- type: "text" as const,
239
- text: `Push failed: ${(err as Error).message}`,
240
- },
241
- ],
242
- isError: true,
243
- };
244
- }
245
- }
246
-
247
- case "memax_get": {
248
- const typedArgs = args as { id: string };
249
- try {
250
- const note = await apiGet<{
251
- id: string;
252
- title: string;
253
- content: string;
254
- summary: string;
255
- category: string;
256
- tags: string[];
257
- source: string;
258
- source_path: string;
259
- created_at: string;
260
- updated_at: string;
261
- }>(`/v1/notes/${typedArgs.id}`);
262
-
263
- const parts = [
264
- `# ${note.title}`,
265
- `Category: ${note.category} | Source: ${note.source} | Created: ${note.created_at}`,
266
- ];
267
- if (note.tags?.length > 0) {
268
- parts.push(`Tags: ${note.tags.join(", ")}`);
269
- }
270
- if (note.source_path) {
271
- parts.push(`Source: ${note.source_path}`);
272
- }
273
- if (note.summary) {
274
- parts.push(`\n## Summary\n${note.summary}`);
275
- }
276
- parts.push(`\n## Content\n${note.content}`);
277
-
278
- return {
279
- content: [{ type: "text" as const, text: parts.join("\n") }],
280
- };
281
- } catch (err) {
282
- return {
283
- content: [
284
- {
285
- type: "text" as const,
286
- text: `Get failed: ${(err as Error).message}`,
287
- },
288
- ],
289
- isError: true,
290
- };
291
- }
292
- }
293
-
294
- case "memax_search": {
295
- const typedArgs = args as {
296
- category?: string;
297
- limit?: number;
298
- };
299
- try {
300
- const notes = (await apiGet<NoteResult[]>("/v1/notes")) ?? [];
301
-
302
- let filtered = notes;
303
- if (typedArgs.category) {
304
- filtered = notes.filter((n) =>
305
- n.category.startsWith(typedArgs.category!),
306
- );
307
- }
308
-
309
- const limited = filtered.slice(0, typedArgs.limit ?? 10);
310
-
311
- if (limited.length === 0) {
312
- return {
313
- content: [{ type: "text" as const, text: "No notes found." }],
314
- };
315
- }
316
-
317
- const formatted = limited
318
- .map(
319
- (n) => `- ${n.title} [${n.category}] — ${n.source} (id: ${n.id})`,
320
- )
321
- .join("\n");
322
-
323
- return { content: [{ type: "text" as const, text: formatted }] };
324
- } catch (err) {
325
- return {
326
- content: [
327
- {
328
- type: "text" as const,
329
- text: `Search failed: ${(err as Error).message}`,
330
- },
331
- ],
332
- isError: true,
333
- };
334
- }
335
- }
336
-
337
- case "memax_forget": {
338
- const typedArgs = args as { id: string };
339
- if (!typedArgs.id) {
340
- return {
341
- content: [{ type: "text" as const, text: "Note ID is required." }],
342
- isError: true,
343
- };
344
- }
345
- try {
346
- await apiDelete(`/v1/notes/${typedArgs.id}`);
347
- return {
348
- content: [
349
- {
350
- type: "text" as const,
351
- text: `Forgotten: ${typedArgs.id}`,
352
- },
353
- ],
354
- };
355
- } catch (err) {
356
- return {
357
- content: [
358
- {
359
- type: "text" as const,
360
- text: `Forget failed: ${(err as Error).message}`,
361
- },
362
- ],
363
- isError: true,
364
- };
365
- }
366
- }
367
-
368
- default:
369
- return {
370
- content: [{ type: "text" as const, text: `Unknown tool: ${name}` }],
371
- isError: true,
372
- };
373
- }
374
- });
375
-
376
- return server;
377
- }
378
-
379
- async function mcpServeCommand(): Promise<void> {
380
- const server = createServer();
381
- const transport = new StdioServerTransport();
382
- await server.connect(transport);
383
-
384
- // Keep the process alive — some agent launchers close stdin early
385
- // which makes Node think the event loop is empty and exit.
386
- await new Promise<void>((resolve) => {
387
- process.on("SIGINT", resolve);
388
- process.on("SIGTERM", resolve);
389
- // Also resolve if stdin closes (transport disconnected)
390
- process.stdin.on("end", resolve);
391
- });
392
-
393
- await server.close();
394
- }
395
-
396
- export function registerMcpCommand(program: Command): void {
397
- const mcp = program
398
- .command("mcp")
399
- .description("Model Context Protocol server for AI agents");
400
-
401
- mcp
402
- .command("serve")
403
- .description("Start MCP server on stdio")
404
- .action(mcpServeCommand);
405
- }
@@ -1,137 +0,0 @@
1
- import chalk from "chalk";
2
- import { readFileSync, statSync } from "node:fs";
3
- import { basename } from "node:path";
4
- import { apiPost } from "../lib/api.js";
5
- import type { Note } from "@memaxlabs/shared";
6
-
7
- const MAX_SIZE = 3 * 1024 * 1024; // 3MB — must match server's MAX_BODY_SIZE default
8
-
9
- function formatSize(bytes: number): string {
10
- if (bytes < 1024) return `${bytes} B`;
11
- if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
12
- return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
13
- }
14
-
15
- interface PushOptions {
16
- file?: string;
17
- category?: string;
18
- tags?: string;
19
- ttl?: string;
20
- stdin?: boolean;
21
- title?: string;
22
- }
23
-
24
- export async function pushCommand(
25
- inlineContent: string | undefined,
26
- options: PushOptions,
27
- ): Promise<void> {
28
- let content: string;
29
- let title = options.title ?? "";
30
- let sourcePath = "";
31
-
32
- if (options.file) {
33
- try {
34
- // Check file size before reading
35
- const stat = statSync(options.file);
36
- if (stat.size > MAX_SIZE) {
37
- console.error(
38
- chalk.red(
39
- `File too large: ${formatSize(stat.size)} (limit: ${formatSize(MAX_SIZE)})`,
40
- ),
41
- );
42
- console.error(
43
- chalk.gray(
44
- " Tip: split large files into smaller sections, or ask your admin to increase MAX_BODY_SIZE on the server.",
45
- ),
46
- );
47
- process.exit(1);
48
- }
49
- content = readFileSync(options.file, "utf-8");
50
- sourcePath = options.file;
51
- if (!title) {
52
- title = basename(options.file);
53
- }
54
- } catch (err) {
55
- if ((err as NodeJS.ErrnoException).code === "ENOENT") {
56
- console.error(chalk.red(`File not found: ${options.file}`));
57
- } else if ((err as { exitCode?: number }).exitCode) {
58
- throw err; // Re-throw our own process.exit calls
59
- } else {
60
- console.error(chalk.red(`Error reading file: ${options.file}`));
61
- }
62
- process.exit(1);
63
- }
64
- } else if (inlineContent) {
65
- // Positional argument: memax push "some content"
66
- content = inlineContent;
67
- } else if (options.stdin || !process.stdin.isTTY) {
68
- const chunks: Buffer[] = [];
69
- for await (const chunk of process.stdin) {
70
- chunks.push(chunk);
71
- }
72
- content = Buffer.concat(chunks).toString("utf-8");
73
- } else {
74
- console.error(
75
- chalk.red(
76
- 'Provide content, --file <path>, or pipe via stdin:\n memax push "your content here"\n memax push --file ./doc.md\n echo "content" | memax push',
77
- ),
78
- );
79
- process.exit(1);
80
- }
81
-
82
- if (!content.trim()) {
83
- console.error(chalk.red("No content to push"));
84
- process.exit(1);
85
- }
86
-
87
- // Check size before sending
88
- const contentBytes = Buffer.byteLength(content, "utf-8");
89
- if (contentBytes > MAX_SIZE) {
90
- console.error(
91
- chalk.red(
92
- `File too large: ${formatSize(contentBytes)} (limit: ${formatSize(MAX_SIZE)})`,
93
- ),
94
- );
95
- console.error(
96
- chalk.gray(
97
- " Tip: split large files into smaller sections, or ask your admin to increase MAX_BODY_SIZE on the server.",
98
- ),
99
- );
100
- process.exit(1);
101
- }
102
-
103
- const tags = options.tags ? options.tags.split(",").map((t) => t.trim()) : [];
104
-
105
- // Auto-detect URL content: single-line http(s) URL -> content_type "link"
106
- const trimmed = content.trim();
107
- const isURL =
108
- (trimmed.startsWith("http://") || trimmed.startsWith("https://")) &&
109
- !trimmed.includes("\n");
110
-
111
- let contentType = sourcePath.endsWith(".md") ? "markdown" : "text";
112
- if (isURL) {
113
- contentType = "link";
114
- }
115
-
116
- try {
117
- const note = await apiPost<Note>("/v1/notes", {
118
- content,
119
- title,
120
- category: options.category ?? "",
121
- tags,
122
- source: "cli",
123
- source_path: sourcePath,
124
- content_type: contentType,
125
- });
126
-
127
- console.log(chalk.green("Saved"), chalk.bold(note.title));
128
- console.log(
129
- chalk.gray(
130
- ` id: ${note.id} category: ${note.category} source: ${note.source}`,
131
- ),
132
- );
133
- } catch (err) {
134
- console.error(chalk.red(`Push failed: ${(err as Error).message}`));
135
- process.exit(1);
136
- }
137
- }