@@ -19,7 +19,8 @@ const polarisUnifiedEnabled =
19
19
* Helper function to make requests to the Shopify dev server
20
20
* @param path The API path (e.g., "/mcp/search", "/mcp/getting_started")
21
21
* @param options Request options including parameters and headers
22
- * @returns The fetch response
22
+ * @returns The response text
23
+ * @throws Error if the response is not ok
23
24
*/
24
25
async function shopifyDevFetch (
25
26
path : string ,
@@ -28,7 +29,7 @@ async function shopifyDevFetch(
28
29
headers ?: Record < string , string > ;
29
30
method ?: string ;
30
31
} ,
31
- ) : Promise < Response > {
32
+ ) : Promise < string > {
32
33
const url = new URL ( path , SHOPIFY_BASE_URL ) ;
33
34
const instrumentation = instrumentationData ( ) ;
34
35
@@ -69,7 +70,11 @@ async function shopifyDevFetch(
69
70
`[shopify-dev] Response status: ${ response . status } ${ response . statusText } ` ,
70
71
) ;
71
72
72
- return response ;
73
+ if ( ! response . ok ) {
74
+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
75
+ }
76
+
77
+ return await response . text ( ) ;
73
78
}
74
79
75
80
const GettingStartedAPISchema = z . object ( {
@@ -102,50 +107,39 @@ const withConversationId = <T extends z.ZodRawShape>(schema: T) => ({
102
107
*/
103
108
export async function searchShopifyDocs (
104
109
prompt : string ,
105
- options ?: { max_num_results ?: number } ,
110
+ max_num_results ?: number ,
106
111
) {
107
112
try {
108
113
// Prepare parameters
109
114
const parameters : Record < string , string > = {
110
115
query : prompt ,
111
116
} ;
112
117
113
- if ( options ?. max_num_results !== undefined ) {
114
- parameters . max_num_results = String ( options . max_num_results ) ;
118
+ if ( max_num_results !== undefined ) {
119
+ parameters . max_num_results = String ( max_num_results ) ;
115
120
}
116
121
117
- const response = await shopifyDevFetch ( "/mcp/search" , {
122
+ const responseText = await shopifyDevFetch ( "/mcp/search" , {
118
123
parameters,
119
124
} ) ;
120
125
121
- if ( ! response . ok ) {
122
- console . error ( `[shopify-docs] HTTP error status: ${ response . status } ` ) ;
123
- return {
124
- success : false ,
125
- formattedText : `HTTP error! status: ${ response . status } ` ,
126
- } ;
127
- }
128
-
129
- // Read and process the response
130
- const responseText = await response . text ( ) ;
131
126
console . error (
132
127
`[shopify-docs] Response text (truncated): ${
133
128
responseText . substring ( 0 , 200 ) +
134
129
( responseText . length > 200 ? "..." : "" )
135
130
} `,
136
131
) ;
137
132
138
- // Parse and format the JSON for human readability
133
+ // Try to parse and format as JSON, otherwise return raw text
139
134
try {
140
135
const jsonData = JSON . parse ( responseText ) ;
141
136
const formattedJson = JSON . stringify ( jsonData , null , 2 ) ;
142
-
143
137
return {
144
138
success : true ,
145
139
formattedText : formattedJson ,
146
140
} ;
147
141
} catch ( e ) {
148
- // If JSON parsing fails, get the raw text
142
+ // If JSON parsing fails, return the raw text
149
143
console . warn ( `[shopify-docs] Error parsing JSON response: ${ e } ` ) ;
150
144
return {
151
145
success : true ,
@@ -217,9 +211,10 @@ export async function shopifyTools(server: McpServer): Promise<void> {
217
211
) ,
218
212
} ) ,
219
213
async ( params ) => {
220
- const result = await searchShopifyDocs ( params . prompt , {
221
- max_num_results : params . max_num_results ,
222
- } ) ;
214
+ const result = await searchShopifyDocs (
215
+ params . prompt ,
216
+ params . max_num_results ,
217
+ ) ;
223
218
224
219
recordUsage ( "search_docs_chunks" , params , result . formattedText ) . catch (
225
220
( ) => { } ,
@@ -256,14 +251,12 @@ export async function shopifyTools(server: McpServer): Promise<void> {
256
251
async function fetchDocText ( path : string ) : Promise < DocResult > {
257
252
try {
258
253
const appendedPath = path . endsWith ( ".txt" ) ? path : `${ path } .txt` ;
259
- const response = await shopifyDevFetch ( appendedPath ) ;
260
-
261
- if ( ! response . ok ) {
262
- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
263
- }
264
-
265
- const text = await response . text ( ) ;
266
- return { text : `## ${ path } \n\n${ text } \n\n` , path, success : true } ;
254
+ const responseText = await shopifyDevFetch ( appendedPath ) ;
255
+ return {
256
+ text : `## ${ path } \n\n${ responseText } \n\n` ,
257
+ path,
258
+ success : true ,
259
+ } ;
267
260
} catch ( error ) {
268
261
console . error ( `Error fetching document at ${ path } : ${ error } ` ) ;
269
262
return {
@@ -385,16 +378,10 @@ export async function shopifyTools(server: McpServer): Promise<void> {
385
378
}
386
379
387
380
try {
388
- const response = await shopifyDevFetch ( "/mcp/getting_started" , {
381
+ const responseText = await shopifyDevFetch ( "/mcp/getting_started" , {
389
382
parameters : { api : params . api } ,
390
383
} ) ;
391
384
392
- if ( ! response . ok ) {
393
- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
394
- }
395
-
396
- const responseText = await response . text ( ) ;
397
-
398
385
recordUsage ( "learn_shopify_api" , params , responseText ) . catch ( ( ) => { } ) ;
399
386
400
387
// Include the conversation ID in the response
@@ -430,15 +417,8 @@ ${responseText}`;
430
417
*/
431
418
async function fetchGettingStartedApis ( ) : Promise < GettingStartedAPI [ ] > {
432
419
try {
433
- const response = await shopifyDevFetch ( "/mcp/getting_started_apis" ) ;
434
-
435
- if ( ! response . ok ) {
436
- console . error ( `[api-information] HTTP error status: ${ response . status } ` ) ;
437
- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
438
- }
420
+ const responseText = await shopifyDevFetch ( "/mcp/getting_started_apis" ) ;
439
421
440
- // Read and process the response
441
- const responseText = await response . text ( ) ;
442
422
console . error (
443
423
`[api-information] Response text (truncated): ${
444
424
responseText . substring ( 0 , 200 ) +
0 commit comments