Open History to see the last commit
1import { useEffect, useRef, useState } from 'react'
2import { registerWebMCPTools, subscribeWebMCPTools } from '@tanstack/ai-client'
3import type {
4 AnyClientTool,
5 InferredClientContext,
6 RegisterWebMCPToolsOptions,
7 SubscribeWebMCPToolsOptions,
8} from '@tanstack/ai-client'
9
10/** Options for the React {@link useRegisterWebMCPTools} lifecycle hook. */
11export type UseRegisterWebMCPToolsOptions<
12 TTools extends ReadonlyArray<AnyClientTool>,
13 TContext = InferredClientContext<TTools>,
14> = Omit<RegisterWebMCPToolsOptions<TTools, TContext>, 'signal'> & {
15 /** Receives an asynchronous registration failure. */
16 onError?: (error: unknown) => void
17}
18
19/** @deprecated Use `UseRegisterWebMCPToolsOptions`. Removed in 1.0.0. */
20export type UseWebMCPToolsOptions<
21 TTools extends ReadonlyArray<AnyClientTool>,
22 TContext = InferredClientContext<TTools>,
23> = UseRegisterWebMCPToolsOptions<TTools, TContext>
24
25type UseRegisterWebMCPToolsArguments<
26 TTools extends ReadonlyArray<AnyClientTool>,
27 TContext,
28> =
29 RegisterWebMCPToolsOptions<TTools, TContext> extends { context: unknown }
30 ? [options: UseRegisterWebMCPToolsOptions<TTools, TContext>]
31 : [options?: UseRegisterWebMCPToolsOptions<TTools, TContext>]
32
33/**
34 * Registers client tools with WebMCP for the lifetime of a React component.
35 *
36 * The hook replaces the registration when `tools` or `options` changes.
37 * Unsupported browsers and server rendering do not register tools.
38 *
39 * @param tools - The executable client tools to expose through WebMCP.
40 * @param options - Runtime context, per-tool options, and an error callback.
41 *
42 * @example
43 * ```tsx
44 * useRegisterWebMCPTools([searchProducts], {
45 * toolOptions: { searchProducts: { title: 'Search products' } },
46 * })
47 * ```
48 */
49export function useRegisterWebMCPTools<
50 const TTools extends ReadonlyArray<AnyClientTool>,
51 TContext = InferredClientContext<TTools>,
52>(
53 tools: TTools,
54 ...[options]: UseRegisterWebMCPToolsArguments<TTools, TContext>
55) {
56 useEffect(() => {
57 const controller = new AbortController()
58 const { onError, ...registrationOptions } = options ?? {}
59
60 registerWebMCPTools(tools, {
61 ...registrationOptions,
62 signal: controller.signal,
63 }).catch((error) => {
64 if (!controller.signal.aborted) onError?.(error)
65 })
66
67 return () => controller.abort()
68 }, [tools, options])
69}
70
71/**
72 * @deprecated Use `useRegisterWebMCPTools`. Removed in 1.0.0.
73 * @alias
74 */
75export const useWebMCPTools = useRegisterWebMCPTools
76
77/** Options for {@link usePageWebMCPTools}. */
78export type UsePageWebMCPToolsOptions = Omit<
79 SubscribeWebMCPToolsOptions,
80 'signal'
81>
82
83/**
84 * Returns the WebMCP tools on the page as client tools for `useChat`.
85 *
86 * The list starts empty and updates when the page registers or removes a
87 * tool. The hook reads the latest `filter` and `onError` on each update.
88 * Unsupported browsers and server rendering return an empty array.
89 *
90 * @param options - A filter that skips tools, and an error callback.
91 *
92 * @example
93 * ```tsx
94 * const pageTools = usePageWebMCPTools({
95 * filter: (tool) => tool.origin === location.origin,
96 * })
97 * const chat = useChat({ connection, tools: pageTools })
98 * ```
99 */
100export function usePageWebMCPTools(
101 options?: UsePageWebMCPToolsOptions,
102): Array<AnyClientTool> {
103 const [tools, setTools] = useState<Array<AnyClientTool>>([])
104 const optionsRef = useRef(options)
105 optionsRef.current = options
106
107 useEffect(() => {
108 const controller = new AbortController()
109 subscribeWebMCPTools(setTools, {
110 signal: controller.signal,
111 filter: (tool) => optionsRef.current?.filter?.(tool) ?? true,
112 onError: (error) => optionsRef.current?.onError?.(error),
113 })
114 return () => controller.abort()
115 }, [])
116
117 return tools
118}