Skip to main content

useModelContextTool

Registers a WebMCP tool for the lifetime of the calling component, so an in-browser AI agent can discover and call it via document.modelContext.

This is a function exported by @mapcairn/react, checked against version 0.2.0.

Signature​

import { useModelContextTool } from "@mapcairn/react";

function useModelContextTool<TInput = unknown>(definition: ModelContextToolDefinition<TInput> | null): void;

Type parameters​

  • TInput?

Parameters​

  • definition (ModelContextToolDefinition<TInput> | null): The tool to register, or null to skip registration (e.g. a tool that only makes sense when some optional data is present).

Returns​

void

Remarks​

A no-op, including in SSR, wherever document.modelContext is unsupported or denied by Permissions-Policy; registerTool can reject asynchronously (e.g. a Permissions-Policy denial delivered as a promise) or throw synchronously (e.g. a spec-validation error on a malformed inputSchema), and both are caught the same way: never re-thrown into the component tree, since there's nothing meaningful to surface to the user for what is inherently a progressive enhancement, but still logged via console.error with the tool's name so a genuinely malformed inputSchema from a real consumer is visible during development. Re-registers only when definition's name changes identity (including transitions to/from null); description and inputSchema are captured at registration time, since every current caller's schema is static for a given tool name, and a fresh execute closure on every render (the common case, since it closes over current component state) is read via a ref instead, so it can't itself trigger register/unregister churn.

See also​

Source: packages/react/src/hooks/useModelContextTool.ts:101