```WebMCP 提案```
WebMCP Proposal

原始链接: https://webmachinelearning.github.io/webmcp/

## WebMCP API 摘要 WebMCP API 引入了一个新的 JavaScript 接口,使 Web 应用程序能够将功能作为“工具”暴露给 AI 代理——包括浏览器集成或外部代理,如 ChatGPT。本质上,网页成为模型上下文协议 (MCP) 服务器,直接在客户端脚本中实现工具。 这允许用户和代理在 Web 界面内进行协作工作流程,利用现有的应用程序逻辑。开发者使用可通过 `navigator.modelContext` 访问的 `ModelContext` 接口来注册和管理这些工具。工具使用 `ModelContextTool` 字典定义,需要一个唯一的名称、自然语言描述、输入模式(使用 JSON Schema)和一个 `execute` 回调函数。 该 API 提供了 `provideContext`(注册工具)、`clearContext`(注销所有工具)、`registerTool`(添加单个工具)和 `unregisterTool`(删除特定工具)等方法。`ModelContextClient` 接口允许工具请求用户交互。安全性和可访问性是规范内的关键考虑因素。该 API 旨在弥合 Web 应用程序和 AI 代理之间的差距,促进更集成和用户控制的 AI 体验。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 WebMCP 提案 (webmachinelearning.github.io) 16 分,作者 Alifatisk 50 分钟前 | 隐藏 | 过去 | 收藏 | 1 条评论 帮助 Flux159 3 分钟前 [–] Chrome 几天前也宣布了早期预览版:https://developer.chrome.com/blog/webmcp-epp 我认为 github 仓库的 README 可能更有用:https://github.com/webmachinelearning/webmcp?tab=readme-ov-f... 此外,之前的实现可能也有参考价值:https://github.com/MiguelsPizza/WebMCP 和 https://github.com/jasonjmcghee/WebMCP 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文
WebMCP

Abstract

The WebMCP API enables web applications to provide JavaScript-based tools to AI agents.

Status of this document

1. Introduction

WebMCP API is a new JavaScript interface that allows web developers to expose their web application functionality as “tools” - JavaScript functions with natural language descriptions and structured schemas that can be invoked by agents, browser’s agents, and assistive technologies. Web pages that use WebMCP can be thought of as Model Context Protocol [MCP] servers that implement tools in client-side script instead of on the backend. WebMCP enables collaborative workflows where users and agents work together within the same web interface, leveraging existing application logic while maintaining shared context and user control.

2. Terminology

An agent is an autonomous assistant that can understand a user’s goals and take actions on the user’s behalf to achieve them. Today, these are typically implemented by large language model (LLM) based AI platforms, interacting with users via text-based chat interfaces.

A browser’s agent is an agent provided by or through the browser that could be built directly into the browser or hosted by it, for example, via an extension or plug-in.

An AI platform is a provider of agentic assistants such as OpenAI’s ChatGPT, Anthropic’s Claude, or Google’s Gemini.

3. Security and privacy considerations

4. Accessibility considerations

5. API

The Navigator interface is extended to provide access to the ModelContext.

partial interface Navigator {
  [SecureContext, SameObject] readonly attribute ModelContext modelContext;
};

5.2. ModelContext Interface

The ModelContext interface provides methods for web applications to register and manage tools that can be invoked by agents.

[Exposed=Window, SecureContext]
interface ModelContext {
  undefined provideContext(optional ModelContextOptions options = {});
  undefined clearContext();
  undefined registerTool(ModelContextTool tool);
  undefined unregisterTool(DOMString name);
};
navigator.modelContext.provideContext(options)

Registers the provided context (tools) with the browser. This method clears any pre-existing tools and other context before registering the new ones.

navigator.modelContext.clearContext()

Unregisters all context (tools) with the browser.

navigator.modelContext.registerTool(tool)

Registers a single tool without clearing the existing set of tools. The method throws an error, if a tool with the same name already exists, or if the inputSchema is invalid.

navigator.modelContext.unregisterTool(name)

Removes the tool with the specified name from the registered set.

The provideContext(options) method steps are:
  1. TODO: fill this out.

The clearContext() method steps are:
  1. TODO: fill this out.

The registerTool(tool) method steps are:
  1. TODO: fill this out.

The unregisterTool(name) method steps are:
  1. TODO: fill this out.

5.2.1. ModelContextOptions Dictionary

dictionary ModelContextOptions {
  sequence<ModelContextTool> tools = [];
};
options["tools"]

