@greatstore/cli 0.2.13 → 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +2 -6
- package/dist/cli.js +101 -81
- package/dist/gs-skill/SKILL.md +6 -27
- package/dist/gs-skill/references/apps.md +3 -1
- package/dist/gs-skill/references/embed-api/call-tool.md +57 -0
- package/dist/gs-skill/references/embed-api/enable-notifications.md +13 -0
- package/dist/gs-skill/references/embed-api/events.md +13 -0
- package/dist/gs-skill/references/embed-api/generate-structured-content.md +15 -0
- package/dist/gs-skill/references/embed-api/panel.md +33 -0
- package/dist/gs-skill/references/embed-api/send-message.md +20 -0
- package/dist/gs-skill/references/embed-api/update-model-context.md +27 -0
- package/dist/gs-skill/references/embed-api/voice-call.md +40 -0
- package/dist/gs-skill/references/embed-api/webmcp.md +124 -0
- package/dist/gs-skill/references/embed-api.md +14 -289
- package/dist/gs-skill/references/store-admin.md +8 -2
- package/package.json +1 -1
- package/dist/gs-skill/recipes/ask-about-this.md +0 -31
- package/dist/gs-skill/recipes/campaign-hero.md +0 -48
- package/dist/gs-skill/recipes/comparison-table.md +0 -80
- package/dist/gs-skill/recipes/complete-the-look.md +0 -59
- package/dist/gs-skill/recipes/gift-finder-funnel.md +0 -88
- package/dist/gs-skill/recipes/launchers.md +0 -109
- package/dist/gs-skill/recipes/page-action-suite.md +0 -87
- package/dist/gs-skill/recipes/product-faq.md +0 -58
package/dist/gs-skill/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: greatstore
|
|
3
|
-
description: Build AI-powered shopping experiences with GreatStore on a merchant's website and store. Use when installing the GreatStore chat widget on a site, generating AI content for custom UI with generateStructuredContent, adding chat entry points (sendMessage), letting the assistant act on the page via WebMCP page tools (document.modelContext.registerTool), authoring custom in-chat apps with the gs CLI, administering the store from the CLI (npx greatstore configure, npx greatstore connectors — origin allowlists, CSP hosts, MCP connectors), setting up web push re-engagement, or connecting AI agents to a store's MCP endpoints. Covers setup, schema design, caching behavior, app authoring, store administration
|
|
3
|
+
description: Build AI-powered shopping experiences with GreatStore on a merchant's website and store. Use when installing the GreatStore chat widget on a site, generating AI content for custom UI with generateStructuredContent, adding chat entry points (sendMessage), letting the assistant act on the page via WebMCP page tools (document.modelContext.registerTool), authoring custom in-chat apps with the gs CLI, administering the store from the CLI (npx greatstore configure, npx greatstore connectors — origin allowlists, CSP hosts, MCP connectors), setting up web push re-engagement, or connecting AI agents to a store's MCP endpoints. Covers setup, schema design, caching behavior, app authoring, and store administration.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Building with GreatStore
|
|
@@ -24,10 +24,10 @@ constraints) that the references spell out.
|
|
|
24
24
|
|---|---|---|
|
|
25
25
|
| Install the widget; control the panel; readiness, events, troubleshooting | `window.GreatStore` SDK | [references/embed-api.md](references/embed-api.md) |
|
|
26
26
|
| AI-generated, catalog-grounded content rendered in **your own HTML/CSS** (highlights, comparisons, FAQs, gift guides) | `generateStructuredContent(schema, prompt)` | [references/structured-content.md](references/structured-content.md) |
|
|
27
|
-
|
|
|
28
|
-
|
|
|
29
|
-
|
|
|
30
|
-
| Let the assistant **act on the page** (cart, navigation, filters) | WebMCP: `document.modelContext.registerTool(...)` | [references/embed-api.md](references/embed-api.md) |
|
|
27
|
+
| Contextual **conversation entry points** anywhere on the page | `sendMessage(text)`, `open()`, `?gs_chat=open` | [references/embed-api/send-message.md](references/embed-api/send-message.md) |
|
|
28
|
+
| Feed the assistant **invisible page/app state** (current product, cart, selected variant) | `updateModelContext(text)`; an app's `onUpdateModelContext` | [references/embed-api/update-model-context.md](references/embed-api/update-model-context.md), [references/apps.md](references/apps.md) |
|
|
29
|
+
| **Fetch store data into your own UI** from a connector's read-only tool, or open a full-screen app from a button | `callTool(name, args)` | [references/embed-api/call-tool.md](references/embed-api/call-tool.md) |
|
|
30
|
+
| Let the assistant **act on the page** (cart, navigation, filters) | WebMCP: `document.modelContext.registerTool(...)` | [references/embed-api/webmcp.md](references/embed-api/webmcp.md) |
|
|
31
31
|
| Custom **interactive UI inside the chat** (configurators, quizzes, size guides, booking forms) | apps shipped with the `gs` CLI | [references/apps.md](references/apps.md) |
|
|
32
32
|
| Run one of those apps **on your own page**, outside the conversation | `surfaces: ["embed"]` + the container snippet | [references/apps.md](references/apps.md) |
|
|
33
33
|
| Expand an app's image in an **on-brand full-screen lightbox** | an app's `onShowLightbox({ src, originRect })` | [references/apps.md](references/apps.md) |
|
|
@@ -51,26 +51,6 @@ constraints) that the references spell out.
|
|
|
51
51
|
- Every SDK call is safe immediately after the script tag — pre-mount calls
|
|
52
52
|
queue and replay in order, and the SDK pre-warms itself in the background.
|
|
53
53
|
|
|
54
|
-
## Recipes
|
|
55
|
-
|
|
56
|
-
Complete, framework-free implementations, one per file. Shared conventions:
|
|
57
|
-
containers start `hidden` and reveal only on success (a failed generation
|
|
58
|
-
changes nothing); generated strings render via `textContent`, never
|
|
59
|
-
`innerHTML`; every recipe guards on `window.GreatStore`; prompts stay
|
|
60
|
-
deterministic per page so repeat renders hit the cache.
|
|
61
|
-
Inline real product/page data into prompts where the platform exposes it.
|
|
62
|
-
|
|
63
|
-
| Recipe | What it builds | Use it for |
|
|
64
|
-
|---|---|---|
|
|
65
|
-
| [GreatStore launchers](recipes/launchers.md) | Horizontally scrollable AI-generated chips — engaging first-person questions about the current page; tap to ask the assistant. | Instant engagement on any page type — product, collection, blog, home. Simple yet effective; start here. |
|
|
66
|
-
| [Ask-about-this entry points](recipes/ask-about-this.md) | One-line `sendMessage` buttons wired to existing page elements. | Size guides, shipping rows, out-of-stock badges — anywhere a shopper hesitates. |
|
|
67
|
-
| [Product FAQ accordion](recipes/product-faq.md) | Grounded pre-purchase Q&A with an "ask us" handoff into chat. | Product pages; answering objections before they cost the sale. |
|
|
68
|
-
| [Comparison table](recipes/comparison-table.md) | AI-picked representative products compared on category-relevant criteria. | Collection pages where shoppers weigh options. |
|
|
69
|
-
| [Complete the look](recipes/complete-the-look.md) | Catalog-grounded cross-sell strip with a reason per pick. | Product pages; raising order value with genuine pairings. |
|
|
70
|
-
| [Campaign hero](recipes/campaign-hero.md) | Seasonal homepage hero copy, cache-keyed to the ISO week. | Fresh homepage/campaign copy without manual rewrites. |
|
|
71
|
-
| [Gift finder funnel](recipes/gift-finder-funnel.md) | Quiz teaser → chat handoff → page-tool navigation; the full funnel. | Gifting seasons, guided discovery, homepage engagement. |
|
|
72
|
-
| [Page-action suite](recipes/page-action-suite.md) | WebMCP cart/page tools every conversation can use. | Any site where the assistant should act, not just advise. |
|
|
73
|
-
|
|
74
54
|
## How the facets combine
|
|
75
55
|
|
|
76
56
|
The strongest pattern is the **teaser → conversation → action** funnel:
|
|
@@ -79,5 +59,4 @@ design; each option's click handler calls `sendMessage` with the shopper's
|
|
|
79
59
|
choice, dropping them into a conversation with momentum; WebMCP page tools
|
|
80
60
|
and apps let that conversation actually do things — add to
|
|
81
61
|
cart, configure a product, book a slot — so it ends in a conversion, not a
|
|
82
|
-
copy-paste.
|
|
83
|
-
recipe is this funnel end to end.
|
|
62
|
+
copy-paste.
|
|
@@ -181,6 +181,8 @@ name yours for the distinct thing it does (`show_catalog_inline` alongside
|
|
|
181
181
|
cleared by the next user turn or an explicit close.
|
|
182
182
|
- `fullscreen` — takes over the full preview surface; persists until the
|
|
183
183
|
next widget-emitting tool call or an explicit close.
|
|
184
|
+
The page can also open one itself with `window.GreatStore.callTool(name,
|
|
185
|
+
props)` (see [embed-api/call-tool.md](embed-api/call-tool.md)).
|
|
184
186
|
|
|
185
187
|
**Safe-zone for `fullscreen` apps.** The floating chat widget sits over the
|
|
186
188
|
bottom of a fullscreen app, covering whatever is rendered there. Leave
|
|
@@ -392,6 +394,6 @@ export default async function Results(props: Props) {
|
|
|
392
394
|
injected `onGenerateStructuredContent` prop instead — same idea, but
|
|
393
395
|
personalized to the shopper and available on every surface without the global.
|
|
394
396
|
- Letting the assistant **act on the page** → WebMCP page tools
|
|
395
|
-
([embed-api.md](embed-api.md)).
|
|
397
|
+
([embed-api/webmcp.md](embed-api/webmcp.md)).
|
|
396
398
|
- Rich, interactive UI **inside the conversation itself**, available on the
|
|
397
399
|
storefront and every embed without page changes → an app.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# `callTool(name: string, args?: object): Promise<object | undefined>`
|
|
2
|
+
|
|
3
|
+
The page's own way to reach two things by name, without the shopper asking
|
|
4
|
+
and without touching the conversation (nothing is added to the chat and the
|
|
5
|
+
assistant never learns the call happened).
|
|
6
|
+
|
|
7
|
+
**A full-screen app.** Name one of the store's `fullscreen` apps and pass the
|
|
8
|
+
props its `inputSchema` declares. The app shows straight away, whether or
|
|
9
|
+
not the chat is open (an open chat collapses out of its way), and stays until
|
|
10
|
+
the shopper closes it or the page opens another; the call resolves with
|
|
11
|
+
`undefined` once it is handed over. No `open()` is needed. The app is the
|
|
12
|
+
page's, not the conversation's — a reload drops it. `inline` and
|
|
13
|
+
`over-input` apps can't be opened this way.
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
window.GreatStore.callTool("show_size_guide", { category: "boots" });
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**A read-only connector tool.** Name any read-only tool from the store's
|
|
20
|
+
connectors (Maker MCP or the merchant's own MCP servers) and it resolves
|
|
21
|
+
with the tool's MCP `tools/call` result exactly as the server returned it:
|
|
22
|
+
`content`, plus `structuredContent` when the tool sends one. Use it to fill
|
|
23
|
+
the page's own UI with the same data the assistant sees.
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const result = await window.GreatStore.callTool("search_catalog", {
|
|
27
|
+
query: "waterproof boots",
|
|
28
|
+
});
|
|
29
|
+
const products = result.structuredContent?.products ?? [];
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The shape of `args` and of the result is the connector's own, so look it up
|
|
33
|
+
rather than guessing a tool name or an argument:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
npx greatstore connectors --store my-store # the store's connectors (Maker MCP is "maker")
|
|
37
|
+
npx greatstore connectors tools maker --store my-store # every tool: group, access, description, inputs
|
|
38
|
+
npx greatstore connectors tools maker search_catalog --store my-store # one tool in full
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`connectors tools` says for each tool whether `callTool` reaches it, and why
|
|
42
|
+
not when it doesn't. Trust that line over your own reading of the tool. It
|
|
43
|
+
also spells out every input: type, whether it's required, allowed values,
|
|
44
|
+
limits and description, nested fields included. Add `--json` for the raw input
|
|
45
|
+
schema. In short, only read-only tools the assistant itself can call are
|
|
46
|
+
reachable. A tool that changes state (cart, checkout) is refused, and so are
|
|
47
|
+
WebMCP page tools (the page already has those). When an app and a tool share
|
|
48
|
+
a name, the app wins. Before writing code against the
|
|
49
|
+
result, call the tool once from the browser console and read what comes back:
|
|
50
|
+
the result's shape is the connector's too, and the listing doesn't describe
|
|
51
|
+
it.
|
|
52
|
+
|
|
53
|
+
It rejects with an `Error` when the name is neither, when an app isn't
|
|
54
|
+
full-screen or the args don't match its schema, when the tool is not
|
|
55
|
+
read-only, when the tool answers with an error (the message is the tool's
|
|
56
|
+
own text), or when the server can't be reached. Loads the chat first if it
|
|
57
|
+
isn't up yet.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# `enableNotifications(): Promise<{ ok: boolean }>`
|
|
2
|
+
|
|
3
|
+
Opts this browser into Web Push notifications from the store. Requirements:
|
|
4
|
+
|
|
5
|
+
- Must be called from a user gesture (e.g. a click handler).
|
|
6
|
+
- The site must host GreatStore's `gs.js` service-worker file. By default the
|
|
7
|
+
SDK looks for it at `/gs.js`; if it's hosted elsewhere, point to it via an
|
|
8
|
+
attribute on the embed script tag:
|
|
9
|
+
`<script src="…/embed.js" data-push-sw-path="/path/to/gs.js"></script>`.
|
|
10
|
+
|
|
11
|
+
Resolves `{ ok: true }` on success and `{ ok: false }` on any failure
|
|
12
|
+
(unsupported browser, no service worker hosted, permission denied). It never
|
|
13
|
+
rejects.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# `on(event: string, handler: (...args) => void): () => void`
|
|
2
|
+
|
|
3
|
+
Subscribe to SDK events. Returns an unsubscribe function. Listeners attached
|
|
4
|
+
before mount are queued and wired up at mount. Handler exceptions are caught
|
|
5
|
+
and reported — they won't break the chat.
|
|
6
|
+
|
|
7
|
+
Events emitted:
|
|
8
|
+
|
|
9
|
+
| Event | Fired when |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `"open"` | Panel transitions closed → open (including via `sendMessage` or the shopper's own click). |
|
|
12
|
+
| `"close"` | Panel transitions open → closed. |
|
|
13
|
+
| `"voicestatus"` | A voice call's status changes; the handler receives the status. See [voice-call.md](voice-call.md). |
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# `generateStructuredContent(schema: object, prompt: string): Promise<unknown>`
|
|
2
|
+
|
|
3
|
+
Generates JSON matching `schema` from `prompt`, grounded in the store's live
|
|
4
|
+
catalog. Resolves to the generated data object itself. Rejects with `Error`
|
|
5
|
+
on any failure (invalid input, decline, validation failure, rate limit,
|
|
6
|
+
network); a decline sets `error.reason`. See
|
|
7
|
+
[structured-content.md](../structured-content.md) for the full
|
|
8
|
+
contract, schema support, caching, and error semantics.
|
|
9
|
+
|
|
10
|
+
Accepts either a plain JSON Schema object or any object exposing a
|
|
11
|
+
`.toJSONSchema()` method (e.g. Zod schemas) — the conversion is called for
|
|
12
|
+
you.
|
|
13
|
+
|
|
14
|
+
Throws synchronously (rejects) if `prompt` is not a non-empty string or
|
|
15
|
+
`schema` is not an object.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Panel: `load` / `open` / `close` / `toggle` / `destroy`
|
|
2
|
+
|
|
3
|
+
## `load(): void`
|
|
4
|
+
|
|
5
|
+
Pre-warms the chat bundle and identity in the background without opening the
|
|
6
|
+
panel. Called automatically when `embed.js` runs, so you rarely need it.
|
|
7
|
+
Idempotent.
|
|
8
|
+
|
|
9
|
+
## `open(): void` / `close(): void` / `toggle(): void`
|
|
10
|
+
|
|
11
|
+
Open, close, or toggle the chat. It is a drawer rising from the bottom of
|
|
12
|
+
the screen — a bounded, floating card on desktop, full-height under 768px
|
|
13
|
+
viewport width. All three queue if called before mount.
|
|
14
|
+
|
|
15
|
+
## `destroy(): void`
|
|
16
|
+
|
|
17
|
+
Fully tears down a mounted panel: unmounts the chat UI, closes any open voice
|
|
18
|
+
connection, and removes the embed's DOM/listeners from the page. Use this
|
|
19
|
+
when your page is done with the embed for good — e.g. a single-page app
|
|
20
|
+
navigating away from the only route that should show it.
|
|
21
|
+
|
|
22
|
+
No-ops (with a console warning) if nothing is mounted. The cached identity
|
|
23
|
+
and downloaded chat bundle are kept, so a later `load()` (or any call that
|
|
24
|
+
triggers a mount, like `open()`) mounts a fresh panel without a network
|
|
25
|
+
round-trip for either. `greatstore:ready` fires again once the next mount
|
|
26
|
+
completes:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
window.GreatStore.destroy();
|
|
30
|
+
// ...later, on the page/route where the embed should come back:
|
|
31
|
+
window.addEventListener("greatstore:ready", onRemounted, { once: true });
|
|
32
|
+
window.GreatStore.load();
|
|
33
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# `sendMessage(text: string): void`
|
|
2
|
+
|
|
3
|
+
Sends `text` as the shopper's own visible chat message and **opens the panel
|
|
4
|
+
if it's closed**. The text is trimmed; empty or whitespace-only strings are
|
|
5
|
+
silently dropped. Queues if called before mount.
|
|
6
|
+
|
|
7
|
+
This is the highest-leverage one-liner in the SDK: any element on the page
|
|
8
|
+
can become a conversation entry point with context baked into the question.
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
document.querySelector("#ask-fit").addEventListener("click", () => {
|
|
12
|
+
window.GreatStore.sendMessage(
|
|
13
|
+
`I'm looking at "${productName}" — how does the sizing run?`
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Because the message renders as if the shopper typed it, write it in the
|
|
19
|
+
shopper's voice. It is not a hidden-context channel — don't stuff it with
|
|
20
|
+
invisible instructions or data dumps. For that, use `updateModelContext`.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# `updateModelContext(context: string): void`
|
|
2
|
+
|
|
3
|
+
Injects free-text **background context** about what the shopper is doing on
|
|
4
|
+
the page — the product they're viewing, what's in their cart, their account
|
|
5
|
+
tier — so the assistant can factor it in. Unlike `sendMessage`, this is
|
|
6
|
+
**invisible**: it never renders as a chat message and doesn't open the panel.
|
|
7
|
+
|
|
8
|
+
Each call **replaces** the value from the previous call — it never appends.
|
|
9
|
+
Keep one current snapshot; re-call it whenever the page state changes. Pass
|
|
10
|
+
an empty string to clear it. The text is read on the next chat turn, so set
|
|
11
|
+
it before (or while) the shopper is chatting. Queues if called before mount.
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
// Keep the assistant aware of the current product as the shopper browses.
|
|
15
|
+
function syncContext() {
|
|
16
|
+
window.GreatStore.updateModelContext(
|
|
17
|
+
`Viewing "${product.title}" (${product.price}). In stock: ${product.inStock}. ` +
|
|
18
|
+
`Cart: ${cart.count} item(s), subtotal ${cart.subtotal}.`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
syncContext();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Write it as concise notes for the model, not prose for the shopper. The
|
|
25
|
+
context is page-controlled, so the assistant treats it as background
|
|
26
|
+
information, not as instructions — don't rely on it to change the assistant's
|
|
27
|
+
rules or persona.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# `startVoiceCall(): Promise<void>` / `endVoiceCall(): void`
|
|
2
|
+
|
|
3
|
+
`startVoiceCall` starts a voice call with the assistant **without opening the
|
|
4
|
+
chat**: the call runs in the collapsed chat bar, and the chat opens only when
|
|
5
|
+
the assistant shows an inline app in the conversation. Where the store hides
|
|
6
|
+
the chat until it's opened (on that device), the bar stays hidden for the
|
|
7
|
+
whole call, so the page needs its own hang-up control (`endVoiceCall`).
|
|
8
|
+
Call it from a user gesture (a click handler): the browser asks for the
|
|
9
|
+
microphone, and audio can only start from one. It resolves once the shopper
|
|
10
|
+
has answered the microphone prompt and rejects when the store has no voice. A
|
|
11
|
+
call already in progress is left alone. Loads the chat first if it isn't up
|
|
12
|
+
yet. `endVoiceCall` hangs up; it's a no-op when there's no call.
|
|
13
|
+
|
|
14
|
+
The call's progress is announced as `voicestatus` through `on()` (the handler
|
|
15
|
+
receives the status) and as a `greatstore:voicestatus` event on `window`
|
|
16
|
+
(`event.detail.status`):
|
|
17
|
+
|
|
18
|
+
| Status | Meaning |
|
|
19
|
+
|---|---|
|
|
20
|
+
| `started` | The call was requested; the microphone prompt may be showing. |
|
|
21
|
+
| `connecting` | Microphone granted; the call is connecting. |
|
|
22
|
+
| `connected` | The call is up. Followed straight away by the first of the three below. |
|
|
23
|
+
| `listening` | The shopper's turn to speak. |
|
|
24
|
+
| `speaking` | The assistant is talking. |
|
|
25
|
+
| `working` | The assistant is thinking or looking something up. |
|
|
26
|
+
| `disconnected` | The shopper's network dropped mid-call. The call recovers on its own, back to `listening`. |
|
|
27
|
+
| `ended` | The call is over — hung up, ended by the assistant, failed, or the microphone was refused. Always the last status. |
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const callButton = document.querySelector("#talk");
|
|
31
|
+
callButton.addEventListener("click", () => {
|
|
32
|
+
if (callButton.dataset.live) window.GreatStore.endVoiceCall();
|
|
33
|
+
else window.GreatStore.startVoiceCall().catch(() => (callButton.hidden = true));
|
|
34
|
+
});
|
|
35
|
+
window.addEventListener("greatstore:voicestatus", (event) => {
|
|
36
|
+
const live = event.detail.status !== "ended";
|
|
37
|
+
callButton.dataset.live = live ? "1" : "";
|
|
38
|
+
callButton.textContent = live ? "Hang up" : "Talk to us";
|
|
39
|
+
});
|
|
40
|
+
```
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Page tools — WebMCP (`document.modelContext`)
|
|
2
|
+
|
|
3
|
+
The recommended way to expose page capabilities to the assistant is the
|
|
4
|
+
WebMCP standard. The GreatStore assistant uses every tool registered on
|
|
5
|
+
`document.modelContext` **once GreatStore is ready** (see [Availability](#availability)
|
|
6
|
+
below), re-reading the list on each conversational turn — so tools
|
|
7
|
+
registered mid-session appear on the next message without a reload. Tools
|
|
8
|
+
registered before GreatStore is ready are ignored.
|
|
9
|
+
|
|
10
|
+
## Availability
|
|
11
|
+
|
|
12
|
+
If the browser implements WebMCP natively, `document.modelContext` is just
|
|
13
|
+
there. Otherwise the SDK installs a minimal fallback the instant `embed.js`
|
|
14
|
+
starts running — synchronously, no async gap — so `document.modelContext`
|
|
15
|
+
is normally available immediately. The embed script tag deliberately has no
|
|
16
|
+
`async`/`defer` — a script with neither blocks parsing and runs at its own
|
|
17
|
+
position, which matters if the page also has another script (a nav widget,
|
|
18
|
+
an analytics tag) registering its own WebMCP tools: whichever executes
|
|
19
|
+
first wins that registration, and only a synchronous, unconditionally-first
|
|
20
|
+
script gives GreatStore's fallback shim a real chance of installing before
|
|
21
|
+
one of those calls happens. Your own code
|
|
22
|
+
can still run on either side of `embed.js` — before it has executed at all
|
|
23
|
+
(`window.GreatStore` doesn't exist yet), or long after it has mounted (a lazily rendered component, a
|
|
24
|
+
tag manager). Listening for `greatstore:ready` covers both: a listener
|
|
25
|
+
attached after GreatStore is already ready still receives the event, once.
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
window.addEventListener(
|
|
29
|
+
"greatstore:ready",
|
|
30
|
+
() => {
|
|
31
|
+
document.modelContext.registerTool(/* … */);
|
|
32
|
+
},
|
|
33
|
+
{ once: true },
|
|
34
|
+
);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
(`navigator.modelContext` is a deprecated alias for the same object; use
|
|
38
|
+
`document.modelContext` in new code.)
|
|
39
|
+
|
|
40
|
+
## `registerTool(tool, options?)`
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
document.modelContext.registerTool(
|
|
44
|
+
{
|
|
45
|
+
name: string, // required, non-empty, unique on the page
|
|
46
|
+
description: string, // required — how the AI decides when to call it
|
|
47
|
+
inputSchema?: object, // JSON Schema for execute's args;
|
|
48
|
+
// defaults to { type: "object", properties: {} }
|
|
49
|
+
execute(args): Result | Promise<Result>,
|
|
50
|
+
},
|
|
51
|
+
options?: { signal?: AbortSignal }, // abort to unregister
|
|
52
|
+
);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
- **Result shape**: `execute` returns MCP content blocks —
|
|
56
|
+
`{ content: [{ type: "text", text: "…" }] }`. For structured data,
|
|
57
|
+
`JSON.stringify` it into `text`. Add `isError: true` to mark a handled
|
|
58
|
+
failure.
|
|
59
|
+
- **Errors**: a thrown error or rejected promise is delivered to the
|
|
60
|
+
assistant as a *failed* tool call carrying the error message — the
|
|
61
|
+
assistant can explain or adapt. Errors never escape into your page.
|
|
62
|
+
- **Duplicate names throw.** To replace a tool, abort its registration first.
|
|
63
|
+
- **Unregistration is `AbortSignal`-driven**: pass `{ signal }` and call
|
|
64
|
+
`abort()` when the tool's context goes away (SPA navigation, modal close).
|
|
65
|
+
A pre-aborted signal skips registration. (A legacy
|
|
66
|
+
`unregisterTool(name)` exists but is deprecated in the spec.)
|
|
67
|
+
- **Treat `args` as untrusted input**: values are AI-generated. Validate
|
|
68
|
+
before passing to your own APIs, and never `eval` anything from them.
|
|
69
|
+
|
|
70
|
+
A complete tool, registered once GreatStore is ready:
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
window.addEventListener("greatstore:ready", () => {
|
|
74
|
+
document.modelContext.registerTool({
|
|
75
|
+
name: "add_to_cart",
|
|
76
|
+
description:
|
|
77
|
+
"Add a product variant to the shopper's cart on this site. " +
|
|
78
|
+
"Use when the shopper asks to add, buy, or get a product.",
|
|
79
|
+
inputSchema: {
|
|
80
|
+
type: "object",
|
|
81
|
+
properties: {
|
|
82
|
+
variantId: { type: "string" },
|
|
83
|
+
quantity: { type: "integer", minimum: 1 },
|
|
84
|
+
},
|
|
85
|
+
required: ["variantId"],
|
|
86
|
+
},
|
|
87
|
+
async execute({ variantId, quantity }) {
|
|
88
|
+
const res = await fetch("/cart/add.js", {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: { "Content-Type": "application/json" },
|
|
91
|
+
body: JSON.stringify({ id: variantId, quantity: quantity ?? 1 }),
|
|
92
|
+
});
|
|
93
|
+
if (!res.ok) throw new Error(`Cart add failed (${res.status})`);
|
|
94
|
+
const cart = await res.json();
|
|
95
|
+
return { content: [{ type: "text", text: JSON.stringify(cart) }] };
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}, { once: true });
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Returning the fresh cart state after the mutation lets the assistant confirm
|
|
102
|
+
accurately. Good tool families: cart (`get_cart`, `add_to_cart`), navigation
|
|
103
|
+
(`go_to_page`), page state (`get_current_product`, `apply_filters`), UI
|
|
104
|
+
(`highlight_section`, `scroll_to_reviews`).
|
|
105
|
+
|
|
106
|
+
And a context-scoped tool, unregistered via `AbortSignal`:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
const ac = new AbortController();
|
|
110
|
+
document.modelContext.registerTool(
|
|
111
|
+
{
|
|
112
|
+
name: "get_product_reviews",
|
|
113
|
+
description: "Read the reviews shown on the current product page.",
|
|
114
|
+
inputSchema: { type: "object", properties: {} },
|
|
115
|
+
execute: () => ({
|
|
116
|
+
content: [{ type: "text", text: JSON.stringify(collectReviews()) }],
|
|
117
|
+
}),
|
|
118
|
+
},
|
|
119
|
+
{ signal: ac.signal },
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
// On SPA route change away from the product page:
|
|
123
|
+
ac.abort();
|
|
124
|
+
```
|