UNPKG

@ankimcp/anki-mcp-server

Version:

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

146 lines • 7.8 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 UpdateModelTemplatesTool_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateModelTemplatesTool = 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 UpdateModelTemplatesTool = UpdateModelTemplatesTool_1 = class UpdateModelTemplatesTool { ankiClient; logger = new common_1.Logger(UpdateModelTemplatesTool_1.name); constructor(ankiClient) { this.ankiClient = ankiClient; } async updateModelTemplates({ modelName, templates, }) { try { this.logger.log(`Updating templates for model: ${modelName}`); const existingTemplates = await this.ankiClient.invoke("modelTemplates", { modelName, }); if (!existingTemplates || Object.keys(existingTemplates).length === 0) { return (0, anki_utils_1.createErrorResponse)(new Error(`Model "${modelName}" has no templates or does not exist`), { modelName, hint: "Model not found. modelNames lists the available models.", }); } const existingNames = new Set(Object.keys(existingTemplates)); const unknownNames = Object.keys(templates).filter((name) => !existingNames.has(name)); if (unknownNames.length > 0) { const offending = unknownNames.map((name) => `"${name}"`).join(", "); const validNames = Object.keys(existingTemplates) .map((name) => `"${name}"`) .join(", "); return (0, anki_utils_1.createErrorResponse)(new Error(`Card template(s) not found in model "${modelName}": ${offending}. ` + `Valid templates: ${validNames}. ` + "modelTemplates lists the current names."), { modelName, hint: `Card template names are case-sensitive and must match exactly. Valid templates: ${validNames}.`, }); } const templateCount = Object.keys(templates).length; await this.ankiClient.invoke("updateModelTemplates", { model: { name: modelName, templates, }, }); this.logger.log(`Successfully updated ${templateCount} templates for model: ${modelName}`); return { success: true, modelName, templateCount, message: `Successfully updated ${templateCount} card template(s) for model "${modelName}"`, hint: "Template changes apply to all cards using this model.", }; } catch (error) { this.logger.error(`Failed to update templates for model ${modelName}`, error); const errorMessage = error instanceof Error ? error.message : String(error); if (errorMessage.includes("not found") || errorMessage.includes("does not exist") || errorMessage.includes("model not found")) { return (0, anki_utils_1.createErrorResponse)(error, { modelName, hint: "Model not found. modelNames lists the available models.", }); } return (0, anki_utils_1.createErrorResponse)(error, { modelName, hint: "This can happen when Anki is not running or the model name is wrong", }); } } }; exports.UpdateModelTemplatesTool = UpdateModelTemplatesTool; __decorate([ (0, mcp_nest_1.Tool)({ name: "updateModelTemplates", description: "Update the card templates (Front and Back HTML) for an existing note type (model). " + "Each card template defines how the card's front and back sides are rendered. " + "The Front and Back HTML given for each named template replace the existing HTML; modelTemplates returns the current templates. " + "Changes apply to all cards using this model. " + "Anki validates every card template when the note type is saved and rejects the update if a template is invalid, for example if it cannot be parsed, references a field the note type does not have, has no field on the front, or has the same front as another template; nothing is changed in that case. " + "Card template names are validated against the model's existing templates (case-sensitive) — unknown or mis-cased names are rejected before any update. " + "An empty string cannot be used to blank a side.", parameters: zod_1.z.object({ modelName: zod_1.z .string() .min(1) .describe('Name of the model to update (e.g., "Basic", "Cloze")'), templates: zod_1.z .record(zod_1.z.string(), zod_1.z.object({ Front: zod_1.z .string() .min(1) .describe("HTML template for the front/question side"), Back: zod_1.z .string() .min(1) .describe("HTML template for the back/answer side"), })) .refine((t) => Object.keys(t).length > 0, { message: "At least one card template is required", }) .describe('Card templates keyed by card name (e.g., { "Card 1": { Front: "...", Back: "..." } }). ' + "modelTemplates returns the current structure in this shape."), }), outputSchema: zod_1.z.object({ success: zod_1.z.boolean(), modelName: zod_1.z.string(), templateCount: zod_1.z.number(), message: zod_1.z.string(), hint: zod_1.z.string(), }), annotations: { title: "Update Note Type Templates", 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) ], UpdateModelTemplatesTool.prototype, "updateModelTemplates", null); exports.UpdateModelTemplatesTool = UpdateModelTemplatesTool = UpdateModelTemplatesTool_1 = __decorate([ (0, mcp_nest_1.McpController)(), __metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient]) ], UpdateModelTemplatesTool); //# sourceMappingURL=update-model-templates.tool.js.map