A list of tools to register with the browser. Each tool name in the list is expected to be unique.

5.2.2. ModelContextTool Dictionary

The ModelContextTool dictionary describes a tool that can be invoked by agents.

dictionary ModelContextTool {
  required DOMString name;
  required DOMString description;
  object inputSchema;
  required ToolExecuteCallback execute;
  ToolAnnotations annotations;
};

dictionary ToolAnnotations {
  boolean readOnlyHint;
};

callback ToolExecuteCallback = Promise<any> (object input, ModelContextClient client);
tool["name"]

A unique identifier for the tool. This is used by agents to reference the tool when making tool calls.

tool["description"]

A natural language description of the tool’s functionality. This helps agents understand when and how to use the tool.

tool["inputSchema"]

A JSON Schema [JSON-SCHEMA] object describing the expected input parameters for the tool.

tool["execute"]

A callback function that is invoked when an agent calls the tool. The function receives the input parameters and a ModelContextClient object.

The function can be asynchronous and return a promise, in which case the agent will receive the result once the promise is resolved.

tool["annotations"]

Optional annotations providing additional metadata about the tool’s behavior.

The ToolAnnotations dictionary provides optional metadata about a tool:

annotations["readOnlyHint"]

If true, indicates that the tool does not modify any state and only reads data. This hint can help agents make decisions about when it is safe to call the tool.

5.2.3. ModelContextClient Interface

The ModelContextClient interface represents an agent executing a tool provided by the site through the ModelContext API.

[Exposed=Window, SecureContext]
interface ModelContextClient {
  Promise<any> requestUserInteraction(UserInteractionCallback callback);
};

callback UserInteractionCallback = Promise<any> ();
client.requestUserInteraction(callback)

Asynchronously requests user input during the execution of a tool.

The callback function is invoked to perform the user interaction (e.g., showing a confirmation dialog), and the promise resolves with the result of the callback.

The requestUserInteraction(callback) method steps are:
  1. TODO: fill this out.

6. Acknowledgements

Thanks to Brandon Walderman, Leo Lee, Andrew Nolan, David Bokan, Khushal Sagar, Hannah Van Opstal, Sushanth Rajasankar for the initial explainer, proposals and discussions that established the foundation for this specification.

Also many thanks to Alex Nahas and Jason McGhee for sharing early implementation experience.

Finally, thanks to the participants of the Web Machine Learning Community Group for feedback and suggestions.

Index

Terms defined by this specification

Terms defined by reference

  • [HTML] defines the following terms:
  • [WAI-ARIA-1.2] defines the following terms:
  • [WEBIDL] defines the following terms:
    • DOMString
    • Exposed
    • Promise
    • SameObject
    • SecureContext
    • any
    • boolean
    • object
    • sequence
    • undefined

References

Normative References

[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[JSON-SCHEMA]
JSON Schema: A Media Type for Describing JSON Documents. URL: https://json-schema.org/draft/2020-12/json-schema-core.html
[MCP]
Model Context Protocol (MCP) Specification. URL: https://modelcontextprotocol.io/specification/latest
[WAI-ARIA-1.2]
Joanmarie Diggs; et al. Accessible Rich Internet Applications (WAI-ARIA) 1.2. URL: https://w3c.github.io/aria/
[WEBIDL]
Edgar Chen; Timothy Gu. Web IDL Standard. Living Standard. URL: https://webidl.spec.whatwg.org/

IDL Index

partial interface Navigator {
  [SecureContext, SameObject] readonly attribute ModelContext modelContext;
};

[Exposed=Window, SecureContext]
interface ModelContext {
  undefined provideContext(optional ModelContextOptions options = {});
  undefined clearContext();
  undefined registerTool(ModelContextTool tool);
  undefined unregisterTool(DOMString name);
};

dictionary ModelContextOptions {
  sequence<ModelContextTool> tools = [];
};

dictionary ModelContextTool {
  required DOMString name;
  required DOMString description;
  object inputSchema;
  required ToolExecuteCallback execute;
  ToolAnnotations annotations;
};

dictionary ToolAnnotations {
  boolean readOnlyHint;
};

callback ToolExecuteCallback = Promise<any> (object input, ModelContextClient client);

[Exposed=Window, SecureContext]
interface ModelContextClient {
  Promise<any> requestUserInteraction(UserInteractionCallback callback);
};

callback UserInteractionCallback = Promise<any> ();

联系我们 contact @ memedata.com