update_document
Update or regenerate specific document types like project briefs, product context, and system patterns within the Memory Bank MCP server to maintain accurate and structured project knowledge.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | No | ||
| documentType | Yes | ||
| regenerate | No |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"content": {
"type": "string"
},
"documentType": {
"enum": [
"projectbrief",
"productContext",
"systemPatterns",
"techContext",
"activeContext",
"progress"
],
"type": "string"
},
"regenerate": {
"default": false,
"type": "boolean"
}
},
"required": [
"documentType"
],
"type": "object"
}
Implementation Reference
- src/mcp/memoryBankMcp.ts:317-370 (handler)The handler function executes the tool logic: checks initialization, constructs file path, creates file if missing, handles regenerate by appending date or saves provided content using saveDocument, returns success/error messages.async ({ documentType, content, regenerate }) => { try { // Check if Memory Bank directory is initialized if (!MEMORY_BANK_DIR) { throw new Error('Memory Bank not initialized. Please use initialize_memory_bank tool first.'); } const filePath = path.join(MEMORY_BANK_DIR, `${documentType}.md`); // Check if file exists if (!await fs.pathExists(filePath)) { // Create file if it doesn't exist await fs.ensureFile(filePath); await fs.writeFile(filePath, `# ${documentType}\n\n`, 'utf-8'); } if (regenerate) { // Read existing document const currentContent = await readDocument(filePath); // Always use en-US locale for date formatting to ensure English output const dateOptions = { year: 'numeric', month: 'long', day: 'numeric' }; const englishDate = new Date().toLocaleDateString('en-US'); // TODO: Generate new content with Gemini (example for now) const newContent = `${currentContent}\n\n## Update\nThis document was regenerated on ${englishDate}.`; // Save document await saveDocument(newContent, filePath); } else if (content) { // Save provided content await saveDocument(content, filePath); } else { throw new Error('Content must be provided or regenerate=true'); } // Always use English for all response messages return { content: [{ type: 'text', text: `✅ "${documentType}.md" document successfully updated!` }] }; } catch (error) { console.error('Error updating document:', error); // Ensure error messages are also in English const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: 'text', text: `❌ Error: ${errorMessage}` }], isError: true }; } } );
- src/mcp/memoryBankMcp.ts:312-316 (schema)Zod input schema defining parameters for the update_document tool: documentType as enum of valid documents, optional content string, optional regenerate boolean (defaults to false).{ documentType: z.enum(['projectbrief', 'productContext', 'systemPatterns', 'techContext', 'activeContext', 'progress']), content: z.string().optional(), regenerate: z.boolean().default(false) },
- src/mcp/memoryBankMcp.ts:310-370 (registration)MCP server.tool registration of the 'update_document' tool with schema and handler function.server.tool( 'update_document', { documentType: z.enum(['projectbrief', 'productContext', 'systemPatterns', 'techContext', 'activeContext', 'progress']), content: z.string().optional(), regenerate: z.boolean().default(false) }, async ({ documentType, content, regenerate }) => { try { // Check if Memory Bank directory is initialized if (!MEMORY_BANK_DIR) { throw new Error('Memory Bank not initialized. Please use initialize_memory_bank tool first.'); } const filePath = path.join(MEMORY_BANK_DIR, `${documentType}.md`); // Check if file exists if (!await fs.pathExists(filePath)) { // Create file if it doesn't exist await fs.ensureFile(filePath); await fs.writeFile(filePath, `# ${documentType}\n\n`, 'utf-8'); } if (regenerate) { // Read existing document const currentContent = await readDocument(filePath); // Always use en-US locale for date formatting to ensure English output const dateOptions = { year: 'numeric', month: 'long', day: 'numeric' }; const englishDate = new Date().toLocaleDateString('en-US'); // TODO: Generate new content with Gemini (example for now) const newContent = `${currentContent}\n\n## Update\nThis document was regenerated on ${englishDate}.`; // Save document await saveDocument(newContent, filePath); } else if (content) { // Save provided content await saveDocument(content, filePath); } else { throw new Error('Content must be provided or regenerate=true'); } // Always use English for all response messages return { content: [{ type: 'text', text: `✅ "${documentType}.md" document successfully updated!` }] }; } catch (error) { console.error('Error updating document:', error); // Ensure error messages are also in English const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: 'text', text: `❌ Error: ${errorMessage}` }], isError: true }; } } );
- src/utils/fileManager.ts:71-84 (helper)Helper utility function called by the handler to persist document content to the filesystem, ensuring directory exists before writing.export async function saveDocument(content: string, filePath: string): Promise<void> { try { // Ensure directory exists await fs.ensureDir(path.dirname(filePath)); // Write file await fs.writeFile(filePath, content, 'utf-8'); console.log(`Document saved: ${filePath}`); } catch (error) { console.error('Error saving document:', error); throw new Error(`Failed to save document: ${error}`); } }
- src/utils/fileManager.ts:91-106 (helper)Helper utility function used in regenerate mode to read existing document content before updating.export async function readDocument(filePath: string): Promise<string> { try { // Check if file exists if (!await fs.pathExists(filePath)) { throw new Error(`Document not found: ${filePath}`); } // Read file const content = await fs.readFile(filePath, 'utf-8'); return content; } catch (error) { console.error('Error reading document:', error); throw new Error(`Failed to read document: ${error}`); } }