Newshot

通过 WebMCP 为 AI 智能体提供导览式引导

Translation(model translation, not the source's own text)

webmcp-tour

npm version
npm version
npm downloads
npm downloads
minzipped size
minzipped size
license: MIT
license: MIT

一个 script 标签,就能为 AI agent 提供引导式导览。引入 webmcp-tour 之后,任何 WebMCP agent 都能带你的用户走一遍你的应用,逐步高亮并讲解。想让导览更贴合你的产品,声明几个目标即可。

实验性(0.x)。API 可能在小版本之间发生变化,而且 WebMCP 本身在浏览器中仍处于实验阶段。
在 Billing 页面上,一个 agent 被问到“How do I set up two-factor?”。一张居中的卡片提供“Go to Security”按钮;点击 Go 之后,Set up two-factor 被高亮,并显示一张“Got it”卡片。
在 Billing 页面上,一个 agent 被问到“How do I set up two-factor?”。一张居中的卡片提供“Go to Security”按钮;点击 Go 之后,Set up two-factor 被高亮,并显示一张“Got it”卡片。

在线演示 · 文档 · Agent 宿主

两种使用方式

1. 零配置:一个 script 标签

试一试(仅限快速实验:始终使用最新版本,不做完整性校验):


        <script src="https://unpkg.com/webmcp-tour"></script>
      

生产环境:锁定确切版本,并用子资源完整性(Subresource Integrity)锁定文件:


        <script src="https://unpkg.com/[email protected]/dist/webmcp-tour.js" integrity="sha384-ZFV7oLCiB6T5KDOyhLF+G6M+t6L1P0nugi+feJ6QYJnKy8pJ1ThykxOFoV7mnn2A" crossorigin="anonymous"></script>
      

不要在生产环境使用 @latest:这样无法使用 SRI,而且 0.x 版本可能悄悄出问题。某个发布版本的哈希值可用 curl -s https://unpkg.com/[email protected]/dist/webmcp-tour.js | openssl dgst -sha384 -binary | openssl base64 -A 取得(在克隆的仓库中则是 pnpm build && pnpm sri)。也可以从 npm 安装:npm install webmcp-tour。

现在任何 WebMCP agent 都能列出页面上的内容(tour_list_targets 会返回可见的标题与控件大纲),并能通过可见文本指向任意元素。无需标记,无需配置。

可选的设置放在一个 JSON 块里,script 标签用 data-config 指向它(仅限可 JSON 序列化的选项:pages、labels、pageSuggestion、resume、takeMeThere;navigate 是函数,所以不能在这里设置)。在 script 标签上加 data-manual 可跳过自动注册,加 data-no-global 则不暴露 window.webmcpTour({ tour, createTour, registerAsWebMcpTool })。


        <script type="application/json" id="tour-config">
  { "takeMeThere": true, "labels": { "gotIt": "Entendi" } }
</script>
<script src="https://unpkg.com/[email protected]/dist/webmcp-tour.js" data-config="tour-config" integrity="sha384-ZFV7oLCiB6T5KDOyhLF+G6M+t6L1P0nugi+feJ6QYJnKy8pJ1ThykxOFoV7mnn2A" crossorigin="anonymous"></script>
      

data-config 的值必须恰好是一个 <script type="application/json"> 的 id(没有或有好几个:会给出警告并使用默认值),当其值以 { 开头时则视为内联 JSON。JSON 无效或结构不对同样会给出警告并使用默认值。配置与 data-* 标志都从 document.currentScript 读取,因此当该文件被打包成模块时它们不会生效。若由服务器渲染这段 JSON 块,请把 < 转义为 \u003c(JSON.stringify(config).replace(/</g, "\\u003c")),这样内容就永远无法提前闭合标签。

2. 精心设计体验

安装它,创建一个导览,注册它:


        npm install webmcp-tour
      

        import { createTour, registerAsWebMcpTool } from "webmcp-tour";

const tour = createTour();
const { unregister } = await registerAsWebMcpTool(tour); // or pass { signal } to unregister on abort
      

只有一个包入口(webmcp-tour),且它没有副作用(sideEffects: false),因此在服务端引入是安全的。

声明一个目标,让 agent 用你的说法回答,而不是靠猜:


        <button data-tour-id="change-plan"
        data-tour-description="Opens the plan picker. Changes apply after you confirm.">
  Change plan
</button>
      

声明页面,这样 agent 就能说“那个控件在另一个页面上”,提供一张 Go to Security 卡片,并在用户点击 Go 之后让该步骤在那里继续:

文章标题:通过 WebMCP 为 AI agent 提供引导式导览

正文:


        const tour = createTour({
  pages: [
    { id: "billing", url: "/billing", title: "Billing" },
    { id: "security", url: "/security", title: "Security" },
  ],
  navigate: (url) => router.push(url), // default: location.assign
});
tour.defineTarget({ id: "two-factor", page: "security", label: "Set up two-factor", description: "Starts two-factor setup." });
      

把它嵌入到你自己的工具中,这样 agent 的回答和高亮就会同时出现:


        const result = await tour.show({ target: "two-factor", message: "Press this to start." });
// { ok: true, matched } | { ok: false, error: "other_page", page, message } | { ok: true, suggested } | ...
      

同一个页面上有多个导览(比如按产品区域划分)时,需要使用不同的工具名:createTour({ toolPrefix: "billing" }) 会生成 billing_show_step 之类的工具,而 registerAsWebMcpTool([billingTour, teamTour]) 会将它们一起注册。

翻译、卡片位置、恢复以及其他内容都在 concepts 和 engine API 页面中。

agent 能得到什么

四个工具,全部标记为 readOnlyHint:

工具作用
tour_list_targets页面大纲(标题、带 role、name、context 的控件),以及声明的 target 和 page
tour_show_step用卡片高亮某个控件(通过声明的 id、可见文本或 selector);或建议另一个页面
tour_wait_for_interaction当用户点击它、按下 Got it、Go 或关闭时 resolve
tour_dismiss清除当前步骤

详情:tools reference。

安全与无障碍

  • 工具只做高亮。只有用户按下 Go 时才会发生导航。
  • 卡片带有 From the AI assistant 标签,因此页面无法把文本冒充为自己的内容。
  • 恢复使用 URL 中一次性的随机 nonce;消息永远不会通过它传递,因此精心构造的链接不会显示任何内容。
  • 大纲绝不包含字段值、输入到编辑器中的文本、属性、URL 或隐藏内容;data-tour-exclude 可隐藏其他任何内容。
  • selector 无法进行探测::has() 和属性值运算符会被拒绝,因此它不能成为隐藏值的 oracle。
  • 同一个 data-tour-id 声明两次会得到 ambiguous_target。如果你渲染用户编写的 HTML,请在清理器中剥离 data-tour-*(DOMPurify:FORBID_ATTR 或 uponSanitizeAttribute hook),用 defineTarget 声明 target,并考虑 domTargets: false。
  • 通过 live region 播报,支持键盘和 Escape,支持浅色/深色、forced-colors 和 reduced-motion。
  • 零依赖;页面文本以文本形式设置,绝不以 HTML 形式设置。

参见 security 和 accessibility。

agent 宿主

真实状态:

宿主状态
启用 WebMCP 的 Chrome(flag chrome://flags/#enable-webmcp-testing,或 origin trial)可用
以 --enable-features=WebMCP 启动 Chrome 的 Playwright MCP可用
页面内 agent(从你的脚本调用的端侧或托管模型)可用,在浏览器没有 WebMCP 时配合 W3C polyfill
ChatGPT Desktop、Edge、Brave、其他未测试

设置与注意事项:agent hosts。

使用编码 agent 来添加它

一个 Agent Skill 会教 Claude Code、Cursor、Codex 等把 webmcp-tour 添加到你的应用中(声明 target、page、描述并测试):


        npx skills add fboldo/webmcp-tour --skill webmcp-tour
      

文档

Quick start · Concepts · Tools · Engine API · Examples · FAQ

贡献

开发需要 Node ≥ 22.18 和 pnpm(通过 packageManager 固定;corepack enable)。


        pnpm install
pnpm exec playwright install chromium   # once: tests run in real browsers
pnpm check       # typecheck, build, tests, size budget (gzip: engine ≤ 17 KB, script tag ≤ 17 KB)
pnpm demo        # http://127.0.0.1:4173/demo/
pnpm docs:dev    # preview the docs site
pnpm example:script-tag   # also: example:embed, example:bundled
      

node scripts/readme-gif.mjs 会重新录制上面的 GIF(需要 ffmpeg)。CI 在每次 push 和 pull request 时运行 pnpm check;请先在本地运行它。

许可证

MIT © Filipe Boldo

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by HN AI
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