@rafaelcg/adobe-commerce-dev-mcp
Version:
A command line tool for setting up Adobe Commerce MCP server
102 lines (100 loc) • 4.32 kB
JavaScript
import { z } from "zod";
import { searchAdobeCommerceSchema } from "./adobe-commerce-schema.js";
const SHOPIFY_BASE_URL = "https://shopify.dev";
/**
* Searches Shopify documentation with the given query
* @param prompt The search query for Shopify documentation
* @returns The formatted response or error message
*/
export async function searchShopifyDocs(prompt) {
try {
// Prepare the URL with query parameters
const url = new URL("/mcp/search", SHOPIFY_BASE_URL);
url.searchParams.append("query", prompt);
console.error(`[shopify-docs] Making GET request to: ${url.toString()}`);
// Make the GET request
const response = await fetch(url.toString(), {
method: "GET",
headers: {
Accept: "application/json",
"Cache-Control": "no-cache",
"X-Shopify-Surface": "mcp",
},
});
console.error(`[shopify-docs] Response status: ${response.status} ${response.statusText}`);
// Convert headers to object for logging
const headersObj = {};
response.headers.forEach((value, key) => {
headersObj[key] = value;
});
console.error(`[shopify-docs] Response headers: ${JSON.stringify(headersObj)}`);
if (!response.ok) {
console.error(`[shopify-docs] HTTP error status: ${response.status}`);
throw new Error(`HTTP error! status: ${response.status}`);
}
// Read and process the response
const responseText = await response.text();
console.error(`[shopify-docs] Response text (truncated): ${responseText.substring(0, 200) +
(responseText.length > 200 ? "..." : "")}`);
// Parse and format the JSON for human readability
try {
const jsonData = JSON.parse(responseText);
const formattedJson = JSON.stringify(jsonData, null, 2);
return {
success: true,
formattedText: formattedJson,
};
}
catch (e) {
console.warn(`[shopify-docs] Error parsing JSON response: ${e}`);
// If parsing fails, return the raw text
return {
success: true,
formattedText: responseText,
};
}
}
catch (error) {
console.error(`[shopify-docs] Error searching Shopify documentation: ${error}`);
return {
success: false,
formattedText: `Error searching Shopify documentation: ${error instanceof Error ? error.message : String(error)}`,
error: error instanceof Error ? error.message : String(error),
};
}
}
export function adobeCommerceTools(server) {
server.tool("introspect_admin_schema", `This tool introspects and returns the portion of the Adobe Commerce GraphQL schema relevant to the user prompt. Only use this for the Adobe Commerce API.
It takes two arguments: query and filter. The query argument is the string search term to filter schema elements by name. The filter argument is an array of strings to filter results to show specific sections.`, {
query: z
.string()
.describe("Search term to filter schema elements by name. Only pass simple terms like 'product', 'category', etc."),
filter: z
.array(z.enum(["all", "types", "queries", "mutations"]))
.optional()
.default(["all"])
.describe("Filter results to show specific sections. Can include 'types', 'queries', 'mutations', or 'all' (default)"),
}, async ({ query, filter }, extra) => {
const result = await searchAdobeCommerceSchema(query, { filter });
if (result.success) {
return {
content: [
{
type: "text",
text: result.responseText,
},
],
};
}
else {
return {
content: [
{
type: "text",
text: `Error processing Adobe Commerce GraphQL schema: ${result.error}. Make sure the schema file exists.`,
},
],
};
}
});
}