@tanstack/ai-react 0.28.0 → 0.29.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.d.ts +3 -3
- package/dist/esm/index.js +3 -3
- package/dist/esm/use-web-mcp-tools.d.ts +34 -7
- package/dist/esm/use-web-mcp-tools.js +42 -5
- package/dist/esm/use-web-mcp-tools.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +15 -2
- package/src/use-web-mcp-tools.ts +69 -10
package/dist/esm/index.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ export { useByok } from './use-byok.js';
|
|
|
4
4
|
export { useRealtimeChat } from './use-realtime-chat.js';
|
|
5
5
|
export { useMcpAppBridge } from './use-mcp-app-bridge.js';
|
|
6
6
|
export type { UseMcpAppBridgeOptions } from './use-mcp-app-bridge.js';
|
|
7
|
-
export { useWebMCPTools } from './use-web-mcp-tools.js';
|
|
8
|
-
export type { UseWebMCPToolsOptions } from './use-web-mcp-tools.js';
|
|
7
|
+
export { usePageWebMCPTools, useRegisterWebMCPTools, useWebMCPTools, } from './use-web-mcp-tools.js';
|
|
8
|
+
export type { UsePageWebMCPToolsOptions, UseRegisterWebMCPToolsOptions, UseWebMCPToolsOptions, } from './use-web-mcp-tools.js';
|
|
9
9
|
export type { DeepPartial, UseChatOptions, UseChatReturn, UIMessage, ChatRequestBody, QueuedMessage, SendMessageOptions, WhenBusy, QueueConfig, QueueStrategy, QueueOption, } from './types.js';
|
|
10
10
|
export type { UseRealtimeChatOptions, UseRealtimeChatReturn, } from './realtime-types.js';
|
|
11
11
|
export { useGeneration } from './use-generation.js';
|
|
@@ -24,4 +24,4 @@ export { useGenerateVideo } from './use-generate-video.js';
|
|
|
24
24
|
export type { UseGenerateVideoOptions, UseGenerateVideoReturn, } from './use-generate-video.js';
|
|
25
25
|
export { useAudioRecorder } from './use-audio-recorder.js';
|
|
26
26
|
export type { UseAudioRecorderOptions, UseAudioRecorderReturn, } from './use-audio-recorder.js';
|
|
27
|
-
export { AudioRecorder, ChatClient, InterruptManager, RealtimeClient, GenerationClient, VideoGenerationClient, GENERATION_EVENTS, reconstructImageResult, reconstructAudioResult, reconstructSpeechResult, reconstructTranscriptionResult, reconstructSummarizeResult, UnsupportedResponseStreamError, createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, modelMessagesToUIMessages, convertMessagesToModelMessages, normalizeToUIMessage, generateMessageId, StreamProcessor, ImmediateStrategy, PunctuationStrategy, BatchStrategy, WordBoundaryStrategy, CompositeStrategy, parsePartialJSON, PartialJSONParser, defaultJSONParser, registerWebMCPTools, clientTools, fetchServerSentEvents, localStoragePersistence, sessionStoragePersistence, indexedDBPersistence, StorageUnavailableError, type ChatClientPersistence, type ChatPersistedState, type ChatPersistenceOption, type ChatStorageAdapter, type WebStoragePersistenceOptions, type IndexedDBPersistenceOptions, fetchHttpStream, xhrServerSentEvents, xhrHttpStream, stream, rpcStream, webSocket, createChatClientOptions, createMcpAppBridge, type McpAppBridge, type CreateMcpAppBridgeOptions, type ChatFetcher, type ChatFetcherInput, type ChatFetcherOptions, type ConnectionAdapter, type ConnectConnectionAdapter, type SubscribeConnectionAdapter, type RunAgentInputContext, type FetchConnectionOptions, type XhrConnectionOptions, type WebSocketConnectionOptions, type InferChatMessages, type GenericInterrupt, type RegisteredGenericInterrupt, type GenerationClientState, type ImageGenerateInput, type AudioGenerateInput, type SpeechGenerateInput, type TranscriptionGenerateInput, type SummarizeGenerateInput, type VideoGenerateInput, type VideoGenerateResult, type VideoStatusInfo, } from '@tanstack/ai-client';
|
|
27
|
+
export { AudioRecorder, ChatClient, InterruptManager, RealtimeClient, GenerationClient, VideoGenerationClient, GENERATION_EVENTS, reconstructImageResult, reconstructAudioResult, reconstructSpeechResult, reconstructTranscriptionResult, reconstructSummarizeResult, UnsupportedResponseStreamError, createAIDevtoolsGenerationPreview, StreamTruncatedError, DurableStreamIncompleteError, StreamReconnectLimitError, uiMessageToModelMessages, modelMessageToUIMessage, modelMessagesToUIMessages, convertMessagesToModelMessages, normalizeToUIMessage, generateMessageId, StreamProcessor, ImmediateStrategy, PunctuationStrategy, BatchStrategy, WordBoundaryStrategy, CompositeStrategy, parsePartialJSON, PartialJSONParser, defaultJSONParser, registerWebMCPTools, getWebMCPTools, subscribeWebMCPTools, type GetWebMCPToolsOptions, type SubscribeWebMCPToolsOptions, type WebMCPPageTool, clientTools, fetchServerSentEvents, localStoragePersistence, sessionStoragePersistence, indexedDBPersistence, StorageUnavailableError, type ChatClientPersistence, type ChatPersistedState, type ChatPersistenceOption, type ChatStorageAdapter, type WebStoragePersistenceOptions, type IndexedDBPersistenceOptions, fetchHttpStream, xhrServerSentEvents, xhrHttpStream, stream, rpcStream, webSocket, createChatClientOptions, createMcpAppBridge, type McpAppBridge, type CreateMcpAppBridgeOptions, type ChatFetcher, type ChatFetcherInput, type ChatFetcherOptions, type ConnectionAdapter, type ConnectConnectionAdapter, type SubscribeConnectionAdapter, type RunAgentInputContext, type FetchConnectionOptions, type XhrConnectionOptions, type WebSocketConnectionOptions, type InferChatMessages, type GenericInterrupt, type RegisteredGenericInterrupt, type GenerationClientState, type ImageGenerateInput, type AudioGenerateInput, type SpeechGenerateInput, type TranscriptionGenerateInput, type SummarizeGenerateInput, type VideoGenerateInput, type VideoGenerateResult, type VideoStatusInfo, } from '@tanstack/ai-client';
|
package/dist/esm/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { createChatHook } from "./create-chat-hook.js";
|
|
|
3
3
|
import { useByok } from "./use-byok.js";
|
|
4
4
|
import { useRealtimeChat } from "./use-realtime-chat.js";
|
|
5
5
|
import { useMcpAppBridge } from "./use-mcp-app-bridge.js";
|
|
6
|
-
import { useWebMCPTools } from "./use-web-mcp-tools.js";
|
|
6
|
+
import { usePageWebMCPTools, useRegisterWebMCPTools, useWebMCPTools } from "./use-web-mcp-tools.js";
|
|
7
7
|
import { useGeneration } from "./use-generation.js";
|
|
8
8
|
import { useGenerateImage } from "./use-generate-image.js";
|
|
9
9
|
import { useGenerateAudio } from "./use-generate-audio.js";
|
|
@@ -12,5 +12,5 @@ import { useTranscription } from "./use-transcription.js";
|
|
|
12
12
|
import { useSummarize } from "./use-summarize.js";
|
|
13
13
|
import { useGenerateVideo } from "./use-generate-video.js";
|
|
14
14
|
import { useAudioRecorder } from "./use-audio-recorder.js";
|
|
15
|
-
import { AudioRecorder, BatchStrategy, ChatClient, CompositeStrategy, DurableStreamIncompleteError, GENERATION_EVENTS, GenerationClient, ImmediateStrategy, InterruptManager, PartialJSONParser, PunctuationStrategy, RealtimeClient, StorageUnavailableError, StreamProcessor, StreamReconnectLimitError, StreamTruncatedError, UnsupportedResponseStreamError, VideoGenerationClient, WordBoundaryStrategy, clientTools, convertMessagesToModelMessages, createAIDevtoolsGenerationPreview, createChatClientOptions, createMcpAppBridge, defaultJSONParser, fetchHttpStream, fetchServerSentEvents, generateMessageId, indexedDBPersistence, localStoragePersistence, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, parsePartialJSON, reconstructAudioResult, reconstructImageResult, reconstructSpeechResult, reconstructSummarizeResult, reconstructTranscriptionResult, registerWebMCPTools, rpcStream, sessionStoragePersistence, stream, uiMessageToModelMessages, webSocket, xhrHttpStream, xhrServerSentEvents } from "@tanstack/ai-client";
|
|
16
|
-
export { AudioRecorder, BatchStrategy, ChatClient, CompositeStrategy, DurableStreamIncompleteError, GENERATION_EVENTS, GenerationClient, ImmediateStrategy, InterruptManager, PartialJSONParser, PunctuationStrategy, RealtimeClient, StorageUnavailableError, StreamProcessor, StreamReconnectLimitError, StreamTruncatedError, UnsupportedResponseStreamError, VideoGenerationClient, WordBoundaryStrategy, clientTools, convertMessagesToModelMessages, createAIDevtoolsGenerationPreview, createChatClientOptions, createChatHook, createMcpAppBridge, defaultJSONParser, fetchHttpStream, fetchServerSentEvents, generateMessageId, indexedDBPersistence, localStoragePersistence, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, parsePartialJSON, reconstructAudioResult, reconstructImageResult, reconstructSpeechResult, reconstructSummarizeResult, reconstructTranscriptionResult, registerWebMCPTools, rpcStream, sessionStoragePersistence, stream, uiMessageToModelMessages, useAudioRecorder, useByok, useChat, useGenerateAudio, useGenerateImage, useGenerateSpeech, useGenerateVideo, useGeneration, useMcpAppBridge, useRealtimeChat, useSummarize, useTranscription, useWebMCPTools, webSocket, xhrHttpStream, xhrServerSentEvents };
|
|
15
|
+
import { AudioRecorder, BatchStrategy, ChatClient, CompositeStrategy, DurableStreamIncompleteError, GENERATION_EVENTS, GenerationClient, ImmediateStrategy, InterruptManager, PartialJSONParser, PunctuationStrategy, RealtimeClient, StorageUnavailableError, StreamProcessor, StreamReconnectLimitError, StreamTruncatedError, UnsupportedResponseStreamError, VideoGenerationClient, WordBoundaryStrategy, clientTools, convertMessagesToModelMessages, createAIDevtoolsGenerationPreview, createChatClientOptions, createMcpAppBridge, defaultJSONParser, fetchHttpStream, fetchServerSentEvents, generateMessageId, getWebMCPTools, indexedDBPersistence, localStoragePersistence, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, parsePartialJSON, reconstructAudioResult, reconstructImageResult, reconstructSpeechResult, reconstructSummarizeResult, reconstructTranscriptionResult, registerWebMCPTools, rpcStream, sessionStoragePersistence, stream, subscribeWebMCPTools, uiMessageToModelMessages, webSocket, xhrHttpStream, xhrServerSentEvents } from "@tanstack/ai-client";
|
|
16
|
+
export { AudioRecorder, BatchStrategy, ChatClient, CompositeStrategy, DurableStreamIncompleteError, GENERATION_EVENTS, GenerationClient, ImmediateStrategy, InterruptManager, PartialJSONParser, PunctuationStrategy, RealtimeClient, StorageUnavailableError, StreamProcessor, StreamReconnectLimitError, StreamTruncatedError, UnsupportedResponseStreamError, VideoGenerationClient, WordBoundaryStrategy, clientTools, convertMessagesToModelMessages, createAIDevtoolsGenerationPreview, createChatClientOptions, createChatHook, createMcpAppBridge, defaultJSONParser, fetchHttpStream, fetchServerSentEvents, generateMessageId, getWebMCPTools, indexedDBPersistence, localStoragePersistence, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, parsePartialJSON, reconstructAudioResult, reconstructImageResult, reconstructSpeechResult, reconstructSummarizeResult, reconstructTranscriptionResult, registerWebMCPTools, rpcStream, sessionStoragePersistence, stream, subscribeWebMCPTools, uiMessageToModelMessages, useAudioRecorder, useByok, useChat, useGenerateAudio, useGenerateImage, useGenerateSpeech, useGenerateVideo, useGeneration, useMcpAppBridge, usePageWebMCPTools, useRealtimeChat, useRegisterWebMCPTools, useSummarize, useTranscription, useWebMCPTools, webSocket, xhrHttpStream, xhrServerSentEvents };
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { AnyClientTool, InferredClientContext, RegisterWebMCPToolsOptions } from '@tanstack/ai-client';
|
|
2
|
-
/** Options for the React {@link
|
|
3
|
-
export type
|
|
1
|
+
import { AnyClientTool, InferredClientContext, RegisterWebMCPToolsOptions, SubscribeWebMCPToolsOptions } from '@tanstack/ai-client';
|
|
2
|
+
/** Options for the React {@link useRegisterWebMCPTools} lifecycle hook. */
|
|
3
|
+
export type UseRegisterWebMCPToolsOptions<TTools extends ReadonlyArray<AnyClientTool>, TContext = InferredClientContext<TTools>> = Omit<RegisterWebMCPToolsOptions<TTools, TContext>, 'signal'> & {
|
|
4
4
|
/** Receives an asynchronous registration failure. */
|
|
5
5
|
onError?: (error: unknown) => void;
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
/** @deprecated Use `UseRegisterWebMCPToolsOptions`. Removed in 1.0.0. */
|
|
8
|
+
export type UseWebMCPToolsOptions<TTools extends ReadonlyArray<AnyClientTool>, TContext = InferredClientContext<TTools>> = UseRegisterWebMCPToolsOptions<TTools, TContext>;
|
|
9
|
+
type UseRegisterWebMCPToolsArguments<TTools extends ReadonlyArray<AnyClientTool>, TContext> = RegisterWebMCPToolsOptions<TTools, TContext> extends {
|
|
8
10
|
context: unknown;
|
|
9
|
-
} ? [options:
|
|
11
|
+
} ? [options: UseRegisterWebMCPToolsOptions<TTools, TContext>] : [options?: UseRegisterWebMCPToolsOptions<TTools, TContext>];
|
|
10
12
|
/**
|
|
11
13
|
* Registers client tools with WebMCP for the lifetime of a React component.
|
|
12
14
|
*
|
|
@@ -18,10 +20,35 @@ type UseWebMCPToolsArguments<TTools extends ReadonlyArray<AnyClientTool>, TConte
|
|
|
18
20
|
*
|
|
19
21
|
* @example
|
|
20
22
|
* ```tsx
|
|
21
|
-
*
|
|
23
|
+
* useRegisterWebMCPTools([searchProducts], {
|
|
22
24
|
* toolOptions: { searchProducts: { title: 'Search products' } },
|
|
23
25
|
* })
|
|
24
26
|
* ```
|
|
25
27
|
*/
|
|
26
|
-
export declare function
|
|
28
|
+
export declare function useRegisterWebMCPTools<const TTools extends ReadonlyArray<AnyClientTool>, TContext = InferredClientContext<TTools>>(tools: TTools, ...[options]: UseRegisterWebMCPToolsArguments<TTools, TContext>): void;
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Use `useRegisterWebMCPTools`. Removed in 1.0.0.
|
|
31
|
+
* @alias
|
|
32
|
+
*/
|
|
33
|
+
export declare const useWebMCPTools: typeof useRegisterWebMCPTools;
|
|
34
|
+
/** Options for {@link usePageWebMCPTools}. */
|
|
35
|
+
export type UsePageWebMCPToolsOptions = Omit<SubscribeWebMCPToolsOptions, 'signal'>;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the WebMCP tools on the page as client tools for `useChat`.
|
|
38
|
+
*
|
|
39
|
+
* The list starts empty and updates when the page registers or removes a
|
|
40
|
+
* tool. The hook reads the latest `filter` and `onError` on each update.
|
|
41
|
+
* Unsupported browsers and server rendering return an empty array.
|
|
42
|
+
*
|
|
43
|
+
* @param options - A filter that skips tools, and an error callback.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```tsx
|
|
47
|
+
* const pageTools = usePageWebMCPTools({
|
|
48
|
+
* filter: (tool) => tool.origin === location.origin,
|
|
49
|
+
* })
|
|
50
|
+
* const chat = useChat({ connection, tools: pageTools })
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function usePageWebMCPTools(options?: UsePageWebMCPToolsOptions): Array<AnyClientTool>;
|
|
27
54
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { registerWebMCPTools } from "@tanstack/ai-client";
|
|
2
|
-
import { useEffect } from "react";
|
|
1
|
+
import { registerWebMCPTools, subscribeWebMCPTools } from "@tanstack/ai-client";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
//#region src/use-web-mcp-tools.ts
|
|
4
4
|
/**
|
|
5
5
|
* Registers client tools with WebMCP for the lifetime of a React component.
|
|
@@ -12,12 +12,12 @@ import { useEffect } from "react";
|
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* ```tsx
|
|
15
|
-
*
|
|
15
|
+
* useRegisterWebMCPTools([searchProducts], {
|
|
16
16
|
* toolOptions: { searchProducts: { title: 'Search products' } },
|
|
17
17
|
* })
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
|
-
function
|
|
20
|
+
function useRegisterWebMCPTools(tools, ...[options]) {
|
|
21
21
|
useEffect(() => {
|
|
22
22
|
const controller = new AbortController();
|
|
23
23
|
const { onError, ...registrationOptions } = options ?? {};
|
|
@@ -30,7 +30,44 @@ function useWebMCPTools(tools, ...[options]) {
|
|
|
30
30
|
return () => controller.abort();
|
|
31
31
|
}, [tools, options]);
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* @deprecated Use `useRegisterWebMCPTools`. Removed in 1.0.0.
|
|
35
|
+
* @alias
|
|
36
|
+
*/
|
|
37
|
+
var useWebMCPTools = useRegisterWebMCPTools;
|
|
38
|
+
/**
|
|
39
|
+
* Returns the WebMCP tools on the page as client tools for `useChat`.
|
|
40
|
+
*
|
|
41
|
+
* The list starts empty and updates when the page registers or removes a
|
|
42
|
+
* tool. The hook reads the latest `filter` and `onError` on each update.
|
|
43
|
+
* Unsupported browsers and server rendering return an empty array.
|
|
44
|
+
*
|
|
45
|
+
* @param options - A filter that skips tools, and an error callback.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```tsx
|
|
49
|
+
* const pageTools = usePageWebMCPTools({
|
|
50
|
+
* filter: (tool) => tool.origin === location.origin,
|
|
51
|
+
* })
|
|
52
|
+
* const chat = useChat({ connection, tools: pageTools })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
function usePageWebMCPTools(options) {
|
|
56
|
+
const [tools, setTools] = useState([]);
|
|
57
|
+
const optionsRef = useRef(options);
|
|
58
|
+
optionsRef.current = options;
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const controller = new AbortController();
|
|
61
|
+
subscribeWebMCPTools(setTools, {
|
|
62
|
+
signal: controller.signal,
|
|
63
|
+
filter: (tool) => optionsRef.current?.filter?.(tool) ?? true,
|
|
64
|
+
onError: (error) => optionsRef.current?.onError?.(error)
|
|
65
|
+
});
|
|
66
|
+
return () => controller.abort();
|
|
67
|
+
}, []);
|
|
68
|
+
return tools;
|
|
69
|
+
}
|
|
33
70
|
//#endregion
|
|
34
|
-
export { useWebMCPTools };
|
|
71
|
+
export { usePageWebMCPTools, useRegisterWebMCPTools, useWebMCPTools };
|
|
35
72
|
|
|
36
73
|
//# sourceMappingURL=use-web-mcp-tools.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-web-mcp-tools.js","names":[],"sources":["../../src/use-web-mcp-tools.ts"],"sourcesContent":["import { useEffect } from 'react'\nimport { registerWebMCPTools } from '@tanstack/ai-client'\nimport type {\n AnyClientTool,\n InferredClientContext,\n RegisterWebMCPToolsOptions,\n} from '@tanstack/ai-client'\n\n/** Options for the React {@link
|
|
1
|
+
{"version":3,"file":"use-web-mcp-tools.js","names":[],"sources":["../../src/use-web-mcp-tools.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react'\nimport { registerWebMCPTools, subscribeWebMCPTools } from '@tanstack/ai-client'\nimport type {\n AnyClientTool,\n InferredClientContext,\n RegisterWebMCPToolsOptions,\n SubscribeWebMCPToolsOptions,\n} from '@tanstack/ai-client'\n\n/** Options for the React {@link useRegisterWebMCPTools} lifecycle hook. */\nexport type UseRegisterWebMCPToolsOptions<\n TTools extends ReadonlyArray<AnyClientTool>,\n TContext = InferredClientContext<TTools>,\n> = Omit<RegisterWebMCPToolsOptions<TTools, TContext>, 'signal'> & {\n /** Receives an asynchronous registration failure. */\n onError?: (error: unknown) => void\n}\n\n/** @deprecated Use `UseRegisterWebMCPToolsOptions`. Removed in 1.0.0. */\nexport type UseWebMCPToolsOptions<\n TTools extends ReadonlyArray<AnyClientTool>,\n TContext = InferredClientContext<TTools>,\n> = UseRegisterWebMCPToolsOptions<TTools, TContext>\n\ntype UseRegisterWebMCPToolsArguments<\n TTools extends ReadonlyArray<AnyClientTool>,\n TContext,\n> =\n RegisterWebMCPToolsOptions<TTools, TContext> extends { context: unknown }\n ? [options: UseRegisterWebMCPToolsOptions<TTools, TContext>]\n : [options?: UseRegisterWebMCPToolsOptions<TTools, TContext>]\n\n/**\n * Registers client tools with WebMCP for the lifetime of a React component.\n *\n * The hook replaces the registration when `tools` or `options` changes.\n * Unsupported browsers and server rendering do not register tools.\n *\n * @param tools - The executable client tools to expose through WebMCP.\n * @param options - Runtime context, per-tool options, and an error callback.\n *\n * @example\n * ```tsx\n * useRegisterWebMCPTools([searchProducts], {\n * toolOptions: { searchProducts: { title: 'Search products' } },\n * })\n * ```\n */\nexport function useRegisterWebMCPTools<\n const TTools extends ReadonlyArray<AnyClientTool>,\n TContext = InferredClientContext<TTools>,\n>(\n tools: TTools,\n ...[options]: UseRegisterWebMCPToolsArguments<TTools, TContext>\n) {\n useEffect(() => {\n const controller = new AbortController()\n const { onError, ...registrationOptions } = options ?? {}\n\n registerWebMCPTools(tools, {\n ...registrationOptions,\n signal: controller.signal,\n }).catch((error) => {\n if (!controller.signal.aborted) onError?.(error)\n })\n\n return () => controller.abort()\n }, [tools, options])\n}\n\n/**\n * @deprecated Use `useRegisterWebMCPTools`. Removed in 1.0.0.\n * @alias\n */\nexport const useWebMCPTools = useRegisterWebMCPTools\n\n/** Options for {@link usePageWebMCPTools}. */\nexport type UsePageWebMCPToolsOptions = Omit<\n SubscribeWebMCPToolsOptions,\n 'signal'\n>\n\n/**\n * Returns the WebMCP tools on the page as client tools for `useChat`.\n *\n * The list starts empty and updates when the page registers or removes a\n * tool. The hook reads the latest `filter` and `onError` on each update.\n * Unsupported browsers and server rendering return an empty array.\n *\n * @param options - A filter that skips tools, and an error callback.\n *\n * @example\n * ```tsx\n * const pageTools = usePageWebMCPTools({\n * filter: (tool) => tool.origin === location.origin,\n * })\n * const chat = useChat({ connection, tools: pageTools })\n * ```\n */\nexport function usePageWebMCPTools(\n options?: UsePageWebMCPToolsOptions,\n): Array<AnyClientTool> {\n const [tools, setTools] = useState<Array<AnyClientTool>>([])\n const optionsRef = useRef(options)\n optionsRef.current = options\n\n useEffect(() => {\n const controller = new AbortController()\n subscribeWebMCPTools(setTools, {\n signal: controller.signal,\n filter: (tool) => optionsRef.current?.filter?.(tool) ?? true,\n onError: (error) => optionsRef.current?.onError?.(error),\n })\n return () => controller.abort()\n }, [])\n\n return tools\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,uBAId,OACA,GAAG,CAAC,UACJ;CACA,gBAAgB;EACd,MAAM,aAAa,IAAI,gBAAgB;EACvC,MAAM,EAAE,SAAS,GAAG,wBAAwB,WAAW,CAAC;EAExD,oBAAoB,OAAO;GACzB,GAAG;GACH,QAAQ,WAAW;EACrB,CAAC,CAAC,CAAC,OAAO,UAAU;GAClB,IAAI,CAAC,WAAW,OAAO,SAAS,UAAU,KAAK;EACjD,CAAC;EAED,aAAa,WAAW,MAAM;CAChC,GAAG,CAAC,OAAO,OAAO,CAAC;AACrB;;;;;AAMA,IAAa,iBAAiB;;;;;;;;;;;;;;;;;;AAyB9B,SAAgB,mBACd,SACsB;CACtB,MAAM,CAAC,OAAO,YAAY,SAA+B,CAAC,CAAC;CAC3D,MAAM,aAAa,OAAO,OAAO;CACjC,WAAW,UAAU;CAErB,gBAAgB;EACd,MAAM,aAAa,IAAI,gBAAgB;EACvC,qBAAqB,UAAU;GAC7B,QAAQ,WAAW;GACnB,SAAS,SAAS,WAAW,SAAS,SAAS,IAAI,KAAK;GACxD,UAAU,UAAU,WAAW,SAAS,UAAU,KAAK;EACzD,CAAC;EACD,aAAa,WAAW,MAAM;CAChC,GAAG,CAAC,CAAC;CAEL,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.29.1",
|
|
4
4
|
"description": "React hooks for TanStack AI streaming chat, realtime voice, structured outputs, and media generation.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -58,14 +58,14 @@
|
|
|
58
58
|
],
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"@tanstack/markdown": "^0.0.13",
|
|
61
|
-
"@tanstack/ai-client": "^0.
|
|
61
|
+
"@tanstack/ai-client": "^0.35.1"
|
|
62
62
|
},
|
|
63
63
|
"peerDependencies": {
|
|
64
64
|
"@mcp-ui/client": "^7",
|
|
65
65
|
"@types/react": ">=18.0.0",
|
|
66
66
|
"react": ">=18.0.0",
|
|
67
67
|
"react-dom": ">=18.0.0",
|
|
68
|
-
"@tanstack/ai": "^0.
|
|
68
|
+
"@tanstack/ai": "^0.61.0"
|
|
69
69
|
},
|
|
70
70
|
"peerDependenciesMeta": {
|
|
71
71
|
"@mcp-ui/client": {
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"react": "^19.2.3",
|
|
86
86
|
"react-dom": "^19.2.3",
|
|
87
87
|
"vite": "^8.2.1",
|
|
88
|
-
"@tanstack/ai": "0.
|
|
88
|
+
"@tanstack/ai": "0.61.0"
|
|
89
89
|
},
|
|
90
90
|
"scripts": {
|
|
91
91
|
"clean": "premove ./build ./dist",
|
package/src/index.ts
CHANGED
|
@@ -4,8 +4,16 @@ export { useByok } from './use-byok'
|
|
|
4
4
|
export { useRealtimeChat } from './use-realtime-chat'
|
|
5
5
|
export { useMcpAppBridge } from './use-mcp-app-bridge'
|
|
6
6
|
export type { UseMcpAppBridgeOptions } from './use-mcp-app-bridge'
|
|
7
|
-
export {
|
|
8
|
-
|
|
7
|
+
export {
|
|
8
|
+
usePageWebMCPTools,
|
|
9
|
+
useRegisterWebMCPTools,
|
|
10
|
+
useWebMCPTools,
|
|
11
|
+
} from './use-web-mcp-tools'
|
|
12
|
+
export type {
|
|
13
|
+
UsePageWebMCPToolsOptions,
|
|
14
|
+
UseRegisterWebMCPToolsOptions,
|
|
15
|
+
UseWebMCPToolsOptions,
|
|
16
|
+
} from './use-web-mcp-tools'
|
|
9
17
|
export type {
|
|
10
18
|
DeepPartial,
|
|
11
19
|
UseChatOptions,
|
|
@@ -105,6 +113,11 @@ export {
|
|
|
105
113
|
PartialJSONParser,
|
|
106
114
|
defaultJSONParser,
|
|
107
115
|
registerWebMCPTools,
|
|
116
|
+
getWebMCPTools,
|
|
117
|
+
subscribeWebMCPTools,
|
|
118
|
+
type GetWebMCPToolsOptions,
|
|
119
|
+
type SubscribeWebMCPToolsOptions,
|
|
120
|
+
type WebMCPPageTool,
|
|
108
121
|
clientTools,
|
|
109
122
|
fetchServerSentEvents,
|
|
110
123
|
localStoragePersistence,
|
package/src/use-web-mcp-tools.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { useEffect } from 'react'
|
|
2
|
-
import { registerWebMCPTools } from '@tanstack/ai-client'
|
|
1
|
+
import { useEffect, useRef, useState } from 'react'
|
|
2
|
+
import { registerWebMCPTools, subscribeWebMCPTools } from '@tanstack/ai-client'
|
|
3
3
|
import type {
|
|
4
4
|
AnyClientTool,
|
|
5
5
|
InferredClientContext,
|
|
6
6
|
RegisterWebMCPToolsOptions,
|
|
7
|
+
SubscribeWebMCPToolsOptions,
|
|
7
8
|
} from '@tanstack/ai-client'
|
|
8
9
|
|
|
9
|
-
/** Options for the React {@link
|
|
10
|
-
export type
|
|
10
|
+
/** Options for the React {@link useRegisterWebMCPTools} lifecycle hook. */
|
|
11
|
+
export type UseRegisterWebMCPToolsOptions<
|
|
11
12
|
TTools extends ReadonlyArray<AnyClientTool>,
|
|
12
13
|
TContext = InferredClientContext<TTools>,
|
|
13
14
|
> = Omit<RegisterWebMCPToolsOptions<TTools, TContext>, 'signal'> & {
|
|
@@ -15,13 +16,19 @@ export type UseWebMCPToolsOptions<
|
|
|
15
16
|
onError?: (error: unknown) => void
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
/** @deprecated Use `UseRegisterWebMCPToolsOptions`. Removed in 1.0.0. */
|
|
20
|
+
export type UseWebMCPToolsOptions<
|
|
21
|
+
TTools extends ReadonlyArray<AnyClientTool>,
|
|
22
|
+
TContext = InferredClientContext<TTools>,
|
|
23
|
+
> = UseRegisterWebMCPToolsOptions<TTools, TContext>
|
|
24
|
+
|
|
25
|
+
type UseRegisterWebMCPToolsArguments<
|
|
19
26
|
TTools extends ReadonlyArray<AnyClientTool>,
|
|
20
27
|
TContext,
|
|
21
28
|
> =
|
|
22
29
|
RegisterWebMCPToolsOptions<TTools, TContext> extends { context: unknown }
|
|
23
|
-
? [options:
|
|
24
|
-
: [options?:
|
|
30
|
+
? [options: UseRegisterWebMCPToolsOptions<TTools, TContext>]
|
|
31
|
+
: [options?: UseRegisterWebMCPToolsOptions<TTools, TContext>]
|
|
25
32
|
|
|
26
33
|
/**
|
|
27
34
|
* Registers client tools with WebMCP for the lifetime of a React component.
|
|
@@ -34,15 +41,18 @@ type UseWebMCPToolsArguments<
|
|
|
34
41
|
*
|
|
35
42
|
* @example
|
|
36
43
|
* ```tsx
|
|
37
|
-
*
|
|
44
|
+
* useRegisterWebMCPTools([searchProducts], {
|
|
38
45
|
* toolOptions: { searchProducts: { title: 'Search products' } },
|
|
39
46
|
* })
|
|
40
47
|
* ```
|
|
41
48
|
*/
|
|
42
|
-
export function
|
|
49
|
+
export function useRegisterWebMCPTools<
|
|
43
50
|
const TTools extends ReadonlyArray<AnyClientTool>,
|
|
44
51
|
TContext = InferredClientContext<TTools>,
|
|
45
|
-
>(
|
|
52
|
+
>(
|
|
53
|
+
tools: TTools,
|
|
54
|
+
...[options]: UseRegisterWebMCPToolsArguments<TTools, TContext>
|
|
55
|
+
) {
|
|
46
56
|
useEffect(() => {
|
|
47
57
|
const controller = new AbortController()
|
|
48
58
|
const { onError, ...registrationOptions } = options ?? {}
|
|
@@ -57,3 +67,52 @@ export function useWebMCPTools<
|
|
|
57
67
|
return () => controller.abort()
|
|
58
68
|
}, [tools, options])
|
|
59
69
|
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @deprecated Use `useRegisterWebMCPTools`. Removed in 1.0.0.
|
|
73
|
+
* @alias
|
|
74
|
+
*/
|
|
75
|
+
export const useWebMCPTools = useRegisterWebMCPTools
|
|
76
|
+
|
|
77
|
+
/** Options for {@link usePageWebMCPTools}. */
|
|
78
|
+
export 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
|
+
*/
|
|
100
|
+
export 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
|
+
}
|