@ankimcp/anki-mcp-server
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
156 lines • 7.72 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 AddModelFieldTool_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddModelFieldTool = 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 AddModelFieldTool = AddModelFieldTool_1 = class AddModelFieldTool {
ankiClient;
logger = new common_1.Logger(AddModelFieldTool_1.name);
constructor(ankiClient) {
this.ankiClient = ankiClient;
}
async addModelField({ modelName, fieldName, index, }) {
try {
this.logger.log(`Adding field "${fieldName}" to model "${modelName}"${index !== undefined ? ` at index ${index}` : ""}`);
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,
hint: "Model not found. modelNames lists the available models.",
});
}
if (fields.includes(fieldName)) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Field "${fieldName}" already exists in model "${modelName}". ` +
"AnkiConnect would silently skip the add instead of erroring " +
"(and reposition the existing field if an index is given)."), {
modelName,
fieldName,
hint: `Field "${fieldName}" already exists. modelFieldNames lists the existing fields.`,
});
}
const caseVariant = fields.find((f) => f.toLowerCase() === fieldName.toLowerCase());
if (caseVariant) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Field "${fieldName}" collides with existing field "${caseVariant}" ` +
`in model "${modelName}" (names differ only in case)`), {
modelName,
fieldName,
hint: `Field names are case-sensitive, but "${fieldName}" differs from existing field "${caseVariant}" only in case, and names that differ only in case are rejected.`,
});
}
if (index !== undefined && (index < 0 || index > fields.length)) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Index ${index} is out of range for model "${modelName}". ` +
`Valid range is 0-${fields.length} (${fields.length} appends at the end). ` +
"AnkiConnect would silently clamp the index instead of erroring."), {
modelName,
fieldName,
index,
hint: "modelFieldNames lists the fields, which shows how many exist.",
});
}
const params = {
modelName,
fieldName,
};
if (index !== undefined) {
params.index = index;
}
await this.ankiClient.invoke("modelFieldAdd", params);
this.logger.log(`Successfully added field "${fieldName}" to model "${modelName}"`);
return {
success: true,
modelName,
fieldName,
index: index ?? null,
message: index !== undefined
? `Successfully added field "${fieldName}" to model "${modelName}" at position ${index}`
: `Successfully added field "${fieldName}" to model "${modelName}"`,
};
}
catch (error) {
this.logger.error(`Failed to add field "${fieldName}" to 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,
hint: "Model not found. modelNames lists the available models.",
});
}
return (0, anki_utils_1.createErrorResponse)(error, {
modelName,
fieldName,
hint: "This can happen when Anki is not running or the model name is wrong",
});
}
}
};
exports.AddModelFieldTool = AddModelFieldTool;
__decorate([
(0, mcp_nest_1.Tool)({
name: "addModelField",
description: "Add a new field to an existing Anki note type (model). " +
"The field is appended to the end by default, or inserted at a specific position. " +
"Existing notes of this type will have the new field set to empty. " +
"modelFieldNames lists the current fields.",
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 new field to add (e.g., "Grammar", "IPA", "Example")'),
index: zod_1.z
.number()
.int()
.min(0)
.optional()
.describe("Position to insert the field (0-based). Omit to append at the end."),
}),
outputSchema: zod_1.z.object({
success: zod_1.z.boolean(),
modelName: zod_1.z.string(),
fieldName: zod_1.z.string(),
index: zod_1.z.number().nullable(),
message: zod_1.z.string(),
}),
annotations: {
title: "Add Field to 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)
], AddModelFieldTool.prototype, "addModelField", null);
exports.AddModelFieldTool = AddModelFieldTool = AddModelFieldTool_1 = __decorate([
(0, mcp_nest_1.McpController)(),
__metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient])
], AddModelFieldTool);
//# sourceMappingURL=add-model-field.tool.js.map