UNPKG

@ankimcp/anki-mcp-server

Version:

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

75 lines • 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TunnelTransport = void 0; const server_1 = require("@modelcontextprotocol/server"); const in_memory_transport_1 = require("./in-memory.transport"); class TunnelTransport { kind = "streamable-http"; server; channel; start(ctx) { this.channel = new in_memory_transport_1.InMemoryTransport(); this.server = ctx.createBoundServer({ transport: this.kind, stateless: true, era: "legacy", }); ctx.logger.log("MCP tunnel transport started"); return this.server.connect(this.channel); } async close() { await this.channel?.close(); await this.server?.close(); this.channel = undefined; this.server = undefined; } async handleRequest(body) { if (!this.channel) { throw new Error("Tunnel transport not started"); } let rawMessage; try { rawMessage = JSON.parse(body); } catch (error) { return JSON.stringify({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error", data: error instanceof Error ? error.message : String(error), }, }); } if (Array.isArray(rawMessage)) { return JSON.stringify({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Batched JSON-RPC requests are not supported", }, }); } const inbound = (0, server_1.classifyInboundRequest)({ httpMethod: "POST", body: rawMessage, }); if (inbound.kind === "modern" && inbound.messageKind === "request") { const error = new server_1.UnsupportedProtocolVersionError({ supported: [...server_1.SUPPORTED_PROTOCOL_VERSIONS], requested: inbound.classification.revision ?? "unknown", }); return JSON.stringify({ jsonrpc: "2.0", id: inbound.message.id, error: { code: error.code, message: error.message, data: error.data }, }); } const response = await this.channel.handleRequest(rawMessage); return response ? JSON.stringify(response) : ""; } } exports.TunnelTransport = TunnelTransport; //# sourceMappingURL=tunnel.transport.js.map