memory_recent
Retrieve memories saved within a configurable number of past days. Use the project filter to narrow down by specific work context.
Instructions
List recently saved memories. Use for a quick overview of what's been recorded.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| days | No | How many days back (default: 7) | |
| project | No | Filter by project name |
Implementation Reference
- The handleMemoryRecent function that queries the SQLite database for memories newer than a cutoff date (default 7 days), optionally filtered by project, returning up to 20 results in a formatted string.
export async function handleMemoryRecent( days?: number, project?: string, ): Promise<string> { const db = getDb(); const cutoff = Date.now() - (days ?? 7) * 24 * 60 * 60 * 1000; let sql = `SELECT id, content, category, project, created_at FROM memories WHERE created_at >= ? AND archived = 0`; const params: unknown[] = [cutoff]; if (project) { sql += ` AND project = ?`; params.push(project); } sql += ` ORDER BY created_at DESC LIMIT 20`; const memories = db.prepare(sql).all(...params) as Array<{ id: string; content: string; category: string; project: string | null; created_at: number; }>; if (memories.length === 0) { const projectStr = project ? ` for project "${project}"` : ""; return `No memories in the last ${days ?? 7} day(s)${projectStr}.`; } const lines = memories.map((m) => { const date = new Date(m.created_at).toISOString().slice(0, 16).replace("T", " "); const projectTag = m.project ? ` [${m.project}]` : ""; return `- **${date}** (${m.category})${projectTag}: ${m.content.slice(0, 150)}`; }); return `${memories.length} recent memory(s):\n\n${lines.join("\n")}`; } - packages/server/src/index.ts:294-297 (schema)Zod schema definitions for the memory_recent tool: optional 'days' (number) and 'project' (string) parameters.
{ days: z.number().optional().describe("How many days back (default: 7)"), project: z.string().optional().describe("Filter by project name"), }, - packages/server/src/index.ts:291-309 (registration)Registration of the 'memory_recent' tool via server.tool(), including the handler that calls handleMemoryRecent and wraps the result in a text content response.
server.tool( "memory_recent", "List recently saved memories. Use for a quick overview of what's been recorded.", { days: z.number().optional().describe("How many days back (default: 7)"), project: z.string().optional().describe("Filter by project name"), }, async ({ days, project }) => { try { const result = await handleMemoryRecent(days, project); return { content: [{ type: "text", text: result }] }; } catch (err) { return { content: [{ type: "text", text: `Error listing memories: ${err}` }], isError: true, }; } }, ); - packages/server/src/index.ts:18-18 (registration)Import statement for handleMemoryRecent from the tools module.
import { handleMemoryRecent } from "./tools/memory-recent.js"; - The getDb() helper function that returns a better-sqlite3 database connection, used by the handler to query memories.
export function getDb(): any { if (_db) return _db; if (_unavailable || !Database) { _unavailable = true; throw new Error( "Memory database unavailable — better-sqlite3 failed to load. " + "This is usually a native compilation issue. Memory features are disabled but everything else works. " + "Try: npm rebuild better-sqlite3" ); } try { fs.mkdirSync(DB_DIR, { recursive: true }); _db = new Database(DB_PATH); _db.pragma("journal_mode = WAL"); _db.exec(` CREATE TABLE IF NOT EXISTS memories ( id TEXT PRIMARY KEY, content TEXT NOT NULL, category TEXT NOT NULL DEFAULT 'general', project TEXT, source_file TEXT, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, accessed_at INTEGER NOT NULL, access_count INTEGER NOT NULL DEFAULT 0, archived INTEGER NOT NULL DEFAULT 0, embedding BLOB