@ankimcp/anki-mcp-server
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
135 lines • 6.19 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 ModelFieldNamesTool_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelFieldNamesTool = 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 ModelFieldNamesTool = ModelFieldNamesTool_1 = class ModelFieldNamesTool {
ankiClient;
logger = new common_1.Logger(ModelFieldNamesTool_1.name);
constructor(ankiClient) {
this.ankiClient = ankiClient;
}
async modelFieldNames({ modelName }) {
try {
this.logger.log(`Retrieving field names for model: ${modelName}`);
const fieldNames = await this.ankiClient.invoke("modelFieldNames", {
modelName: modelName,
});
if (!fieldNames) {
this.logger.warn(`Model not found: ${modelName}`);
return (0, anki_utils_1.createErrorResponse)(new Error(`Model "${modelName}" not found`), {
modelName: modelName,
hint: "modelNames lists the available models",
});
}
if (fieldNames.length === 0) {
this.logger.warn(`No fields found for model: ${modelName}`);
return {
success: true,
modelName: modelName,
fieldNames: [],
total: 0,
message: `Model "${modelName}" has no fields`,
};
}
this.logger.log(`Found ${fieldNames.length} fields for model ${modelName}`);
let exampleFields;
const lowerModelName = modelName.toLowerCase();
if (lowerModelName.includes("basic") &&
!lowerModelName.includes("reversed")) {
exampleFields = {
Front: "Question or prompt text",
Back: "Answer or response text",
};
}
else if (lowerModelName.includes("basic") &&
lowerModelName.includes("reversed")) {
exampleFields = {
Front: "First side of the card",
Back: "Second side of the card",
};
}
else if (lowerModelName.includes("cloze")) {
exampleFields = {
Text: "The {{c1::hidden}} text will be replaced with [...] on the card",
"Back Extra": "Additional information or hints",
};
}
const response = {
success: true,
modelName: modelName,
fieldNames: fieldNames,
total: fieldNames.length,
message: `Model "${modelName}" has ${fieldNames.length} field${fieldNames.length !== 1 ? "s" : ""}`,
};
if (exampleFields) {
response.example = exampleFields;
response.hint =
"These field names are the keys of addNote's fields object for this model";
}
return response;
}
catch (error) {
this.logger.error(`Failed to retrieve field names for model ${modelName}`, error);
return (0, anki_utils_1.createErrorResponse)(error, {
modelName: modelName,
hint: "This can happen when Anki is not running or the model name is wrong",
});
}
}
};
exports.ModelFieldNamesTool = ModelFieldNamesTool;
__decorate([
(0, mcp_nest_1.Tool)({
name: "modelFieldNames",
description: "Get the field names for a specific note type (model). Use this to know what fields are required when creating notes of this type.",
parameters: zod_1.z.object({
modelName: zod_1.z
.string()
.min(1)
.describe("The name of the model/note type to get fields for"),
}),
outputSchema: zod_1.z.object({
success: zod_1.z.boolean(),
modelName: zod_1.z.string(),
fieldNames: zod_1.z.array(zod_1.z.string()),
total: zod_1.z.number(),
message: zod_1.z.string(),
example: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
hint: zod_1.z.string().optional(),
}),
annotations: {
title: "Get Note Type Fields",
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true,
openWorldHint: false,
},
}),
__param(0, (0, microservices_1.Payload)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], ModelFieldNamesTool.prototype, "modelFieldNames", null);
exports.ModelFieldNamesTool = ModelFieldNamesTool = ModelFieldNamesTool_1 = __decorate([
(0, mcp_nest_1.McpController)(),
__metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient])
], ModelFieldNamesTool);
//# sourceMappingURL=model-field-names.tool.js.map