Imperative API

Alexandra Klepper
Alexandra Klepper

Yayınlanma tarihi: 18 Mayıs 2026

Açıklayıcı Web Uzantılar Chrome Durumu Amaç
GitHub Geliştirici denemesi Geliştirici denemesi Geliştirici denemesi Geliştirici denemesi Görünüm Deneme Yapma Amacı (Intent to Experiment)

Standart JavaScript ile birçok araç türünü tanımlamak için WebMCP Imperative API'yi kullanabilirsiniz. Araçlarınız, form girişi, site gezinme ve durum yönetimi gibi farklı işlevleri yerine getirebilir.

Bu API'yi kullanmadan önce kullanım alanları örnekleri hakkında bilgi edinin.

Model bağlamı sağlama

Araçları kaydetmek için modelContext arayüzünü kullanın. Araç kaydı için ad, açıklama ve alakalı özelliklere sahip giriş şeması gerekir.

Model bağlamına tek bir araç eklemek için registertool simgesini kullanın.

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

Sipariş durumunu alma

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

İsteğe bağlı parametre olarak iletildiğinde AbortSignal ile bir aracı kaldırabilirsiniz.

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

Etkileşim kurma ve geri bildirim paylaşma

WebMCP aktif olarak tartışılmaktadır ve gelecekte değişebilir. Bu API'yi denerseniz ve geri bildiriminiz olursa lütfen bizimle paylaşın.