Skip to main content
Dynamic tools and resources let React components register MCP capabilities on mount and automatically unregister them on unmount. This enables UI-driven tool availability — tools exist only while the component that defines them is rendered.

useDynamicTool

Registers an MCP tool for the lifetime of the component. Supports both zod schemas (recommended) and raw JSON Schema.
When a zod schema is provided:
  • The schema is converted to JSON Schema automatically via toJSONSchema from zod/v4
  • Input is validated via safeParse before reaching your execute callback
  • Invalid input returns an error CallToolResult with issue details
  • The execute callback receives fully typed, validated args

With JSON Schema (Backward Compat)

Options

Conditional Registration

Use enabled to conditionally register/unregister tools based on application state:

Stale Closure Prevention

The execute function is stored in a ref internally, so it always captures the latest closure values. You don’t need to memoize it.

useDynamicResource

Registers an MCP resource for the lifetime of the component.

Options


useComponentTree

Exposes the DOM subtree under a ref as a JSON MCP resource. Useful for giving agents visibility into the rendered component hierarchy.

Options

Output Format

The resource returns a JSON tree with component, tag, children, and optional props:
Elements with data-component attributes use that value as component. Others fall back to the tag name.

Mount/Unmount Lifecycle

Dynamic tools and resources follow React’s effect lifecycle:
  1. Mount: The tool/resource is registered with the DynamicRegistry
  2. Update: If dependencies change, the old registration is cleaned up and a new one is created
  3. Unmount: The tool/resource is automatically unregistered
This means agents only see tools that correspond to currently rendered UI. When a user navigates away from a page, its tools disappear; when they navigate back, the tools reappear.