useDynamicTool
Registers an MCP tool for the lifetime of the component. Supports both zod schemas (recommended) and raw JSON Schema.With Zod Schema (Recommended)
schema is provided:
- The schema is converted to JSON Schema automatically via
toJSONSchemafromzod/v4 - Input is validated via
safeParsebefore reaching yourexecutecallback - Invalid input returns an error
CallToolResultwith issue details - The
executecallback receives fully typed, validated args
With JSON Schema (Backward Compat)
Options
Conditional Registration
Useenabled 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 withcomponent, tag, children, and optional props:
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:- Mount: The tool/resource is registered with the
DynamicRegistry - Update: If dependencies change, the old registration is cleaned up and a new one is created
- Unmount: The tool/resource is automatically unregistered