@ankimcp/anki-mcp-server
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
143 lines • 6.92 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 RepositionModelFieldTool_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RepositionModelFieldTool = 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 RepositionModelFieldTool = RepositionModelFieldTool_1 = class RepositionModelFieldTool {
ankiClient;
logger = new common_1.Logger(RepositionModelFieldTool_1.name);
constructor(ankiClient) {
this.ankiClient = ankiClient;
}
async repositionModelField({ modelName, fieldName, index, }) {
try {
this.logger.log(`Repositioning field "${fieldName}" to index ${index} in model "${modelName}"`);
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,
fieldName,
index,
hint: "Model not found. modelNames lists the available models.",
});
}
if (!fields.includes(fieldName)) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Field "${fieldName}" does not exist in model "${modelName}"`), {
modelName,
fieldName,
index,
hint: "Field names are case-sensitive. modelFieldNames lists the current field names.",
});
}
if (index < 0 || index > fields.length - 1) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Index ${index} is out of range for model "${modelName}". ` +
`Valid range is 0-${fields.length - 1} (the model has ${fields.length} fields). ` +
"AnkiConnect would silently clamp the index instead of erroring."), {
modelName,
fieldName,
index,
hint: "Index out of range. modelFieldNames lists the fields, which shows how many exist.",
});
}
await this.ankiClient.invoke("modelFieldReposition", {
modelName,
fieldName,
index,
});
this.logger.log(`Successfully repositioned field "${fieldName}" to index ${index} in model "${modelName}"`);
return {
success: true,
modelName,
fieldName,
newIndex: index,
message: `Successfully moved field "${fieldName}" to position ${index} in model "${modelName}"`,
};
}
catch (error) {
this.logger.error(`Failed to reposition field "${fieldName}" 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,
fieldName,
index,
hint: "Model or field not found. modelNames and modelFieldNames list the valid names.",
});
}
return (0, anki_utils_1.createErrorResponse)(error, {
modelName,
fieldName,
index,
hint: "This can happen when Anki is not running or the model or field name is wrong",
});
}
}
};
exports.RepositionModelFieldTool = RepositionModelFieldTool;
__decorate([
(0, mcp_nest_1.Tool)({
name: "repositionModelField",
description: "Change the position of a field within an Anki note type (model). " +
"Fields are ordered 0-based: index 0 is the first field. " +
"(Which field is the sort field is a separate model setting and is not changed by repositioning.) " +
"modelFieldNames lists the current field order.",
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")'),
fieldName: zod_1.z
.string()
.min(1)
.describe('Name of the field to reposition (e.g., "Grammar")'),
index: zod_1.z
.number()
.int()
.min(0)
.describe("New 0-based position for the field (0 = first field)."),
}),
outputSchema: zod_1.z.object({
success: zod_1.z.boolean(),
modelName: zod_1.z.string(),
fieldName: zod_1.z.string(),
newIndex: zod_1.z.number(),
message: zod_1.z.string(),
}),
annotations: {
title: "Reposition Field in Note Type",
readOnlyHint: false,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
}),
__param(0, (0, microservices_1.Payload)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], RepositionModelFieldTool.prototype, "repositionModelField", null);
exports.RepositionModelFieldTool = RepositionModelFieldTool = RepositionModelFieldTool_1 = __decorate([
(0, mcp_nest_1.McpController)(),
__metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient])
], RepositionModelFieldTool);
//# sourceMappingURL=reposition-model-field.tool.js.map