@ankimcp/anki-mcp-server
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
122 lines • 5.83 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 FindNotesTool_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FindNotesTool = 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 FindNotesTool = FindNotesTool_1 = class FindNotesTool {
ankiClient;
logger = new common_1.Logger(FindNotesTool_1.name);
constructor(ankiClient) {
this.ankiClient = ankiClient;
}
async findNotes({ query }) {
try {
this.logger.log(`Searching for notes with query: "${query}"`);
const noteIds = await this.ankiClient.invoke("findNotes", {
query: query,
});
if (!noteIds || noteIds.length === 0) {
this.logger.log("No notes found matching the query");
return {
success: true,
noteIds: [],
count: 0,
query: query,
message: "No notes found matching the search criteria",
hint: "The query may be too narrow, or a deck/tag name in it may be misspelled",
};
}
this.logger.log(`Found ${noteIds.length} notes matching the query`);
return {
success: true,
noteIds: noteIds,
count: noteIds.length,
query: query,
message: `Found ${noteIds.length} note${noteIds.length === 1 ? "" : "s"} matching the query`,
hint: noteIds.length > 100
? "Large result set. notesInfo returns note details and accepts the IDs in smaller batches."
: "notesInfo returns details for these note IDs.",
};
}
catch (error) {
this.logger.error("Failed to search for notes", error);
if (error instanceof Error) {
if (error.message.includes("query")) {
return (0, anki_utils_1.createErrorResponse)(error, {
query,
hint: "Invalid query syntax. Anki's manual documents the valid search syntax.",
examples: [
'"deck:DeckName" - all notes in a deck',
'"tag:important" - notes with specific tag',
'"is:due" - cards that are due for review',
'"added:7" - notes added in last 7 days',
'"front:word" - notes with "word" in front field',
],
});
}
}
return (0, anki_utils_1.createErrorResponse)(error, {
query,
hint: "This can happen when Anki is not running or the query syntax is invalid",
});
}
}
};
exports.FindNotesTool = FindNotesTool;
__decorate([
(0, mcp_nest_1.Tool)({
name: "findNotes",
description: "Search for notes using Anki query syntax. Returns an array of note IDs matching the query. " +
'Examples: "deck:Spanish", "tag:verb", "is:due", "front:hello", "added:1" (cards added today), ' +
'"prop:due<=2" (cards due within 2 days), "flag:1" (red flag), "is:suspended"',
parameters: zod_1.z.object({
query: zod_1.z
.string()
.min(1)
.describe('Anki search query. Use Anki query syntax like "deck:DeckName", "tag:tagname", ' +
'"is:due", "is:new", "is:review", "front:text", "back:text", or combine with spaces for AND, ' +
"OR for alternatives. Empty string returns all notes."),
}),
outputSchema: zod_1.z.object({
success: zod_1.z.boolean(),
noteIds: zod_1.z.array(zod_1.z.number()),
count: zod_1.z.number(),
query: zod_1.z.string(),
message: zod_1.z.string(),
hint: zod_1.z.string().optional(),
}),
annotations: {
title: "Find Notes",
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)
], FindNotesTool.prototype, "findNotes", null);
exports.FindNotesTool = FindNotesTool = FindNotesTool_1 = __decorate([
(0, mcp_nest_1.McpController)(),
__metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient])
], FindNotesTool);
//# sourceMappingURL=find-notes.tool.js.map