portage-ucp-webmcp 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +164 -1
- data/README.md +222 -11
- data/lib/portage/ucp/webmcp/assets/autofill.js +288 -0
- data/lib/portage/ucp/webmcp/assets/registrar.js +72 -12
- data/lib/portage/ucp/webmcp/autofill.rb +74 -0
- data/lib/portage/ucp/webmcp/bridges/script_evaluator.rb +67 -8
- data/lib/portage/ucp/webmcp/capabilities.rb +44 -0
- data/lib/portage/ucp/webmcp/fingerprint.rb +41 -0
- data/lib/portage/ucp/webmcp/matcher.rb +170 -0
- data/lib/portage/ucp/webmcp/page_wait.rb +84 -0
- data/lib/portage/ucp/webmcp/presets.rb +88 -0
- data/lib/portage/ucp/webmcp/rack/call_endpoint.rb +144 -51
- data/lib/portage/ucp/webmcp/rack/request_limits.rb +55 -0
- data/lib/portage/ucp/webmcp/registrar.rb +12 -1
- data/lib/portage/ucp/webmcp/transport.rb +20 -9
- data/lib/portage/ucp/webmcp/version.rb +1 -1
- data/lib/portage/ucp/webmcp.rb +44 -3
- metadata +40 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d6b6ed40cbfc7d72bfb2354e1964250d4fa118bf0e505134ad8b3ad0b57b2214
|
|
4
|
+
data.tar.gz: af53799f73cd814caf017cf86ddd0c511b92e83290a2f49d1f6c494c4064c8d3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9c07a7bac723dfd5fe5911ee856602409dca6a7c76196cf87eaa559753793dcd96ab258d4cbd82ac253ed7b73cd515f7eb8e022ac34f3d050435522e263aafeb
|
|
7
|
+
data.tar.gz: 8f1bd216b96fbe4f63324ec7bdfcd1fc01aecf823c4c72df050a499fb654ed65d45295e4d5baf65eddcfe9f4331428d7019d99bb4bc1adc7539ac2e777c726d1
|
data/CHANGELOG.md
CHANGED
|
@@ -4,7 +4,170 @@ All notable changes to this project are documented here. Format loosely follows
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.0.0/); this project is
|
|
5
5
|
pre-1.0, so APIs may still shift between minor versions.
|
|
6
6
|
|
|
7
|
-
## [
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.2.0] - 2026-09-29
|
|
10
|
+
|
|
11
|
+
- **Fixed: `assets/autofill.js` didn't parse** (docs/design-log.md §51). A
|
|
12
|
+
`*/` inside its header comment closed the comment early, so every real
|
|
13
|
+
`Autofill.call` raised `BridgeError` (`SyntaxError: Unexpected identifier
|
|
14
|
+
'context'`). New `asset_syntax_spec.rb` runs `node --check` on every file
|
|
15
|
+
in `assets/`, in the form the gem actually hands a browser, and skips with
|
|
16
|
+
a message when node isn't on PATH. New `autofill_js_spec.rb` runs the real
|
|
17
|
+
script through `ScriptEvaluator#autofill` against a small fake checkout
|
|
18
|
+
DOM (`spec/support/fake_checkout_dom.js`, no npm dependency).
|
|
19
|
+
- **Autofill fills a `<select>`** reached through `checkout_selectors`: it
|
|
20
|
+
picks the option whose value matches, then the one whose visible text
|
|
21
|
+
matches (case-insensitive, trimmed), using `HTMLSelectElement`'s own value
|
|
22
|
+
setter. No matching option reports the token as `unmatched` instead of
|
|
23
|
+
throwing on the input setter.
|
|
24
|
+
- **Shipping-rate prices in any currency.** `selectCheapestRate` reads a
|
|
25
|
+
price through a new `ratePrice`, which tries four patterns in order and
|
|
26
|
+
takes the first hit: currency symbol (`\p{Sc}`, so `฿`, `¥`, `₹` …) then
|
|
27
|
+
amount, amount then symbol, ISO code then amount, amount then ISO code.
|
|
28
|
+
An ISO code only counts if it's in `Intl.supportedValuesOf("currency")`
|
|
29
|
+
(any three capitals where that's missing). So "Royal Mail Tracked 48
|
|
30
|
+
£3.50" reads as 3.50 and "DPD 24 hours £6.00" as 6, not 48 or 24.
|
|
31
|
+
Thousands separators are handled (`฿1,950.00` is 1950, not 1.95), and
|
|
32
|
+
"Free" still counts as 0.
|
|
33
|
+
- **`Presets::SHOPIFY.checkout_selectors`** now has two fallbacks, taken
|
|
34
|
+
from a live Shopify checkout (docs/design-log.md §50): `email` →
|
|
35
|
+
`input[autocomplete='shipping email']` and `shipping tel` →
|
|
36
|
+
`input[autocomplete='shipping tel-national']`. Shopify names both fields
|
|
37
|
+
differently from the tokens `WebmcpAutofillFields` asks for.
|
|
38
|
+
- **Docs for Phases 1-3** (docs/plans/webmcp-universal-outbound.md Phase 4,
|
|
39
|
+
no code change): the README's "Stores that don't run Portage" section now
|
|
40
|
+
documents `Presets`/`preset:`, `Matcher`/the read-vs-mutating confirm
|
|
41
|
+
rule, the fingerprint-keyed confirmed-mapping store, and a new "Approved
|
|
42
|
+
autofill of the store's checkout" section covering `Autofill`'s outcomes
|
|
43
|
+
and `Preset#checkout_selectors`. None of this had been written up before
|
|
44
|
+
now even though it shipped in earlier phases.
|
|
45
|
+
- **Checkout autofill** (docs/plans/webmcp-universal-outbound.md Phase 3):
|
|
46
|
+
`Bridges::ScriptEvaluator#autofill(fields, selectors:)` fills a checkout
|
|
47
|
+
page's own contact/shipping fields directly — by `autocomplete` attribute,
|
|
48
|
+
never a WebMCP tool call — via a new `assets/autofill.js`, and picks the
|
|
49
|
+
cheapest shipping rate among same-named radio groups it can read a price
|
|
50
|
+
from. Never touches a field whose own `autocomplete` is payment-shaped
|
|
51
|
+
(`cc-*`/`transaction-*`) or whose `type` is hidden/password, regardless of
|
|
52
|
+
what's asked for; never clicks a submit/pay control; stops and reports
|
|
53
|
+
`blocked` on the first sign of a CAPTCHA/challenge, without trying to
|
|
54
|
+
solve or route around it. `ScriptEvaluator.ferrum`/`.playwright`/
|
|
55
|
+
`.selenium` all take a new `headless:` (default `nil`, meaning "unknown"),
|
|
56
|
+
exposed as `#headless?`. `WebMcp::Autofill.call(bridge:, fields:,
|
|
57
|
+
selectors:)` is the Ruby-side gate in front of it: `:needs_headed_browser`
|
|
58
|
+
when the bridge is headless or never says (unknown is treated as
|
|
59
|
+
headless — a shopper who can't see the browser can't pay in it either),
|
|
60
|
+
`:unsupported` for a hand-rolled Bridge with no `#autofill` at all,
|
|
61
|
+
`:blocked` when the page script signals a challenge, `:filled` otherwise
|
|
62
|
+
(with `#filled`/`#unmatched`/`#rate`). `Presets::Preset` gained
|
|
63
|
+
`checkout_selectors` (default `{}`), a platform's fallback CSS selectors
|
|
64
|
+
per autocomplete token — Shopify's is empty pending a live check (no
|
|
65
|
+
browser or live storefront available this session; noted as pending in
|
|
66
|
+
the plan's Progress log). Everything upstream of this — the opt-in gate,
|
|
67
|
+
the shopper-approval prompt, building `fields` from `PORTAGE_SHIP_*` — is
|
|
68
|
+
`portage-cli`'s job (`WebmcpAutofillMode`, `WebmcpAutofillConfirm`,
|
|
69
|
+
`WebmcpAutofillFields`); this gem never reads shopper data itself.
|
|
70
|
+
- **Schema matching for unknown pages** (docs/plans/webmcp-universal-outbound.md
|
|
71
|
+
Phase 2): `WebMcp::Matcher.propose(tools)` proposes a `tool_names:` mapping
|
|
72
|
+
for a page `Presets.detect` doesn't recognize, scoring each of the page's
|
|
73
|
+
tools against every UCP action it might be by name-token overlap
|
|
74
|
+
(`findProducts` ↔ `search_catalog`), input-schema shape (a `query` string
|
|
75
|
+
→ search; `product_id`/`variant_id` + `quantity` → add to cart), and the
|
|
76
|
+
`readOnlyHint` annotation — never a tool's `description`, which is
|
|
77
|
+
untrusted page content. Returns a `Proposal` (`tool:`, `confidence:`,
|
|
78
|
+
`reason:`) per action scored above a floor; an action nothing scores
|
|
79
|
+
against is simply absent. No LLM in this gem. `Matcher.tool_names(proposal)`
|
|
80
|
+
flattens a proposal to the plain `tool_names:` shape. Confirming a
|
|
81
|
+
proposal, and persisting a confirmed mapping, is `portage-cli`'s job
|
|
82
|
+
(`WebmcpMappingConfirm`, `WebmcpMappings`) — nothing here calls a tool or
|
|
83
|
+
writes anything.
|
|
84
|
+
- `WebMcp::Fingerprint`, extracted from `Presets.detect`'s own name-sorting:
|
|
85
|
+
`.names(tools)` (sorted tool names, what `Presets.detect` matches against)
|
|
86
|
+
and `.for(tools)` (a digest of every tool's name *and* its own input
|
|
87
|
+
schema — what `portage-cli`'s confirmed-mapping store keys a Phase 2
|
|
88
|
+
mapping by, so a lookalike page with the same tool names but a different
|
|
89
|
+
schema shape gets a different fingerprint).
|
|
90
|
+
- `WebMcp.connect` with no `capabilities:` now derives them from the tools
|
|
91
|
+
the page registers (`WebMcp::Capabilities`), so `Session#advertises?`
|
|
92
|
+
answers `true`/`false` instead of `nil`. This reads the page once inside
|
|
93
|
+
`connect`, so a `BridgeError` can now raise from `connect` itself.
|
|
94
|
+
`Transport#answers?(action)` reports whether a page tool answers an
|
|
95
|
+
action after `tool_names:`/`prefix:` resolution.
|
|
96
|
+
- **Platform presets** (docs/plans/webmcp-universal-outbound.md Phase 1):
|
|
97
|
+
`WebMcp.connect` takes a new `preset:` (default `:auto`), which detects a
|
|
98
|
+
known platform from the exact set of tool names the page registers right
|
|
99
|
+
now — never from page text, which is untrusted content a page could
|
|
100
|
+
forge — and applies its `tool_names:`/`wire:`. `nil` turns presets off;
|
|
101
|
+
a Symbol (`:shopify`) forces one without reading the page to detect it.
|
|
102
|
+
An explicit `tool_names:` still wins over the preset's own, key by key.
|
|
103
|
+
`WebMcp::Presets::SHOPIFY` is the first entry: `tool_names: {create_cart:
|
|
104
|
+
"add_to_cart"}`, and a `handoff_checkout: "proceed_to_checkout"` naming
|
|
105
|
+
the tool that only navigates the browser to Shopify's own checkout. Its
|
|
106
|
+
fingerprint is the 11 tools seven live Shopify storefronts registered
|
|
107
|
+
on 2026-09-28, all with identical input schemas.
|
|
108
|
+
- `WebMcp::Capabilities.for` takes a new `handoff_checkout:` (a preset's
|
|
109
|
+
hand-off-only checkout tool name) — a page that answers it now counts as
|
|
110
|
+
advertising checkout even with no `create_checkout` tool at all.
|
|
111
|
+
- `Bridges::ScriptEvaluator#location` reads the tab's current URL
|
|
112
|
+
(`window.location.href`) through the same `evaluate:` callable every
|
|
113
|
+
other call uses. Optional on the Bridge contract — used by `portage-cli`
|
|
114
|
+
`Buy`'s hand-off checkout path to read the checkout URL after a tool
|
|
115
|
+
that only navigates the tab, when the tool's own result carries none.
|
|
116
|
+
|
|
117
|
+
## [0.1.1] - 2026-09-24
|
|
118
|
+
|
|
119
|
+
- `Rack::CallEndpoint` resolves the caller through core's
|
|
120
|
+
`Rack::ForwardedRequest`: `X-Forwarded-*`/`Forwarded` are trusted only
|
|
121
|
+
from a configured `trusted_proxies` list, `server_context[:client_ip]`
|
|
122
|
+
(seen by the rate limiter and authenticator) is the right-most untrusted
|
|
123
|
+
hop, and `X-Forwarded-Host` never widens `allowed_origins`. Requires
|
|
124
|
+
`portage-ucp` `~> 0.10`.
|
|
125
|
+
- An outbound call now waits out a tool that answers the page isn't ready,
|
|
126
|
+
as it already waited out one the page dropped. Shopify storefronts reload
|
|
127
|
+
after their own `add_to_cart`/`cancel_cart` and, until they're back, every
|
|
128
|
+
cart tool answers "Standard Actions are not available… Try again" (as an
|
|
129
|
+
`isError` result, a JSON string or a thrown error), which surfaced as
|
|
130
|
+
`ServerError` (seen live on thelightyard.co.uk and burton.com).
|
|
131
|
+
Only the messages in `PageWait::NOT_READY` are retried; any other tool
|
|
132
|
+
error is still raised at once, so a mutation can't run twice. The default
|
|
133
|
+
`reregister_wait:` goes from 2s to 5s: with several browsers busy the tools
|
|
134
|
+
took up to ~3s to come back.
|
|
135
|
+
|
|
136
|
+
- `Rack::CallEndpoint` no longer raises `TypeError` when a `tools/call` body
|
|
137
|
+
sends `params` as a string, number or boolean instead of an object; it
|
|
138
|
+
answers `-32602 Invalid params` like any other bad call. A top-level
|
|
139
|
+
JSON-RPC batch (an array) and a non-string `method` were already handled
|
|
140
|
+
without crashing; both now have specs.
|
|
141
|
+
- `Rack::CallEndpoint` takes `max_body_bytes:` (default 1MiB). A
|
|
142
|
+
`Content-Length` over the cap is refused with `413` before anything is
|
|
143
|
+
read; otherwise at most `max_body_bytes + 1` bytes are ever read off the
|
|
144
|
+
body, so a missing or understated `Content-Length` can't be used to buffer
|
|
145
|
+
an unbounded body first.
|
|
146
|
+
- `Rack::CallEndpoint` takes `call_timeout:` (default 30s, `nil` disables it),
|
|
147
|
+
bounding one dispatch into the catalog's `Mcp::Server#handle` so a slow
|
|
148
|
+
adapter call can't hold a browser's `fetch` open indefinitely. Every
|
|
149
|
+
rejection this endpoint answers — origin/method/content-type/size checks
|
|
150
|
+
included — now shares one JSON-RPC error envelope shape.
|
|
151
|
+
- `registrar.js` no longer races a Turbo/SPA reload against itself: a second
|
|
152
|
+
`GET /webmcp.js` now awaits the previous generation's own in-flight
|
|
153
|
+
`registerTool` calls (and that generation's `unregister`) before
|
|
154
|
+
registering the same names again, bounded by `reregister_wait_ms:`
|
|
155
|
+
(default 2000) so a previous generation that never settles can't block the
|
|
156
|
+
page forever. Previously, a fast-enough reload could start registering
|
|
157
|
+
before the first generation had actually dropped anything, and the second
|
|
158
|
+
generation's calls would collide on every tool name.
|
|
159
|
+
- Security review: added a spec confirming `ScriptEvaluator` can't be broken
|
|
160
|
+
out of by a tool name or argument containing JS-breaking characters (it's
|
|
161
|
+
JSON-encoded, never interpolated into the expression's own syntax).
|
|
162
|
+
README: new "Content-Security-Policy" and "CSRF posture" sections, and a
|
|
163
|
+
"Request limits" section documenting `max_body_bytes:`/`call_timeout:`.
|
|
164
|
+
- `spec/portage/ucp/webmcp/real_browser_spec.rb`: registrar.js register ->
|
|
165
|
+
tool call -> re-register against a real, headless Chrome (via a new
|
|
166
|
+
`ferrum` dev dependency), not just the `:node` specs' stand-in tab.
|
|
167
|
+
Excluded by default (slow, needs Chrome installed); run with
|
|
168
|
+
`REAL_BROWSER=1 bundle exec rspec spec/portage/ucp/webmcp/real_browser_spec.rb`.
|
|
169
|
+
|
|
170
|
+
## [0.1.0] - 2026-09-23
|
|
8
171
|
|
|
9
172
|
- Initial release. WebMCP as a transport to the existing `Adapter` contract.
|
|
10
173
|
- The registrar reports a non-JSON reply from its endpoint (a proxy's HTML
|
data/README.md
CHANGED
|
@@ -83,6 +83,13 @@ What the catalog exposes:
|
|
|
83
83
|
`Rack::CallEndpoint` enforces the same filter on the server side. An action the
|
|
84
84
|
page doesn't register can't be reached by POSTing its name.
|
|
85
85
|
|
|
86
|
+
A second `GET /webmcp.js` (a Turbo/SPA reload re-running the script tag)
|
|
87
|
+
re-registers cleanly: it waits for the previous generation's own in-flight
|
|
88
|
+
`registerTool` calls to settle and unregister before registering the same
|
|
89
|
+
names again, bounded by `registrar_options: { reregister_wait_ms: }` (default
|
|
90
|
+
2000) so a previous generation that never settles can't block the page
|
|
91
|
+
forever.
|
|
92
|
+
|
|
86
93
|
### Security
|
|
87
94
|
|
|
88
95
|
The endpoint is browser-facing and the page's cookies go with every call. For
|
|
@@ -117,6 +124,73 @@ Portage::Ucp::WebMcp::Rack::App.new(
|
|
|
117
124
|
The endpoint then answers the CORS preflight and sends credentialed CORS
|
|
118
125
|
headers for that origin only.
|
|
119
126
|
|
|
127
|
+
### Request limits
|
|
128
|
+
|
|
129
|
+
Two more limits bound a single `POST /webmcp`, on top of the `Origin`/method/
|
|
130
|
+
content-type checks above. Every rejection — these two, the checks above, and
|
|
131
|
+
a JSON-RPC error `dispatch` itself returns — answers with the same JSON-RPC
|
|
132
|
+
error envelope, so a caller never needs to branch on HTTP status to read why
|
|
133
|
+
a call failed.
|
|
134
|
+
|
|
135
|
+
- **`max_body_bytes:`** (default 1MiB). A `Content-Length` over the cap is
|
|
136
|
+
refused with `413` before anything is read. Otherwise at most
|
|
137
|
+
`max_body_bytes + 1` bytes are ever read off the request body, so a body
|
|
138
|
+
with no `Content-Length` (or one that under-reports) still can't be
|
|
139
|
+
buffered in full first.
|
|
140
|
+
- **`call_timeout:`** (default 30 seconds, `nil` disables it). Bounds one
|
|
141
|
+
`tools/call`/`tools/list` dispatch into the catalog's `Mcp::Server#handle`.
|
|
142
|
+
A call that runs past it answers a JSON-RPC error rather than holding the
|
|
143
|
+
browser's `fetch` (and the request thread) open indefinitely. This is a
|
|
144
|
+
floor under the endpoint, not a replacement for your own `Adapter`/HTTP
|
|
145
|
+
client timeouts — a slow adapter call still ties up the thread until it
|
|
146
|
+
fires.
|
|
147
|
+
|
|
148
|
+
```ruby
|
|
149
|
+
Portage::Ucp::WebMcp::Rack::App.new(
|
|
150
|
+
catalog: catalog,
|
|
151
|
+
call_options: { max_body_bytes: 262_144, call_timeout: 10 }
|
|
152
|
+
)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Content-Security-Policy
|
|
156
|
+
|
|
157
|
+
The registrar's script and its `fetch` calls need two directives, if your
|
|
158
|
+
store sets a CSP:
|
|
159
|
+
|
|
160
|
+
- **`script-src`**: allow the origin `GET /webmcp.js` is served from (usually
|
|
161
|
+
your own, `'self'`). The script has no inline `<script>` body — it's a
|
|
162
|
+
`src=` include — so no `'unsafe-inline'` or nonce is needed for it.
|
|
163
|
+
- **`connect-src`**: allow the origin `POST /webmcp` targets. For a
|
|
164
|
+
same-origin endpoint, `'self'` already covers it. For the cross-origin
|
|
165
|
+
pattern above (`registrar_options: { endpoint: "https://api.shop.example/..." }`),
|
|
166
|
+
add that origin explicitly: `connect-src 'self' https://api.shop.example`.
|
|
167
|
+
Also add it for every origin passed to `exposed_to:` if that agent surface
|
|
168
|
+
itself calls the endpoint from a different frame's document.
|
|
169
|
+
|
|
170
|
+
Nothing here needs `'unsafe-eval'`: the registrar only calls `fetch` and
|
|
171
|
+
`document.modelContext.registerTool`, never `eval` or `new Function`.
|
|
172
|
+
|
|
173
|
+
### CSRF posture
|
|
174
|
+
|
|
175
|
+
`allowed_origins`/`Origin` checking (above) is the endpoint's primary CSRF
|
|
176
|
+
defense: a browser attaches the real `Origin` on every `fetch`, and page
|
|
177
|
+
script can't override it, so a cross-site page's request is rejected before
|
|
178
|
+
your `Authenticator` ever sees it — the same protection `SameSite` cookies
|
|
179
|
+
give a classic form POST, enforced here explicitly since the endpoint has to
|
|
180
|
+
work from a cross-origin storefront too (`allowed_origins:`).
|
|
181
|
+
|
|
182
|
+
If your `Authenticator` reads a session cookie (cookie-auth, `credentials:
|
|
183
|
+
"same-origin"` or `"include"`), also set a CSRF token, the same as you would
|
|
184
|
+
for any other cookie-authenticated POST endpoint: put it on the page's
|
|
185
|
+
requests with `registrar_options: { headers: { "x-csrf-token" => token } }`
|
|
186
|
+
and check `server_context[:request]` for it in your `Authenticator`, exactly
|
|
187
|
+
as `spec/support/store.rb` does in this gem's own specs. `Origin` checking
|
|
188
|
+
alone is not a substitute for this when the endpoint's `Authenticator` trusts
|
|
189
|
+
a cookie: a same-origin `Origin` only proves the request came from a page on
|
|
190
|
+
your site, not that the page is the one your store served (an XSS on another
|
|
191
|
+
page of the same origin, or a subdomain sharing the cookie's domain, can
|
|
192
|
+
still fetch same-origin).
|
|
193
|
+
|
|
120
194
|
### Browsers without WebMCP
|
|
121
195
|
|
|
122
196
|
When the page has no `modelContext`, the registrar does nothing. It records
|
|
@@ -181,7 +255,63 @@ something the consumer can list.
|
|
|
181
255
|
|
|
182
256
|
### Stores that don't run Portage
|
|
183
257
|
|
|
184
|
-
`Transport` does not assume that the page was built with this gem
|
|
258
|
+
`Transport` does not assume that the page was built with this gem. Three
|
|
259
|
+
ways to get a working `tool_names:`/`wire:` for a page you don't control,
|
|
260
|
+
tried in this order (`docs/plans/webmcp-universal-outbound.md`):
|
|
261
|
+
|
|
262
|
+
1. **A built-in preset.** `WebMcp::Presets.detect(tools)` matches a page's
|
|
263
|
+
tools against a known platform's exact fingerprint — the whole sorted set
|
|
264
|
+
of tool names it registers *right now*, never anything the page's own
|
|
265
|
+
text says about itself (a `generator` meta tag, `window.Shopify`, a
|
|
266
|
+
tool's own description — all untrusted page content, same boundary as
|
|
267
|
+
the confirm rule below). `WebMcp.connect(bridge:, preset: :auto)` — the
|
|
268
|
+
default — runs detection (one extra page read, on top of the one
|
|
269
|
+
`capabilities: nil` already does) and applies the matching preset's
|
|
270
|
+
`tool_names:`/`wire:` before anything else. `preset: nil` turns presets
|
|
271
|
+
off entirely; a symbol (`preset: :shopify`) forces one with no detection
|
|
272
|
+
read at all. An explicit `tool_names:`/`wire:` you also pass to `connect`
|
|
273
|
+
still wins over the preset's own — `tool_names:` key by key, `wire:`
|
|
274
|
+
outright. Shopify (`Presets::SHOPIFY`) is the only entry so far; see
|
|
275
|
+
"Shopify storefronts" below. A platform can change its tool set without
|
|
276
|
+
notice — when it does, `detect` simply misses and the page falls through
|
|
277
|
+
to the next mechanism rather than being mapped wrongly.
|
|
278
|
+
2. **A matched-and-confirmed mapping**, for a page no preset recognizes.
|
|
279
|
+
`WebMcp::Matcher.propose(tools)` scores each of the page's own tools
|
|
280
|
+
against every UCP action Session can drive over WebMCP, by name-token
|
|
281
|
+
overlap (`findProducts` ↔ `search_catalog`), input-schema shape (a
|
|
282
|
+
`query` string → search; `product_id`/`variant_id` + `quantity` → add to
|
|
283
|
+
cart), and the tool's own `readOnlyHint` — **never** its `description`,
|
|
284
|
+
which is page text and therefore untrusted (a page could name a mutating
|
|
285
|
+
tool `search_catalog` with a friendly description; nothing here reads
|
|
286
|
+
that string for scoring or for the mapping it returns). It returns a
|
|
287
|
+
`Proposal` (`tool:`, `confidence:`, `reason:`) per action that scored
|
|
288
|
+
above a floor; an action nothing plausible matched is simply absent
|
|
289
|
+
rather than given a weak guess. `Matcher` itself never calls a tool,
|
|
290
|
+
prompts anyone, or persists anything — that's the caller's job, and the
|
|
291
|
+
confirm rule it leans on is: a **read action** (`search_catalog`,
|
|
292
|
+
`get_product`, `get_cart`) can be used straight off the proposal, since a
|
|
293
|
+
wrong match there wastes a call and changes nothing; a **mutating
|
|
294
|
+
action** (`create_cart`, `update_cart`, `create_checkout`) needs
|
|
295
|
+
confirmation before its first call, since a wrong guess there could add
|
|
296
|
+
to, discard, or attempt to charge a stranger's cart. `portage-cli`
|
|
297
|
+
enforces that rule for `portage buy`: `Portage::Cli::WebmcpMappingConfirm`
|
|
298
|
+
prints the proposed mapping — quoting a mutating tool's own `description`
|
|
299
|
+
as what it is, page content, never as an instruction — and prompts on a
|
|
300
|
+
real TTY with `--json` off; under `--json`, or with no TTY, the run
|
|
301
|
+
stops instead (outcome `webmcp_mapping_unconfirmed`) and returns the
|
|
302
|
+
proposal for the caller to pass back as `tool_names:` itself.
|
|
303
|
+
`Portage::Cli::WebmcpMappings` persists a confirmed mapping in
|
|
304
|
+
`~/.portage/webmcp_mappings.json`, keyed by the page's tool
|
|
305
|
+
**fingerprint** (`WebMcp::Fingerprint.for` — sorted tool names plus a
|
|
306
|
+
hash of each one's own input schema) rather than by origin: any later
|
|
307
|
+
store whose WebMCP tools have that exact shape reuses the mapping with
|
|
308
|
+
no prompt at all, in effect a locally-grown preset shared across every
|
|
309
|
+
store running that tool set. A lookalike page whose schemas differ even
|
|
310
|
+
slightly gets a different fingerprint and has to be confirmed again, so
|
|
311
|
+
it can't borrow a mapping that turns out to have a different shape
|
|
312
|
+
underneath the same tool names.
|
|
313
|
+
3. **An explicit `tool_names:` you pass yourself.** Always available,
|
|
314
|
+
preset or confirmed mapping or not — see the next bullet.
|
|
185
315
|
|
|
186
316
|
- **Tool names.** An action resolves to `tool_names[action]` first, then to
|
|
187
317
|
`"#{prefix}#{action}"`, then to the action itself. Map a store's own names:
|
|
@@ -199,8 +329,12 @@ something the consumer can list.
|
|
|
199
329
|
`executeTool` resolves to one) is parsed. Anything else is returned as it is.
|
|
200
330
|
- **Re-registration.** Some pages drop all their tools and register them again
|
|
201
331
|
while they re-render. A call to a tool that vanished that way waits up to
|
|
202
|
-
`reregister_wait:` (default
|
|
203
|
-
The call never ran the first time, so the retry is safe. The wait
|
|
332
|
+
`reregister_wait:` (default 5 seconds) for it to come back, then retries.
|
|
333
|
+
The call never ran the first time, so the retry is safe. The same wait
|
|
334
|
+
covers a tool that is registered but answers that the page isn't ready
|
|
335
|
+
(`PageWait::NOT_READY`: Shopify's "Standard Actions are not available",
|
|
336
|
+
which its storefronts answer while they reload after their own
|
|
337
|
+
`add_to_cart` or `cancel_cart`). No other tool error is retried. The wait blocks
|
|
204
338
|
the calling thread; see "Timeouts" below.
|
|
205
339
|
|
|
206
340
|
### Timeouts
|
|
@@ -212,8 +346,9 @@ Two limits bound an outbound call, and both block the thread that made it:
|
|
|
212
346
|
30 seconds) to `evaluate_async`. Playwright and Selenium use the page's or
|
|
213
347
|
driver's own script timeout, so set it there. A driver that times out
|
|
214
348
|
raises, and the error surfaces as `BridgeError`.
|
|
215
|
-
- **`reregister_wait:`** (default
|
|
216
|
-
reregister_wait:)`) bounds the wait for a tool the page dropped mid-call
|
|
349
|
+
- **`reregister_wait:`** (default 5 seconds, `WebMcp.connect(...,
|
|
350
|
+
reregister_wait:)`) bounds the wait for a tool the page dropped mid-call,
|
|
351
|
+
or one that answered "not ready".
|
|
217
352
|
It's a plain `sleep`, polling every 0.25 seconds. One monotonic deadline
|
|
218
353
|
covers the whole call: it's set before the first attempt, a retry never
|
|
219
354
|
resets it, the retried calls count against it, and no sleep runs past it.
|
|
@@ -223,11 +358,12 @@ Two limits bound an outbound call, and both block the thread that made it:
|
|
|
223
358
|
|
|
224
359
|
### Shopify storefronts
|
|
225
360
|
|
|
226
|
-
Checked live on 2026-09-
|
|
227
|
-
tentree, Kylie Cosmetics, Brooklinen, Allbirds, Billabong
|
|
228
|
-
WebMCP tools of their own
|
|
229
|
-
|
|
230
|
-
|
|
361
|
+
Checked live on 2026-09-28: 7 of 9 Shopify storefronts tried (ColourPop,
|
|
362
|
+
tentree, Kylie Cosmetics, Brooklinen, Allbirds, Billabong, The Light Yard)
|
|
363
|
+
register the same 11 WebMCP tools of their own, with identical schemas, and
|
|
364
|
+
`preset: :auto` detects all of them as `:shopify`. Gymshark and Fashion Nova
|
|
365
|
+
registered none. The tools take UCP-shaped arguments, so `wire: :auto` picks
|
|
366
|
+
the UCP shape. Two names differ from Session's:
|
|
231
367
|
|
|
232
368
|
```ruby
|
|
233
369
|
session = Portage::Ucp::WebMcp.connect(bridge: bridge, tool_names: { create_cart: "add_to_cart" })
|
|
@@ -248,6 +384,71 @@ Things to know about these tools:
|
|
|
248
384
|
`proceed_to_checkout` navigates the browser to Shopify checkout, where the
|
|
249
385
|
shopper pays. Neither maps onto a Session method.
|
|
250
386
|
|
|
387
|
+
### Approved autofill of the store's checkout
|
|
388
|
+
|
|
389
|
+
Once a WebMCP session hands off to the store's own checkout — a preset's
|
|
390
|
+
`handoff_checkout` tool (Shopify's `proceed_to_checkout`) navigates the same
|
|
391
|
+
bridge's browser there — the shopper can opt into having contact and
|
|
392
|
+
shipping fields filled in for them before they take over to pay. This is
|
|
393
|
+
off by default and gated behind two separate approvals, neither of which
|
|
394
|
+
lives in this gem:
|
|
395
|
+
|
|
396
|
+
- **The opt-in itself** is `portage-cli`'s: `portage buy --autofill`, or
|
|
397
|
+
`PORTAGE_WEBMCP_AUTOFILL=approve` (config.json's `"webmcp_autofill":
|
|
398
|
+
"approve"` also works) for every run, until the caller opts out again.
|
|
399
|
+
Only the literal string `"approve"` turns the env/config level on — an
|
|
400
|
+
unrelated truthy convention like `PORTAGE_WEBMCP_AUTOFILL=true` does
|
|
401
|
+
nothing, deliberately, since a mistaken export of some other tool's
|
|
402
|
+
boolean shouldn't silently start typing into a checkout page.
|
|
403
|
+
- **The shopper's approval of the exact fields.** Even opted in, nothing is
|
|
404
|
+
typed until a prompt lists every field and value about to be entered and
|
|
405
|
+
the shopper says yes — refused outright under `--json` or with no TTY on
|
|
406
|
+
stdin, the same posture as the mapping-confirm prompt above.
|
|
407
|
+
|
|
408
|
+
What it will fill, once both approvals are in: the contact email and
|
|
409
|
+
shipping address `portage-cli` already builds from `PORTAGE_SHIP_*` (plus
|
|
410
|
+
the new `PORTAGE_SHIP_EMAIL`), matched to the checkout page's own fields by
|
|
411
|
+
their WHATWG `autocomplete` attribute (`email`, `shipping given-name`,
|
|
412
|
+
`shipping address-line1`, `shipping postal-code`, …), and the cheapest
|
|
413
|
+
priced option in a same-named radio-button group it can read a price out
|
|
414
|
+
of (a best-effort DOM heuristic, unverified against a real rate picker — see
|
|
415
|
+
the Progress log in the plan). What it will never do, regardless of what
|
|
416
|
+
it's asked for: touch a field whose own `autocomplete` is payment-shaped
|
|
417
|
+
(`cc-*`, `transaction-*`) or whose `type` is `hidden`/`password` — checked
|
|
418
|
+
against the page's own attribute, not against what was requested, so a
|
|
419
|
+
mislabeled card field still can't be reached — or click a submit/pay
|
|
420
|
+
control. The run always still ends in a hand-off; autofill only changes
|
|
421
|
+
what's already filled in when the shopper gets there.
|
|
422
|
+
|
|
423
|
+
`WebMcp::Autofill.call(bridge:, fields:, selectors:)` is the gate that
|
|
424
|
+
actually runs it, and the outcomes a caller (or a report's `autofill:` key)
|
|
425
|
+
can see are:
|
|
426
|
+
|
|
427
|
+
| Outcome | Meaning |
|
|
428
|
+
|---|---|
|
|
429
|
+
| `:filled` | Ran. `#filled`/`#unmatched` list which fields matched a page element and which didn't; `#rate` lists any shipping-rate group it picked from. |
|
|
430
|
+
| `:needs_headed_browser` | The bridge is headless, or never says either way. A bridge's `#headless?` defaults to unknown, and unknown is treated the same as headless — the shopper has to be able to see and pay in this browser, so "assume headed" isn't the safe default. |
|
|
431
|
+
| `:blocked` | The checkout page itself showed a CAPTCHA/challenge marker (an embedded reCAPTCHA/hCaptcha/Turnstile iframe, a Cloudflare interstitial, a matching page title). Nothing was touched, and nothing here ever tries to solve or route around it. |
|
|
432
|
+
| `:unsupported` | The bridge doesn't implement `#autofill` at all — a hand-rolled `Bridge` that only meets the base `#list_tools`/`#execute_tool` contract. |
|
|
433
|
+
|
|
434
|
+
The DOM work — finding fields, refusing payment ones, detecting a
|
|
435
|
+
challenge, filling through each element's native value setter so a
|
|
436
|
+
React/Vue-controlled input actually notices — lives entirely in
|
|
437
|
+
`assets/autofill.js`, run through `Bridges::ScriptEvaluator#autofill`, never
|
|
438
|
+
as a WebMCP tool call. It treats the checkout page as untrusted content the
|
|
439
|
+
same way the matcher above treats a tool's `description`: it never follows
|
|
440
|
+
a link or acts on any instruction the page's own text might contain, and
|
|
441
|
+
only ever reads `autocomplete`/`type` attributes and radio-group prices,
|
|
442
|
+
sets a field's `.value`, or checks a radio button already on the page.
|
|
443
|
+
|
|
444
|
+
A field the standard `autocomplete` match misses falls back to
|
|
445
|
+
`Presets::Preset#checkout_selectors` — a platform's own CSS selectors,
|
|
446
|
+
keyed by the same autocomplete token, for a checkout whose markup doesn't
|
|
447
|
+
carry one. Shopify's has two, both taken from a live checkout on 2026-09-28:
|
|
448
|
+
its email field is `autocomplete="shipping email"` (not `email`) and its phone
|
|
449
|
+
field is `shipping tel-national` (not `shipping tel`). Every other contact and
|
|
450
|
+
shipping field matched by its own `autocomplete` value.
|
|
451
|
+
|
|
251
452
|
### Errors
|
|
252
453
|
|
|
253
454
|
All errors are under `Portage::Ucp::Client::Error`, so existing `rescue` blocks
|
|
@@ -257,7 +458,7 @@ still catch them.
|
|
|
257
458
|
|---|---|
|
|
258
459
|
| `WebMcp::BridgeError` | The page has no WebMCP surface, the driver failed, or the result couldn't be read. It quotes at most 300 characters of the driver's own error. |
|
|
259
460
|
| `WebMcp::ToolNotFoundError` | No page tool answers the action. `#available` lists what the page registers. |
|
|
260
|
-
| `Client::ServerError` | The tool ran and failed: `isError`,
|
|
461
|
+
| `Client::ServerError` | The tool ran and failed: `isError`, the page's tool threw, or the endpoint's own `call_timeout:` fired. |
|
|
261
462
|
|
|
262
463
|
## Development
|
|
263
464
|
|
|
@@ -273,3 +474,13 @@ the real `Rack::App`. This makes the round-trip specs go page, then endpoint,
|
|
|
273
474
|
then `Mcp::Server`, then `ReferenceAdapter`, with nothing mocked. They also
|
|
274
475
|
check that WebMCP returns the same documents as the in-process Loopback
|
|
275
476
|
transport. These specs are skipped when `node` isn't installed.
|
|
477
|
+
|
|
478
|
+
`spec/portage/ucp/webmcp/real_browser_spec.rb` runs the same register ->
|
|
479
|
+
call -> re-register sequence against a real, headless Chrome (via `ferrum`)
|
|
480
|
+
instead of the node stand-in, for a second confirmation against an actual
|
|
481
|
+
browser's WebMCP/fetch behavior. It's slow and needs Chrome or Chromium
|
|
482
|
+
installed, so it's excluded by default:
|
|
483
|
+
|
|
484
|
+
```bash
|
|
485
|
+
REAL_BROWSER=1 bundle exec rspec spec/portage/ucp/webmcp/real_browser_spec.rb
|
|
486
|
+
```
|