UNPKG

@salesforce/pwa-kit-mcp

Version:

MCP server that helps you build Salesforce Commerce Cloud PWA Kit Composable Storefront

157 lines (154 loc) 7.9 kB
#!/usr/bin/env node /* * Copyright (c) 2025, Salesforce, Inc. * All rights reserved. * SPDX-License-Identifier: BSD-3-Clause * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ "use strict"; var _mcp = require("@modelcontextprotocol/sdk/server/mcp.js"); var _stdio = require("@modelcontextprotocol/sdk/server/stdio.js"); var _zod = require("zod"); var _tools = require("../tools"); var _telemetry = require("../utils/telemetry"); var _constants = require("../utils/constants"); function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } // NOTE: This is a workaround to import JSON files as ES modules. // eslint-disable-next-line @typescript-eslint/no-var-requires const packageJson = require('../package.json'); const FALLBACK_VERSION = '0.1.0'; class PwaStorefrontMCPServerHighLevel { constructor() { var _this = this; // Using McpServer instead of Server this.server = new _mcp.McpServer({ name: 'pwa-kit-mcp', version: (packageJson === null || packageJson === void 0 ? void 0 : packageJson.version) || FALLBACK_VERSION }, { capabilities: { tools: {} } }); // Wrap server.tool so all handlers are decorated with telemetry const _origTool = this.server.tool.bind(this.server); this.server.tool = (name, description, inputSchema, handler) => { const wrappedHandler = /*#__PURE__*/function () { var _ref = _asyncToGenerator(function* (...handlerArgs) { const start = Date.now(); try { var _this$telemetry; const result = yield handler(...handlerArgs); (_this$telemetry = _this.telemetry) === null || _this$telemetry === void 0 ? void 0 : _this$telemetry.sendEvent('TOOL_CALLED_' + name, { toolName: name, runTimeMs: Date.now() - start, isError: false }); return result; } catch (error) { var _this$telemetry2; (_this$telemetry2 = _this.telemetry) === null || _this$telemetry2 === void 0 ? void 0 : _this$telemetry2.sendEvent('TOOL_CALLED_' + name, { toolName: name, runTimeMs: Date.now() - start, isError: true }); throw error; } }); return function wrappedHandler() { return _ref.apply(this, arguments); }; }(); return _origTool(name, description, inputSchema, wrappedHandler); }; this.createNewComponentTool = new _tools.CreateNewComponentTool(); this.createAppGuidelinesTool = new _tools.CreateAppGuidelinesTool(); this.testWithPlaywrightTool = new _tools.TestWithPlaywrightTool(); this.exploreCommerceAPITool = new _tools.ExploreCommerceAPITool(); this.hooksRecommendationTool = new _tools.HooksRecommendationTool(); this.setupTools(); } setupTools() { // Register CreateProjectTool this.server.tool(this.createAppGuidelinesTool.name, this.createAppGuidelinesTool.description, this.createAppGuidelinesTool.inputSchema, this.createAppGuidelinesTool.fn); this.server.tool(_tools.DeveloperGuidelinesTool.name, _tools.DeveloperGuidelinesTool.description, _tools.DeveloperGuidelinesTool.inputSchema, _tools.DeveloperGuidelinesTool.fn); this.server.tool('pwakit_run_site_test', `Run the ${_constants.PWA_KIT_DESCRIPTIVE_NAME} site or app performance or accessibility test for a given site URL`, { testType: _zod.z.enum(['performance', 'accessibility']).describe('Type of test to run'), siteUrl: _zod.z.string().describe('Site URL to test') }, ({ testType, siteUrl }) => this.testWithPlaywrightTool.run(testType, siteUrl)); this.server.tool(_tools.InstallAgentRulesTool.name, _tools.InstallAgentRulesTool.description, _tools.InstallAgentRulesTool.inputSchema, _tools.InstallAgentRulesTool.fn); this.server.tool(this.createNewComponentTool.name, this.createNewComponentTool.description, this.createNewComponentTool.inputSchema, this.createNewComponentTool.handler); this.server.tool(_tools.CreateNewPageTool.name, _tools.CreateNewPageTool.description, _tools.CreateNewPageTool.inputSchema, _tools.CreateNewPageTool.handler); this.server.tool(this.exploreCommerceAPITool.name, this.exploreCommerceAPITool.description, this.exploreCommerceAPITool.inputSchema, this.exploreCommerceAPITool.handler); this.server.tool(this.hooksRecommendationTool.name, this.hooksRecommendationTool.description, this.hooksRecommendationTool.inputSchema, this.hooksRecommendationTool.handler); this.server.tool(_tools.CustomApiTool.name, _tools.CustomApiTool.description, _tools.CustomApiTool.inputSchema, _tools.CustomApiTool.fn); } run() { var _this2 = this; return _asyncToGenerator(function* () { // Read args passed by the MCP client (from mcp.json "args") const argv = process.argv.slice(2); const readFlag = (name, def) => { const i = argv.findIndex(a => a === `--${name}` || a.startsWith(`--${name}=`)); if (i === -1) return process.env[name.toUpperCase()] ?? def; const curr = argv[i]; if (curr.includes('=')) return curr.split('=').slice(1).join('='); return argv[i + 1] ?? true; }; const noTelemetry = !!readFlag('no-telemetry', false); // Store dw.json path globally so tools can access it const dwJsonPath = readFlag('dw-json', null); if (dwJsonPath) { global.DW_JSON_PATH = dwJsonPath; } const transport = new _stdio.StdioServerTransport(); yield _this2.server.connect(transport); // when telemetry is enabled, then send telemetry events if (!noTelemetry) { try { var _this2$server$getClie, _this2$server, _this2$telemetry; _this2.telemetry = new _telemetry.Telemetry(); yield _this2.telemetry.start(); const clientInfo = (_this2$server$getClie = (_this2$server = _this2.server).getClientVersion) === null || _this2$server$getClie === void 0 ? void 0 : _this2$server$getClie.call(_this2$server); if (clientInfo) { _this2.telemetry.addAttributes({ clientName: clientInfo.name, clientVersion: clientInfo.version }); } (_this2$telemetry = _this2.telemetry) === null || _this2$telemetry === void 0 ? void 0 : _this2$telemetry.sendEvent('SERVER_STATUS', { status: 'started' }); } catch (error) { var _this2$telemetry2; (_this2$telemetry2 = _this2.telemetry) === null || _this2$telemetry2 === void 0 ? void 0 : _this2$telemetry2.sendEvent('SERVER_STATUS', { status: 'error' }); throw error; } const sendStop = signal => { var _this2$telemetry3; (_this2$telemetry3 = _this2.telemetry) === null || _this2$telemetry3 === void 0 ? void 0 : _this2$telemetry3.sendEvent('SERVER_STATUS', { status: 'stopped', signal }); _this2.telemetry.stop(); }; process.on('exit', () => sendStop('exit')); process.on('SIGINT', () => { sendStop('SIGINT'); process.exit(0); }); process.on('SIGTERM', () => { sendStop('SIGTERM'); process.exit(0); }); } })(); } } const server = new PwaStorefrontMCPServerHighLevel(); server.run().catch(console.error);