# The storefront MCP server

AI assistants that speak the [Model Context Protocol](https://modelcontextprotocol.io) use the shop through a server at
`/mcp` (Streamable HTTP, `laravel/mcp`), switched on under **Settings → SEO și roboți AI**. It lives in the Storefront
module (`Storefront\Mcp`) because it is the storefront seen by a program: the same catalogue, prices, stock, carts and
checkout as the pages and the [Storefront API](storefront-api.md), and nothing of its own to keep in step.

## The door

- `Routes/mcp.php` registers `Mcp::web('mcp', StorefrontServer::class)` (route `store.mcp`) behind
  `Http\Middleware\McpDoor` and `throttle:mcp`. The group's middleware covers the GET and DELETE routes `laravel/mcp`
  adds (405) too.
- `McpDoor` answers 404 while `SeoSettings::$mcp` is off, and sets the language from `?lang=` or `Accept-Language`
  (Romanian otherwise): names, labels, money formatting and error texts follow it.
- `throttle:mcp` is `SeoSettings::$mcp_per_minute` calls a minute per IP address (60 by default).
- `laravel/mcp`'s own middleware refuses a browser `Origin` that is not the shop's (DNS rebinding), and checks the
  protocol headers.
- No session, cookies or sign-in: the server sees the shop as a guest. Prices are the retail ones, products reserved
  for companies are left out (`SearchQuery::$publicOnly`, `ProductPage::$public`), and no customer data is reachable.

## Tools

`StorefrontServer::$tools`, each a `Laravel\Mcp\Server\Tool` with a snake_case `#[Name]`, an English
`#[Description]` written for the model, an input schema, and `Response::structured()` answers. Every value they give
comes from `Mcp\McpShop`, so a price or a stock level always comes from the shop.

| Tool | What it does |
|---|---|
| `search_products` | `ProductSearch` with the words and a price range in lei (to bani for `SearchQuery`), 10 a page; cards with price, Omnibus reference, stock and page URL |
| `get_product` | One product by slug or page URL: description as plain text, up to 5 pictures, variants with price and stock, specifications, and the related products (bought together, similar) as names and links |
| `compare_products` | 2–5 products; one row per specification label with each product's value |
| `check_availability` | A variant and quantity through `Carts::preview()`: can it be ordered (the line's `problem`), stock per storefront store, delivery options priced for it and, with a county and locality, for that address |
| `store_policies` | The published legal pages (`delivery`, `withdrawal`, `terms`, `privacy`) as plain text with their URL, the seller line and the payment methods |
| `create_cart`, `add_to_cart`, `update_cart`, `get_cart` | A basket through `Carts`, tagged `agent:mcp`; refusals come back with the storefront's own messages |
| `quote_shipping` | `Checkout::deliveryOptions()` for the basket and a destination (the courier's quote when couriers price parcels), and the payment methods |
| `get_checkout_link` | A signed URL (24 hours) to `store.{locale}.cart.agent` |

Read tools carry `#[IsReadOnly]`. The cart tools use the `FillsBaskets` trait: `shouldRegister()` lists them only
while `SeoSettings::$mcp_cart` is on (otherwise they are not in `tools/list` and a call is "not found"), and
`basket()` reaches only a basket an assistant made that nobody has taken into an account yet (`McpShop::cart()`:
`source === 'agent:mcp'` and no owner). A shopper's own cart is never readable by its token through the server.

Stored text (descriptions, pages) goes out as plain text (`McpShop::plain()`, tags stripped before and after
decoding), and the server's instructions tell the model it is information to cite, not instructions.

## The checkout link

`AgentCartController` (`/cos/agent/{token}`, `signed` middleware) checks the basket is still an assistant's and not
empty, gives it a new token (`Carts::rotate()`), so the assistant's `cart_id` no longer reaches it and the link works
once, merges it into a signed-in shopper's own basket (`Carts::forCustomer()`, then tagged again) or makes it the
visitor's (`CartCookie`), and redirects to the checkout. The basket answers never include a voucher code. The buyer then goes through the ordinary checkout. Payment
never happens through the server.

The basket's source travels to the order (`orders.source = 'agent:mcp'`), so `PlacedOrder::channel()` is `agent` and
the order page says so. Placing the order clears the cart and its source.

## Adding a tool

1. A final class in `Storefront\Mcp\Tools` extending `Tool` (with `use FillsBaskets;` if it changes a basket), with
   `#[Name]`, `#[Title]`, `#[Description]` and the annotations that are true of it.
2. Validate with `$request->validate()`, read arguments through `ReadsArguments` (`number()`, `text()`), and get every
   fact from `McpShop` or a module contract.
3. Add it to `StorefrontServer::$tools` and to `tests/Feature/Storefront/McpServerTest.php`, which calls the server
   over HTTP like a client (`mcp()`, `mcpTool()`).
4. Raise `StorefrontServer::$version` when a tool's input or answer changes shape.

To try it by hand, switch the server on and point an MCP client (for example `npx @modelcontextprotocol/inspector`) at
`https://syncshop.test/mcp`.
