Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

use-web-mcp-tools.ts

106 lines3.3 KB

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
18​
19type UseRegisterWebMCPToolsArguments<
20 TTools extends ReadonlyArray<AnyClientTool>,
21 TContext,
22> =
23 RegisterWebMCPToolsOptions<TTools, TContext> extends { context: unknown }
24 ? [options: UseRegisterWebMCPToolsOptions<TTools, TContext>]
25 : [options?: UseRegisterWebMCPToolsOptions<TTools, TContext>]
26​
27/**
28 * Registers client tools with WebMCP for the lifetime of a React component.
29 *
30 * The hook replaces the registration when `tools` or `options` changes.
31 * Unsupported browsers and server rendering do not register tools.
32 *
33 * @param tools - The executable client tools to expose through WebMCP.
34 * @param options - Runtime context, per-tool options, and an error callback.
35 *
36 * @example
37 * ```tsx
38 * useRegisterWebMCPTools([searchProducts], {
39 * toolOptions: { searchProducts: { title: 'Search products' } },
40 * })
41 * ```
42 */
43export function useRegisterWebMCPTools<
44 const TTools extends ReadonlyArray<AnyClientTool>,
45 TContext = InferredClientContext<TTools>,
46>(
47 tools: TTools,
48 ...[options]: UseRegisterWebMCPToolsArguments<TTools, TContext>
49) {
50 useEffect(() => {
51 const controller = new AbortController()
52 const { onError, ...registrationOptions } = options ?? {}
53​
54 registerWebMCPTools(tools, {
55 ...registrationOptions,
56 signal: controller.signal,
57 }).catch((error) => {
58 if (!controller.signal.aborted) onError?.(error)
59 })
60​
61 return () => controller.abort()
62 }, [tools, options])
64​
65/** Options for {@link usePageWebMCPTools}. */
66export type UsePageWebMCPToolsOptions = Omit<
67 SubscribeWebMCPToolsOptions,
68 'signal'
70​
71/**
72 * Returns the WebMCP tools on the page as client tools for `useChat`.
73 *
74 * The list starts empty and updates when the page registers or removes a
75 * tool. The hook reads the latest `filter` and `onError` on each update.
76 * Unsupported browsers and server rendering return an empty array.
77 *
78 * @param options - A filter that skips tools, and an error callback.
79 *
80 * @example
81 * ```tsx
82 * const pageTools = usePageWebMCPTools({
83 * filter: (tool) => tool.origin === location.origin,
84 * })
85 * const chat = useChat({ threadId: 'support-1', connection, tools: pageTools })
86 * ```
87 */
88export function usePageWebMCPTools(
89 options?: UsePageWebMCPToolsOptions,
90): Array<AnyClientTool> {
91 const [tools, setTools] = useState<Array<AnyClientTool>>([])
92 const optionsRef = useRef(options)
93 optionsRef.current = options
94​
95 useEffect(() => {
96 const controller = new AbortController()
97 subscribeWebMCPTools(setTools, {
98 signal: controller.signal,
99 filter: (tool) => optionsRef.current?.filter?.(tool) ?? true,
100 onError: (error) => optionsRef.current?.onError?.(error),
101 })
102 return () => controller.abort()
103 }, [])
104​
105 return tools