Skip to main content

MCP 集成模式

本指南涵盖在生产环境 Shopify 工作流中使用 MCP 服务器的高级模式。从单服务器设置到复杂的多服务器编排、CI/CD 集成和自动化数据管道。


模式 1:单服务器配置

最简单的模式。一个 MCP 服务器处理所有交互。

┌──────────┐          ┌───────────────┐          ┌─────────────┐
│ AI Client │ ──MCP──► │ shopify-mcp │ ──API──► │ Shopify Store│
│ │ │ (GeLi2001) │ │ │
└──────────┘ └───────────────┘ └─────────────┘

适用场景:

  • 单商店管理
  • MCP 入门
  • 简单的自动化任务

配置:

{
"mcpServers": {
"shopify": {
"command": "npx",
"args": ["-y", "shopify-mcp@latest"],
"env": {
"SHOPIFY_ACCESS_TOKEN": "shpat_xxxxxxxxxxxxx",
"MYSHOPIFY_DOMAIN": "my-store.myshopify.com"
}
}
}
}

局限性:

  • 无文档访问(仅有实时商店数据)
  • 无法根据 API 架构验证
  • 单点故障

模式 2:双服务器(文档 + 商店)

大多数开发者的推荐模式。将文档智能与商店管理结合。

┌──────────┐     ┌───────────────────┐
│ │────►│ Shopify Dev MCP │──► Docs, Schemas, Liquid
│ AI Client │ └───────────────────┘
│ │ ┌───────────────────┐
│ │────►│ Shopify Store MCP │──► Products, Orders, etc.
└──────────┘ └───────────────────┘

适用场景:

  • 日常 Shopify 开发
  • 在管理商店的同时构建应用
  • 学习 Shopify 平台

配置:

{
"mcpServers": {
"shopify-dev": {
"command": "npx",
"args": ["-y", "@anthropic-ai/shopify-dev-mcp@latest"]
},
"shopify-store": {
"command": "npx",
"args": ["-y", "shopify-mcp@latest"],
"env": {
"SHOPIFY_ACCESS_TOKEN": "shpat_xxxxxxxxxxxxx",
"MYSHOPIFY_DOMAIN": "my-store.myshopify.com"
}
}
}
}

工作流示例:

Prompt: "I need to add a 'care_instructions' metafield to all products
in the 'Apparel' collection. First, search the docs for the correct
metafield type and mutation. Then execute it on my store."

1. AI uses Dev MCP → search_docs("metafield definitions product")
2. AI uses Dev MCP → explore_api_schema("MetafieldDefinitionInput")
3. AI uses Store MCP → get_collections(type: "custom", title: "Apparel")
4. AI uses Store MCP → get_products(collection_id: "gid://...")
5. AI uses Store MCP → manage_product_metafields(product_id: "...", ...)

模式 3:多商店管理

从单个 AI 会话管理多个 Shopify 商店。适用于代理商、连锁店和拥有区域商店的商家。

┌──────────┐     ┌─────────────────────┐
│ │────►│ Store: US Production │──► us-store.myshopify.com
│ │ └─────────────────────┘
│ AI Client │ ┌─────────────────────┐
│ │────►│ Store: EU Production │──► eu-store.myshopify.com
│ │ └─────────────────────┘
│ │ ┌─────────────────────┐
│ │────►│ Store: Staging │──► staging.myshopify.com
└──────────┘ └─────────────────────┘

配置:

{
"mcpServers": {
"shopify-dev": {
"command": "npx",
"args": ["-y", "@anthropic-ai/shopify-dev-mcp@latest"]
},
"store-us": {
"command": "npx",
"args": ["-y", "shopify-mcp@latest"],
"env": {
"SHOPIFY_ACCESS_TOKEN": "shpat_us_token",
"MYSHOPIFY_DOMAIN": "us-store.myshopify.com"
}
},
"store-eu": {
"command": "npx",
"args": ["-y", "shopify-mcp@latest"],
"env": {
"SHOPIFY_ACCESS_TOKEN": "shpat_eu_token",
"MYSHOPIFY_DOMAIN": "eu-store.myshopify.com"
}
},
"store-staging": {
"command": "npx",
"args": ["-y", "shopify-mcp@latest"],
"env": {
"SHOPIFY_ACCESS_TOKEN": "shpat_staging_token",
"MYSHOPIFY_DOMAIN": "staging-store.myshopify.com"
}
}
}
}

工作流示例:

Prompt: "Sync the product catalog from the US store to the EU store.
For each product, convert the price from USD to EUR using a 1.08 rate
and update the description to include EU shipping information."

1. AI uses store-us → get_products(limit: 250)
2. For each product:
a. AI converts price * 1.08
b. AI uses store-eu → create_product(...) or update_product(...)
跨商店操作

