תאריך פרסום: 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: async ({ layer, action }) => {
await 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: async ({ 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 to-do list",
inputSchema: {
type: "object",
properties: { text: { type: "string" } },
},
execute: async ({ text }) => {
// You should handle the persistence logic here (omitted for demo)
return `Added to-do: ${text}`;
},
annotations: {
readOnlyHint: false,
untrustedContentHint: true
},
};
const controller = new AbortController();
navigator.modelContext.registerTool(addTodoTool, { signal: controller.signal });
// Unregister the tool later...
controller.abort();
לכלים
כדי לראות את הכלים הזמינים, משתמשים ב-navigator.modelContext.getTools(). השיטה האסינכרונית הזו מחזירה רשימה של כלים שהמסמך שמפעיל את השיטה מורשה לגשת אליהם על סמך המקור שלו.
const [tool] = await navigator.modelContext.getTools();
console.log(tool);
// {
// annotations: { readOnlyHint: false, untrustedContentHint: true },
// description: "Add a new item to the to-do list",
// inputSchema: '{"type":"object","properties":{"text":{"type":"string"}}}',
// name: "addTodo",
// origin: "https://example.com",
// window: Window {window: Window, self: Window, ...},
// }
הפעלת הכלי
כדי להפעיל ידנית כלי שזוהה ב-getTools(), קוראים ל-navigator.modelContext.executeTool() עם ארגומנטים של קלט כמחרוזת JSON תקינה. השיטה האסינכרונית הזו מחזירה את התוצאה של הפעלת הכלי, או null כשמופעלת ניווט.
const result = await navigator.modelContext.executeTool(tool, '{"text": "Buy milk"}');
console.log(result);
// 'Added to-do: Buy milk'
אפשר לבטל הפעלה של כלי בהמתנה באמצעות AbortSignal, כשמעבירים אותו כפרמטר אופציונלי.
const controller = new AbortController();
navigator.modelContext.executeTool(tool, '{"text": "Buy milk"}', {
signal: controller.signal,
});
// Cancel tool execution later...
controller.abort();
אירועים
אפשר להגדיר ל-Frames האזנה לאירוע toolchange ב-navigator.modelContext כדי לקבל הודעה כשרשימת הכלים הזמינים משתנה.
navigator.modelContext.addEventListener("toolchange", (event) => {
// Tools have changed.
});
מסגרות iframe חוצות-מקורות
WebMCP תומך במסגרות iframe ממקורות שונים שמשתמשות גם במדיניות הרשאות וגם בסינון מפורש של מקורות.
מדיניות הרשאות
כברירת מחדל, רישום כלי מושבת ב-iframes חוצי-מקורות. דף צריך להעניק גישה באמצעות tools
מדיניות ההרשאות:
<iframe src="https://example.com" allow="tools"></iframe>
חשיפה לנגיף
כברירת מחדל, כלים לא זמינים למסמכים ממקורות שונים. אפשר להשתמש במערך exposedTo בתוך registerTool כדי לציין רשימה של מקורות ספציפיים שמורשים להציג כלי ולהפעיל אותו. המערך הזה תומך רק במקורות עם פרוטוקול HTTPS.
navigator.modelContext.registerTool({
name: 'my_shared_tool',
description: 'Shared across origins',
// ...
}, {
exposedTo: ['https://trusted.com', 'https://partner.org']
});
אינטראקציה ושיתוף משוב
ההצעה לגבי WebMCP נמצאת כרגע בתהליכי דיון ויכול להיות שהיא תשתנה. אם תנסו את ה-API הזה ונשמח לקבל מכם משוב.
- לקרוא את ההסבר על WebMCP, לשאול שאלות ולהשתתף בדיון.
- קרא שיטות מומלצות לשימוש ב-WebMCP
- אפשר לבדוק את ההטמעה ב-Chrome בסטטוס של Chrome.
- הצטרפות לתוכנית הגישה המוקדמת כדי לקבל הצצה מוקדמת לממשקי API חדשים וגישה לרשימת התפוצה שלנו.
- אם יש לכם משוב על ההטמעה של Chrome, אתם יכולים לדווח על באג ב-Chromium.