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

inject-web-mcp-tools.ts

111 lines3.4 KB

1import {
2 DestroyRef,
3 assertInInjectionContext,
4 inject,
5 signal,
6} from '@angular/core'
7import { registerWebMCPTools, subscribeWebMCPTools } from '@tanstack/ai-client'
8import type { Signal } from '@angular/core'
9import type {
10 AnyClientTool,
11 InferredClientContext,
12 RegisterWebMCPToolsOptions,
13 SubscribeWebMCPToolsOptions,
14} from '@tanstack/ai-client'
15​
16/**
17 * Options for {@link injectRegisterWebMCPTools}.
18 *
19 * Context is required when a client tool declares a required runtime context.
20 */
21export type InjectRegisterWebMCPToolsOptions<
22 TTools extends ReadonlyArray<AnyClientTool>,
23 TContext = InferredClientContext<TTools>,
24> = Omit<RegisterWebMCPToolsOptions<TTools, TContext>, 'signal'> & {
25 /** Receives asynchronous WebMCP registration errors. */
26 onError?: (error: unknown) => void
28​
29/**
30 * Registers executable client tools with WebMCP for the current Angular owner.
31 *
32 * Angular removes the registrations when it destroys the injection owner. Call
33 * this function in an injection context, such as a component field initializer.
34 *
35 * @param tools - The executable client tools to expose through WebMCP.
36 * @param options - Runtime context, per-tool options, and error handling.
37 *
38 * @example
39 * ```ts
40 * registration = injectRegisterWebMCPTools([statusTool], {
41 * onError: (error) => console.error(error),
42 * })
43 * ```
44 */
45export function injectRegisterWebMCPTools<
46 const TTools extends ReadonlyArray<AnyClientTool>,
47 TContext = InferredClientContext<TTools>,
48>(
49 tools: TTools,
50 ...[options]: {} extends InjectRegisterWebMCPToolsOptions<TTools, TContext>
51 ? [options?: InjectRegisterWebMCPToolsOptions<TTools, TContext>]
52 : [options: InjectRegisterWebMCPToolsOptions<TTools, TContext>]
53) {
54 assertInInjectionContext(injectRegisterWebMCPTools)
55 const destroyRef = inject(DestroyRef)
56 const registrationController = new AbortController()
57 const { onError, ...registrationOptions } = options ?? {}
58​
59 destroyRef.onDestroy(() => registrationController.abort())
60​
61 void registerWebMCPTools(tools, {
62 ...registrationOptions,
63 signal: registrationController.signal,
64 }).catch((error) => {
65 if (!registrationController.signal.aborted) {
66 onError?.(error)
67 }
68 })
70​
71/** Options for {@link injectPageWebMCPTools}. */
72export type InjectPageWebMCPToolsOptions = Omit<
73 SubscribeWebMCPToolsOptions,
74 'signal'
76​
77/**
78 * Returns the WebMCP tools on the page as client tools for `injectChat`.
79 *
80 * The signal starts empty and updates when the page registers or removes a
81 * tool. Angular stops the updates when it destroys the injection owner. Call
82 * this function in an injection context.
83 *
84 * @param options - A filter that skips tools, and an error callback.
85 *
86 * @example
87 * ```ts
88 * pageTools = injectPageWebMCPTools({
89 * filter: (tool) => tool.origin === location.origin,
90 * })
91 * chat = injectChat({ threadId: 'support-1', connection, tools: this.pageTools })
92 * ```
93 */
94export function injectPageWebMCPTools(
95 options?: InjectPageWebMCPToolsOptions,
96): Signal<Array<AnyClientTool>> {
97 assertInInjectionContext(injectPageWebMCPTools)
98 const destroyRef = inject(DestroyRef)
99 const tools = signal<Array<AnyClientTool>>([])
100 const controller = new AbortController()
101​
102 destroyRef.onDestroy(() => controller.abort())
103​
104 subscribeWebMCPTools((nextTools) => tools.set(nextTools), {
105 signal: controller.signal,
106 filter: (tool) => options?.filter?.(tool) ?? true,
107 onError: (error) => options?.onError?.(error),
108 })
109​
110 return tools.asReadonly()