始终先在暂存环境上测试跨商店同步。产品 ID 在不同商店之间是不同的,因此你必须通过 SKU、handle 或其他唯一标识符进行匹配——而不是 GID。


模式 4:Composio 工具路由器

Composio 充当中间件层,通过单个 MCP 连接将工具调用路由到多个服务。

┌──────────┐     ┌──────────────┐     ┌──────────────┐
│ │ │ │────►│ Shopify Admin │
│ │ │ │ └──────────────┘
│ AI Client │────►│ Composio │ ┌──────────────┐
│ │ │ Tool Router │────►│ Google Sheets │
│ │ │ │ └──────────────┘
└──────────┘ │ │ ┌──────────────┐
│ │────►│ Slack │
└──────────────┘ └──────────────┘

适用场景:

  • 跨多个服务的工作流
  • 希望使用 OAuth 而非原始令牌
  • 有审计要求的企业环境

设置:

# Install Composio
pip install composio-core

# Add integrations
composio add shopify
composio add google-sheets
composio add slack

# Optionally filter to only the tools you need
composio actions --app shopify --filter "product,order"

配置:

{
"mcpServers": {
"composio": {
"command": "composio",
"args": ["serve", "--apps", "shopify,google-sheets,slack"]
}
}
}

工作流示例:

Prompt: "Every morning, pull yesterday's orders from Shopify,
summarize them in a Google Sheet, and post the summary to #sales in Slack."

1. AI uses Composio → shopify.get_orders(created_at_min: yesterday)
2. AI uses Composio → google_sheets.append_rows(spreadsheet_id, data)
3. AI uses Composio → slack.post_message(channel: "#sales", text: summary)

模式 5:CI/CD 与 MCP

将 MCP 服务器集成到持续集成管道中,在部署时自动管理商店。

┌──────────┐     ┌──────────┐     ┌──────────────┐     ┌─────────┐
│ Git Push │────►│ CI/CD │────►│ Claude Code │────►│ Shopify │
│ │ │ Pipeline │ │ + MCP Servers │ │ Store │
└──────────┘ └──────────┘ └──────────────┘ └─────────┘

GitHub Actions 示例

name: Shopify Store Sync
on:
push:
branches: [main]
paths:
- 'products/**'
- 'collections/**'

jobs:
sync-store:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Claude Code
run: npm install -g @anthropic-ai/claude-code

- name: Configure MCP Servers
run: |
claude mcp add shopify-store \
-e SHOPIFY_ACCESS_TOKEN=${{ secrets.SHOPIFY_ACCESS_TOKEN }} \
-e MYSHOPIFY_DOMAIN=${{ secrets.SHOP_DOMAIN }} \
-- npx -y shopify-mcp@latest

- name: Sync Products
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude -p "Read the product definitions in ./products/
and sync them to the Shopify store. Create new products
that don't exist, update existing ones by SKU match."

CI/CD + MCP 的使用场景

触发器操作示例
推送到 main同步产品目录Git 中的产品数据作为唯一信息来源
新发布标签更新商店主题 metafields商店中显示的版本信息
定时 cron审计库存水平每日低库存预警
PR 合并更新产品系列规则代码中的产品系列定义
来自 ERP 的 Webhook同步库存变化外部系统集成
CI/CD 中的速率限制

自动化管道可能很快达到 Shopify 速率限制。请实施:

  • 对 429 响应的指数退避
  • 尽可能使用批量操作
  • 在管道日志中监控速率限制

模式 6:自动化商店管理

使用 MCP 服务器配合定时任务进行免人工商店运营。

每日产品审计

Schedule: Every day at 6:00 AM UTC

Prompt: "Run a product catalog audit:
1. Find all products with no description
2. Find all products with no images
3. Find all products with zero inventory across all locations
4. Find all draft products older than 30 days
5. Generate a summary report with recommendations"

每周价格优化

Schedule: Every Monday at 8:00 AM UTC

Prompt: "Analyze the last 7 days of orders:
1. Identify the top 20 best-selling products
2. For products with >90% sell-through, suggest a 10% price increase
3. For products with <10% sell-through and >60 days inventory, suggest a 15% discount
4. Present recommendations -- do NOT auto-apply changes"

库存补货预警

Schedule: Every 4 hours

Prompt: "Check inventory levels across all locations:
1. Find variants with quantity below their reorder point (stored in metafield 'custom.reorder_point')
2. Calculate recommended reorder quantity
3. Generate a reorder list grouped by vendor"

模式 7:数据管道架构

构建将 Shopify 数据通过转换和分析阶段流转的数据管道。

