@ankimcp/anki-mcp-server
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
140 lines • 6.61 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 PresentCardTool_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PresentCardTool = 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 PresentCardTool = PresentCardTool_1 = class PresentCardTool {
ankiClient;
logger = new common_1.Logger(PresentCardTool_1.name);
constructor(ankiClient) {
this.ankiClient = ankiClient;
}
async presentCard({ card_id, show_answer }) {
try {
const showAnswer = show_answer || false;
this.logger.log(`Retrieving card ${card_id} for presentation (show_answer: ${showAnswer})`);
const cardsInfo = await this.ankiClient.invoke("cardsInfo", {
cards: [card_id],
});
if (typeof cardsInfo?.[0]?.cardId !== "number") {
this.logger.warn(`Card not found: ${card_id}`);
return (0, anki_utils_1.createErrorResponse)(new Error(`Card with ID ${card_id} not found`), { cardId: card_id });
}
const card = cardsInfo[0];
const { front, back } = (0, anki_utils_1.extractRenderedCardContent)(card);
const cardType = (0, anki_utils_1.getCardType)(card.type);
const tags = await this.fetchNoteTags(card.note);
const presentation = {
cardId: card.cardId,
front,
deckName: card.deckName,
modelName: card.modelName,
...(tags !== undefined && { tags }),
currentInterval: card.interval || 0,
easeFactor: card.factor || 2500,
reviews: card.reps || 0,
lapses: card.lapses || 0,
cardType,
noteId: card.note,
};
if (showAnswer) {
presentation.back = back;
}
this.logger.log(`Retrieved card ${card_id} for presentation`);
const instruction = !showAnswer
? "Question only; the answer is not included. Calling present_card again with show_answer=true returns it."
: "Answer included. No review has been recorded; rate_card records one with the user's rating (1-4).";
return {
success: true,
card: presentation,
instruction,
};
}
catch (error) {
this.logger.error(`Failed to retrieve card ${card_id}`, error);
return (0, anki_utils_1.createErrorResponse)(error, { cardId: card_id });
}
}
async fetchNoteTags(noteId) {
try {
const notes = await this.ankiClient.invoke("notesInfo", { notes: [noteId] });
const tags = notes?.[0]?.tags;
if (Array.isArray(tags)) {
return tags;
}
this.logger.warn(`Note ${noteId} not found; presenting card without tags`);
}
catch (error) {
this.logger.warn(`Failed to fetch tags for note ${noteId}: ${error instanceof Error ? error.message : String(error)}`);
}
return undefined;
}
};
exports.PresentCardTool = PresentCardTool;
__decorate([
(0, mcp_nest_1.Tool)({
name: "present_card",
description: "Returns a card's rendered front plus its deck, note type, tags and scheduling info. The back is included only when show_answer=true, so a card can be shown to the user before its answer is revealed. Reading a card does not record a review; rate_card does.",
parameters: zod_1.z.object({
card_id: zod_1.z.number().describe("The ID of the card to retrieve"),
show_answer: zod_1.z
.boolean()
.default(false)
.describe("Whether to include the answer/back content in the response"),
}),
outputSchema: zod_1.z.object({
success: zod_1.z.boolean(),
card: zod_1.z.object({
cardId: zod_1.z.number(),
front: zod_1.z.string(),
back: zod_1.z.string().optional(),
deckName: zod_1.z.string(),
modelName: zod_1.z.string(),
tags: zod_1.z
.array(zod_1.z.string())
.optional()
.describe("The note's tags; absent when the note lookup failed after the card was found"),
currentInterval: zod_1.z.number(),
easeFactor: zod_1.z.number(),
reviews: zod_1.z.number(),
lapses: zod_1.z.number(),
cardType: zod_1.z.string(),
noteId: zod_1.z.number(),
}),
instruction: zod_1.z.string(),
}),
annotations: {
title: "Present Card for Review",
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)
], PresentCardTool.prototype, "presentCard", null);
exports.PresentCardTool = PresentCardTool = PresentCardTool_1 = __decorate([
(0, mcp_nest_1.McpController)(),
__metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient])
], PresentCardTool);
//# sourceMappingURL=present-card.tool.js.map