anki-mcp-http
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
111 lines • 5.93 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 GuiAddCardsTool_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GuiAddCardsTool = void 0;
const common_1 = require("@nestjs/common");
const mcp_nest_1 = require("@rekog/mcp-nest");
const zod_1 = require("zod");
const anki_connect_client_1 = require("../../../clients/anki-connect.client");
const anki_utils_1 = require("../../../utils/anki.utils");
let GuiAddCardsTool = GuiAddCardsTool_1 = class GuiAddCardsTool {
ankiClient;
logger = new common_1.Logger(GuiAddCardsTool_1.name);
constructor(ankiClient) {
this.ankiClient = ankiClient;
}
async guiAddCards({ note, }, context) {
try {
this.logger.log(`Opening Add Cards dialog for deck "${note.deckName}"`);
await context.reportProgress({ progress: 25, total: 100 });
const emptyFields = Object.entries(note.fields).filter(([_, value]) => !value || value.trim() === '');
if (emptyFields.length > 0) {
return (0, anki_utils_1.createErrorResponse)(new Error(`Fields cannot be empty: ${emptyFields.map(([key]) => key).join(', ')}`), {
deckName: note.deckName,
modelName: note.modelName,
emptyFields: emptyFields.map(([key]) => key),
});
}
await context.reportProgress({ progress: 50, total: 100 });
const noteId = await this.ankiClient.invoke('guiAddCards', { note });
await context.reportProgress({ progress: 100, total: 100 });
this.logger.log(`Add Cards dialog opened, potential note ID: ${noteId}`);
return (0, anki_utils_1.createSuccessResponse)({
success: true,
noteId,
deckName: note.deckName,
modelName: note.modelName,
message: `Add Cards dialog opened with preset details for deck "${note.deckName}"`,
hint: 'The user can now review and finalize the note in the Anki GUI. The note will be created when they click Add.',
});
}
catch (error) {
this.logger.error('Failed to open Add Cards dialog', error);
if (error instanceof Error) {
const errorMessage = error.message.toLowerCase();
if (errorMessage.includes('field')) {
return (0, anki_utils_1.createErrorResponse)(error, {
modelName: note.modelName,
providedFields: Object.keys(note.fields),
hint: 'Field mismatch. Use modelFieldNames tool to see required fields.',
});
}
if (errorMessage.includes('model')) {
return (0, anki_utils_1.createErrorResponse)(error, {
modelName: note.modelName,
hint: 'Model not found. Use modelNames tool to see available models.',
});
}
if (errorMessage.includes('deck')) {
return (0, anki_utils_1.createErrorResponse)(error, {
deckName: note.deckName,
hint: 'Deck not found. Use list_decks tool to see available decks.',
});
}
}
return (0, anki_utils_1.createErrorResponse)(error, {
deckName: note.deckName,
modelName: note.modelName,
hint: 'Make sure Anki is running and the deck/model names are correct',
});
}
}
};
exports.GuiAddCardsTool = GuiAddCardsTool;
__decorate([
(0, mcp_nest_1.Tool)({
name: 'guiAddCards',
description: 'Open Anki Add Cards dialog with preset note details (deck, model, fields, tags). Returns potential note ID. ' +
'IMPORTANT: Only use when user explicitly requests opening the Add Cards dialog. ' +
'This tool is for note editing/creation workflows. Use this when user wants to manually review and finalize note creation in the GUI.',
parameters: zod_1.z.object({
note: zod_1.z.object({
deckName: zod_1.z.string().min(1).describe('Deck to add the note to'),
modelName: zod_1.z.string().min(1).describe('Note type/model (e.g., "Basic", "Cloze")'),
fields: zod_1.z
.record(zod_1.z.string())
.describe('Field values to pre-fill (e.g., {"Front": "question", "Back": "answer"})'),
tags: zod_1.z
.array(zod_1.z.string())
.optional()
.describe('Optional tags to add'),
}),
}),
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], GuiAddCardsTool.prototype, "guiAddCards", null);
exports.GuiAddCardsTool = GuiAddCardsTool = GuiAddCardsTool_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient])
], GuiAddCardsTool);
//# sourceMappingURL=gui-add-cards.tool.js.map