move_document
Transfer a document from one location to another and optionally update references to it in other files. Ideal for managing and organizing markdown documentation effectively.
Instructions
Move a document from one location to another. Optionally updates references to the document in other files.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| destinationPath | Yes | ||
| path | No | ||
| sourcePath | Yes | ||
| updateReferences | No |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"destinationPath": {
"type": "string"
},
"path": {
"type": "string"
},
"sourcePath": {
"type": "string"
},
"updateReferences": {
"default": true,
"type": "boolean"
}
},
"required": [
"sourcePath",
"destinationPath"
],
"type": "object"
}
Implementation Reference
- src/handlers/documents.ts:424-489 (handler)The core handler function in DocumentHandler class that implements the move_document tool logic. It validates paths, copies the file content from source to destination, deletes the source, and optionally updates references in other documents.async moveDocument( sourcePath: string, destinationPath: string, updateReferences = true ): Promise<ToolResponse> { try { const validSourcePath = await this.validatePath(sourcePath); const validDestPath = await this.validatePath(destinationPath); // Check if source exists try { await fs.access(validSourcePath); } catch { throw new Error(`Source file does not exist: ${sourcePath}`); } // Create destination directory if it doesn't exist const destDir = path.dirname(validDestPath); await fs.mkdir(destDir, { recursive: true }); // Read the source file const content = await fs.readFile(validSourcePath, "utf-8"); // Write to destination await fs.writeFile(validDestPath, content, "utf-8"); // Delete the source file await fs.unlink(validSourcePath); // Update references if requested let referencesUpdated = 0; if (updateReferences) { referencesUpdated = await this.updateReferences( sourcePath, destinationPath ); } return { content: [ { type: "text", text: `Successfully moved document from ${sourcePath} to ${destinationPath}` + (referencesUpdated > 0 ? `. Updated ${referencesUpdated} references.` : ""), }, ], metadata: { sourcePath, destinationPath, referencesUpdated, }, }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error moving document: ${errorMessage}` }, ], isError: true, }; } }
- src/schemas/tools.ts:69-73 (schema)Zod schema defining the input validation for the move_document tool, extending ToolInputSchema with sourcePath, destinationPath, and optional updateReferences.export const MoveDocumentSchema = ToolInputSchema.extend({ sourcePath: z.string(), destinationPath: z.string(), updateReferences: z.boolean().default(true), });
- src/index.ts:259-264 (registration)Tool registration in the ListToolsRequestHandler, defining the tool name, description, and input schema for move_document.name: "move_document", description: "Move a document from one location to another. Optionally updates references to the " + "document in other files.", inputSchema: zodToJsonSchema(MoveDocumentSchema) as any, },
- src/index.ts:414-426 (registration)Dispatch handler in CallToolRequestSchema that parses input with MoveDocumentSchema and calls documentHandler.moveDocument. Note: case label mismatch with tool name ("move_documentation_document" vs "move_document").case "move_documentation_document": { const parsed = MoveDocumentSchema.safeParse(args); if (!parsed.success) { throw new Error( `Invalid arguments for move_document: ${parsed.error}` ); } return await documentHandler.moveDocument( parsed.data.sourcePath, parsed.data.destinationPath, parsed.data.updateReferences ); }
- src/handlers/documents.ts:743-790 (helper)Private helper method used by moveDocument (and renameDocument) to update references to the moved/renamed document across all markdown files.private async updateReferences( oldPath: string, newPath: string ): Promise<number> { // Normalize paths for comparison const normalizedOldPath = oldPath.replace(/\\/g, "/"); const normalizedNewPath = newPath.replace(/\\/g, "/"); // Find all markdown files const files = await glob("**/*.md", { cwd: this.docsDir }); let updatedCount = 0; for (const file of files) { const filePath = path.join(this.docsDir, file); const content = await fs.readFile(filePath, "utf-8"); // Look for references to the old path // Match markdown links: [text](path) const linkRegex = new RegExp( `\\[([^\\]]+)\\]\\(${normalizedOldPath.replace( /[.*+?^${}()|[\]\\]/g, "\\$&" )}\\)`, "g" ); // Match direct path references const pathRegex = new RegExp( normalizedOldPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g" ); // Replace references let updatedContent = content.replace( linkRegex, `[$1](${normalizedNewPath})` ); updatedContent = updatedContent.replace(pathRegex, normalizedNewPath); // If content changed, write the updated file if (updatedContent !== content) { await fs.writeFile(filePath, updatedContent, "utf-8"); updatedCount++; } } return updatedCount; }