ModelContext Class

The tools of a document (https://webmachinelearning.github.io/webmcp/#modelcontext, document.modelContext): the functions that a page registers with a natural language description and a JSON Schema of their input, so that an agent (a script of the page or of another frame, or the application with BrowsingContext.GetModelContextTools and BrowsingContext.ExecuteModelContextToolAsync) can discover and execute them.

public sealed class ModelContext : EventTarget

Remarks

The tools are visible to the documents of the same frame tree that are same origin with the document that registered them, and to the origins listed in ModelContextRegisterToolOptions.ExposedTo. The documents that are not allowed to use the tools permissions policy feature (its default allowlist is 'self') cannot use the API.

See also

Inheritance
EventTarget ModelContext

Properties

NameDescription
OnToolActivatedGets or sets the handler of the toolactivated event (ToolActivatedEvent).
OnToolCancelGets or sets the handler of the toolcancel event (ToolCancelEvent).
OnToolChangeGets or sets the handler of the toolchange event, fired when a tool exposed to the document is registered or unregistered.
ToolsReadyGets whether the tools of the document are ready to be read: false while a promise given to ModelContext.WaitForToolsUntil is pending (webmachinelearning/webmcp#373).

OnToolActivated

ModelContext.cs

Gets or sets the handler of the toolactivated event (ToolActivatedEvent).

public Action<Event>? OnToolActivated { get; set; }

OnToolCancel

ModelContext.cs

Gets or sets the handler of the toolcancel event (ToolCancelEvent).

public Action<Event>? OnToolCancel { get; set; }

OnToolChange

ModelContext.cs

Gets or sets the handler of the toolchange event, fired when a tool exposed to the document is registered or unregistered.

public Action<Event>? OnToolChange { get; set; }

ToolsReady

ModelContext.cs
  • Experimental (WEBPLATEXP0018)

Gets whether the tools of the document are ready to be read: false while a promise given to ModelContext.WaitForToolsUntil is pending (webmachinelearning/webmcp#373).

[Experimental("WEBPLATEXP0018", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public bool ToolsReady { get; }

Methods

NameDescription
ExecuteTool(Tool, object?, ModelContextExecuteToolOptions?)Executes a tool returned by ModelContext.GetTools (https://webmachinelearning.github.io/webmcp/#dom-modelcontext-executetool): the input arguments are serialized to JSON, the execute callback of the tool is invoked in its document, and the task is completed with the JSON serialization of its result. The task fails with an InvalidStateError when this document is not fully active, when the document of the tool was destroyed or belongs to another frame tree; with a NotFoundError when the tool was unregistered or is no longer exposed to this document; with an OperationError when the tool fails or its result cannot be serialized to JSON; with an AbortError when the document of the tool is unloaded; and with the abort reason when the signal of the options is aborted (the signal given to the execute callback is then aborted, and a toolcancel event is fired at the model context of the tool).
GetTools(ModelContextGetToolOptions?)Returns the tools exposed to the document by the documents of its frame tree (https://webmachinelearning.github.io/webmcp/#dom-modelcontext-gettools), sorted by name: the tools of the documents same origin with this document, and the tools of the documents whose origin is listed in ModelContextGetToolOptions.FromOrigins that expose them to the origin of this document.
RegisterTool(ModelContextTool, ModelContextRegisterToolOptions?)Registers a tool (https://webmachinelearning.github.io/webmcp/#dom-modelcontext-registertool). The returned task fails with an InvalidStateError when the document is not fully active, when a tool with the same name is registered, when the name is not valid or when the description is empty; with a NotAllowedError when the document is not allowed to use the tools feature; with a SecurityError when an origin of ModelContextRegisterToolOptions.ExposedTo is not a potentially trustworthy URL; and with a TypeError when a schema cannot be serialized to JSON. The tool is unregistered when the signal of the options is aborted.
WaitForToolsUntil(Task)Signals that the document is registering or updating its tools until promise is settled (webmachinelearning/webmcp#373): ModelContext.ToolsReady is false meanwhile, and BrowsingContext.ModelContextToolsReady tells the application whether the tools of a frame tree are ready. Throws an InvalidStateError when the document is not fully active, and a NotAllowedError when it is not allowed to use the tools feature.

ExecuteTool(Tool, object?, ModelContextExecuteToolOptions?)

ModelContext.cs

Executes a tool returned by ModelContext.GetTools (https://webmachinelearning.github.io/webmcp/#dom-modelcontext-executetool): the input arguments are serialized to JSON, the execute callback of the tool is invoked in its document, and the task is completed with the JSON serialization of its result. The task fails with an InvalidStateError when this document is not fully active, when the document of the tool was destroyed or belongs to another frame tree; with a NotFoundError when the tool was unregistered or is no longer exposed to this document; with an OperationError when the tool fails or its result cannot be serialized to JSON; with an AbortError when the document of the tool is unloaded; and with the abort reason when the signal of the options is aborted (the signal given to the execute callback is then aborted, and a toolcancel event is fired at the model context of the tool).

public Task<string> ExecuteTool(Tool tool, object? inputObject = null, ModelContextExecuteToolOptions? options = null)

Parameters

tool Tool
The tool.
inputObject object?
The input arguments: a JavaScript object, or a JsonNode; null is an empty object.
options ModelContextExecuteToolOptions?
The options.

Returns

Task<string>

See also

GetTools(ModelContextGetToolOptions?)

ModelContext.cs

Returns the tools exposed to the document by the documents of its frame tree (https://webmachinelearning.github.io/webmcp/#dom-modelcontext-gettools), sorted by name: the tools of the documents same origin with this document, and the tools of the documents whose origin is listed in ModelContextGetToolOptions.FromOrigins that expose them to the origin of this document.

public Task<IReadOnlyList<Tool>> GetTools(ModelContextGetToolOptions? options = null)

RegisterTool(ModelContextTool, ModelContextRegisterToolOptions?)

ModelContext.cs

Registers a tool (https://webmachinelearning.github.io/webmcp/#dom-modelcontext-registertool). The returned task fails with an InvalidStateError when the document is not fully active, when a tool with the same name is registered, when the name is not valid or when the description is empty; with a NotAllowedError when the document is not allowed to use the tools feature; with a SecurityError when an origin of ModelContextRegisterToolOptions.ExposedTo is not a potentially trustworthy URL; and with a TypeError when a schema cannot be serialized to JSON. The tool is unregistered when the signal of the options is aborted.

public Task RegisterTool(ModelContextTool tool, ModelContextRegisterToolOptions? options = null)

WaitForToolsUntil(Task)

ModelContext.cs
  • Experimental (WEBPLATEXP0018)

Signals that the document is registering or updating its tools until promise is settled (webmachinelearning/webmcp#373): ModelContext.ToolsReady is false meanwhile, and BrowsingContext.ModelContextToolsReady tells the application whether the tools of a frame tree are ready. Throws an InvalidStateError when the document is not fully active, and a NotAllowedError when it is not allowed to use the tools feature.

[Experimental("WEBPLATEXP0018", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public void WaitForToolsUntil(Task promise)