┌──────────┐     ┌──────────────┐     ┌──────────────┐     ┌──────────┐
│ Shopify │ │ Extract │ │ Transform │ │ Load │
│ Store MCP │────►│ (get_orders, │────►│ (AI-powered │────►│ (Sheets, │
│ │ │ get_products)│ │ analysis) │ │ DB, CSV)│
└──────────┘ └──────────────┘ └──────────────┘ └──────────┘

提取:从 Shopify 拉取数据

Prompt: "Extract all orders from the last 30 days with their line items,
customer data, and fulfillment status. Include calculated fields:
- Order value
- Number of items
- Days since order placed
- Fulfillment delay (days between order and first fulfillment)"

转换:AI 驱动的分析

Prompt: "Analyze the extracted orders:
1. Customer cohort analysis (new vs returning)
2. Product affinity analysis (frequently bought together)
3. Geographic distribution of orders
4. Average order value trends by week
5. Fulfillment performance by location"

加载:输出结果

Prompt: "Format the analysis as:
1. A CSV file with the raw data for our data warehouse
2. A summary table for the weekly stakeholder report
3. A list of actionable recommendations"

模式 8:构建自定义 MCP 服务器

当现有服务器无法满足你的需求时,构建自定义服务器。

最小自定义服务器(TypeScript)

import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";

const server = new Server(
{ name: "my-shopify-tools", version: "1.0.0" },
{ capabilities: { tools: {} } }
);

// Define a custom tool
server.setRequestHandler("tools/list", async () => ({
tools: [
{
name: "calculate_shipping_estimate",
description: "Calculate estimated shipping cost based on weight and destination",
inputSchema: {
type: "object",
properties: {
weight_kg: { type: "number", description: "Package weight in kg" },
destination_country: { type: "string", description: "ISO country code" },
shipping_method: {
type: "string",
enum: ["standard", "express", "overnight"],
description: "Shipping speed"
}
},
required: ["weight_kg", "destination_country"]
}
}
]
}));

// Handle tool execution
server.setRequestHandler("tools/call", async (request) => {
if (request.params.name === "calculate_shipping_estimate") {
const { weight_kg, destination_country, shipping_method } = request.params.arguments;

// Your custom business logic here
const rates = calculateRates(weight_kg, destination_country, shipping_method);

return {
content: [
{
type: "text",
text: JSON.stringify(rates, null, 2)
}
]
};
}
});

// Start the server
const transport = new StdioServerTransport();
await server.connect(transport);

注册你的自定义服务器

# Claude Code
claude mcp add my-shopify-tools -- node /path/to/my-server/index.js

# Or with environment variables
claude mcp add my-shopify-tools \
-e SHOPIFY_ACCESS_TOKEN=xxx \
-e CUSTOM_CONFIG=value \
-- node /path/to/my-server/index.js

模式 9:安全优先配置

令牌轮换模式

# Store tokens in a secrets manager, not in config files
# Use a wrapper script that fetches the token at runtime

#!/bin/bash
# start-shopify-mcp.sh
export SHOPIFY_ACCESS_TOKEN=$(aws secretsmanager get-secret-value \
--secret-id shopify/production/api-token \
--query 'SecretString' --output text)
export MYSHOPIFY_DOMAIN="my-store.myshopify.com"

npx -y shopify-mcp@latest
{
"mcpServers": {
"shopify-store": {
"command": "/path/to/start-shopify-mcp.sh"
}
}
}

生产环境只读访问

对于生产商店,创建仅具有读取权限范围的令牌:

Scopes: read_products, read_orders, read_customers, read_inventory

使用单独的开发商店令牌进行写入操作。

审计日志

用日志代理包装你的 MCP 服务器:

// Log every tool call for compliance
server.setRequestHandler("tools/call", async (request) => {
const startTime = Date.now();

// Log the request
await auditLog({
timestamp: new Date().toISOString(),
tool: request.params.name,
arguments: request.params.arguments,
user: process.env.MCP_USER_ID
});

// Execute the actual tool
const result = await executeShopifyTool(request);

// Log the result
await auditLog({
timestamp: new Date().toISOString(),
tool: request.params.name,
duration_ms: Date.now() - startTime,
success: !result.isError
});

return result;
});

选择合适的模式

模式复杂度最适合
单服务器学习、简单任务
双服务器(文档 + 商店)日常开发
多商店代理商、连锁店
Composio 路由器跨服务工作流
CI/CD 集成自动化部署
自动化管理定时运营
数据管道分析、报告
自定义服务器独特的业务逻辑
安全优先生产环境
从简单开始,按需扩展

从模式 2(双服务器)开始。当你熟悉了 MCP 交互后,随着工作流需求的增加再添加复杂性。大多数开发者不需要超过模式 2-3。