UNPKG

@ankimcp/anki-mcp-server

Version:

Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards

115 lines • 5.42 kB
"use strict"; 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 GuiBrowseTool_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.GuiBrowseTool = 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 GuiBrowseTool = GuiBrowseTool_1 = class GuiBrowseTool { ankiClient; logger = new common_1.Logger(GuiBrowseTool_1.name); constructor(ankiClient) { this.ankiClient = ankiClient; } async guiBrowse({ query, reorderCards, }) { try { this.logger.log(`Opening Card Browser with query: "${query}"`); const params = { query }; if (reorderCards) { params.reorderCards = reorderCards; } const cardIds = await this.ankiClient.invoke("guiBrowse", params); this.logger.log(`Card Browser opened with ${cardIds.length} card(s) found`); return { success: true, cardIds, cardCount: cardIds.length, query, message: `Card Browser opened with ${cardIds.length} card(s) matching query "${query}"`, hint: cardIds.length === 0 ? "No cards found; a different search query may match cards." : "The Card Browser shows the matching cards.", }; } catch (error) { this.logger.error("Failed to open Card Browser", error); if (error instanceof Error) { if (error.message.includes("query") || error.message.includes("syntax")) { return (0, anki_utils_1.createErrorResponse)(error, { query, hint: 'Invalid search query; it does not follow Anki search syntax. Examples: "deck:MyDeck", "tag:important", "is:due"', }); } } return (0, anki_utils_1.createErrorResponse)(error, { query, hint: "This can happen when Anki is not running or its GUI is not visible", }); } } }; exports.GuiBrowseTool = GuiBrowseTool; __decorate([ (0, mcp_nest_1.Tool)({ name: "guiBrowse", description: "Opens the Card Browser window in the Anki desktop app on the user's screen and runs a search in it (Anki query syntax). Returns the matching card IDs. " + "For when the user asks to open the browser, e.g. to find and edit notes by hand; findNotes searches without opening a window. Not part of a review session.", parameters: zod_1.z.object({ query: zod_1.z .string() .min(1) .describe('Anki search query using standard syntax (e.g., "deck:Spanish tag:verb", "is:due", "added:7")'), reorderCards: zod_1.z .object({ order: zod_1.z .enum(["ascending", "descending"]) .describe("Sort order for the cards in browser"), columnId: zod_1.z .string() .describe('Column to sort by (e.g., "noteFld", "noteCrt", "cardDue")'), }) .optional() .describe("Optional reordering of cards in the browser"), }), outputSchema: zod_1.z.object({ success: zod_1.z.boolean(), cardIds: zod_1.z.array(zod_1.z.number()), cardCount: zod_1.z.number(), query: zod_1.z.string(), message: zod_1.z.string(), hint: zod_1.z.string(), }), annotations: { title: "Open Card Browser", readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, }), __param(0, (0, microservices_1.Payload)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], GuiBrowseTool.prototype, "guiBrowse", null); exports.GuiBrowseTool = GuiBrowseTool = GuiBrowseTool_1 = __decorate([ (0, mcp_nest_1.McpController)(), __metadata("design:paramtypes", [anki_connect_client_1.AnkiConnectClient]) ], GuiBrowseTool); //# sourceMappingURL=gui-browse.tool.js.map