@ankimcp/anki-mcp-server
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
143 lines • 6.7 kB
JavaScript
;
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 UpdateModelStylingTool_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateModelStylingTool = 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 UpdateModelStylingTool = UpdateModelStylingTool_1 = class UpdateModelStylingTool {
ankiClient;
logger = new common_1.Logger(UpdateModelStylingTool_1.name);
constructor(ankiClient) {
this.ankiClient = ankiClient;
}
async updateModelStyling({ modelName, css }) {
try {
this.logger.log(`Updating styling for model: ${modelName}`);
let oldStyling = null;
try {
oldStyling = await this.ankiClient.invoke("modelStyling", {
modelName,
});
}
catch (_error) {
this.logger.warn(`Could not fetch old styling for ${modelName}`);
}
await this.ankiClient.invoke("updateModelStyling", {
model: {
name: modelName,
css,
},
});
this.logger.log(`Successfully updated styling for model: ${modelName}`);
const cssLength = css.length;
const hasRtl = css.includes("direction: rtl") || css.includes("direction:rtl");
const hasCardClass = css.includes(".card");
const hasFrontClass = css.includes(".front");
const hasBackClass = css.includes(".back");
const hasClozeClass = css.includes(".cloze");
const response = {
success: true,
modelName,
cssLength,
cssInfo: {
hasRtlSupport: hasRtl,
hasCardStyling: hasCardClass,
hasFrontStyling: hasFrontClass,
hasBackStyling: hasBackClass,
hasClozeStyling: hasClozeClass,
},
message: `Successfully updated CSS styling for model "${modelName}"`,
};
if (oldStyling) {
response.oldCssLength = oldStyling.css.length;
response.cssLengthChange = cssLength - oldStyling.css.length;
}
return response;
}
catch (error) {
this.logger.error(`Failed to update styling 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.UpdateModelStylingTool = UpdateModelStylingTool;
__decorate([
(0, mcp_nest_1.Tool)({
name: "updateModelStyling",
description: "Update the CSS styling for an existing note type (model). " +
"This changes how cards of this type are rendered in Anki. " +
"Useful for adding RTL (Right-to-Left) support, changing fonts, colors, or layout. " +
"The given CSS replaces the note type's entire existing CSS (modelStyling returns the current CSS). " +
"Changes apply to all cards using this model.",
parameters: zod_1.z.object({
modelName: zod_1.z
.string()
.min(1)
.describe('Name of the model to update (e.g., "Basic", "Basic RTL")'),
css: zod_1.z
.string()
.min(1)
.describe('New CSS styling content. For RTL languages, include "direction: rtl;" in .card class. ' +
"This will completely replace the existing CSS."),
}),
outputSchema: zod_1.z.object({
success: zod_1.z.boolean(),
modelName: zod_1.z.string(),
cssLength: zod_1.z.number(),
cssInfo: zod_1.z.object({
hasRtlSupport: zod_1.z.boolean(),
hasCardStyling: zod_1.z.boolean(),
hasFrontStyling: zod_1.z.boolean(),
hasBackStyling: zod_1.z.boolean(),
hasClozeStyling: zod_1.z.boolean(),
}),
message: zod_1.z.string(),
oldCssLength: zod_1.z.number().optional(),
cssLengthChange: zod_1.z.number().optional(),
}),
annotations: {
title: "Update Note Type CSS",
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)
], UpdateModelStylingTool.prototype, "updateModelStyling", null);
exports.UpdateModelStylingTool = UpdateModelStylingTool = UpdateModelStylingTool_1 = __decorate([
(0, mcp_nest_1.McpController)(),
__metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient])
], UpdateModelStylingTool);
//# sourceMappingURL=update-model-styling.tool.js.map