@mseep/falkordb-mcpserver
Version:
Model Context Protocol server for FalkorDB
90 lines (89 loc) • 3.51 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.falkorDBService = void 0;
const falkordb_1 = require("falkordb");
const config_1 = require("../config");
class FalkorDBService {
constructor() {
this.client = null;
this.init();
}
init() {
return __awaiter(this, void 0, void 0, function* () {
try {
this.client = yield falkordb_1.FalkorDB.connect({
socket: {
host: config_1.config.falkorDB.host,
port: config_1.config.falkorDB.port,
},
password: config_1.config.falkorDB.password,
username: config_1.config.falkorDB.username,
});
// Test connection
const connection = yield this.client.connection;
yield connection.ping();
console.log('Successfully connected to FalkorDB');
}
catch (error) {
console.error('Failed to connect to FalkorDB:', error);
// Retry connection after a delay
setTimeout(() => this.init(), 5000);
}
});
}
executeQuery(graphName, query, params) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.client) {
throw new Error('FalkorDB client not initialized');
}
try {
const graph = this.client.selectGraph(graphName);
const result = yield graph.query(query, params);
return result;
}
catch (error) {
const sanitizedGraphName = graphName.replace(/\n|\r/g, "");
console.error('Error executing FalkorDB query on graph %s:', sanitizedGraphName, error);
throw error;
}
});
}
/**
* Lists all available graphs in FalkorDB
* @returns Array of graph names
*/
listGraphs() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.client) {
throw new Error('FalkorDB client not initialized');
}
try {
// Using the simplified list method which always returns an array
return yield this.client.list();
}
catch (error) {
console.error('Error listing FalkorDB graphs:', error);
throw error;
}
});
}
close() {
return __awaiter(this, void 0, void 0, function* () {
if (this.client) {
yield this.client.close();
this.client = null;
}
});
}
}
// Export a singleton instance
exports.falkorDBService = new FalkorDBService();