Skip to content

✨ [Feature]: Provide a lightweight WebMCP polyfill entry point #538

Description

@onlybye

此特性解决了什么问题 (What problem does this feature solve)

A browser library using @opentiny/next-sdk@0.4.4 may only need to initialize the WebMCP polyfill and then use the standard document.modelContext API:

const existing = document.modelContext;
if (!existing) {
  const module = await import('@opentiny/next-sdk/core');
  module.initializeBuiltinWebMCP();
}

However, @opentiny/next-sdk/core also exports AJV, Zod, OpenTiny Auth, MCP client/server classes, transports, and other functionality. A dynamic namespace import makes bundlers retain a large portion of this export graph. Installing the package also brings AI SDK providers, page-agent, QR code, MCP transport, validation, and other dependencies that are not needed by a polyfill-only consumer.

Measured with esbuild 0.25.9, platform: browser, format: esm, target: es2022, splitting and minification enabled:

Integration Raw Gzip
Dynamic import('@opentiny/next-sdk/core') 1,543,212 bytes 372,582 bytes
Local adapter with a static named import from /core 588,967 bytes 144,334 bytes
Direct @mcp-b/webmcp-polyfill initialization experiment 34,395 bytes 9,783 bytes

The local named-import adapter is a useful workaround, but the remaining chunk and installation dependency graph are still much larger than the actual capability being used.

This matters for browser applications where WebMCP Tool registration is optional and lazily loaded: the first Tool registration should not need to download a general AI/MCP client-server SDK.

提议的 API 是什么样的 (What does the proposed API look like)

Please provide a stable, lightweight public subpath that only exposes WebMCP feature detection and polyfill initialization:

import { initializeBuiltinWebMCP } from '@opentiny/next-sdk/webmcp-polyfill';

if (!document.modelContext) {
  initializeBuiltinWebMCP();
}

An independent package would also work:

import { initializeBuiltinWebMCP } from '@opentiny/webmcp-polyfill';

Suggested properties:

  • Depends only on the WebMCP polyfill and its minimum schema/type dependencies.
  • Does not import AI providers, MCP remote transports, page-agent, QR code, or OpenTiny Auth.
  • Provides ESM and TypeScript declarations through a documented exports entry.
  • Keeps initialization explicit and idempotent; importing the module should not require loading unrelated runtime features.
  • Preserves the current behavior of using native document.modelContext when available and installing the polyfill only when needed.
  • Includes a bundle-size regression check for the lightweight entry.

This would let consumers stay on the supported OpenTiny API instead of importing a transitive dependency or a hashed internal build file.

项目名称 (What is your project name)

Not applicable (minimal reproduction)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions