Skip to main content
Glama

set_metafield

Create or update a metafield on any Shopify resource by specifying ownerId, namespace, key, type, and value. Replaces existing metafield with same (ownerId, namespace, key) triple, otherwise creates new.

Instructions

Create or update (upsert) a single metafield on any supported Shopify resource — product, variant, collection, customer, order, draft order, shop, or shop policies. The (ownerId, namespace, key) triple is the unique identifier; calling this tool with an existing triple replaces the value, otherwise creates a new metafield. The type must be a Shopify-supported metafield type and the value must serialize per that type — e.g. JSON types take a JSON string, references take a target GID, primitives take literal text. Errors come back as MCP tool errors with the validation messages from Shopify.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
ownerIdYesGID of the resource to attach the metafield to (e.g. gid://shopify/Product/123, gid://shopify/Collection/456, gid://shopify/Customer/789, gid://shopify/Order/...)
namespaceYesMetafield namespace (2-255 chars). Convention: app-specific prefix.
keyYesMetafield key within the namespace (1-64 chars).
typeYesMetafield type: 'single_line_text_field', 'multi_line_text_field', 'number_integer', 'number_decimal', 'boolean', 'json', 'url', 'date', 'date_time', 'rating', 'color', 'weight', 'volume', 'dimension', 'money', 'rich_text_field', or reference types like 'product_reference', 'collection_reference', 'file_reference'.
valueYesMetafield value, serialized per the type. JSON/reference types take a JSON string; primitives take the literal string.

Implementation Reference

  • The handler/function that executes the 'set_metafield' tool logic — calls the Shopify GraphQL MetafieldsSet mutation with ownerId, namespace, key, type, and value, then returns the saved metafield details.
    async (args) => {
      const data = await client.graphql<{
        metafieldsSet: {
          metafields: Metafield[];
          userErrors: ShopifyUserError[];
        };
      }>(METAFIELDS_SET_MUTATION, {
        metafields: [
          {
            ownerId: args.ownerId,
            namespace: args.namespace,
            key: args.key,
            type: args.type,
            value: args.value,
          },
        ],
      });
      throwIfUserErrors(data.metafieldsSet.userErrors, "metafieldsSet");
      const mf = data.metafieldsSet.metafields[0];
      if (!mf) {
        return {
          content: [
            { type: "text" as const, text: "metafieldsSet returned no metafield." },
          ],
        };
      }
      return {
        content: [
          {
            type: "text" as const,
            text: [
              "Metafield saved:",
              `  ${mf.namespace}.${mf.key} (${mf.type})`,
              `  = ${mf.value}`,
              `  ID: ${mf.id}`,
              `  Owner: ${mf.ownerType}`,
            ].join("\n"),
          },
        ],
      };
    },
  • Zod schema defining the input parameters for set_metafield: ownerId, namespace, key, type, and value.
    const setMetafieldSchema = {
      ownerId: z
        .string()
        .describe(
          "GID of the resource to attach the metafield to (e.g. gid://shopify/Product/123, gid://shopify/Collection/456, gid://shopify/Customer/789, gid://shopify/Order/...)",
        ),
      namespace: z
        .string()
        .min(2)
        .max(255)
        .describe("Metafield namespace (2-255 chars). Convention: app-specific prefix."),
      key: z
        .string()
        .min(1)
        .max(64)
        .describe("Metafield key within the namespace (1-64 chars)."),
      type: z
        .string()
        .describe(
          "Metafield type: 'single_line_text_field', 'multi_line_text_field', 'number_integer', 'number_decimal', 'boolean', 'json', 'url', 'date', 'date_time', 'rating', 'color', 'weight', 'volume', 'dimension', 'money', 'rich_text_field', or reference types like 'product_reference', 'collection_reference', 'file_reference'.",
        ),
      value: z
        .string()
        .describe(
          "Metafield value, serialized per the type. JSON/reference types take a JSON string; primitives take the literal string.",
        ),
    };
  • Registration of the 'set_metafield' tool on the McpServer with its name, description, schema, and handler function via server.tool().
    server.tool(
      "set_metafield",
      "Create or update (upsert) a single metafield on any supported Shopify resource — product, variant, collection, customer, order, draft order, shop, or shop policies. The (ownerId, namespace, key) triple is the unique identifier; calling this tool with an existing triple replaces the value, otherwise creates a new metafield. The `type` must be a Shopify-supported metafield type and the `value` must serialize per that type — e.g. JSON types take a JSON string, references take a target GID, primitives take literal text. Errors come back as MCP tool errors with the validation messages from Shopify.",
      setMetafieldSchema,
      async (args) => {
        const data = await client.graphql<{
          metafieldsSet: {
            metafields: Metafield[];
            userErrors: ShopifyUserError[];
          };
        }>(METAFIELDS_SET_MUTATION, {
          metafields: [
            {
              ownerId: args.ownerId,
              namespace: args.namespace,
              key: args.key,
              type: args.type,
              value: args.value,
            },
          ],
        });
        throwIfUserErrors(data.metafieldsSet.userErrors, "metafieldsSet");
        const mf = data.metafieldsSet.metafields[0];
        if (!mf) {
          return {
            content: [
              { type: "text" as const, text: "metafieldsSet returned no metafield." },
            ],
          };
        }
        return {
          content: [
            {
              type: "text" as const,
              text: [
                "Metafield saved:",
                `  ${mf.namespace}.${mf.key} (${mf.type})`,
                `  = ${mf.value}`,
                `  ID: ${mf.id}`,
                `  Owner: ${mf.ownerType}`,
              ].join("\n"),
            },
          ],
        };
      },
    );
  • src/server.ts:61-71 (registration)
    Top-level invocation where registerMetafieldTools is called with the MCP server and Shopify client in the server setup.
      registerMetafieldTools(s, shopify);
      registerDraftOrderTools(s, shopify);
      registerCollectionTools(s, shopify);
      registerVariantTools(s, shopify);
      registerFulfillmentTools(s, shopify);
      registerWebhookTools(s, shopify);
      registerMetaobjectTools(s, shopify);
      registerAnalyticsTools(s, shopify);
      registerBridgeTools(s, shopify, comfyui, config.comfyUIDefaultCkpt);
      return s;
    };
  • GraphQL mutation string METAFIELDS_SET_MUTATION used by the set_metafield handler to send the metafieldsSet mutation to Shopify.
    const METAFIELDS_SET_MUTATION = /* GraphQL */ `
      mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
        metafieldsSet(metafields: $metafields) {
          metafields {
            id
            namespace
            key
            type
            value
            ownerType
            createdAt
            updatedAt
          }
          userErrors { field message code }
        }
      }
    `;
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries the burden. It explains upsert behavior, unique key semantics, and error handling (validation messages). Lacks details on idempotency, authorization needs, or rate limits, but provides sufficient behavioral insight for a mutation tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two sentences, front-loaded with the primary purpose, no redundant information. Every sentence adds essential context.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

No output schema and description does not mention return values on success. For an upsert tool, the agent needs to know what is returned (likely the metafield object). This is a significant gap.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema descriptions cover 100% of parameters. The description adds value beyond the schema by explaining the uniqueness of (ownerId, namespace, key) and value serialization per type (JSON vs references vs primitives), which is not in the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description clearly states 'Create or update (upsert) a single metafield on any supported Shopify resource', specifying the unique identifier triple and the range of resources. This effectively distinguishes from sibling tools like delete_metafield and list_metafields by naming the operation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Implies use for creating/updating a metafield based on the triple, but does not explicitly state when to choose this over other metafield operations or mention prerequisites. No exclusions or alternatives given.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/miller-joe/shopify-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server