Imperative API

Alexandra Klepper
Alexandra Klepper

תאריך פרסום: 18 במאי 2026

סרטון הסבר פיתוח אתרים תוספים הסטטוס של Chrome הרציונל
GitHub תקופת ניסיון למפתחים ל-תקופת ניסיון למפתחים תקופת ניסיון למפתחים ל-תקופת ניסיון למפתחים תצוגה הבעת עניין בהשתתפות בניסוי

אפשר להשתמש ב-WebMCP Imperative API כדי להגדיר הרבה סוגים של כלים באמצעות JavaScript רגיל. הכלים יכולים לבצע פונקציות שונות, כמו הזנת טופס, ניווט באתר וניהול מצב.

לפני שמשתמשים ב-API הזה, כדאי לקרוא על תרחישי שימוש לדוגמה.

מתן הקשר למודל

משתמשים בממשק modelContext כדי לרשום כלים. כדי לרשום כלי צריך להזין שם, תיאור וסכימת קלט עם מאפיינים רלוונטיים.

משתמשים ב-registertool כדי להוסיף כלי יחיד להקשר של המודל.

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}`;
  },
});

קבלת סטטוס ההזמנה

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.
  },
});

אפשר להסיר כלי באמצעות AbortSignal, כשמעבירים אותו כפרמטר אופציונלי.

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();

אינטראקציה ושיתוף משוב

ההצעה לגבי WebMCP נמצאת כרגע בתהליכי דיון ויכול להיות שהיא תשתנה. אם תנסו את ה-API הזה ונשמח לקבל מכם משוב.