UNPKG

@translated/lara-mcp

Version:

Lara API official MCP server

86 lines (85 loc) 2.74 kB
import express from "express"; import helmet from "helmet"; import cors from "./middleware/cors.js"; import { env } from "#env"; import { ServerException } from "#exception"; import { logger } from "#logger"; import loggingMiddleware from "./middleware/logging.js"; import { createServer } from "node:http"; export class RestServer { port; host; express; httpServer; constructor() { // -- Member variables this.port = env.PORT; this.host = env.HOST; this.express = express(); // -- Json parser this.express.use(express.json()); // -- Security this.express.use(cors); this.express.use(helmet()); // -- Logging this.express.use(loggingMiddleware); } start() { logger.info(`Starting HTTP server on ${this.host}:${this.port}...`); // Bind specifically on 127.0.0.1 to avoid binding to all interfaces this.httpServer = createServer(this.express).listen(this.port, this.host, () => { logger.info(`HTTP server successfully started on ${this.host}:${this.port}`); }); } stop() { if (!this.httpServer) { logger.error("Cannot stop HTTP server, it is not started"); return; } logger.info(`Stopping HTTP server on ${this.host}:${this.port}...`); this.httpServer.close(() => { logger.info(`HTTP server successfully stopped on ${this.host}:${this.port}`); }); } configure() { return this.express; } send(res, payload) { logger.debug("Sending response to client: " + JSON.stringify(payload)); if (payload instanceof ServerException) { res.status(400).json({ error: { code: payload.code, message: payload.message, }, }); return; } res.status(200).json(payload); } sendJsonRpc(res, payload) { logger.debug("Sending JSON-RPC response to client: " + JSON.stringify(payload)); if (payload instanceof ServerException) { res.status(400).json(this.createJsonRpcResponse(payload)); return; } res.status(200).json(this.createJsonRpcResponse(payload)); } createJsonRpcResponse(payload) { if (payload instanceof ServerException) { return { jsonrpc: "2.0", error: { code: payload.code, message: payload.message, }, id: "", }; } return { jsonrpc: "2.0", result: payload, id: "", }; } }