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

use-web-mcp-tools.ts

105 lines3.0 KB

1import { createSignal, onCleanup } from 'solid-js'
2import { registerWebMCPTools, subscribeWebMCPTools } from '@tanstack/ai-client'
3import type { Accessor } from 'solid-js'
4import type {
5 AnyClientTool,
6 InferredClientContext,
7 RegisterWebMCPToolsOptions,
8 SubscribeWebMCPToolsOptions,
9} from '@tanstack/ai-client'
10​
11/** Options for {@link useRegisterWebMCPTools}. */
12export type UseRegisterWebMCPToolsOptions<
13 TTools extends ReadonlyArray<AnyClientTool>,
14 TContext = InferredClientContext<TTools>,
15> = Omit<RegisterWebMCPToolsOptions<TTools, TContext>, 'signal'> & {
16 /** Receives an asynchronous registration error. */
17 onError?: (error: unknown) => void
19​
20type UseRegisterWebMCPToolsArguments<
21 TTools extends ReadonlyArray<AnyClientTool>,
22 TContext,
23> =
24 {} extends UseRegisterWebMCPToolsOptions<TTools, TContext>
25 ? [options?: UseRegisterWebMCPToolsOptions<TTools, TContext>]
26 : [options: UseRegisterWebMCPToolsOptions<TTools, TContext>]
27​
28/**
29 * Registers executable client tools with WebMCP for the current Solid owner.
30 *
31 * The owner cleanup removes every tool registered by this call.
32 *
33 * @param tools - The executable client tools to expose through WebMCP.
34 * @param options - Runtime context, per-tool options, and error handling.
35 *
36 * @example
37 * ```ts
38 * useRegisterWebMCPTools([searchProducts])
39 * ```
40 */
41export function useRegisterWebMCPTools<
42 const TTools extends ReadonlyArray<AnyClientTool>,
43 TContext = InferredClientContext<TTools>,
44>(
45 tools: TTools,
46 ...[options]: UseRegisterWebMCPToolsArguments<TTools, TContext>
47) {
48 const controller = new AbortController()
49​
50 void registerWebMCPTools(tools, {
51 ...options,
52 signal: controller.signal,
53 }).catch((error) => {
54 if (!controller.signal.aborted) {
55 options?.onError?.(error)
56 }
57 })
58​
59 onCleanup(() => controller.abort())
61​
62/** Options for {@link usePageWebMCPTools}. */
63export type UsePageWebMCPToolsOptions = Omit<
64 SubscribeWebMCPToolsOptions,
65 'signal'
67​
68/**
69 * Returns the WebMCP tools on the page as client tools for `useChat`.
70 *
71 * The accessor starts empty and updates when the page registers or removes a
72 * tool. The owner cleanup stops the updates. Unsupported browsers and server
73 * rendering keep an empty array.
74 *
75 * @param options - A filter that skips tools, and an error callback.
76 *
77 * @example
78 * ```tsx
79 * const pageTools = usePageWebMCPTools({
80 * filter: (tool) => tool.origin === location.origin,
81 * })
82 * const chat = useChat({
83 * threadId: 'support-1',
84 * connection,
85 * get tools() {
86 * return pageTools()
87 * },
88 * })
89 * ```
90 */
91export function usePageWebMCPTools(
92 options?: UsePageWebMCPToolsOptions,
93): Accessor<Array<AnyClientTool>> {
94 const [tools, setTools] = createSignal<Array<AnyClientTool>>([])
95 const controller = new AbortController()
96​
97 subscribeWebMCPTools((nextTools) => setTools(nextTools), {
98 signal: controller.signal,
99 filter: (tool) => options?.filter?.(tool) ?? true,
100 onError: (error) => options?.onError?.(error),
101 })
102​
103 onCleanup(() => controller.abort())
104 return tools