> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cope.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MCP for AI shopping agents (cope-sdk/mcp)

> Mount an MCP endpoint on your own server so a buyer's AI agent can build a cart in your store and hand the buyer a COPE checkout link.

# MCP for AI shopping agents (cope-sdk/mcp)

A buyer can ask an AI agent to shop for them. With the MCP endpoint in the Checkout SDK, that agent can find your products, build a cart and prepare a checkout in your store, then give the buyer a link to COPE's checkout page. The buyer accepts your terms there themselves, waives their right of withdrawal when a product in the cart does not ship (a digital product), and pays. An agent cannot accept terms, waive a right or pay on the buyer's behalf.

The endpoint runs on your server, under your domain, with your publishable key. It speaks the [Model Context Protocol](https://modelcontextprotocol.io) (MCP), which AI agents use to call tools.

## Requirements

* A COPE publishable key (`cope_pk_...`).
* The success and cancel URLs you use for checkout, registered as redirect URLs in the COPE dashboard.
* A server that runs Node.js 20 or later, Bun, Deno, Cloudflare Workers or a Next.js route handler.

## Install

The endpoint ships in the Checkout SDK, published on npm as [`cope-sdk`](https://www.npmjs.com/package/cope-sdk), under the `cope-sdk/mcp` entry point.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
npm install cope-sdk @modelcontextprotocol/server zod
npm install @modelcontextprotocol/node   # only for node:http or Express
```

## Create a cart key

The endpoint gives agents an encrypted handle for each cart, so an agent never holds the cart's secret. Create a 32-byte key for that once, and store it as a server-side secret:

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { generateCartRefKey } from "cope-sdk/mcp"

console.log(generateCartRefKey())
```

Keep this key out of browsers and source control. Anyone holding it can open and change the carts its handles point to.

## Mount the endpoint

On Node.js:

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { createServer } from "node:http"
import { createCopeMcpNodeHandler } from "cope-sdk/mcp/node"

const mcp = createCopeMcpNodeHandler({
  publishableKey: process.env.COPE_PUBLISHABLE_KEY!,
  cartRefKeys: [process.env.COPE_CART_REF_KEY!],
  checkout: {
    successUrl: "https://shop.example.com/thanks",
    cancelUrl: "https://shop.example.com/cart",
  },
  allowedHosts: ["shop.example.com"],
})

createServer((req, res) => {
  if (req.url?.startsWith("/mcp")) return void mcp(req, res)
  res.statusCode = 404
  res.end()
}).listen(3000)
```

On Bun, Deno, Cloudflare Workers or a Next.js route handler, use the fetch handler instead. It does not need `@modelcontextprotocol/node`:

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { createCopeMcpHandler } from "cope-sdk/mcp"

const mcp = createCopeMcpHandler({
  publishableKey: process.env.COPE_PUBLISHABLE_KEY!,
  cartRefKeys: [process.env.COPE_CART_REF_KEY!],
  checkout: {
    successUrl: "https://shop.example.com/thanks",
    cancelUrl: "https://shop.example.com/cart",
  },
  allowedHosts: ["shop.example.com"],
})

export function POST(request: Request) {
  return mcp.fetch(request)
}
```

Agents then connect to `https://shop.example.com/mcp`.

## Options

| Option | Required | Meaning |
| - | - | - |
| `publishableKey` | Yes | Your `cope_pk_...` key |
| `cartRefKeys` | Yes | Cart keys, base64 or base64url, 32 bytes each. The first one encrypts and all of them decrypt |
| `checkout` | Yes | `successUrl` and `cancelUrl` for the checkout page. Both must be registered redirect URLs |
| `allowedHosts` | Yes | Host names the endpoint answers to. Requests for any other host are refused |
| `allowedOrigins` | No | Browser origins allowed to call the endpoint. Agents that call it from a server are not affected |
| `rateLimit` | No | `{ perClientPerMinute, perStorePerMinute }`, 120 and 1200 by default, or `false` if you limit requests yourself. Counted in memory, per process |
| `clientKey` | No | How to tell clients apart for the per-client limit, for example from a trusted proxy's header |

## Tools

| Tool | What the agent can do |
| - | - |
| `get_product` | Read a product and its prices. The payment methods it lists are those offered for a one-time purchase |
| `create_cart` | Start a cart. Returns a `cartRef` that the other cart tools take |
| `get_cart` | Read the cart with current prices. It also lists the payment methods the cart can be paid with, as COPE decides them: on a subscription, instalment or trial cart, only those that renew |
| `add_line`, `update_line`, `remove_line` | Change what is in the cart |
| `apply_promo_code`, `remove_promo_code` | Apply or remove a promo code |
| `set_buyer_identity` | Record who is buying and where: email, name, country, postal code, company and VAT ID. Country and postal code decide tax |
| `start_checkout` | Prepare the checkout and return the link for the buyer, with `buyerNext`: what the buyer does on the checkout page (accept the terms and, if the page asks them to, waive the right of withdrawal, which it can only for a cart with a product that does not ship) and that the agent cannot do it for them. Calling it again returns the same checkout |

When a tool fails, its result includes a `guidance` string telling the agent what to do next.

## Rotate the cart key

1. Add a new key at the front of `cartRefKeys` and deploy. New handles use it, and existing handles still work.
2. After your longest-lived carts have expired, remove the old key and deploy again.

## Give your AI tools these docs

If you build with an AI coding assistant, point it at these docs rather than pasting pages:

| Resource | URL |
| - | - |
| Page index for AI tools | `https://docs.cope.com/llms.txt` |
| Every page in one file | `https://docs.cope.com/llms-full.txt` |
| Docs search MCP server | `https://docs.cope.com/mcp` |

The MCP server searches these docs from any MCP client. It reads documentation only and needs no key.

## Security

* The endpoint uses only your publishable key, the same key your storefront already exposes.
* Terms, consents and payment happen on COPE's checkout page, in the buyer's own browser.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.