diff --git a/mcp-server/src/PenpotMcpServer.ts b/mcp-server/src/PenpotMcpServer.ts index 464cb86..84af960 100644 --- a/mcp-server/src/PenpotMcpServer.ts +++ b/mcp-server/src/PenpotMcpServer.ts @@ -12,6 +12,7 @@ import { Tool } from "./Tool"; import { HighLevelOverviewTool } from "./tools/HighLevelOverviewTool"; import { PenpotApiInfoTool } from "./tools/PenpotApiInfoTool"; import { ExportShapeTool } from "./tools/ExportShapeTool"; +import { ReplServer } from "./ReplServer"; export class PenpotMcpServer { private readonly logger = createLogger("PenpotMcpServer"); @@ -21,6 +22,7 @@ export class PenpotMcpServer { private app: any; private readonly port: number; public readonly pluginBridge: PluginBridge; + private readonly replServer: ReplServer; private readonly transports = { streamable: {} as Record, @@ -44,6 +46,7 @@ export class PenpotMcpServer { this.tools = new Map>(); this.pluginBridge = new PluginBridge(webSocketPort); + this.replServer = new ReplServer(this.pluginBridge); this.registerTools(); } @@ -141,13 +144,28 @@ export class PenpotMcpServer { this.setupHttpEndpoints(); return new Promise((resolve) => { - this.app.listen(this.port, () => { + this.app.listen(this.port, async () => { this.logger.info(`Penpot MCP Server started on port ${this.port}`); this.logger.info(`Modern Streamable HTTP endpoint: http://localhost:${this.port}/mcp`); this.logger.info(`Legacy SSE endpoint: http://localhost:${this.port}/sse`); this.logger.info("WebSocket server is on ws://localhost:8080"); + + // start the REPL server + await this.replServer.start(); + resolve(); }); }); } + + /** + * Stops the MCP server and associated services. + * + * Gracefully shuts down the REPL server and other components. + */ + public async stop(): Promise { + this.logger.info("Stopping Penpot MCP Server..."); + await this.replServer.stop(); + this.logger.info("Penpot MCP Server stopped"); + } } diff --git a/mcp-server/src/ReplServer.ts b/mcp-server/src/ReplServer.ts new file mode 100644 index 0000000..26e23ed --- /dev/null +++ b/mcp-server/src/ReplServer.ts @@ -0,0 +1,331 @@ +import express from "express"; +import { PluginBridge } from "./PluginBridge"; +import { ExecuteCodePluginTask } from "./tasks/ExecuteCodePluginTask"; +import { createLogger } from "./logger"; + +/** + * Web-based REPL server for executing code through the PluginBridge. + * + * Provides a simple HTML interface on port 4403 that allows users to input + * JavaScript code and execute it via ExecuteCodePluginTask instances. + * The interface displays the result member of ExecuteCodeTaskResultData. + */ +export class ReplServer { + private readonly logger = createLogger("ReplServer"); + private readonly app: express.Application; + private readonly port: number; + private server: any; + + constructor( + private readonly pluginBridge: PluginBridge, + port: number = 4403 + ) { + this.port = port; + this.app = express(); + this.setupMiddleware(); + this.setupRoutes(); + } + + /** + * Sets up Express middleware for request parsing and static content. + */ + private setupMiddleware(): void { + this.app.use(express.json()); + this.app.use(express.static("public")); // for serving static files if needed + } + + /** + * Sets up HTTP routes for the REPL interface and API endpoints. + */ + private setupRoutes(): void { + // serve the main REPL interface + this.app.get("/", (req, res) => { + res.send(this.getReplHtml()); + }); + + // API endpoint for executing code + this.app.post("/execute", async (req, res) => { + try { + const { code } = req.body; + + if (!code || typeof code !== "string") { + return res.status(400).json({ + error: "Code parameter is required and must be a string", + }); + } + + const task = new ExecuteCodePluginTask({ code }); + const result = await this.pluginBridge.executePluginTask(task); + + // extract the result member from ExecuteCodeTaskResultData + const executeResult = result.data?.result; + + res.json({ + success: true, + result: executeResult, + log: result.data?.log || "", + }); + } catch (error) { + this.logger.error(error, "Failed to execute code in REPL"); + res.status(500).json({ + error: error instanceof Error ? error.message : "Unknown error occurred", + }); + } + }); + } + + /** + * Generates the HTML content for the REPL interface. + * + * Creates a simple web page with a resizable textarea for code input, + * an execute button, and a results display area using jQuery. + */ + private getReplHtml(): string { + return ` + + + + + Penpot MCP REPL + + + + +

Penpot MCP REPL

+ +
+ + +
+ +
+ + Shortcut: Ctrl+Enter +
+ +
+ +
Click "Execute Code" to run your JavaScript...
+
+ + + +`; + } + + /** + * Starts the REPL web server. + * + * Begins listening on the configured port and logs server startup information. + */ + public async start(): Promise { + return new Promise((resolve) => { + this.server = this.app.listen(this.port, () => { + this.logger.info(`REPL server started on port ${this.port}`); + this.logger.info(`REPL interface available at: http://localhost:${this.port}`); + resolve(); + }); + }); + } + + /** + * Stops the REPL web server. + */ + public async stop(): Promise { + if (this.server) { + return new Promise((resolve) => { + this.server.close(() => { + this.logger.info("REPL server stopped"); + resolve(); + }); + }); + } + } +} diff --git a/mcp-server/src/index.ts b/mcp-server/src/index.ts index 8438eb1..a09ab17 100644 --- a/mcp-server/src/index.ts +++ b/mcp-server/src/index.ts @@ -45,13 +45,15 @@ async function main(): Promise { await server.start(); // Keep the process alive - process.on("SIGINT", () => { + process.on("SIGINT", async () => { logger.info("Received SIGINT, shutting down gracefully..."); + await server.stop(); process.exit(0); }); - process.on("SIGTERM", () => { + process.on("SIGTERM", async () => { logger.info("Received SIGTERM, shutting down gracefully..."); + await server.stop(); process.exit(0); }); } catch (error) {