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

inject-web-mcp-tools.ts

123 lines3.8 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/** @deprecated Use `InjectRegisterWebMCPToolsOptions`. Removed in 1.0.0. */
30export type InjectWebMCPToolsOptions<
31 TTools extends ReadonlyArray<AnyClientTool>,
32 TContext = InferredClientContext<TTools>,
33> = InjectRegisterWebMCPToolsOptions<TTools, TContext>
34​
35/**
36 * Registers executable client tools with WebMCP for the current Angular owner.
37 *
38 * Angular removes the registrations when it destroys the injection owner. Call
39 * this function in an injection context, such as a component field initializer.
40 *
41 * @param tools - The executable client tools to expose through WebMCP.
42 * @param options - Runtime context, per-tool options, and error handling.
43 *
44 * @example
45 * ```ts
46 * registration = injectRegisterWebMCPTools([statusTool], {
47 * onError: (error) => console.error(error),
48 * })
49 * ```
50 */
51export function injectRegisterWebMCPTools<
52 const TTools extends ReadonlyArray<AnyClientTool>,
53 TContext = InferredClientContext<TTools>,
54>(
55 tools: TTools,
56 ...[options]: {} extends InjectRegisterWebMCPToolsOptions<TTools, TContext>
57 ? [options?: InjectRegisterWebMCPToolsOptions<TTools, TContext>]
58 : [options: InjectRegisterWebMCPToolsOptions<TTools, TContext>]
59) {
60 assertInInjectionContext(injectRegisterWebMCPTools)
61 const destroyRef = inject(DestroyRef)
62 const registrationController = new AbortController()
63 const { onError, ...registrationOptions } = options ?? {}
64​
65 destroyRef.onDestroy(() => registrationController.abort())
66​
67 void registerWebMCPTools(tools, {
68 ...registrationOptions,
69 signal: registrationController.signal,
70 }).catch((error) => {
71 if (!registrationController.signal.aborted) {
72 onError?.(error)
73 }
74 })
76​
77/**
78 * @deprecated Use `injectRegisterWebMCPTools`. Removed in 1.0.0.
79 * @alias
80 */
81export const injectWebMCPTools = injectRegisterWebMCPTools
82​
83/** Options for {@link injectPageWebMCPTools}. */
84export type InjectPageWebMCPToolsOptions = Omit<
85 SubscribeWebMCPToolsOptions,
86 'signal'
88​
89/**
90 * Returns the WebMCP tools on the page as client tools for `injectChat`.
91 *
92 * The signal starts empty and updates when the page registers or removes a
93 * tool. Angular stops the updates when it destroys the injection owner. Call
94 * this function in an injection context.
95 *
96 * @param options - A filter that skips tools, and an error callback.
97 *
98 * @example
99 * ```ts
100 * pageTools = injectPageWebMCPTools({
101 * filter: (tool) => tool.origin === location.origin,
102 * })
103 * chat = injectChat({ connection, tools: this.pageTools })
104 * ```
105 */
106export function injectPageWebMCPTools(
107 options?: InjectPageWebMCPToolsOptions,
108): Signal<Array<AnyClientTool>> {
109 assertInInjectionContext(injectPageWebMCPTools)
110 const destroyRef = inject(DestroyRef)
111 const tools = signal<Array<AnyClientTool>>([])
112 const controller = new AbortController()
113​
114 destroyRef.onDestroy(() => controller.abort())
115​
116 subscribeWebMCPTools((nextTools) => tools.set(nextTools), {
117 signal: controller.signal,
118 filter: (tool) => options?.filter?.(tool) ?? true,
119 onError: (error) => options?.onError?.(error),
120 })
121​
122 return tools.asReadonly()