Starting and stopping from JavaScript

The SubcontextWebMCP object, the result of init, and the console messages the script writes.

The script adds one object to the page, window.SubcontextWebMCP:

Member What it does
init(options) Fetches your tool list and registers each tool. If tools are already registered (or an earlier init is still running), it removes or cancels those first, so you can call it again with another key. Returns a promise. The promise always resolves with a result (below), so you don't need to catch errors.
destroy() Removes every tool the script registered. An init that is still running is cancelled. It registers nothing and reports cancelled. Safe to call at any time.
version The script's version, for example "0.1.0".

The result of init#

Field Type Meaning
ok boolean true only when status is registered.
status string registered: the tools are offered. unsupported: this browser has no WebMCP. failed: there was no key, the tool list couldn't be fetched, or the browser refused a tool (see error). cancelled: destroy() or another init ran before this one finished.
tools string[] The names of the tools registered. Empty unless status is registered (and it can be empty then too, if no tools are ticked).
org string Your firm's name, when registered.
error string Why, when failed. See Troubleshooting.

For example, to stop offering the tools when a visitor opts out of AI assistants:

js
SubcontextWebMCP.destroy()

Behaviour on the page#

  • Tool names must be unique on the page. If another script on your page has already registered a tool with the same name as one of these, the browser refuses it. The script then removes the tools it had already registered (so nothing is left half done) and init reports failed.
  • No events or callbacks. The script doesn't fire page events. Its only outputs are the tools it registers and its console messages.
  • Calling a tool. Each registered tool has an execute(input) function, which is what the AI assistant calls. It returns a promise of the tool's result, or fails with the error message from Subcontext.
  • Other Subcontext embeds. It works alongside the chat widget, lead form and quote form on the same page. It has its own key and doesn't interact with them. What assistants do through it shows up in the same inbox, CRM and insights as everything else, marked as the WebMCP channel.

Console messages#

Every message starts with Subcontext.webmcp. Filter the console on that.

Level Message When
info v0.1.0 The script loaded.
info this browser doesn't support WebMCP (no document.modelContext or navigator.modelContext): no tools registered No WebMCP in this browser. Nothing else happens.
info registered N tool(s) for <your firm> It worked.
error failed to register WebMCP tools followed by the error No key, the tool list couldn't be fetched, or the browser refused a tool.
debug fetching config from <address>, then config fetched: ... With debug on: the tool list request and what came back.
debug registered tool <name>, unregistered N tool(s) With debug on: each tool added, and tools removed (by destroy(), another init, or after a failure).
debug call <name> with the input, then <name> returned or <name> failed With debug on: each tool call.
debug cancelled before it finished With debug on: destroy() or another init cancelled a start that was still running.

Stuck, or something missing?

Get in touch and a person on the team will help.

Contact us