@relyon/kit 0.0.0-stage → 1.0.0

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.
Files changed (225) hide show
  1. package/AGENTS.md +75 -0
  2. package/CHANGELOG.md +1053 -0
  3. package/LICENSE +21 -0
  4. package/QUICKSTART.md +239 -0
  5. package/README.md +117 -2
  6. package/dist/adapters/express.d.ts +39 -0
  7. package/dist/adapters/express.js +106 -0
  8. package/dist/adapters/hono.d.ts +16 -0
  9. package/dist/adapters/hono.js +15 -0
  10. package/dist/adapters/next.d.ts +29 -0
  11. package/dist/adapters/next.js +38 -0
  12. package/dist/agent-card.d.ts +14 -0
  13. package/dist/agent-card.js +58 -0
  14. package/dist/agent-docs.d.ts +36 -0
  15. package/dist/agent-docs.js +78 -0
  16. package/dist/agent-hooks.d.ts +66 -0
  17. package/dist/agent-hooks.js +280 -0
  18. package/dist/agents.d.ts +24 -0
  19. package/dist/agents.js +120 -0
  20. package/dist/api-key.d.ts +6 -0
  21. package/dist/api-key.js +33 -0
  22. package/dist/cache.d.ts +46 -0
  23. package/dist/cache.js +86 -0
  24. package/dist/cart-tokens.d.ts +31 -0
  25. package/dist/cart-tokens.js +58 -0
  26. package/dist/cart-transfer.d.ts +95 -0
  27. package/dist/cart-transfer.js +387 -0
  28. package/dist/catalog-enrichment.d.ts +6 -0
  29. package/dist/catalog-enrichment.js +71 -0
  30. package/dist/catalog-reads.d.ts +126 -0
  31. package/dist/catalog-reads.js +104 -0
  32. package/dist/catalog.d.ts +78 -0
  33. package/dist/catalog.js +288 -0
  34. package/dist/charge-claims.d.ts +11 -0
  35. package/dist/charge-claims.js +47 -0
  36. package/dist/check.d.ts +44 -0
  37. package/dist/check.js +152 -0
  38. package/dist/checklist-manifest.d.ts +72 -0
  39. package/dist/checklist-manifest.js +341 -0
  40. package/dist/checkout.d.ts +222 -0
  41. package/dist/checkout.js +353 -0
  42. package/dist/cli.d.ts +2 -0
  43. package/dist/cli.js +727 -0
  44. package/dist/confirmation.d.ts +78 -0
  45. package/dist/confirmation.js +124 -0
  46. package/dist/context.d.ts +81 -0
  47. package/dist/context.js +10 -0
  48. package/dist/discovery.d.ts +151 -0
  49. package/dist/discovery.js +346 -0
  50. package/dist/doctor-cart-transfer.d.ts +33 -0
  51. package/dist/doctor-cart-transfer.js +186 -0
  52. package/dist/doctor-catalog.d.ts +8 -0
  53. package/dist/doctor-catalog.js +47 -0
  54. package/dist/doctor-evidence.d.ts +22 -0
  55. package/dist/doctor-evidence.js +57 -0
  56. package/dist/doctor-merchant.d.ts +48 -0
  57. package/dist/doctor-merchant.js +74 -0
  58. package/dist/doctor-source.d.ts +5 -0
  59. package/dist/doctor-source.js +142 -0
  60. package/dist/doctor-variants.d.ts +70 -0
  61. package/dist/doctor-variants.js +102 -0
  62. package/dist/doctor.d.ts +215 -0
  63. package/dist/doctor.js +1689 -0
  64. package/dist/events.d.ts +95 -0
  65. package/dist/events.js +200 -0
  66. package/dist/feed-mapping.d.ts +114 -0
  67. package/dist/feed-mapping.js +152 -0
  68. package/dist/feed-values.d.ts +13 -0
  69. package/dist/feed-values.js +40 -0
  70. package/dist/feeds.d.ts +104 -0
  71. package/dist/feeds.js +366 -0
  72. package/dist/identify.d.ts +102 -0
  73. package/dist/identify.js +124 -0
  74. package/dist/identity.d.ts +37 -0
  75. package/dist/identity.js +54 -0
  76. package/dist/index.d.ts +42 -0
  77. package/dist/index.js +41 -0
  78. package/dist/init.d.ts +157 -0
  79. package/dist/init.js +912 -0
  80. package/dist/kit-config.d.ts +19 -0
  81. package/dist/kit-config.js +43 -0
  82. package/dist/kit-environment.d.ts +20 -0
  83. package/dist/kit-environment.js +32 -0
  84. package/dist/kit-updates.d.ts +47 -0
  85. package/dist/kit-updates.js +77 -0
  86. package/dist/kit-version-project.d.ts +18 -0
  87. package/dist/kit-version-project.js +214 -0
  88. package/dist/kit-version.d.ts +37 -0
  89. package/dist/kit-version.js +162 -0
  90. package/dist/kit.d.ts +516 -0
  91. package/dist/kit.js +1309 -0
  92. package/dist/layers/pages.d.ts +3 -0
  93. package/dist/layers/pages.js +5 -0
  94. package/dist/layers/payments.d.ts +7 -0
  95. package/dist/layers/payments.js +9 -0
  96. package/dist/layers/products.d.ts +10 -0
  97. package/dist/layers/products.js +13 -0
  98. package/dist/live.d.ts +13 -0
  99. package/dist/live.js +52 -0
  100. package/dist/mcp.d.ts +270 -0
  101. package/dist/mcp.js +312 -0
  102. package/dist/next-steps.d.ts +101 -0
  103. package/dist/next-steps.js +257 -0
  104. package/dist/order-pending.d.ts +57 -0
  105. package/dist/order-pending.js +127 -0
  106. package/dist/orders.d.ts +12 -0
  107. package/dist/orders.js +21 -0
  108. package/dist/payments-init.d.ts +19 -0
  109. package/dist/payments-init.js +70 -0
  110. package/dist/payments-routes.d.ts +54 -0
  111. package/dist/payments-routes.js +123 -0
  112. package/dist/payments.d.ts +165 -0
  113. package/dist/payments.js +192 -0
  114. package/dist/pnpm-workspace.d.ts +58 -0
  115. package/dist/pnpm-workspace.js +250 -0
  116. package/dist/policies.d.ts +111 -0
  117. package/dist/policies.js +317 -0
  118. package/dist/preview-hosts.d.ts +64 -0
  119. package/dist/preview-hosts.js +175 -0
  120. package/dist/private-config.d.ts +169 -0
  121. package/dist/private-config.js +820 -0
  122. package/dist/progress.d.ts +54 -0
  123. package/dist/progress.js +153 -0
  124. package/dist/protocol-inputs.d.ts +22 -0
  125. package/dist/protocol-inputs.js +151 -0
  126. package/dist/public-origin.d.ts +50 -0
  127. package/dist/public-origin.js +180 -0
  128. package/dist/refusals.d.ts +86 -0
  129. package/dist/refusals.js +157 -0
  130. package/dist/relay.d.ts +107 -0
  131. package/dist/relay.js +655 -0
  132. package/dist/release-notes.d.ts +2 -0
  133. package/dist/release-notes.js +290 -0
  134. package/dist/relyon-cart-tokens.d.ts +101 -0
  135. package/dist/relyon-cart-tokens.js +521 -0
  136. package/dist/relyon.d.ts +141 -0
  137. package/dist/relyon.js +238 -0
  138. package/dist/robots.d.ts +27 -0
  139. package/dist/robots.js +127 -0
  140. package/dist/rules.d.ts +144 -0
  141. package/dist/rules.js +347 -0
  142. package/dist/sdk/generated/public-api.d.ts +4816 -0
  143. package/dist/sdk/generated/public-api.js +1 -0
  144. package/dist/sdk/index.d.ts +24 -0
  145. package/dist/sdk/index.js +34 -0
  146. package/dist/sdk/signatures.d.ts +17 -0
  147. package/dist/sdk/signatures.js +33 -0
  148. package/dist/setup.d.ts +37 -0
  149. package/dist/setup.js +94 -0
  150. package/dist/signatures.d.ts +112 -0
  151. package/dist/signatures.js +443 -0
  152. package/dist/state.d.ts +185 -0
  153. package/dist/state.js +96 -0
  154. package/dist/store-payments.d.ts +48 -0
  155. package/dist/store-payments.js +143 -0
  156. package/dist/structured-fields.d.ts +26 -0
  157. package/dist/structured-fields.js +200 -0
  158. package/dist/templates.d.ts +19 -0
  159. package/dist/templates.js +193 -0
  160. package/dist/test-checkout-command.d.ts +32 -0
  161. package/dist/test-checkout-command.js +161 -0
  162. package/dist/test-checkout.d.ts +88 -0
  163. package/dist/test-checkout.js +292 -0
  164. package/dist/types.d.ts +103 -0
  165. package/dist/types.js +18 -0
  166. package/dist/ucp-versions.d.ts +57 -0
  167. package/dist/ucp-versions.js +129 -0
  168. package/dist/util.d.ts +23 -0
  169. package/dist/util.js +68 -0
  170. package/dist/variants.d.ts +67 -0
  171. package/dist/variants.js +146 -0
  172. package/dist/version.d.ts +9 -0
  173. package/dist/version.js +10 -0
  174. package/dist/waiting.d.ts +43 -0
  175. package/dist/waiting.js +57 -0
  176. package/dist/webhook-command.d.ts +48 -0
  177. package/dist/webhook-command.js +367 -0
  178. package/dist/webhook-registration.d.ts +47 -0
  179. package/dist/webhook-registration.js +69 -0
  180. package/dist/webmcp-schema.d.ts +14 -0
  181. package/dist/webmcp-schema.js +140 -0
  182. package/dist/webmcp-script.d.ts +3 -0
  183. package/dist/webmcp-script.js +176 -0
  184. package/dist/webmcp-store.d.ts +102 -0
  185. package/dist/webmcp-store.js +116 -0
  186. package/dist/webmcp.d.ts +242 -0
  187. package/dist/webmcp.js +866 -0
  188. package/dist/wire-names.d.ts +85 -0
  189. package/dist/wire-names.js +109 -0
  190. package/package.json +118 -4
  191. package/release-notes.json +290 -0
  192. package/scripts/install-notice.cjs +7 -0
  193. package/skills/relyon-agents/SKILL.md +327 -0
  194. package/skills/relyon-catalog/SKILL.md +499 -0
  195. package/skills/relyon-catalog/references/example-store.md +218 -0
  196. package/skills/relyon-checkout/SKILL.md +603 -0
  197. package/skills/relyon-checkout/references/checkout-flow.md +257 -0
  198. package/skills/relyon-checkout/references/checkout-page.md +315 -0
  199. package/skills/relyon-go-live/SKILL.md +344 -0
  200. package/skills/relyon-orders/SKILL.md +191 -0
  201. package/skills/relyon-orders/references/webhook-receiver.md +113 -0
  202. package/skills/relyon-payments/SKILL.md +117 -0
  203. package/skills/relyon-setup/SKILL.md +1061 -0
  204. package/skills/relyon-setup/references/QUICKSTART.md +239 -0
  205. package/skills/relyon-setup/references/durable-state.md +209 -0
  206. package/skills/relyon-setup/references/express-server.md +388 -0
  207. package/skills/relyon-setup/references/next-app-router.md +251 -0
  208. package/templates/common/STORE-README.md +67 -0
  209. package/templates/common/gitignore +8 -0
  210. package/templates/common/scripts/check-pricing.ts +22 -0
  211. package/templates/common/scripts/serve.mjs +13 -0
  212. package/templates/common/server/catalog.ts +79 -0
  213. package/templates/common/server/checkout-flow.d.ts +34 -0
  214. package/templates/common/server/pages.ts +184 -0
  215. package/templates/common/server/storage.ts +114 -0
  216. package/templates/common/server/store.ts +67 -0
  217. package/templates/express/server/express.ts +20 -0
  218. package/templates/express/tsconfig.build.json +6 -0
  219. package/templates/express/tsconfig.json +9 -0
  220. package/templates/next/app/[[...path]]/route.ts +24 -0
  221. package/templates/next/next.config.mjs +19 -0
  222. package/templates/next/tsconfig.json +12 -0
  223. package/templates/payments/express.ts +39 -0
  224. package/templates/payments/next.ts +38 -0
  225. package/templates/payments/node.ts +39 -0
