@ankimcp/anki-mcp-server
Version:
Model Context Protocol server for Anki - enables AI assistants to interact with your Anki flashcards
71 lines • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryTransport = void 0;
const common_1 = require("@nestjs/common");
const tunnel_protocol_1 = require("./tunnel.protocol");
const INTERNAL_ID_PREFIX = "tunnel-req-";
class InMemoryTransport {
logger = new common_1.Logger(InMemoryTransport.name);
pendingRequests = new Map();
nextInternalId = 1;
closed = false;
onclose;
onerror;
onmessage;
async start() {
}
async close() {
this.closed = true;
for (const [_id, pending] of this.pendingRequests) {
clearTimeout(pending.timeout);
pending.reject(new Error("Transport closed"));
}
this.pendingRequests.clear();
this.onclose?.();
}
async send(message) {
if ("id" in message && message.id !== undefined) {
const pending = this.pendingRequests.get(message.id);
if (pending) {
clearTimeout(pending.timeout);
this.pendingRequests.delete(message.id);
pending.resolve({ ...message, id: pending.originalId });
}
else {
this.logger.warn(`Received response for unknown request ID: ${message.id}`);
}
}
}
async handleRequest(request) {
if (this.closed) {
throw new Error("Transport is closed");
}
if (!this.onmessage) {
throw new Error("Transport not connected - call McpServer.connect() first");
}
if (!("id" in request) || request.id === undefined) {
this.onmessage(request);
return null;
}
const originalId = request.id;
const internalId = `${INTERNAL_ID_PREFIX}${this.nextInternalId++}`;
return new Promise((resolve, reject) => {
const pending = {
originalId,
resolve,
reject,
timeout: null,
};
this.pendingRequests.set(internalId, pending);
pending.timeout = setTimeout(() => {
if (this.pendingRequests.get(internalId) === pending) {
this.pendingRequests.delete(internalId);
}
reject(new Error("MCP request timeout"));
}, tunnel_protocol_1.TUNNEL_DEFAULTS.REQUEST_TIMEOUT);
this.onmessage({ ...request, id: internalId });
});
}
}
exports.InMemoryTransport = InMemoryTransport;
//# sourceMappingURL=in-memory.transport.js.map