save_memory
Store key-value pairs in persistent memory to save user preferences, project decisions, and session context for future use.
Instructions
Save a key-value pair to persistent memory. Use this to remember user preferences, project decisions, or important context across sessions.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Unique key for this memory | |
| value | Yes | Value to store | |
| project | No | Optional project scope for organizing memories |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"key": {
"description": "Unique key for this memory",
"type": "string"
},
"project": {
"description": "Optional project scope for organizing memories",
"type": "string"
},
"value": {
"description": "Value to store",
"type": "string"
}
},
"required": [
"key",
"value"
],
"type": "object"
}
Implementation Reference
- src/tools/memory.ts:53-58 (handler)The main handler function for the 'save_memory' tool. It ensures the memory file exists, reads the current data, updates it with the new key-value pair (including optional project and timestamp), writes back to the JSON file at ~/.code-mcp-memory.json, and returns a confirmation message.export async function saveMemoryHandler(args: any) { await ensureMemoryFile(); const data = JSON.parse(await fs.readFile(MEMORY_FILE, "utf-8")); data[args.key] = { value: args.value, project: args.project, timestamp: new Date().toISOString() }; await fs.writeFile(MEMORY_FILE, JSON.stringify(data, null, 2)); return { content: [{ type: "text", text: `Saved memory: ${args.key}` }] };
- src/tools/memory.ts:17-25 (schema)Zod-based input schema definition for the 'save_memory' tool, specifying required 'key' and 'value' strings, and optional 'project' string.export const saveMemorySchema = { name: "save_memory", description: "Save a key-value pair to persistent memory. Use this to remember user preferences, project decisions, or important context across sessions.", inputSchema: z.object({ key: z.string().describe("Unique key for this memory"), value: z.string().describe("Value to store"), project: z.string().optional().describe("Optional project scope for organizing memories") }) };
- src/index.ts:89-89 (registration)Registration of the 'save_memory' tool in the toolRegistry Map used by the stdio MCP server (index.ts).["save_memory", { schema: saveMemorySchema, handler: saveMemoryHandler }],
- src/server.ts:98-98 (registration)Registration of the 'save_memory' tool in the toolRegistry Map used by the HTTP MCP server (server.ts).["save_memory", { schema: saveMemorySchema, handler: saveMemoryHandler }],
- src/tools/memory.ts:9-15 (helper)Helper function to ensure the persistent memory JSON file exists, creating an empty object if not.async function ensureMemoryFile() { try { await fs.access(MEMORY_FILE); } catch { await fs.writeFile(MEMORY_FILE, JSON.stringify({}, null, 2)); } }