Imperative API

Alexandra Klepper
Alexandra Klepper

Xuất bản: Ngày 18 tháng 5 năm 2026

Video giải thích Web Phần mở rộng Trạng thái của Chrome Mục đích
GitHub Bản dùng thử dành cho nhà phát triển Dùng thử dành cho nhà phát triển Bản dùng thử dành cho nhà phát triển Dùng thử dành cho nhà phát triển Xem Ý định thử nghiệm

Bạn có thể sử dụng WebMCP Imperative API để xác định nhiều loại công cụ bằng JavaScript tiêu chuẩn. Các công cụ của bạn có thể thực hiện nhiều chức năng, chẳng hạn như nhập biểu mẫu, điều hướng trang web và quản lý trạng thái.

Trước khi sử dụng API này, hãy đọc về các trường hợp sử dụng ví dụ.

Cung cấp bối cảnh cho mô hình

Sử dụng giao diện modelContext để đăng ký các công cụ. Để đăng ký công cụ, bạn cần có tên, nội dung mô tả và giản đồ đầu vào có các thuộc tính liên quan,

Dùng registertool để thêm một công cụ vào ngữ cảnh mô hình.

WebMCPza Maker

navigator.modelContext.registerTool({
  name: 'toggle_layer',
  description: 'Control pizza layers (sauce, cheese). Use "add", "remove", or "toggle".',
  inputSchema: {
    type: 'object',
    properties: {
      layer: { type: 'string', enum: ['sauce-layer', 'cheese-layer'] },
      action: { type: 'string', enum: ['add', 'remove', 'toggle'] },
    },
    required: ['layer'],
  },
  execute: ({ layer, action }) => {
    toggleLayer(layer, action);
    return `Performed ${action || 'toggle'} on layer: ${layer}`;
  },
});

Lấy trạng thái đơn đặt hàng

navigator.modelContext.registerTool({
  name: 'get_order_status',
  description: 'Search orders in a given timeframe. Returns order number, shipping status and location',
  inputSchema: {
    "type": "object",
    "properties": {
      "timeframe": { "type": "string", "oneOf": [
        { "type": "string", "const": "today", "title": "Today" },
        { "type": "string", "const": "yesterday", "title": "Yesterday" },
        { "type": "string", "const": "last_7_days", "title": "Last 7 Days" },
        { "type": "string", "const": "last_30_days", "title": "Last 30 Days" },
        { "type": "string", "const": "last_6_months", "title": "Last 6 Months" }],
      "enum": [ "today", "yesterday", "last_7_days", "last_30_days", "last_6_months" ],
      "description": "Timeframe for the order lookup." }
    },
    "required": [ "timeframe" ]
  },
  execute: ({ timeframe }) => {
    // Add your API or database logic here to fetch and return the order data as a string.
  },
});

Bạn có thể xoá một công cụ bằng AbortSignal khi được truyền dưới dạng một tham số không bắt buộc.

const addTodoTool = {
  name: "addTodo",
  description: "Add a new item to the todo list",
  inputSchema: {
    type: "object",
    properties: { text: { type: "string" } },
  },
  execute: ({ text }) => {
    // You should handle the persistence logic here (omitted for demo)
    return `Added todo: ${text}`;
  },
  annotations: {
    readOnlyHint: false,
    untrustedContentHint: true
  },
};
const controller = new AbortController();
navigator.modelContext.registerTool(addTodoTool, { signal: controller.signal });

// Unregister the tool later...
controller.abort();

Tương tác và chia sẻ ý kiến phản hồi

WebMCP đang được thảo luận tích cực và có thể thay đổi trong tương lai. Nếu bạn dùng thử API này và có ý kiến phản hồi, chúng tôi rất mong được lắng nghe.