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
| Option | Type | Default | Description |
|---|---|---|---|
name | string | — | Required. MCP tool name |
description | string | — | Required. Description for agents |
schema | z.ZodObject | — | Zod schema (mutually exclusive with inputSchema) |
inputSchema | Record<string, unknown> | — | JSON Schema (mutually exclusive with schema) |
execute | (args) => Promise<CallToolResult> | — | Required. Tool handler |
enabled | boolean | true | Conditionally enable/disable |
server | string | — | Target a named server |
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
| Option | Type | Default | Description |
|---|---|---|---|
uri | string | — | Required. Resource URI |
name | string | — | Required. Human-readable name |
description | string | — | Description for agents |
mimeType | string | — | Content MIME type |
read | () => Promise<ReadResourceResult> | — | Required. Read handler |
enabled | boolean | true | Conditionally enable/disable |
server | string | — | Target a named server |
useComponentTree
Exposes the DOM subtree under a ref as a JSON MCP resource. Useful for giving agents visibility into the rendered component hierarchy.Options
| Option | Type | Default | Description |
|---|---|---|---|
rootRef | RefObject<HTMLElement | null> | — | Required. Root element ref |
uri | string | 'react://component-tree' | Resource URI |
maxDepth | number | 10 | Maximum traversal depth |
includeProps | boolean | false | Include data-* attributes as props |
server | string | — | Target a named server |
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