@adobe-commerce/elsie 1.8.0 → 1.9.0-alpha-20260325145838

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @adobe-commerce/elsie
2
2
 
3
+ ## 1.9.0-alpha-20260325145838
4
+
5
+ ### Minor Changes
6
+
7
+ - f1f629b: Add `useWebMCP` hook for registering WebMCP tools with `navigator.modelContext`, enabling AI agents to interact with storefront drop-ins through structured function calls.
8
+
3
9
  ## 1.8.0
4
10
 
5
11
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe-commerce/elsie",
3
- "version": "1.8.0",
3
+ "version": "1.9.0-alpha-20260325145838",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "description": "Domain Package SDK",
6
6
  "engines": {
@@ -0,0 +1,50 @@
1
+ import { Meta, Unstyled } from '@storybook/blocks';
2
+
3
+ <Meta title="Utilities/useWebMCP" />
4
+ <Unstyled>
5
+
6
+ # useWebMCP(tool)
7
+
8
+ A Preact hook that registers a WebMCP tool with the browser's `navigator.modelContext` API on mount and unregisters it on unmount. This allows AI agents to interact with the storefront through structured function calls.
9
+
10
+ The tool is registered once. If the `execute` function is re-created between renders (for example because it closes over component state or callbacks), the hook automatically forwards calls to the latest version without re-registering the tool.
11
+
12
+ The hook degrades gracefully in browsers that do not support WebMCP — no errors are thrown and existing functionality is unaffected.
13
+
14
+ ## Params
15
+
16
+ `tool.name`: Unique tool identifier used by agents to call it
17
+
18
+ `tool.description`: Human-readable description of what the tool does
19
+
20
+ `tool.inputSchema`: JSON Schema object describing the parameters the tool accepts
21
+
22
+ `tool.execute`: Function called when the agent invokes the tool. Receives the agent-supplied args and returns any value.
23
+
24
+ ## Returns
25
+
26
+ `void`
27
+
28
+ ## Examples
29
+
30
+ ```ts
31
+ import { useWebMCP } from '@adobe-commerce/elsie/lib';
32
+
33
+ useWebMCP({
34
+ name: 'apply_coupon',
35
+ description: 'Apply a coupon code to the cart',
36
+ inputSchema: {
37
+ type: 'object',
38
+ properties: {
39
+ code: { type: 'string', description: 'The coupon code to apply' },
40
+ },
41
+ required: ['code'],
42
+ },
43
+ execute: async ({ code }) => {
44
+ await applyCoupon(code as string);
45
+ return { content: [{ type: 'text', text: 'Coupon applied' }] };
46
+ },
47
+ });
48
+ ```
49
+
50
+ </Unstyled>
package/src/lib/index.ts CHANGED
@@ -26,3 +26,4 @@ export * from '@adobe-commerce/elsie/lib/deviceUtils';
26
26
  export * from '@adobe-commerce/elsie/lib/get-path-value';
27
27
  export * from '@adobe-commerce/elsie/lib/get-cookie';
28
28
  export * from '@adobe-commerce/elsie/lib/get-price-formatter';
29
+ export * from '@adobe-commerce/elsie/lib/webmcp';
@@ -0,0 +1,47 @@
1
+ /********************************************************************
2
+ * Copyright 2026 Adobe
3
+ * All Rights Reserved.
4
+ *
5
+ * NOTICE: Adobe permits you to use, modify, and distribute this
6
+ * file in accordance with the terms of the Adobe license agreement
7
+ * accompanying it.
8
+ *******************************************************************/
9
+
10
+ import { useEffect, useRef } from 'preact/hooks';
11
+
12
+ export interface WebMCPToolDefinition {
13
+ name: string;
14
+ description: string;
15
+ inputSchema: Record<string, unknown>;
16
+ execute: (args: Record<string, unknown>) => unknown;
17
+ }
18
+
19
+ declare global {
20
+ interface Navigator {
21
+ modelContext?: {
22
+ registerTool: (tool: WebMCPToolDefinition) => void;
23
+ unregisterTool: (name: string) => void;
24
+ };
25
+ }
26
+ }
27
+
28
+ export function useWebMCP(tool: WebMCPToolDefinition): void {
29
+ const executeRef = useRef(tool.execute);
30
+ executeRef.current = tool.execute;
31
+
32
+ useEffect(() => {
33
+ if (!navigator.modelContext) return;
34
+
35
+ const stableTool: WebMCPToolDefinition = {
36
+ ...tool,
37
+ execute: (args) => executeRef.current(args),
38
+ };
39
+
40
+ navigator.modelContext.registerTool(stableTool);
41
+
42
+ return () => {
43
+ navigator.modelContext?.unregisterTool(tool.name);
44
+ };
45
+ // eslint-disable-next-line react-hooks/exhaustive-deps
46
+ }, []);
47
+ }