Browse Source

fix: ShopRenter MCP to search orders by innerId instead of API id #82

- Modified ShopRenterOrderFilters to include innerId parameter
- Updated fetchOrders to support innerId filter in API request
- Changed handleGetOrder to search by innerId (customer-visible order number)
- Updated formatOrderForLlm to use innerId as orderNumber
- Updated MCP tool description to clarify order_id is innerId
- Customers now provide innerId (e.g., '172') instead of API id (e.g., 'b3JkZXItb3JkZXJfaWQ9MTcy')
Claude 5 months ago
parent
commit
2e50d061a1

+ 4 - 0
supabase/functions/_shared/shoprenter-client.ts

@@ -590,6 +590,7 @@ export interface ShopRenterOrderFilters {
   created_to?: string;       // ISO 8601 datetime
   updated_from?: string;     // ISO 8601 datetime
   updated_to?: string;       // ISO 8601 datetime
+  innerId?: string;          // Filter by customer-visible order number (innerId)
 }
 
 export interface ShopRenterCustomerFilters {
@@ -649,6 +650,9 @@ export async function fetchOrders(
     if (filters.updated_to) {
       endpoint += `&updated_to=${encodeURIComponent(filters.updated_to)}`
     }
+    if (filters.innerId) {
+      endpoint += `&innerId=${encodeURIComponent(filters.innerId)}`
+    }
   }
 
   return shopRenterApiRequest(storeId, endpoint, 'GET')

+ 20 - 6
supabase/functions/mcp-shoprenter/index.ts

@@ -62,7 +62,7 @@ const SERVER_VERSION = '2.0.0';
 const TOOLS: McpTool[] = [
   {
     name: 'shoprenter_get_order',
-    description: 'Get a specific order from a ShopRenter store by order ID. Returns complete order details including customer info, items, status, and totals. Use this when a customer provides their order number.',
+    description: 'Get a specific order from a ShopRenter store by customer-visible order number (innerId). Returns complete order details including customer info, items, status, and totals. Use this when a customer provides their order number.',
     inputSchema: {
       type: 'object',
       properties: {
@@ -72,7 +72,7 @@ const TOOLS: McpTool[] = [
         },
         order_id: {
           type: 'string',
-          description: 'The ShopRenter order ID'
+          description: 'The customer-visible order number (innerId), e.g., "172" - NOT the internal API ID'
         }
       },
       required: ['shop_id', 'order_id']
@@ -169,7 +169,7 @@ function formatOrderForLlm(order: any): LlmOrder {
 
   return {
     id: order.id,
-    orderNumber: order.order_number || order.number || order.id,
+    orderNumber: order.innerId || order.order_number || order.number || order.id,
     customer: {
       name: customerName,
       email: customerEmail,
@@ -259,11 +259,25 @@ async function handleGetOrder(args: Record<string, any>): Promise<ToolCallResult
   }
 
   try {
-    // Fetch the specific order
-    const order = await fetchOrder(shop_id, order_id);
+    // Search for order by innerId (customer-visible order number)
+    // The order_id parameter is actually the innerId from the customer's perspective
+    const response = await fetchOrders(shop_id, 1, 1, { innerId: order_id });
+    const orders = Array.isArray(response) ? response : (response.data || response.orders || []);
+
+    if (orders.length === 0) {
+      return {
+        content: [{
+          type: 'text',
+          text: JSON.stringify({
+            error: `No order found with order number: ${order_id}`
+          })
+        }],
+        isError: true
+      };
+    }
 
     // Format for LLM
-    const formattedOrder = formatOrderForLlm(order);
+    const formattedOrder = formatOrderForLlm(orders[0]);
 
     return {
       content: [{