@codemem/mcp 0.31.3 → 0.32.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/dist/audit.d.ts +54 -0
- package/dist/audit.d.ts.map +1 -0
- package/dist/content.d.ts +13 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/http.d.ts +38 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +17075 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +2 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -602
- package/dist/memory-access.d.ts.map +1 -1
- package/dist/memory-kinds.d.ts +2 -0
- package/dist/memory-kinds.d.ts.map +1 -0
- package/dist/oauth.d.ts +176 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oidc.d.ts +35 -0
- package/dist/oidc.d.ts.map +1 -0
- package/dist/project-scope.d.ts +4 -0
- package/dist/project-scope.d.ts.map +1 -1
- package/dist/provider.d.ts +51 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/schemas.d.ts +36 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/server-Djvf0Yik.js +488 -0
- package/dist/server-Djvf0Yik.js.map +1 -0
- package/dist/server.d.ts +5 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/stdio.d.ts +9 -0
- package/dist/stdio.d.ts.map +1 -0
- package/dist/stdio.js +69 -0
- package/dist/stdio.js.map +1 -0
- package/dist/tool-context.d.ts +12 -0
- package/dist/tool-context.d.ts.map +1 -0
- package/dist/tools/items.d.ts +4 -0
- package/dist/tools/items.d.ts.map +1 -0
- package/dist/tools/learn.d.ts +3 -0
- package/dist/tools/learn.d.ts.map +1 -0
- package/dist/tools/schema.d.ts +3 -0
- package/dist/tools/schema.d.ts.map +1 -0
- package/dist/tools/search.d.ts +4 -0
- package/dist/tools/search.d.ts.map +1 -0
- package/dist/tools/timeline.d.ts +4 -0
- package/dist/tools/timeline.d.ts.map +1 -0
- package/package.json +19 -7
- package/dist/index.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @codemem/mcp — MCP stdio server.
|
|
4
|
-
*
|
|
5
|
-
* Runs as a separate process spawned by the host (OpenCode/Claude).
|
|
6
|
-
* Owns its own better-sqlite3 connection. Communicates via stdio JSON-RPC.
|
|
7
|
-
*
|
|
8
|
-
* Port of codemem/mcp_server.py — all 13 tools.
|
|
9
|
-
*/
|
|
10
|
-
export {};
|
|
1
|
+
export { createCodememMcpServer } from "./server.js";
|
|
2
|
+
export type { CodememMcpServerOptions } from "./tool-context.js";
|
|
11
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,YAAY,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,602 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { existsSync } from "node:fs";
|
|
4
|
-
import { dirname, join } from "node:path";
|
|
5
|
-
import { MemoryStore, VERSION, dedupeOrderedIds, projectMatchesFilter, resolveDbPath, resolveProject, storeVectors, toJson } from "@codemem/core";
|
|
6
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
7
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
8
|
-
import { z } from "zod";
|
|
9
|
-
//#region src/memory-access.ts
|
|
10
|
-
function getMemoryForMcp(store, memoryId, filters) {
|
|
11
|
-
return store.timeline(null, memoryId, 0, 0, filters ?? null).find((row) => row.id === memoryId) ?? null;
|
|
12
|
-
}
|
|
13
|
-
function getManyForMcp(store, ids, filters) {
|
|
14
|
-
if (ids.length === 0) return [];
|
|
15
|
-
const results = [];
|
|
16
|
-
for (const id of ids) {
|
|
17
|
-
const item = getMemoryForMcp(store, id, filters);
|
|
18
|
-
if (item) results.push(item);
|
|
19
|
-
}
|
|
20
|
-
return results;
|
|
21
|
-
}
|
|
22
|
-
function forgetMemoryForMcp(store, memoryId, filters) {
|
|
23
|
-
if (!getMemoryForMcp(store, memoryId, filters)) return false;
|
|
24
|
-
store.forget(memoryId);
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
function rememberMemoryForMcp(store, input, context = {}) {
|
|
28
|
-
return store.db.transaction(() => {
|
|
29
|
-
const now = context.now?.() ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
30
|
-
const user = context.user ?? process.env.USER ?? "unknown";
|
|
31
|
-
const cwd = context.cwd ?? process.cwd();
|
|
32
|
-
const project = input.project ?? context.envProject ?? null;
|
|
33
|
-
const sessionInfo = store.db.prepare(`INSERT INTO sessions(started_at, ended_at, cwd, project, user, tool_version, metadata_json)
|
|
34
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)`).run(now, now, cwd, project, user, "mcp-ts", toJson({ mcp: true }));
|
|
35
|
-
const sessionId = Number(sessionInfo.lastInsertRowid);
|
|
36
|
-
const memId = store.remember(sessionId, input.kind, input.title, input.body, input.confidence);
|
|
37
|
-
if (!getMemoryForMcp(store, memId)) throw new Error("unauthorized_scope");
|
|
38
|
-
store.db.prepare("UPDATE sessions SET ended_at = ?, metadata_json = ? WHERE id = ?").run(context.now?.() ?? (/* @__PURE__ */ new Date()).toISOString(), toJson({ mcp: true }), sessionId);
|
|
39
|
-
return {
|
|
40
|
-
memId,
|
|
41
|
-
title: input.title,
|
|
42
|
-
body: input.body
|
|
43
|
-
};
|
|
44
|
-
})();
|
|
45
|
-
}
|
|
46
|
-
//#endregion
|
|
47
|
-
//#region src/project-scope.ts
|
|
48
|
-
function resolveDefaultProject() {
|
|
49
|
-
return process.env.CODEMEM_PROJECT?.trim() || resolveProject(process.cwd());
|
|
50
|
-
}
|
|
51
|
-
function buildFilters(raw, defaultProject = resolveDefaultProject()) {
|
|
52
|
-
const filters = {};
|
|
53
|
-
let hasAny = false;
|
|
54
|
-
const resolvedProject = (typeof raw.project === "string" ? raw.project.trim() : void 0) || defaultProject || void 0;
|
|
55
|
-
if (resolvedProject) {
|
|
56
|
-
filters.project = resolvedProject;
|
|
57
|
-
hasAny = true;
|
|
58
|
-
}
|
|
59
|
-
for (const key of [
|
|
60
|
-
"kind",
|
|
61
|
-
"visibility",
|
|
62
|
-
"scope_id",
|
|
63
|
-
"include_scope_ids",
|
|
64
|
-
"exclude_scope_ids",
|
|
65
|
-
"include_visibility",
|
|
66
|
-
"exclude_visibility",
|
|
67
|
-
"include_workspace_ids",
|
|
68
|
-
"exclude_workspace_ids",
|
|
69
|
-
"include_workspace_kinds",
|
|
70
|
-
"exclude_workspace_kinds",
|
|
71
|
-
"include_actor_ids",
|
|
72
|
-
"exclude_actor_ids",
|
|
73
|
-
"include_trust_states",
|
|
74
|
-
"exclude_trust_states",
|
|
75
|
-
"ownership_scope",
|
|
76
|
-
"personal_first",
|
|
77
|
-
"trust_bias",
|
|
78
|
-
"widen_shared_when_weak",
|
|
79
|
-
"widen_shared_min_personal_results",
|
|
80
|
-
"widen_shared_min_personal_score"
|
|
81
|
-
]) {
|
|
82
|
-
const val = raw[key];
|
|
83
|
-
if (val !== void 0 && val !== null) {
|
|
84
|
-
filters[key] = val;
|
|
85
|
-
hasAny = true;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return hasAny ? filters : void 0;
|
|
89
|
-
}
|
|
90
|
-
//#endregion
|
|
91
|
-
//#region src/index.ts
|
|
92
|
-
/**
|
|
93
|
-
* @codemem/mcp — MCP stdio server.
|
|
94
|
-
*
|
|
95
|
-
* Runs as a separate process spawned by the host (OpenCode/Claude).
|
|
96
|
-
* Owns its own better-sqlite3 connection. Communicates via stdio JSON-RPC.
|
|
97
|
-
*
|
|
98
|
-
* Port of codemem/mcp_server.py — all 13 tools.
|
|
99
|
-
*/
|
|
100
|
-
var MEMORY_KINDS = {
|
|
101
|
-
discovery: "Something learned about the codebase, architecture, or tools",
|
|
102
|
-
change: "A code change that was made",
|
|
103
|
-
feature: "A new feature that was implemented",
|
|
104
|
-
bugfix: "A bug that was found and fixed",
|
|
105
|
-
refactor: "Code that was refactored or restructured",
|
|
106
|
-
decision: "A design or architecture decision",
|
|
107
|
-
exploration: "An experiment or investigation (may not have shipped)"
|
|
108
|
-
};
|
|
109
|
-
var scopeFilterSchema = {
|
|
110
|
-
scope_id: z.string().optional().describe("Filter by a single sharing domain scope_id"),
|
|
111
|
-
include_scope_ids: z.array(z.string()).optional().describe("Sharing domain scope_ids to include"),
|
|
112
|
-
exclude_scope_ids: z.array(z.string()).optional().describe("Sharing domain scope_ids to exclude")
|
|
113
|
-
};
|
|
114
|
-
var filterSchema = {
|
|
115
|
-
kind: z.string().optional().describe("Filter by memory kind"),
|
|
116
|
-
project: z.string().optional().describe("Filter by project scope (matches sessions.project)"),
|
|
117
|
-
...scopeFilterSchema,
|
|
118
|
-
visibility: z.array(z.string()).optional(),
|
|
119
|
-
include_visibility: z.array(z.string()).optional(),
|
|
120
|
-
exclude_visibility: z.array(z.string()).optional(),
|
|
121
|
-
include_workspace_ids: z.array(z.string()).optional(),
|
|
122
|
-
exclude_workspace_ids: z.array(z.string()).optional(),
|
|
123
|
-
include_workspace_kinds: z.array(z.string()).optional(),
|
|
124
|
-
exclude_workspace_kinds: z.array(z.string()).optional(),
|
|
125
|
-
include_actor_ids: z.array(z.string()).optional(),
|
|
126
|
-
exclude_actor_ids: z.array(z.string()).optional(),
|
|
127
|
-
include_trust_states: z.array(z.string()).optional(),
|
|
128
|
-
exclude_trust_states: z.array(z.string()).optional(),
|
|
129
|
-
ownership_scope: z.string().optional(),
|
|
130
|
-
personal_first: z.union([z.boolean(), z.string()]).optional(),
|
|
131
|
-
trust_bias: z.string().optional(),
|
|
132
|
-
widen_shared_when_weak: z.union([z.boolean(), z.string()]).optional(),
|
|
133
|
-
widen_shared_min_personal_results: z.number().int().optional(),
|
|
134
|
-
widen_shared_min_personal_score: z.number().optional()
|
|
135
|
-
};
|
|
136
|
-
var defaultProject = resolveDefaultProject();
|
|
137
|
-
/** Wrap a JSON result into the MCP text content envelope. */
|
|
138
|
-
function jsonContent(data) {
|
|
139
|
-
return { content: [{
|
|
140
|
-
type: "text",
|
|
141
|
-
text: JSON.stringify(data)
|
|
142
|
-
}] };
|
|
143
|
-
}
|
|
144
|
-
/** Wrap an error message into the MCP text content envelope. */
|
|
145
|
-
function errorContent(message) {
|
|
146
|
-
return { content: [{
|
|
147
|
-
type: "text",
|
|
148
|
-
text: JSON.stringify({ error: message })
|
|
149
|
-
}] };
|
|
150
|
-
}
|
|
151
|
-
/** Default viewer host/port for health checks. */
|
|
152
|
-
var VIEWER_HOST = process.env.CODEMEM_VIEWER_HOST ?? "127.0.0.1";
|
|
153
|
-
var VIEWER_PORT = process.env.CODEMEM_VIEWER_PORT ?? "38888";
|
|
154
|
-
/** Resolve the `codemem` CLI binary path. Checks package-local paths first, then PATH. */
|
|
155
|
-
function resolveCliPath() {
|
|
156
|
-
const candidates = [];
|
|
157
|
-
const selfDir = dirname(import.meta.dirname ?? ".");
|
|
158
|
-
candidates.push(join(selfDir, "..", "cli", "dist", "index.js"));
|
|
159
|
-
candidates.push(join(selfDir, "..", ".bin", "codemem"));
|
|
160
|
-
candidates.push(join(selfDir, "..", "..", ".bin", "codemem"));
|
|
161
|
-
for (const p of candidates) if (existsSync(p)) return p;
|
|
162
|
-
return "codemem";
|
|
163
|
-
}
|
|
164
|
-
/** Check if the viewer is already running. */
|
|
165
|
-
async function isViewerHealthy() {
|
|
166
|
-
try {
|
|
167
|
-
const url = `http://${VIEWER_HOST}:${VIEWER_PORT}/api/health`;
|
|
168
|
-
return (await fetch(url, { signal: AbortSignal.timeout(2e3) })).ok;
|
|
169
|
-
} catch {
|
|
170
|
-
return false;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Attempt to start the viewer server as a detached background process.
|
|
175
|
-
* Best-effort: failures are logged to stderr but never block MCP startup.
|
|
176
|
-
*/
|
|
177
|
-
async function ensureViewer() {
|
|
178
|
-
if (process.env.CODEMEM_VIEWER === "0" || process.env.CODEMEM_VIEWER_AUTO === "0") return;
|
|
179
|
-
if (await isViewerHealthy()) return;
|
|
180
|
-
const cli = resolveCliPath();
|
|
181
|
-
if (!cli) return;
|
|
182
|
-
try {
|
|
183
|
-
const isJsFile = cli.endsWith(".js");
|
|
184
|
-
const cmd = isJsFile ? process.execPath : cli;
|
|
185
|
-
const args = isJsFile ? [
|
|
186
|
-
cli,
|
|
187
|
-
"serve",
|
|
188
|
-
"start"
|
|
189
|
-
] : ["serve", "start"];
|
|
190
|
-
if (VIEWER_HOST !== "127.0.0.1") args.push("--host", VIEWER_HOST);
|
|
191
|
-
if (VIEWER_PORT !== "38888") args.push("--port", VIEWER_PORT);
|
|
192
|
-
const child = spawn(cmd, args, {
|
|
193
|
-
detached: true,
|
|
194
|
-
stdio: "ignore",
|
|
195
|
-
env: {
|
|
196
|
-
...process.env,
|
|
197
|
-
CODEMEM_PLUGIN_IGNORE: "1"
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
child.on("error", () => {});
|
|
201
|
-
child.unref();
|
|
202
|
-
for (let i = 0; i < 5; i++) {
|
|
203
|
-
await new Promise((r) => setTimeout(r, 1e3));
|
|
204
|
-
if (await isViewerHealthy()) return;
|
|
205
|
-
}
|
|
206
|
-
} catch {}
|
|
207
|
-
}
|
|
208
|
-
async function main() {
|
|
209
|
-
const store = new MemoryStore(resolveDbPath());
|
|
210
|
-
ensureViewer().catch(() => {});
|
|
211
|
-
const server = new McpServer({
|
|
212
|
-
name: "codemem",
|
|
213
|
-
version: VERSION
|
|
214
|
-
});
|
|
215
|
-
server.tool("memory_search", "Search memories by text query. Returns full body text for each match.", {
|
|
216
|
-
query: z.string().describe("Search query"),
|
|
217
|
-
limit: z.number().int().min(1).max(50).default(5).describe("Max results"),
|
|
218
|
-
...filterSchema
|
|
219
|
-
}, async (args) => {
|
|
220
|
-
try {
|
|
221
|
-
const filters = buildFilters(args);
|
|
222
|
-
return jsonContent({ items: store.search(args.query, args.limit, filters).map((m) => ({
|
|
223
|
-
id: m.id,
|
|
224
|
-
title: m.title,
|
|
225
|
-
kind: m.kind,
|
|
226
|
-
body: m.body_text,
|
|
227
|
-
confidence: m.confidence,
|
|
228
|
-
score: m.score,
|
|
229
|
-
session_id: m.session_id,
|
|
230
|
-
metadata: m.metadata
|
|
231
|
-
})) });
|
|
232
|
-
} catch (err) {
|
|
233
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
server.tool("memory_search_index", "Search memories by text query. Returns compact index entries (no body) for browsing.", {
|
|
237
|
-
query: z.string().describe("Search query"),
|
|
238
|
-
limit: z.number().int().min(1).max(50).default(8).describe("Max results"),
|
|
239
|
-
...filterSchema
|
|
240
|
-
}, async (args) => {
|
|
241
|
-
try {
|
|
242
|
-
const filters = buildFilters(args);
|
|
243
|
-
return jsonContent({ items: store.search(args.query, args.limit, filters).map((m) => ({
|
|
244
|
-
id: m.id,
|
|
245
|
-
kind: m.kind,
|
|
246
|
-
title: m.title,
|
|
247
|
-
score: m.score,
|
|
248
|
-
created_at: m.created_at,
|
|
249
|
-
session_id: m.session_id,
|
|
250
|
-
metadata: m.metadata
|
|
251
|
-
})) });
|
|
252
|
-
} catch (err) {
|
|
253
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
server.tool("memory_timeline", "Get a chronological window of memories around an anchor (by ID or query).", {
|
|
257
|
-
query: z.string().optional().describe("Search query to find anchor"),
|
|
258
|
-
memory_id: z.number().int().optional().describe("Anchor memory ID"),
|
|
259
|
-
depth_before: z.number().int().min(0).default(3).describe("Items before anchor"),
|
|
260
|
-
depth_after: z.number().int().min(0).default(3).describe("Items after anchor"),
|
|
261
|
-
...filterSchema
|
|
262
|
-
}, async (args) => {
|
|
263
|
-
try {
|
|
264
|
-
const filters = buildFilters(args);
|
|
265
|
-
return jsonContent({ items: store.timeline(args.query ?? null, args.memory_id ?? null, args.depth_before, args.depth_after, filters) });
|
|
266
|
-
} catch (err) {
|
|
267
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
server.tool("memory_explain", "Explain search results with detailed scoring breakdown.", {
|
|
271
|
-
query: z.string().optional().describe("Search query"),
|
|
272
|
-
ids: z.array(z.number().int()).max(200).optional().describe("Specific memory IDs to explain"),
|
|
273
|
-
limit: z.number().int().min(1).max(50).default(10).describe("Max results"),
|
|
274
|
-
include_pack_context: z.boolean().default(false).describe("Include formatted pack context"),
|
|
275
|
-
...filterSchema
|
|
276
|
-
}, async (args) => {
|
|
277
|
-
try {
|
|
278
|
-
const filters = buildFilters(args);
|
|
279
|
-
return jsonContent(store.explain(args.query ?? null, args.ids ?? null, args.limit, filters, { includePackContext: args.include_pack_context }));
|
|
280
|
-
} catch (err) {
|
|
281
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
server.tool("memory_expand", "Fetch memories by ID with surrounding timeline context.", {
|
|
285
|
-
ids: z.array(z.union([z.number(), z.string()])).max(200).describe("Memory IDs to expand"),
|
|
286
|
-
depth_before: z.number().int().min(0).default(3).describe("Timeline items before"),
|
|
287
|
-
depth_after: z.number().int().min(0).default(3).describe("Timeline items after"),
|
|
288
|
-
include_observations: z.boolean().default(false).describe("Include full observation details"),
|
|
289
|
-
...filterSchema
|
|
290
|
-
}, async (args) => {
|
|
291
|
-
try {
|
|
292
|
-
const filters = buildFilters(args, args.project !== void 0 && !args.project.trim() ? null : defaultProject);
|
|
293
|
-
const resolvedProject = filters?.project ?? null;
|
|
294
|
-
const { ordered: orderedIds, invalid: invalidIds } = dedupeOrderedIds(args.ids);
|
|
295
|
-
const errors = [];
|
|
296
|
-
if (invalidIds.length > 0) errors.push({
|
|
297
|
-
code: "INVALID_ARGUMENT",
|
|
298
|
-
field: "ids",
|
|
299
|
-
message: "some ids are not valid integers",
|
|
300
|
-
ids: invalidIds
|
|
301
|
-
});
|
|
302
|
-
const missingNotFound = [];
|
|
303
|
-
const missingProjectMismatch = [];
|
|
304
|
-
const missingFilterMismatch = [];
|
|
305
|
-
const anchors = [];
|
|
306
|
-
const timelineItems = [];
|
|
307
|
-
const timelineSeen = /* @__PURE__ */ new Set();
|
|
308
|
-
const sessionProjects = /* @__PURE__ */ new Map();
|
|
309
|
-
for (const memoryId of orderedIds) {
|
|
310
|
-
const item = store.get(memoryId);
|
|
311
|
-
if (!item?.active) {
|
|
312
|
-
missingNotFound.push(memoryId);
|
|
313
|
-
continue;
|
|
314
|
-
}
|
|
315
|
-
const sessionId = item.session_id;
|
|
316
|
-
if (resolvedProject && sessionId > 0) {
|
|
317
|
-
if (!sessionProjects.has(sessionId)) {
|
|
318
|
-
const row = store.db.prepare("SELECT project FROM sessions WHERE id = ? LIMIT 1").get(sessionId);
|
|
319
|
-
sessionProjects.set(sessionId, typeof row?.project === "string" ? row.project : null);
|
|
320
|
-
}
|
|
321
|
-
if (!projectMatchesFilter(resolvedProject, sessionProjects.get(sessionId) ?? null)) {
|
|
322
|
-
missingProjectMismatch.push(memoryId);
|
|
323
|
-
continue;
|
|
324
|
-
}
|
|
325
|
-
} else if (resolvedProject && sessionId <= 0) {
|
|
326
|
-
missingProjectMismatch.push(memoryId);
|
|
327
|
-
continue;
|
|
328
|
-
}
|
|
329
|
-
const expanded = store.timeline(null, memoryId, args.depth_before, args.depth_after, filters);
|
|
330
|
-
const anchor = expanded.find((expandedItem) => expandedItem.id === memoryId);
|
|
331
|
-
if (!anchor) {
|
|
332
|
-
missingFilterMismatch.push(memoryId);
|
|
333
|
-
continue;
|
|
334
|
-
}
|
|
335
|
-
anchors.push(anchor);
|
|
336
|
-
for (const expandedItem of expanded) {
|
|
337
|
-
const expandedId = expandedItem.id;
|
|
338
|
-
if (expandedId <= 0 || timelineSeen.has(expandedId)) continue;
|
|
339
|
-
timelineSeen.add(expandedId);
|
|
340
|
-
timelineItems.push(expandedItem);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
if (missingNotFound.length > 0) errors.push({
|
|
344
|
-
code: "NOT_FOUND",
|
|
345
|
-
field: "ids",
|
|
346
|
-
message: "some requested ids were not found",
|
|
347
|
-
ids: missingNotFound
|
|
348
|
-
});
|
|
349
|
-
if (missingProjectMismatch.length > 0) errors.push({
|
|
350
|
-
code: "PROJECT_MISMATCH",
|
|
351
|
-
field: "project",
|
|
352
|
-
message: "some requested ids are outside the requested project scope",
|
|
353
|
-
ids: missingProjectMismatch
|
|
354
|
-
});
|
|
355
|
-
if (missingFilterMismatch.length > 0) errors.push({
|
|
356
|
-
code: "FILTER_MISMATCH",
|
|
357
|
-
field: "filters",
|
|
358
|
-
message: "some requested ids are outside the requested filters",
|
|
359
|
-
ids: missingFilterMismatch
|
|
360
|
-
});
|
|
361
|
-
let observations = [];
|
|
362
|
-
if (args.include_observations) {
|
|
363
|
-
const observationSeen = /* @__PURE__ */ new Set();
|
|
364
|
-
const observationIds = [];
|
|
365
|
-
for (const item of [...anchors, ...timelineItems]) if (item.id > 0 && !observationSeen.has(item.id)) {
|
|
366
|
-
observationSeen.add(item.id);
|
|
367
|
-
observationIds.push(item.id);
|
|
368
|
-
}
|
|
369
|
-
observations = getManyForMcp(store, observationIds, filters);
|
|
370
|
-
}
|
|
371
|
-
return jsonContent({
|
|
372
|
-
anchors,
|
|
373
|
-
timeline: timelineItems,
|
|
374
|
-
observations,
|
|
375
|
-
missing_ids: orderedIds.filter((memoryId) => missingNotFound.includes(memoryId) || missingProjectMismatch.includes(memoryId) || missingFilterMismatch.includes(memoryId)),
|
|
376
|
-
errors,
|
|
377
|
-
metadata: {
|
|
378
|
-
project: resolvedProject,
|
|
379
|
-
requested_ids_count: orderedIds.length,
|
|
380
|
-
returned_anchor_count: anchors.length,
|
|
381
|
-
timeline_count: timelineItems.length,
|
|
382
|
-
include_observations: args.include_observations
|
|
383
|
-
}
|
|
384
|
-
});
|
|
385
|
-
} catch (err) {
|
|
386
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
server.tool("memory_get", "Fetch a single memory item by ID.", {
|
|
390
|
-
memory_id: z.number().int().describe("Memory ID"),
|
|
391
|
-
...filterSchema
|
|
392
|
-
}, async (args) => {
|
|
393
|
-
try {
|
|
394
|
-
const item = getMemoryForMcp(store, args.memory_id, buildFilters(args, null));
|
|
395
|
-
if (!item) return errorContent("not_found");
|
|
396
|
-
return jsonContent(item);
|
|
397
|
-
} catch (err) {
|
|
398
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
server.tool("memory_get_observations", "Fetch multiple memory items by their IDs.", {
|
|
402
|
-
ids: z.array(z.number().int()).max(200).describe("Memory IDs to fetch"),
|
|
403
|
-
...filterSchema
|
|
404
|
-
}, async (args) => {
|
|
405
|
-
try {
|
|
406
|
-
return jsonContent({ items: getManyForMcp(store, args.ids, buildFilters(args, null)) });
|
|
407
|
-
} catch (err) {
|
|
408
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
server.tool("memory_recent", "Return recent memories, newest first.", {
|
|
412
|
-
limit: z.number().int().min(1).max(100).default(8).describe("Max results"),
|
|
413
|
-
...filterSchema
|
|
414
|
-
}, async (args) => {
|
|
415
|
-
try {
|
|
416
|
-
const filters = buildFilters(args);
|
|
417
|
-
return jsonContent({ items: store.recent(args.limit, filters) });
|
|
418
|
-
} catch (err) {
|
|
419
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
420
|
-
}
|
|
421
|
-
});
|
|
422
|
-
server.tool("memory_pack", "Build a formatted memory pack from search results — quick one-shot context block.", {
|
|
423
|
-
context: z.string().describe("Context description to search for"),
|
|
424
|
-
limit: z.number().int().min(1).max(50).optional().describe("Max items to include"),
|
|
425
|
-
compact: z.boolean().optional().describe("When true, render a scannable index of all items with full detail only for the top N (default 3). Saves tokens when broad overview matters more than per-item detail."),
|
|
426
|
-
compact_detail_count: z.number().int().min(0).max(50).optional().describe("Number of items to show in full detail in compact mode (default 3)"),
|
|
427
|
-
compression_mode: z.enum([
|
|
428
|
-
"off",
|
|
429
|
-
"compact",
|
|
430
|
-
"ids"
|
|
431
|
-
]).optional().describe("Near-related compression mode: off disables it, compact applies only to compact rendering, ids applies in all modes. Defaults to CODEMEM_PACK_COMPRESSION or compact."),
|
|
432
|
-
...filterSchema
|
|
433
|
-
}, async (args) => {
|
|
434
|
-
try {
|
|
435
|
-
const filters = buildFilters(args);
|
|
436
|
-
const renderOptions = args.compact || args.compact_detail_count != null || args.compression_mode != null ? {
|
|
437
|
-
compact: args.compact ?? (args.compact_detail_count != null ? true : void 0),
|
|
438
|
-
compactDetailCount: args.compact_detail_count,
|
|
439
|
-
compressionMode: args.compression_mode
|
|
440
|
-
} : void 0;
|
|
441
|
-
return jsonContent(await store.buildMemoryPackAsync(args.context, args.limit ?? void 0, null, filters, renderOptions));
|
|
442
|
-
} catch (err) {
|
|
443
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
444
|
-
}
|
|
445
|
-
});
|
|
446
|
-
server.tool("memory_remember", "Create a new memory. Use for milestones, decisions, and notable facts.", {
|
|
447
|
-
kind: z.enum([
|
|
448
|
-
"discovery",
|
|
449
|
-
"change",
|
|
450
|
-
"feature",
|
|
451
|
-
"bugfix",
|
|
452
|
-
"refactor",
|
|
453
|
-
"decision",
|
|
454
|
-
"exploration"
|
|
455
|
-
]).describe("Memory kind"),
|
|
456
|
-
title: z.string().describe("Short title"),
|
|
457
|
-
body: z.string().describe("Body text (high-signal content)"),
|
|
458
|
-
confidence: z.number().min(0).max(1).default(.5).describe("Confidence 0-1"),
|
|
459
|
-
project: z.string().optional().describe("Project identifier")
|
|
460
|
-
}, async (args) => {
|
|
461
|
-
try {
|
|
462
|
-
const result = rememberMemoryForMcp(store, args, { envProject: process.env.CODEMEM_PROJECT ?? null });
|
|
463
|
-
try {
|
|
464
|
-
await storeVectors(store.db, result.memId, result.title, result.body);
|
|
465
|
-
} catch {}
|
|
466
|
-
return jsonContent({ id: result.memId });
|
|
467
|
-
} catch (err) {
|
|
468
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
469
|
-
}
|
|
470
|
-
});
|
|
471
|
-
server.tool("memory_forget", "Soft-delete a memory item. Use for incorrect or sensitive data.", {
|
|
472
|
-
memory_id: z.number().int().describe("Memory ID to forget"),
|
|
473
|
-
...filterSchema
|
|
474
|
-
}, async (args) => {
|
|
475
|
-
try {
|
|
476
|
-
if (!forgetMemoryForMcp(store, args.memory_id, buildFilters(args, null))) return errorContent("not_found");
|
|
477
|
-
return jsonContent({ status: "ok" });
|
|
478
|
-
} catch (err) {
|
|
479
|
-
return errorContent(err instanceof Error ? err.message : String(err));
|
|
480
|
-
}
|
|
481
|
-
});
|
|
482
|
-
server.tool("memory_schema", "Return the memory schema — kinds, fields, and available filters.", {}, async () => {
|
|
483
|
-
return jsonContent({
|
|
484
|
-
kinds: Object.keys(MEMORY_KINDS),
|
|
485
|
-
kind_descriptions: MEMORY_KINDS,
|
|
486
|
-
fields: {
|
|
487
|
-
title: "short text",
|
|
488
|
-
body: "long text",
|
|
489
|
-
subtitle: "short text",
|
|
490
|
-
facts: "list<string>",
|
|
491
|
-
narrative: "long text",
|
|
492
|
-
concepts: "list<string>",
|
|
493
|
-
files_read: "list<string>",
|
|
494
|
-
files_modified: "list<string>",
|
|
495
|
-
prompt_number: "int"
|
|
496
|
-
},
|
|
497
|
-
filters: [
|
|
498
|
-
"kind",
|
|
499
|
-
"session_id",
|
|
500
|
-
"since",
|
|
501
|
-
"project",
|
|
502
|
-
"scope_id",
|
|
503
|
-
"include_scope_ids",
|
|
504
|
-
"exclude_scope_ids",
|
|
505
|
-
"include_actor_ids",
|
|
506
|
-
"exclude_actor_ids",
|
|
507
|
-
"include_visibility",
|
|
508
|
-
"exclude_visibility",
|
|
509
|
-
"include_workspace_ids",
|
|
510
|
-
"exclude_workspace_ids",
|
|
511
|
-
"include_workspace_kinds",
|
|
512
|
-
"exclude_workspace_kinds"
|
|
513
|
-
]
|
|
514
|
-
});
|
|
515
|
-
});
|
|
516
|
-
server.tool("memory_learn", "Learn how to use codemem memory tools. Call this first if unfamiliar.", {}, async () => {
|
|
517
|
-
return jsonContent({
|
|
518
|
-
intro: "Use this tool when you're new to codemem or unsure when to recall/persist.",
|
|
519
|
-
client_hint: "If you are unfamiliar with codemem, call memory.learn first.",
|
|
520
|
-
recall: {
|
|
521
|
-
when: ["Start of a task or when the user references prior work.", "When you need background context, decisions, or recent changes."],
|
|
522
|
-
how: [
|
|
523
|
-
"Use memory.search_index to get compact candidates.",
|
|
524
|
-
"Use memory.timeline to expand around a promising memory.",
|
|
525
|
-
"Use memory.get_observations for full details only when needed.",
|
|
526
|
-
"Use memory.pack for quick one-shot context blocks.",
|
|
527
|
-
"Use the project filter unless the user requests cross-project context."
|
|
528
|
-
],
|
|
529
|
-
examples: [
|
|
530
|
-
"memory.search_index(\"billing cache bug\", limit=5)",
|
|
531
|
-
"memory.timeline(memory_id=123)",
|
|
532
|
-
"memory.get_observations([123, 456])"
|
|
533
|
-
]
|
|
534
|
-
},
|
|
535
|
-
persistence: {
|
|
536
|
-
when: ["Milestones (task done, key decision, new facts learned).", "Notable regressions or follow-ups that should be remembered."],
|
|
537
|
-
how: [
|
|
538
|
-
"Use memory.remember with kind decision/discovery/change/exploration.",
|
|
539
|
-
"Keep titles short and bodies high-signal.",
|
|
540
|
-
"ALWAYS pass the project parameter if known."
|
|
541
|
-
],
|
|
542
|
-
examples: ["memory.remember(kind=\"decision\", title=\"Switch to async cache\", body=\"...why...\", project=\"my-service\")", "memory.remember(kind=\"change\", title=\"Fixed retry loop\", body=\"...impact...\", project=\"my-service\")"]
|
|
543
|
-
},
|
|
544
|
-
forget: {
|
|
545
|
-
when: ["Accidental or sensitive data stored in memory items.", "Obsolete or incorrect items that should no longer surface."],
|
|
546
|
-
how: ["Call memory.forget(id) to mark the item inactive.", "Prefer forgetting over overwriting to preserve auditability."],
|
|
547
|
-
examples: ["memory.forget(123)"]
|
|
548
|
-
},
|
|
549
|
-
prompt_hint: "At task start: call memory.search_index; during work: memory.timeline + memory.get_observations; at milestones: memory.remember.",
|
|
550
|
-
recommended_system_prompt: [
|
|
551
|
-
"Trigger policy (1-liner): If the user references prior work or starts a task,",
|
|
552
|
-
"immediately call memory.search_index; then use memory.timeline + memory.get_observations;",
|
|
553
|
-
"at milestones, call memory.remember; use memory.forget for incorrect/sensitive items.",
|
|
554
|
-
"",
|
|
555
|
-
"System prompt:",
|
|
556
|
-
"You have access to codemem MCP tools. If unfamiliar, call memory.learn first.",
|
|
557
|
-
"",
|
|
558
|
-
"Recall:",
|
|
559
|
-
"- Start of any task: call memory.search_index with a concise task query.",
|
|
560
|
-
"- If prior work is referenced (\"as before\", \"last time\", \"we already did…\", \"regression\"),",
|
|
561
|
-
" call memory.search_index or memory.timeline.",
|
|
562
|
-
"- Use memory.get_observations only after filtering IDs.",
|
|
563
|
-
"- Prefer project-scoped queries unless the user asks for cross-project.",
|
|
564
|
-
"",
|
|
565
|
-
"Persistence:",
|
|
566
|
-
"- On milestones (task done, key decision, new facts learned), call memory.remember.",
|
|
567
|
-
"- Use kind=decision for tradeoffs, kind=change for outcomes, kind=discovery/exploration for useful findings.",
|
|
568
|
-
"- Keep titles short and bodies high-signal.",
|
|
569
|
-
"- ALWAYS pass the project parameter if known.",
|
|
570
|
-
"",
|
|
571
|
-
"Safety:",
|
|
572
|
-
"- Use memory.forget(id) for incorrect or sensitive items.",
|
|
573
|
-
"",
|
|
574
|
-
"Examples:",
|
|
575
|
-
"- memory.search_index(\"billing cache bug\")",
|
|
576
|
-
"- memory.timeline(memory_id=123)",
|
|
577
|
-
"- memory.get_observations([123, 456])",
|
|
578
|
-
"- memory.remember(kind=\"decision\", title=\"Use async cache\", body=\"Chose async cache to avoid lock contention in X.\", project=\"my-service\")",
|
|
579
|
-
"- memory.remember(kind=\"change\", title=\"Fixed retry loop\", body=\"Root cause was Y; added guard in Z.\", project=\"my-service\")",
|
|
580
|
-
"- memory.forget(123)"
|
|
581
|
-
].join("\n")
|
|
582
|
-
});
|
|
583
|
-
});
|
|
584
|
-
const transport = new StdioServerTransport();
|
|
585
|
-
const shutdown = () => {
|
|
586
|
-
try {
|
|
587
|
-
store.close();
|
|
588
|
-
} catch {}
|
|
589
|
-
process.exit(0);
|
|
590
|
-
};
|
|
591
|
-
process.on("SIGINT", shutdown);
|
|
592
|
-
process.on("SIGTERM", shutdown);
|
|
593
|
-
await server.connect(transport);
|
|
594
|
-
}
|
|
595
|
-
main().catch((err) => {
|
|
596
|
-
console.error("codemem MCP server failed to start:", err);
|
|
597
|
-
process.exit(1);
|
|
598
|
-
});
|
|
599
|
-
//#endregion
|
|
600
|
-
export {};
|
|
601
|
-
|
|
602
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import { t as e } from "./server-Djvf0Yik.js";
|
|
2
|
+
export { e as createCodememMcpServer };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-access.d.ts","sourceRoot":"","sources":["../src/memory-access.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,WAAW,EAEhB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"memory-access.d.ts","sourceRoot":"","sources":["../src/memory-access.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,WAAW,EAEhB,MAAM,eAAe,CAAC;AAGvB,wBAAgB,eAAe,CAC9B,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,aAAa,GACrB,kBAAkB,GAAG,IAAI,CAG3B;AAED,wBAAgB,aAAa,CAC5B,KAAK,EAAE,WAAW,EAClB,GAAG,EAAE,MAAM,EAAE,EACb,OAAO,CAAC,EAAE,aAAa,GACrB,kBAAkB,EAAE,CAQtB;AAED,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,aAAa,GACrB,OAAO,CAKT;AAED,MAAM,WAAW,yBAAyB;IACzC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAED,MAAM,WAAW,2BAA2B;IAC3C,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACnB;AAED,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,WAAW,EAClB,KAAK,EAAE,yBAAyB,EAChC,OAAO,GAAE,2BAAgC,GACvC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CA6BhD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-kinds.d.ts","sourceRoot":"","sources":["../src/memory-kinds.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAQ/C,CAAC"}
|