เผยแพร่: 18 พฤษภาคม 2026
| วิดีโออธิบาย | เว็บ | ส่วนขยาย | สถานะของ Chrome | ความตั้งใจ |
|---|---|---|---|---|
| GitHub | ดู | ความตั้งใจที่จะทดลอง |
คุณใช้ WebMCP Imperative API เพื่อกำหนดเครื่องมือหลายประเภทด้วย JavaScript มาตรฐานได้ เครื่องมือของคุณสามารถดำเนินการฟังก์ชันต่างๆ ได้ เช่น การป้อนข้อมูลในแบบฟอร์ม การไปยังส่วนต่างๆ ของเว็บไซต์ และการจัดการสถานะ
โปรดอ่านตัวอย่าง Use Case ก่อนใช้ 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 to-do list",
inputSchema: {
type: "object",
properties: { text: { type: "string" } },
},
execute: ({ 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();
กิจกรรม
เฟรมสามารถฟังเหตุการณ์ toolchange ใน navigator.modelContext เพื่อรับการแจ้งเตือนเมื่อรายการเครื่องมือที่พร้อมใช้งานมีการเปลี่ยนแปลง
navigator.modelContext.addEventListener("toolchange", (event) => {
// Tools have changed.
});
iframe แบบข้ามต้นทาง
WebMCP รองรับ iframe แบบข้ามต้นทางที่ใช้นโยบายสิทธิ์และ Origin Gating ที่ชัดเจน
นโยบายสิทธิ์
ระบบจะปิดใช้การลงทะเบียนเครื่องมือโดยค่าเริ่มต้นใน iframe แบบข้ามต้นทาง หน้าเว็บต้อง
มอบสิทธิ์เข้าถึงโดยใช้tools
นโยบายสิทธิ์
<iframe src="https://example.com" allow="tools"></iframe>
การเปิดเผยแหล่งที่มา
เครื่องมือจะไม่พร้อมใช้งานสำหรับเอกสารแบบ Cross-Origin โดยค่าเริ่มต้น คุณสามารถใช้
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 Status
- เข้าร่วมโปรแกรมทดลองใช้ก่อนเปิดตัว เพื่อดู API ใหม่ๆ ก่อนใครและรับสิทธิ์เข้าถึงรายชื่ออีเมลของเรา
- หากมีความคิดเห็นเกี่ยวกับการติดตั้งใช้งานของ Chrome โปรดรายงานข้อบกพร่องของ Chromium