Chapter 4 25 min read

Intro to Model Context Protocol (MCP)

The USB-C port for the AI economy. How we connect the "Brain" of the LLM to the "Body" of the Commerce Platform.

In the previous chapters, we established that agents need to "see" the world (Perception) and "act" on it (Tools). But how exactly does an LLM hosted by Anthropic connect to a product database hosted on Shopify?

Historically, this required custom glue code. You had to build a specific integration for every single service you wanted your AI to talk to. This is unscalable.

Enter the Model Context Protocol (MCP).

Why RAG is Not Enough

Before we dive into MCP, we must address the elephant in the room: RAG (Retrieval Augmented Generation). Most AI developers are familiar with RAG—vectorizing documents and retrieving relevant chunks.

RAG is excellent for static knowledge (e.g., "What is your return policy?"). It is terrible for dynamic commerce.

The Problem with RAG

  • Stale Data: Embeddings are snapshots. If a price changes 1 second after you embed it, the AI is lying.
  • Read-Only: RAG can tell you about a product, but it cannot buy it. It has no "hands."
  • No State: It doesn't know you just added an item to the cart.

The MCP Solution

  • Live Data: Fetches data directly from the source API at query time.
  • Read & Write: Can execute tools (`add_to_cart`, `update_address`).
  • Stateful: Can maintain a session with the commerce backend.

The Technical Architecture

MCP is an open standard that defines how AI models interact with external data and tools. Think of it as a universal adapter. It uses a Client-Host-Server architecture.

MCP Host

Claude Desktop, IDE, Agent Runtime

The Application

↔️
MCP Client

Protocol Connector

The Translator

↔️
MCP Server

Shopify, Stripe, Postgres

The Capability Provider

The Transport Layer

Communication happens via JSON-RPC. This can run over stdio (local processes) or SSE (Server-Sent Events over HTTP) for remote agents.

Core Primitives: Resources, Prompts, Tools

To understand how we build commerce agents, we need to understand the three primitives that an MCP Server exposes.

1. Resources

"Data acting as files."

The server exposes data as virtual files that the LLM can read. This provides the "Context."

shopify://products/12345/inventory
shopify://orders/recent

2. Prompts

"Standardized workflows."

Pre-written templates that help the user (or agent) start a task.

"Find Gift" -> Pulls trending items + user preferences
"Check Order Status" -> Pulls last 3 orders

3. Tools

"Executable functions."

Things the model can do. This is where the action happens.

tools/add_to_cart(sku, quantity)
tools/calculate_shipping(address)

Building a Commerce MCP Server

What does this look like in code? Imagine we are building a `shopify-mcp-server`. We don't need to write complex AI logic. We just need to map Shopify's API to MCP primitives.

import { Server } from "@modelcontextprotocol/sdk/server";

// 1. Define the Server
const server = new Server({
  name: "shopify-mcp",
  version: "1.0.0"
});

// 2. Expose a Tool (The "Hands")
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  if (request.params.name === "search_products") {
    const query = request.params.arguments.query;
    // Call real Shopify API
    const products = await shopifyClient.product.search(query);
    
    return {
      content: [{ type: "text", text: JSON.stringify(products) }]
    };
  }
});

// 3. Expose a Resource (The "Eyes")
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
  // Allow LLM to read "shopify://inventory/{id}"
  const id = parseId(request.params.uri);
  const inventory = await shopifyClient.inventory.get(id);
  
  return {
    contents: [{ uri: request.params.uri, text: inventory.count.toString() }]
  };
});

Once this server is running, any MCP-compliant agent (Claude, a custom bot, an IDE) can instantly search products and check inventory on your store. You write the integration once, and it works everywhere.

Security: The "Human-in-the-Loop"

Giving an AI access to your store's API sounds dangerous. What if it buys 1,000 iPhones?

MCP handles this through Capabilities and Sampling.

  • Granular Permissions: You can configure the client to allow `read_resource` automatically, but require User Approval for every `call_tool` that involves money.
  • Sampling (Human Feedback): The server can request "Sampling". Before executing a high-stakes action, it can ask the LLM to summarize what it's about to do and present it to the user for a "Yes/No" confirmation.

The Network Effect

The true power of MCP is not in a single connection, but in the network.

Imagine an agent connected to:

  • + Shopify MCP Server (Products)
  • + Stripe MCP Server (Payments)
  • + FedEx MCP Server (Shipping)
  • + Google Calendar MCP Server (Scheduling)

The agent can now: "Find a gift for Mom (Shopify), buy it (Stripe), ensure it arrives by her birthday (FedEx + Calendar)."

This combinatorial power is what unlocks the Agentic Economy.

Key Takeaways

  • MCP is the 'USB-C' for AI, replacing custom integrations with a standard Client-Server model.
  • It beats RAG for commerce because it provides live, read/write access to data.
  • The 3 Primitives: Resources (Context), Prompts (Workflows), and Tools (Actions).
  • Security is handled via granular permissions and human-in-the-loop approval flows.