-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathserver.js
More file actions
223 lines (202 loc) · 8.01 KB
/
server.js
File metadata and controls
223 lines (202 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/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
*/
import {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'
import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js'
import {z} from 'zod'
import {CreateAppGuidelinesTool, CreateNewComponentTool, DeveloperGuidelinesTool} from '../utils'
import {TestWithPlaywrightTool} from '../utils/run-site-test-tool'
// NOTE: This is a workaround to import JSON files as ES modules.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const productDocument = require('../data/ProductDocument.json')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const categoryDocument = require('../data/CategoryDocument.json')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const documentList = require('../data/DocumentList.json')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJson = require('../../package.json')
const FALLBACK_VERSION = '0.1.0'
class PwaStorefrontMCPServerHighLevel {
constructor() {
// Using McpServer instead of Server
this.server = new McpServer(
{
name: 'pwa-storefront-mcp-server',
version: packageJson?.version || FALLBACK_VERSION
},
{
capabilities: {
tools: {}
}
}
)
this.CreateNewComponentTool = new CreateNewComponentTool()
this.testWithPlaywrightTool = new TestWithPlaywrightTool()
this.setupTools()
// 1. Add in-memory session management
this.sessions = {}
this.sessionCounter = 1
}
setupTools() {
// Register CreateProjectTool
this.server.tool(
CreateAppGuidelinesTool.name,
CreateAppGuidelinesTool.description,
CreateAppGuidelinesTool.inputSchema,
CreateAppGuidelinesTool.fn
)
// Register DeveloperGuidelinesTool
this.server.tool(
DeveloperGuidelinesTool.name,
DeveloperGuidelinesTool.description,
DeveloperGuidelinesTool.inputSchema,
DeveloperGuidelinesTool.fn
)
this.server.tool(
'run_site_test',
'Run site performance or accessibility test for a given site URL (e.g. https://pwa-kit.mobify-storefront.com)',
{
testType: z.enum(['performance', 'accessibility']).describe('Type of test to run'),
siteUrl: z.string().optional().describe('Site URL to test (optional)')
},
({testType, siteUrl}) => this.testWithPlaywrightTool.run(testType, siteUrl)
)
this.server.tool(
'create_new_sample_component',
'Conversationally collect parameters and create a new sample React component.',
{
sessionId: z.string().optional().describe('Session ID for the conversational flow'),
answer: z.string().optional().describe('User answer to the current question')
},
(args) => this.handleCreateNewSampleComponent(args)
)
}
/**
* Helper to handle the conversational flow for create_new_sample_component
*/
async handleCreateNewSampleComponent(args) {
let sessionId = args.sessionId
if (!sessionId) {
sessionId = `session-interactive-${this.sessionCounter++}`
this.sessions[sessionId] = {step: 1, answers: {}}
}
const session = this.sessions[sessionId]
const {step, answers} = session
const answer = args.answer?.trim()
switch (step) {
case 1:
return this._handleComponentNameStep(session, answer, sessionId)
case 2:
return this._handleDirectoryStep(session, answer, sessionId)
case 3:
return this._handleSingleOrListStep(session, answer, sessionId)
default:
return this._handleDoneStep(sessionId)
}
}
_next(sessionId, question) {
return {
content: [{type: 'text', text: JSON.stringify({sessionId, question})}]
}
}
_done(sessionId, message) {
return {
content: [{type: 'text', text: JSON.stringify({sessionId, message})}]
}
}
_handleComponentNameStep(session, answer, sessionId) {
if (answer) {
session.answers.name = answer
// If PWA_STOREFRONT_APP_PATH is defined, automatically set location and go to step 3
if (process.env.PWA_STOREFRONT_APP_PATH) {
session.answers.location = process.env.PWA_STOREFRONT_APP_PATH + '/components'
session.step = 3
return this._next(
sessionId,
'Should this component display a single product or a list of products? Reply with "single" or "list".'
)
} else {
session.step = 2
return this._next(
sessionId,
'What should be the directory where the component should be created? Please provide the full absolute path.'
)
}
}
return this._next(sessionId, 'What would you like to name your new React component?')
}
_handleDirectoryStep(session, answer, sessionId) {
if (answer) {
session.answers.location = answer
session.step = 3
return this._next(
sessionId,
'Should this component display a single product or a list of products? Reply with "single" or "list".'
)
}
return this._next(
sessionId,
'What should be the directory where the component should be created? Please provide the full absolute path.'
)
}
async _handleSingleOrListStep(session, answer, sessionId) {
let isList = null
if (answer && /list/i.test(answer)) {
isList = true
} else if (answer && /single/i.test(answer)) {
isList = false
} else {
return this._next(sessionId, 'Please reply with "single" or "list".')
}
const tool = new CreateNewComponentTool()
tool.componentData = {
name: session.answers.name,
location: session.answers.location,
createTestFile: false,
customCode: '',
entityType: 'product'
}
const dataModel = this.getDataModel('product')
let schemaObj = dataModel && dataModel.properties ? dataModel.properties : {}
let presentationalResult = await tool.updateComponentToPresentational(
'product',
session.answers.name,
session.answers.location,
schemaObj,
{list: isList}
)
session.step = 99
return this._done(
sessionId,
(session.basicComponentResult || '') +
`\n\n${presentationalResult}\nComponent creation flow complete.`
)
}
_handleDoneStep(sessionId) {
return this._done(sessionId, 'Component creation flow complete.')
}
/**
* Simple method to get data models directly from imports
* @param {string} modelName - Name of the model (e.g., 'product', 'category')
* @returns {object|null} The data model object or null if not found
*/
getDataModel(modelName) {
const models = {
product: productDocument,
category: categoryDocument,
documentList: documentList
}
return models[modelName.toLowerCase()] || null
}
async run() {
const transport = new StdioServerTransport()
await this.server.connect(transport)
console.error('PWA Storefront MCP server (McpServer version) running on stdio')
}
}
const server = new PwaStorefrontMCPServerHighLevel()
server.run().catch(console.error)