UNPKG

@ankimcp/anki-mcp-server

Version:

Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards

140 lines • 7.24 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var DeleteNotesTool_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeleteNotesTool = void 0; const common_1 = require("@nestjs/common"); const microservices_1 = require("@nestjs/microservices"); const mcp_nest_1 = require("@rekog/mcp-nest"); const zod_1 = require("zod"); const anki_connect_client_1 = require("../../../clients/anki-connect.client.js"); const anki_utils_1 = require("../../../utils/anki.utils.js"); let DeleteNotesTool = DeleteNotesTool_1 = class DeleteNotesTool { ankiClient; logger = new common_1.Logger(DeleteNotesTool_1.name); constructor(ankiClient) { this.ankiClient = ankiClient; } async deleteNotes({ notes, confirmDeletion }) { try { if (!confirmDeletion) { return (0, anki_utils_1.createErrorResponse)(new Error("Deletion not confirmed"), { requestedNotes: notes, noteCount: notes.length, hint: "Deletion runs only when confirmDeletion is true; it permanently deletes these notes and all their cards", warning: "Deleting permanently removes these notes and all their cards from the collection", }); } this.logger.log(`Deleting ${notes.length} note(s)`); const notesInfo = await this.ankiClient.invoke("notesInfo", { notes: notes, }); const validNotes = notesInfo.filter((note) => note && note.noteId); const validNoteIds = validNotes.map((note) => note.noteId); const notFoundCount = notes.length - validNotes.length; if (validNoteIds.length === 0) { this.logger.warn("No valid notes found to delete"); return { success: true, deletedCount: 0, notFoundCount: notes.length, requestedIds: notes, message: "No notes were deleted (none of the provided IDs were valid)", hint: "The notes may have already been deleted or the IDs are invalid", }; } const totalCards = validNotes.reduce((sum, note) => sum + (note.cards?.length || 0), 0); await this.ankiClient.invoke("deleteNotes", { notes: validNoteIds, }); this.logger.log(`Successfully deleted ${validNoteIds.length} note(s) and ${totalCards} card(s)`); const message = notFoundCount > 0 ? `Successfully deleted ${validNoteIds.length} note(s) and ${totalCards} card(s). ${notFoundCount} note(s) were not found.` : `Successfully deleted ${validNoteIds.length} note(s) and ${totalCards} card(s)`; return { success: true, deletedCount: validNoteIds.length, deletedNoteIds: validNoteIds, cardsDeleted: totalCards, notFoundCount, requestedIds: notes, message: message, warning: "These notes and cards have been permanently deleted", hint: "Other devices see the deletions after the collection is synced with AnkiWeb", }; } catch (error) { this.logger.error("Failed to delete notes", error); if (error instanceof Error) { if (error.message.includes("permission")) { return (0, anki_utils_1.createErrorResponse)(error, { requestedNotes: notes, hint: "Permission denied. AnkiConnect's configuration in Anki may not allow deletions.", }); } } return (0, anki_utils_1.createErrorResponse)(error, { requestedNotes: notes, hint: "This can happen when Anki is not running or the note IDs are invalid", }); } } }; exports.DeleteNotesTool = DeleteNotesTool; __decorate([ (0, mcp_nest_1.Tool)({ name: "deleteNotes", description: "Delete notes by their IDs. Permanently removes the notes and ALL their cards, including the cards' scheduling. " + "Anki's undo history (Edit > Undo, or guiUndo, which reverts the newest step) can restore them until that history is cleared, for example by a change Anki does not record for undo such as updateNoteFields; after that only a backup restores them. " + "The call is rejected unless confirmDeletion is true.", parameters: zod_1.z.object({ notes: zod_1.z .array(zod_1.z.number()) .min(1) .max(100) .describe("Array of note IDs to delete (max 100 at once for safety). " + "Get these IDs from findNotes tool. ALL cards associated with these notes will be deleted."), confirmDeletion: zod_1.z .boolean() .describe("Confirms the permanent deletion of these notes and their cards; the call is rejected unless this is true"), }), outputSchema: zod_1.z.object({ success: zod_1.z.boolean(), deletedCount: zod_1.z.number(), deletedNoteIds: zod_1.z.array(zod_1.z.number()).optional(), cardsDeleted: zod_1.z.number().optional(), notFoundCount: zod_1.z.number(), requestedIds: zod_1.z.array(zod_1.z.number()), message: zod_1.z.string(), warning: zod_1.z.string().optional(), hint: zod_1.z.string().optional(), }), annotations: { title: "Delete Notes", readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false, }, }), __param(0, (0, microservices_1.Payload)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], DeleteNotesTool.prototype, "deleteNotes", null); exports.DeleteNotesTool = DeleteNotesTool = DeleteNotesTool_1 = __decorate([ (0, mcp_nest_1.McpController)(), __metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient]) ], DeleteNotesTool); //# sourceMappingURL=delete-notes.tool.js.map