@ankimcp/anki-mcp-server
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
159 lines • 8.17 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 RenameModelFieldTool_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenameModelFieldTool = 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 RenameModelFieldTool = RenameModelFieldTool_1 = class RenameModelFieldTool {
ankiClient;
logger = new common_1.Logger(RenameModelFieldTool_1.name);
constructor(ankiClient) {
this.ankiClient = ankiClient;
}
async renameModelField({ modelName, oldFieldName, newFieldName, }) {
try {
this.logger.log(`Renaming field "${oldFieldName}" → "${newFieldName}" in model "${modelName}"`);
if (oldFieldName === newFieldName) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Old and new field names are identical ("${oldFieldName}") — nothing to rename`), {
modelName,
oldFieldName,
newFieldName,
hint: "The new field name is the same as the current one, so there is nothing to rename.",
});
}
const fields = await this.ankiClient.invoke("modelFieldNames", {
modelName,
});
if (!fields || fields.length === 0) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Model "${modelName}" has no fields or does not exist`), {
modelName,
oldFieldName,
newFieldName,
hint: "Model not found. modelNames lists the available models.",
});
}
if (!fields.includes(oldFieldName)) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Field "${oldFieldName}" does not exist in model "${modelName}"`), {
modelName,
oldFieldName,
newFieldName,
hint: "Field names are case-sensitive. modelFieldNames lists the current field names.",
});
}
if (fields.includes(newFieldName)) {
return (0, anki_utils_1.createErrorResponse)(new Error(`A field named "${newFieldName}" already exists in model "${modelName}". ` +
'AnkiConnect would silently mangle the name with a "+" suffix instead of erroring.'), {
modelName,
oldFieldName,
newFieldName,
hint: `A field named "${newFieldName}" already exists in this model.`,
});
}
const caseVariant = fields.find((f) => f !== oldFieldName && f.toLowerCase() === newFieldName.toLowerCase());
if (caseVariant) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Field "${newFieldName}" collides with existing field "${caseVariant}" ` +
`in model "${modelName}" (names differ only in case)`), {
modelName,
oldFieldName,
newFieldName,
hint: `Field names are case-sensitive, but "${newFieldName}" differs from existing field "${caseVariant}" only in case, and names that differ only in case are rejected.`,
});
}
await this.ankiClient.invoke("modelFieldRename", {
modelName,
oldFieldName,
newFieldName,
});
this.logger.log(`Successfully renamed field "${oldFieldName}" to "${newFieldName}" in model "${modelName}"`);
return {
success: true,
modelName,
oldFieldName,
newFieldName,
message: `Successfully renamed field "${oldFieldName}" to "${newFieldName}" in model "${modelName}"`,
};
}
catch (error) {
this.logger.error(`Failed to rename field "${oldFieldName}" in model "${modelName}"`, error);
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage.includes("not found") ||
errorMessage.includes("does not exist")) {
return (0, anki_utils_1.createErrorResponse)(error, {
modelName,
oldFieldName,
newFieldName,
hint: "Model or field not found. modelNames and modelFieldNames list the valid names.",
});
}
return (0, anki_utils_1.createErrorResponse)(error, {
modelName,
oldFieldName,
newFieldName,
hint: "This can happen when Anki is not running or the model or field name is wrong",
});
}
}
};
exports.RenameModelFieldTool = RenameModelFieldTool;
__decorate([
(0, mcp_nest_1.Tool)({
name: "renameModelField",
description: "Rename a field in an existing Anki note type (model). " +
"Anki rewrites references to the old name in the note type's card templates (e.g., {{OldName}} becomes {{NewName}}); " +
"Anki then validates all card templates when the note type is saved, and if any is invalid (for example it cannot be parsed, references a field the note type does not have, or has no field on the front), the rename fails. " +
"Field names are case-sensitive; modelFieldNames lists the current ones.",
parameters: zod_1.z.object({
modelName: zod_1.z
.string()
.min(1)
.describe('Name of the note type to modify (e.g., "Basic", "Latin Vocabulary")'),
oldFieldName: zod_1.z
.string()
.min(1)
.describe('Current name of the field to rename (e.g., "Notes")'),
newFieldName: zod_1.z
.string()
.min(1)
.describe('New name for the field (e.g., "Grammar Notes")'),
}),
outputSchema: zod_1.z.object({
success: zod_1.z.boolean(),
modelName: zod_1.z.string(),
oldFieldName: zod_1.z.string(),
newFieldName: zod_1.z.string(),
message: zod_1.z.string(),
}),
annotations: {
title: "Rename Field in Note Type",
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
openWorldHint: false,
},
}),
__param(0, (0, microservices_1.Payload)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], RenameModelFieldTool.prototype, "renameModelField", null);
exports.RenameModelFieldTool = RenameModelFieldTool = RenameModelFieldTool_1 = __decorate([
(0, mcp_nest_1.McpController)(),
__metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient])
], RenameModelFieldTool);
//# sourceMappingURL=rename-model-field.tool.js.map