package/dist/mcp.js ADDED
@@ -0,0 +1,312 @@
1
+ import { WAITING_STATUS } from "./waiting.js";
2
+ import { describeError } from "./relyon-cart-tokens.js";
3
+ import { decimalAmount } from "./discovery.js";
4
+ import { onPublicOrigin } from "./public-origin.js";
5
+ import { requestFacts } from "./events.js";
6
+ import { mcpInputs } from "./protocol-inputs.js";
7
+ import { existingCheckoutMessage, mcpErrorBody, paymentOutcomeUnknown, refusalMessage } from "./refusals.js";
8
+ import { orderPendingMcp } from "./order-pending.js";
9
+ import { orderPendingFor, relayStep } from "./relay.js";
10
+ import { isRecord, jsonResponse, randomId, tryParseJson } from "./util.js";
11
+ import { productVariants } from "./variants.js";
12
+ import { KIT_VERSION } from "./version.js";
13
+ const PROTOCOL_VERSIONS = ["2026-07-28", "2025-06-18", "2025-03-26"];
14
+ const MAX_SESSIONS = 5_000;
15
+ const LINES_SCHEMA = {
16
+ type: "array", minItems: 1, maxItems: 100,
17
+ items: {
18
+ type: "object", required: ["product_id", "quantity"], additionalProperties: false,
19
+ properties: {
20
+ product_id: { type: "string" }, quantity: { type: "integer", minimum: 1 },
21
+ options: { type: "object", description: "Choices for a configurable product." },
22
+ },
23
+ },
24
+ };
25
+ const ADDRESS_SCHEMA = {
26
+ type: "object", required: ["name", "line1", "city", "postal_code", "country"],
27
+ properties: {
28
+ name: { type: "string" }, line1: { type: "string" }, line2: { type: "string" }, city: { type: "string" },
29
+ region: { type: "string" }, postal_code: { type: "string" }, country: { type: "string", description: "ISO 3166-1 alpha-2" },
30
+ },
31
+ };
32
+ const CHECKOUT_ID = { checkout_id: { type: "string", description: "The checkout_id create_checkout returned." } };
33
+ export const MCP_TOOLS = [
34
+ {
35
+ name: "search_products", title: "Search products",
36
+ description: "Search this store's catalog. Prices are display prices; the checkout prices the cart.",
37
+ inputSchema: { type: "object", required: ["query"], properties: { query: { type: "string" }, limit: { type: "integer", minimum: 1, maximum: 50 } } },
38
+ annotations: { readOnlyHint: true },
39
+ },
40
+ {
41
+ name: "get_product", title: "Get product", description: "One product from this store's catalog.",
42
+ inputSchema: { type: "object", required: ["product_id"], properties: { product_id: { type: "string" } } },
43
+ annotations: { readOnlyHint: true },
44
+ },
45
+ {
46
+ name: "create_checkout", title: "Create checkout",
47
+ description: "Start a checkout; the store prices the cart. Checkout tools need this store's agent credential (Authorization: Bearer agt_…), issued by the merchant in the Relyon dashboard under AI agents → Credentials. A product option with only one value never has to be named.",
48
+ inputSchema: {
49
+ type: "object", required: ["lines"],
50
+ properties: { lines: LINES_SCHEMA, shipping_address: ADDRESS_SCHEMA, shipping_option_id: { type: "string" }, coupons: { type: "array", items: { type: "string" } } },
51
+ },
52
+ },
53
+ {
54
+ name: "update_checkout", title: "Update checkout", description: "Change the lines, address, shipping option or coupons.",
55
+ inputSchema: {
56
+ type: "object", required: ["checkout_id"],
57
+ properties: { ...CHECKOUT_ID, lines: LINES_SCHEMA, shipping_address: ADDRESS_SCHEMA, shipping_option_id: { type: "string" }, coupons: { type: "array", items: { type: "string" } } },
58
+ },
59
+ },
60
+ {
61
+ name: "get_checkout", title: "Get checkout", description: "Read a checkout.",
62
+ inputSchema: { type: "object", required: ["checkout_id"], properties: CHECKOUT_ID }, annotations: { readOnlyHint: true },
63
+ },
64
+ {
65
+ name: "complete_checkout", title: "Complete checkout", description: "Finish the checkout as shown.",
66
+ inputSchema: { type: "object", required: ["checkout_id"], properties: CHECKOUT_ID },
67
+ },
68
+ {
69
+ name: "cancel_checkout", title: "Cancel checkout", description: "Cancel a checkout.",
70
+ inputSchema: { type: "object", required: ["checkout_id"], properties: CHECKOUT_ID },
71
+ },
72
+ ];
73
+ const CHECKOUT_TOOLS = {
74
+ create_checkout: "create", update_checkout: "update", get_checkout: "get", complete_checkout: "complete", cancel_checkout: "cancel",
75
+ };
76
+ /** Whether agents may buy the item: not `agent.purchasable: false`, no restrictions, nothing they must ask first. */
77
+ function agentPurchasable(product) {
78
+ const agent = product.agent;
79
+ if (!agent)
80
+ return true;
81
+ return !(agent.purchasable === false || agent.restrictions || (Array.isArray(agent.must_ask) && agent.must_ask.length));
82
+ }
83
+ function productSummary(product) {
84
+ // Callers pass the product already moved onto the store's public address.
85
+ return {
86
+ product_id: product.merchant_product_id,
87
+ title: product.title,
88
+ ...(product.description ? { description: product.description } : {}),
89
+ ...(product.url ? { url: product.url } : {}),
90
+ ...(product.images?.length ? { image: product.images[0] } : {}),
91
+ ...(product.from_price ? {
92
+ price: { amount: product.from_price.amount, currency: product.from_price.currency, display: `${decimalAmount(product.from_price.amount, product.from_price.currency)} ${product.from_price.currency}` },
93
+ } : {}),
94
+ ...(product.availability ? { availability: product.availability } : {}),
95
+ configurable: product.configurable ?? false,
96
+ // false when agents can see but not buy the item (doctor skips it when picking an item to transfer).
97
+ purchasable: agentPurchasable(product),
98
+ // A configurable product's variants (feed ID, the store's options, price), so agents and doctor see each choice.
99
+ ...(product.configurable === true && productVariants(product).length ? {
100
+ variants: productVariants(product).slice(0, 100).map((variant) => ({
101
+ id: variant.id, options: variant.options,
102
+ ...(variant.amount !== undefined && typeof variant.currency === "string" ? { price: { amount: variant.amount, currency: variant.currency } } : {}),
103
+ ...(variant.availability ? { availability: variant.availability } : {}),
104
+ })),
105
+ } : {}),
106
+ ...(typeof product.group_id === "string" && product.group_id ? { group_id: product.group_id } : {}),
107
+ };
108
+ }
109
+ /**
110
+ * The store's MCP endpoint (Streamable HTTP, JSON responses). Browse tools
111
+ * answer from the store's last good catalog; checkout tools relay to Relyon
112
+ * with `protocol=mcp`. Tool refusals are in-band JSON-RPC errors (HTTP 200)
113
+ * with `data.error`.
114
+ */
115
+ export class McpEndpoint {
116
+ ctx;
117
+ store;
118
+ hooks;
119
+ clients = new Map();
120
+ constructor(ctx, store, hooks) {
121
+ this.ctx = ctx;
122
+ this.store = store;
123
+ this.hooks = hooks;
124
+ }
125
+ /** The MCP client a session announced in `initialize`, if any. */
126
+ clientFor(request) {
127
+ const session = request.headers.get("mcp-session-id");
128
+ return session ? this.clients.get(session) : undefined;
129
+ }
130
+ async handle(request, rawBody, info) {
131
+ if (request.method === "GET" || request.method === "DELETE") {
132
+ if (request.method === "DELETE") {
133
+ const session = request.headers.get("mcp-session-id");
134
+ if (session)
135
+ this.clients.delete(session);
136
+ return new Response(null, { status: 204 });
137
+ }
138
+ return new Response(null, { status: 405, headers: { allow: "POST, DELETE" } });
139
+ }
140
+ if (request.method !== "POST")
141
+ return new Response(null, { status: 405, headers: { allow: "POST, DELETE" } });
142
+ const message = tryParseJson(rawBody);
143
+ if (message === undefined)
144
+ return jsonResponse(200, { jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } });
145
+ if (Array.isArray(message))
146
+ return jsonResponse(200, { jsonrpc: "2.0", id: null, error: { code: -32600, message: "Batches are not supported." } });
147
+ if (!isRecord(message) || message.jsonrpc !== "2.0" || typeof message.method !== "string") {
148
+ return jsonResponse(200, { jsonrpc: "2.0", id: null, error: { code: -32600, message: "Invalid request" } });
149
+ }
150
+ const id = message.id;
151
+ const params = isRecord(message.params) ? message.params : {};
152
+ if (id === undefined)
153
+ return new Response(null, { status: 202 }); // a notification
154
+ switch (message.method) {
155
+ case "initialize": {
156
+ const requested = typeof params.protocolVersion === "string" ? params.protocolVersion : "";
157
+ const protocolVersion = PROTOCOL_VERSIONS.includes(requested) ? requested : PROTOCOL_VERSIONS[0];
158
+ const session = randomId("mcp-");
159
+ const clientInfo = isRecord(params.clientInfo) && typeof params.clientInfo.name === "string"
160
+ ? { name: params.clientInfo.name.slice(0, 200), ...(typeof params.clientInfo.version === "string" ? { version: params.clientInfo.version.slice(0, 100) } : {}) }
161
+ : { name: "unknown" };
162
+ this.clients.set(session, clientInfo);
163
+ while (this.clients.size > MAX_SESSIONS)
164
+ this.clients.delete(this.clients.keys().next().value);
165
+ return jsonResponse(200, {
166
+ jsonrpc: "2.0", id,
167
+ result: {
168
+ protocolVersion, capabilities: { tools: { listChanged: false } },
169
+ serverInfo: { name: this.store.name, version: KIT_VERSION },
170
+ ...(this.ctx.gateway.waitingForSandboxKey ? { _meta: { "relyon/status": WAITING_STATUS } } : {}),
171
+ instructions: this.ctx.gateway.waitingForSandboxKey
172
+ ? `Browse ${this.store.name}'s products. Checkout status: ${WAITING_STATUS}; checkout is still being set up, so no checkout tools are offered yet.`
173
+ : this.ctx.gateway.livemode
174
+ ? `Browse ${this.store.name}'s products. Store-hosted MCP checkout is not available (protocol_not_available).`
175
+ : `Browse ${this.store.name}'s products and check out (Test mode: no real charges). create_checkout prices the cart; complete_checkout hands the shopper a continue_url on the store's checkout page, where they pay and the order is created. Call get_checkout for a checkout's status; never start a second checkout for the same purchase.`,
176
+ },
177
+ }, { "mcp-session-id": session });
178
+ }
179
+ case "ping":
180
+ return jsonResponse(200, { jsonrpc: "2.0", id, result: {} });
181
+ case "tools/list":
182
+ // No checkout capability is advertised while the kit waits for a sandbox key.
183
+ return jsonResponse(200, { jsonrpc: "2.0", id, result: { tools: this.ctx.gateway.waitingForSandboxKey ? MCP_TOOLS.filter((tool) => !CHECKOUT_TOOLS[tool.name]) : MCP_TOOLS } });
184
+ case "tools/call":
185
+ return this.callTool(request, rawBody, info, id, params);
186
+ default:
187
+ return jsonResponse(200, { jsonrpc: "2.0", id, error: { code: -32601, message: `Method not found: ${message.method}` } });
188
+ }
189
+ }
190
+ report(request, info, tool, status) {
191
+ const facts = requestFacts(request, this.ctx.publicUrl(request), info);
192
+ if (!facts)
193
+ return;
194
+ const client = this.clientFor(request);
195
+ this.ctx.events.record({ type: "mcp_tool_call", request: facts, status, mcp_tool: tool.slice(0, 100), ...(client ? { mcp_client: client } : {}) });
196
+ }
197
+ async callTool(request, rawBody, info, id, params) {
198
+ const name = typeof params.name === "string" ? params.name : "";
199
+ const args = isRecord(params.arguments) ? params.arguments : {};
200
+ const operation = CHECKOUT_TOOLS[name];
201
+ if (operation)
202
+ return this.checkoutTool(request, rawBody, info, id, name, operation, args);
203
+ let response;
204
+ const origin = this.ctx.publicOrigin(request);
205
+ const onOrigin = (product) => onPublicOrigin(product, origin, this.ctx.hostnames);
206
+ if (name === "search_products") {
207
+ const query = typeof args.query === "string" ? args.query : "";
208
+ const limit = typeof args.limit === "number" && Number.isInteger(args.limit) ? Math.min(50, Math.max(1, args.limit)) : 10;
209
+ const products = (this.ctx.catalogReads ? await this.ctx.catalogReads.search(query, limit) : this.ctx.catalog.search(query, limit)).map((product) => productSummary(onOrigin(product)));
210
+ response = jsonResponse(200, {
211
+ jsonrpc: "2.0", id,
212
+ result: {
213
+ content: [{ type: "text", text: products.length ? products.map((p) => `${p.title} (${p.product_id})${p.price ? `: ${p.price.display}` : ""}`).join("\n") : "No products match." }],
214
+ structuredContent: { products },
215
+ },
216
+ });
217
+ }
218
+ else if (name === "get_product") {
219
+ const found = typeof args.product_id === "string"
220
+ ? this.ctx.catalogReads ? await this.ctx.catalogReads.get(args.product_id) : this.ctx.catalog.get(args.product_id)
221
+ : undefined;
222
+ const product = found ? onOrigin(found) : undefined;
223
+ response = product
224
+ ? jsonResponse(200, {
225
+ jsonrpc: "2.0", id,
226
+ result: { content: [{ type: "text", text: product.title }], structuredContent: { product: { ...productSummary(product), images: product.images ?? [] } } },
227
+ })
228
+ : jsonResponse(200, mcpErrorBody(id, "not_found", "No product with that product_id."));
229
+ }
230
+ else {
231
+ return jsonResponse(200, { jsonrpc: "2.0", id, error: { code: -32602, message: `Unknown tool: ${name}` } });
232
+ }
233
+ this.report(request, info, name, 200);
234
+ return response;
235
+ }
236
+ async checkoutTool(request, rawBody, info, id, tool, operation, args) {
237
+ const sessionId = operation === "create" ? null : typeof args.checkout_id === "string" ? args.checkout_id : null;
238
+ if (operation !== "create" && !sessionId) {
239
+ return jsonResponse(200, mcpErrorBody(id, "invalid_request", "checkout_id is required."));
240
+ }
241
+ /**
242
+ * The kit's own refusal (reported, since Relyon never recorded it). On a
243
+ * call that names an existing checkout it is never "nothing was charged":
244
+ * order pending when the checkout is, charge-neutral otherwise.
245
+ */
246
+ const refuse = async (code, message) => {
247
+ this.report(request, info, tool, 200);
248
+ return mcpRefusal(this.ctx, id, sessionId, code, message);
249
+ };
250
+ try {
251
+ const paused = await this.hooks.checkoutRefusal();
252
+ if (paused)
253
+ return await refuse(paused.code, paused.message);
254
+ const outcome = await relayStep(this.ctx, {
255
+ protocol: "mcp", operation, sessionId, request, rawBody,
256
+ patch: operation === "create" || operation === "update" ? mcpInputs(args) : null,
257
+ });
258
+ if (outcome.kind === "relayed")
259
+ return outcome.response;
260
+ // A safety net: Relyon refuses MCP checkout before any charge today. If a
261
+ // relayed complete ever carried a succeeded payment without the store's
262
+ // order, the agent hears order pending, never a completed result.
263
+ if (outcome.kind === "order_pending")
264
+ return orderPendingMcp(id, sessionId ?? outcome.sessionId ?? "");
265
+ if (outcome.kind === "outcome_unknown") {
266
+ // An order-pending checkout's payment is known to have succeeded.
267
+ if (sessionId && await orderPendingFor(this.ctx, "mcp", sessionId))
268
+ return await refuse("internal_error");
269
+ // Relyon may or may not have recorded this call; the kit reports it either way.
270
+ this.report(request, info, tool, 200);
271
+ return paymentOutcomeUnknown("mcp", { paymentStatus: outcome.paymentStatus, checkoutId: sessionId ?? "", id });
272
+ }
273
+ return await refuse(outcome.code, outcome.message);
274
+ }
275
+ catch (error) {
276
+ // An unexpected failure: an in-band JSON-RPC error like every other
277
+ // refusal (order pending or charge-neutral), never the plain-text one.
278
+ this.ctx.logger.error("the store kit could not answer an MCP checkout tool", { tool, error: describeError(error, true) });
279
+ return await refuse("internal_error");
280
+ }
281
+ }
282
+ }
283
+ /**
284
+ * The kit's own MCP refusal of a checkout tool call: `order_pending` when the
285
+ * named checkout is paid with its order not created, a charge-neutral message
286
+ * when it names any other existing checkout, the default message on create.
287
+ */
288
+ export async function mcpRefusal(ctx, id, sessionId, code, message) {
289
+ if (sessionId && await orderPendingFor(ctx, "mcp", sessionId))
290
+ return orderPendingMcp(id, sessionId);
291
+ if (sessionId)
292
+ return jsonResponse(200, mcpErrorBody(id, code, message === undefined ? refusalMessage(code, true, "mcp") : existingCheckoutMessage(message, "mcp")));
293
+ return jsonResponse(200, mcpErrorBody(id, code, message ?? defaultMessage(code)));
294
+ }
295
+ /** The checkout tool call a JSON-RPC message makes, or null when it isn't one. */
296
+ export function mcpCheckoutCall(message) {
297
+ if (!isRecord(message) || message.method !== "tools/call" || !isRecord(message.params))
298
+ return null;
299
+ const operation = typeof message.params.name === "string" ? CHECKOUT_TOOLS[message.params.name] : undefined;
300
+ if (!operation)
301
+ return null;
302
+ const args = isRecord(message.params.arguments) ? message.params.arguments : {};
303
+ return { id: message.id, sessionId: operation !== "create" && typeof args.checkout_id === "string" ? args.checkout_id : null };
304
+ }
305
+ function defaultMessage(code) {
306
+ switch (code) {
307
+ case "protocol_not_available": return "Checkout over MCP is not available on this store yet. Nothing was charged.";
308
+ case "service_unavailable": return "Checkout is temporarily unavailable. Nothing was charged; try again shortly.";
309
+ case "not_found": return "This checkout is not known to this store.";
310
+ default: return "The checkout step could not be completed. Nothing was charged.";
311
+ }
312
+ }
@@ -0,0 +1,101 @@
1
+ import { type StoreDoctorOptions, type StoreDoctorResult } from "./doctor.js";
2
+ import { readSandboxProgress, type KeySource, type ProgressOutcome } from "./progress.js";
3
+ import { type KitVersionResult } from "./kit-version.js";
4
+ export declare const DOCTOR_COMMAND = "npx @relyon/store-kit doctor";
5
+ export declare const DONE_LINE = "DONE: Test integration verified by Relyon.";
6
+ /** Live runs have no Test checkout confirmation; DONE there means every Live check passed. */
7
+ export declare const LIVE_DONE_LINE = "DONE: Live checks passed.";
8
+ export declare const THEN_RUN = "Then run: npx @relyon/store-kit doctor";
9
+ /** The builder-facing hint the kit adds to waiting and setup errors (defined with the waiting body). */
10
+ export { DEVELOPER_HINT } from "./waiting.js";
11
+ /** Where the Test purchase is documented, as init copies it for every agent. */
12
+ export declare const CHECKOUT_SKILL_FILE = ".agents/skills/relyon-checkout/SKILL.md";
13
+ export declare const GET_KEY_URL = "https://core.getrelyon.com/get-sandbox-key";
14
+ /** Advisory only: a local key file is not the deployment's environment settings. */
15
+ export declare const HOST_ENVIRONMENT_ITEM = "Host environment key";
16
+ export declare const HOST_ENVIRONMENT_DETAIL = "Before publishing, add RELYON_API_KEY in the host's environment settings. This run uses a key saved only in a local file; that does not configure the published server. Get your Test key from the Relyon dashboard (https://core.getrelyon.com/get-sandbox-key); never paste keys in chat.";
17
+ export interface NextStep {
18
+ order: number;
19
+ action: string;
20
+ command?: string;
21
+ file?: string;
22
+ }
23
+ export interface DoctorVerdict {
24
+ status: "done" | "not_done";
25
+ live: boolean;
26
+ /** Required conditions in this mode (4 in Test, 3 in Live) and how many are unmet. */
27
+ required: number;
28
+ remaining: number;
29
+ conditions: {
30
+ testKey: boolean;
31
+ doctorPasses: boolean;
32
+ checkoutConfirmed: boolean;
33
+ feedsLive: boolean;
34
+ };
35
+ confirmed: {
36
+ test_key_used: boolean;
37
+ test_checkout_completed: boolean;
38
+ };
39
+ /** Empty when DONE; otherwise ends with the "Then run" step. */
40
+ nextSteps: NextStep[];
41
+ /** NOT DONE with domain verification as the only blocker: the verdict line reads "1 agent step left: verify the domain". */
42
+ onlyDomainStep?: boolean;
43
+ }
44
+ /** A doctor result with the overall verdict next to `ok` (which keeps its meaning: no FAIL). */
45
+ export interface DoctorReport extends StoreDoctorResult {
46
+ verdict: DoctorVerdict;
47
+ }
48
+ /** How doctor's "Relyon key" item starts when a saved Test key was withheld from a non-official Relyon base URL (L1). */
49
+ export declare const SAVED_KEY_WITHHELD_PREFIX = "The saved Test key was not sent";
50
+ export declare const SAVED_KEY_ALTERNATIVE = "Otherwise put the store's Test key in RELYON_API_KEY, or unset RELYON_BASE_URL (and --base-url) to use Relyon's own API.";
51
+ /**
52
+ * The overall verdict. `progress` is what Relyon's progress record said; when
53
+ * absent, the PROGRESS_ITEM in `result` decides (PASS means confirmed).
54
+ */
55
+ export declare function doctorVerdict(result: StoreDoctorResult, context?: {
56
+ live?: boolean;
57
+ progress?: ProgressOutcome;
58
+ }): DoctorVerdict;
59
+ /** The verdict line: "NOT DONE: n of m required steps remain.", the single-agent-step line, or the DONE line. */
60
+ export declare function verdictLine(verdict: DoctorVerdict): string;
61
+ /** The verdict line and the numbered "Do this next" list, without the item table. */
62
+ export declare function formatNextSteps(verdict: DoctorVerdict, secrets?: readonly (string | undefined)[]): string;
63
+ /** The human doctor output: the verdict and the steps first, then every item. */
64
+ export declare function formatDoctorReport(report: DoctorReport, secrets?: readonly (string | undefined)[]): string;
65
+ export interface DoctorJson {
66
+ status: "done" | "not_done";
67
+ version: string;
68
+ /** The checklist-manifest version this kit ships (additive, kit 0.1.14). */
69
+ manifest_version: number;
70
+ /** Legacy optional string derived from kit_version's installed/latest evidence. */
71
+ newer_kit_version?: string;
72
+ /** Unavailable evidence is consistently null; checked_at is the registry observation time, including cache hits. */
73
+ kit_version: KitVersionResult;
74
+ checks: Array<{
75
+ name: string;
76
+ status: string;
77
+ detail: string;
78
+ id?: string;
79
+ }>;
80
+ next_steps: NextStep[];
81
+ confirmed: {
82
+ test_key_used: boolean;
83
+ test_checkout_completed: boolean;
84
+ };
85
+ /** The owner's to-dos (policies, support contact, domain): list them in the final chat message. */
86
+ owner_todos: string[];
87
+ }
88
+ /** `doctor --json`: one object, every string redacted. */
89
+ export declare function doctorJson(report: DoctorReport, secrets?: readonly (string | undefined)[]): DoctorJson;
90
+ /** 0 only when DONE, 1 on any FAIL, 2 when NOT DONE without a FAIL. */
91
+ export declare function doctorExitCode(report: Pick<DoctorReport, "items" | "verdict">): 0 | 1 | 2;
92
+ export interface DoctorReportOptions extends StoreDoctorOptions {
93
+ /** Where `apiKey` came from: a saved key is sent to the progress endpoint only on Relyon's official origin. */
94
+ keySource?: KeySource | undefined;
95
+ /** Test seam for the progress read. Default: readSandboxProgress with the doctor's fetch and timeout. */
96
+ readProgress?: ((options: Parameters<typeof readSandboxProgress>[0]) => Promise<ProgressOutcome>) | undefined;
97
+ /** `--allow-saved-key-to-base-url`: the merchant trusts this base URL with the saved Test key. */
98
+ allowSavedKeyToBaseUrl?: boolean | undefined;
99
+ }
100
+ /** Runs the doctor, adds Relyon's Test checkout confirmation, and computes the verdict. */
101
+ export declare function runDoctorReport(options: DoctorReportOptions): Promise<DoctorReport>;