@mcp-b/react-webmcp 1.1.1 → 1.6.0
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/README.md +24 -565
- package/dist/index.d.ts +120 -243
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +18 -9
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,49 @@
|
|
|
1
|
-
import { ElicitationFormParams, ElicitationParams, ElicitationParams as ElicitationParams$1, ElicitationResult, ElicitationResult as ElicitationResult$1, ElicitationUrlParams, ModelContext as ModelContextProtocol, PromptDescriptor, ResourceDescriptor, SamplingRequestParams, SamplingRequestParams as SamplingRequestParams$1, SamplingResult, SamplingResult as SamplingResult$1, ToolDescriptor } from "@mcp-b/global";
|
|
2
1
|
import { DependencyList, ReactElement, ReactNode } from "react";
|
|
2
|
+
import { BrowserMcpServer as ModelContextProtocol, Client, PromptDescriptor, PromptMessage, RequestOptions, Resource, ResourceContents, ResourceDescriptor, SamplingRequestParams, SamplingResult, ServerCapabilities, Tool, Transport } from "@mcp-b/webmcp-ts-sdk";
|
|
3
|
+
import { ToolInputSchema, ToolInputSchema as ToolInputSchema$1 } from "@mcp-b/webmcp-polyfill";
|
|
4
|
+
import { CallToolResult, ElicitationParams, ElicitationResult, InferArgsFromInputSchema, InferJsonSchema, InputSchema, JsonSchemaObject, ToolAnnotations, ToolDescriptor } from "@mcp-b/webmcp-types";
|
|
3
5
|
import { z } from "zod";
|
|
4
|
-
import { CallToolResult, Client, PromptMessage, RequestOptions, Resource, Resource as Resource$1, ResourceContents, ServerCapabilities, ServerCapabilities as ServerCapabilities$1, Tool, Tool as Tool$1, ToolAnnotations, Transport } from "@mcp-b/webmcp-ts-sdk";
|
|
5
6
|
|
|
7
|
+
//#region src/zod-utils.d.ts
|
|
8
|
+
type ZodSchemaObject = Record<string, z.ZodTypeAny>;
|
|
9
|
+
//#endregion
|
|
6
10
|
//#region src/types.d.ts
|
|
7
|
-
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
12
|
+
* Union of all input schema types supported by react-webmcp:
|
|
13
|
+
* - `ToolInputSchema` (JSON Schema + Standard Schema v1)
|
|
14
|
+
* - `ZodSchemaObject` (Zod v3 `Record<string, z.ZodTypeAny>`)
|
|
15
|
+
*/
|
|
16
|
+
type ReactWebMCPInputSchema = ToolInputSchema$1 | ZodSchemaObject;
|
|
17
|
+
/**
|
|
18
|
+
* Infers handler input type from a Standard Schema, Zod v3 schema, or JSON Schema.
|
|
19
|
+
*
|
|
20
|
+
* - **Standard Schema** (Zod v4, Valibot, ArkType): extracts `~standard.types.input`
|
|
21
|
+
* - **Zod v3** (`Record<string, z.ZodTypeAny>`): uses `z.infer<z.ZodObject<T>>`
|
|
22
|
+
* - **JSON Schema** (`as const`): uses `InferArgsFromInputSchema` for structural inference
|
|
23
|
+
* - **Fallback**: `Record<string, unknown>`
|
|
10
24
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
25
|
+
* @template T - The input schema type
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
type InferToolInput<T> = T extends {
|
|
29
|
+
readonly '~standard': {
|
|
30
|
+
readonly types?: infer Types;
|
|
31
|
+
};
|
|
32
|
+
} ? Types extends {
|
|
33
|
+
readonly input: infer I;
|
|
34
|
+
} ? I : Record<string, unknown> : T extends Record<string, z.ZodTypeAny> ? z.infer<z.ZodObject<T>> : T extends InputSchema ? InferArgsFromInputSchema<T> : Record<string, unknown>;
|
|
35
|
+
/**
|
|
36
|
+
* Utility type to infer the output type from a JSON Schema object.
|
|
37
|
+
*
|
|
38
|
+
* When `TOutputSchema` is a literal `JsonSchemaObject`, this resolves to
|
|
39
|
+
* `InferJsonSchema<TOutputSchema>`. When it's `undefined`,
|
|
13
40
|
* it falls back to the provided `TFallback` type.
|
|
14
41
|
*
|
|
15
|
-
* @template TOutputSchema -
|
|
42
|
+
* @template TOutputSchema - JSON Schema object for output inference
|
|
16
43
|
* @template TFallback - Fallback type when no schema is provided
|
|
17
44
|
* @internal
|
|
18
45
|
*/
|
|
19
|
-
type InferOutput<TOutputSchema extends
|
|
46
|
+
type InferOutput<TOutputSchema extends JsonSchemaObject | undefined = undefined, TFallback = unknown> = TOutputSchema extends JsonSchemaObject ? InferJsonSchema<TOutputSchema> : TFallback;
|
|
20
47
|
/**
|
|
21
48
|
* Represents the current execution state of a tool, including loading status,
|
|
22
49
|
* results, errors, and execution history.
|
|
@@ -42,7 +69,7 @@ interface ToolExecutionState<TOutput = unknown> {
|
|
|
42
69
|
error: Error | null;
|
|
43
70
|
/**
|
|
44
71
|
* Total number of times this tool has been executed.
|
|
45
|
-
* Increments on
|
|
72
|
+
* Increments on successful executions only.
|
|
46
73
|
*/
|
|
47
74
|
executionCount: number;
|
|
48
75
|
}
|
|
@@ -50,10 +77,10 @@ interface ToolExecutionState<TOutput = unknown> {
|
|
|
50
77
|
* Configuration options for the `useWebMCP` hook.
|
|
51
78
|
*
|
|
52
79
|
* Defines a tool's metadata, schema, handler, and lifecycle callbacks.
|
|
53
|
-
*
|
|
80
|
+
* Uses JSON Schema for type inference via `as const`.
|
|
54
81
|
*
|
|
55
|
-
* @template TInputSchema -
|
|
56
|
-
* @template TOutputSchema -
|
|
82
|
+
* @template TInputSchema - JSON Schema defining input parameters
|
|
83
|
+
* @template TOutputSchema - JSON Schema object defining output structure (enables structuredContent)
|
|
57
84
|
*
|
|
58
85
|
* @public
|
|
59
86
|
*
|
|
@@ -63,8 +90,10 @@ interface ToolExecutionState<TOutput = unknown> {
|
|
|
63
90
|
* name: 'posts_like',
|
|
64
91
|
* description: 'Like a post by its ID',
|
|
65
92
|
* inputSchema: {
|
|
66
|
-
*
|
|
67
|
-
*
|
|
93
|
+
* type: 'object',
|
|
94
|
+
* properties: { postId: { type: 'string' } },
|
|
95
|
+
* required: ['postId'],
|
|
96
|
+
* } as const,
|
|
68
97
|
* handler: async ({ postId }) => {
|
|
69
98
|
* await api.likePost(postId);
|
|
70
99
|
* return { success: true };
|
|
@@ -74,28 +103,29 @@ interface ToolExecutionState<TOutput = unknown> {
|
|
|
74
103
|
*
|
|
75
104
|
* @example Tool with output schema (enables MCP structuredContent):
|
|
76
105
|
* ```typescript
|
|
77
|
-
*
|
|
78
|
-
* { postId: z.ZodString },
|
|
79
|
-
* { likes: z.ZodNumber; likedAt: z.ZodString }
|
|
80
|
-
* > = {
|
|
106
|
+
* useWebMCP({
|
|
81
107
|
* name: 'posts_like',
|
|
82
108
|
* description: 'Like a post and return updated like count',
|
|
83
109
|
* inputSchema: {
|
|
84
|
-
*
|
|
85
|
-
*
|
|
110
|
+
* type: 'object',
|
|
111
|
+
* properties: { postId: { type: 'string' } },
|
|
112
|
+
* required: ['postId'],
|
|
113
|
+
* } as const,
|
|
86
114
|
* outputSchema: {
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
115
|
+
* type: 'object',
|
|
116
|
+
* properties: {
|
|
117
|
+
* likes: { type: 'number', description: 'Updated like count' },
|
|
118
|
+
* likedAt: { type: 'string', description: 'ISO timestamp of the like' },
|
|
119
|
+
* },
|
|
120
|
+
* } as const,
|
|
91
121
|
* handler: async ({ postId }) => {
|
|
92
122
|
* const result = await api.likePost(postId);
|
|
93
123
|
* return { likes: result.likes, likedAt: new Date().toISOString() };
|
|
94
124
|
* },
|
|
95
|
-
* };
|
|
125
|
+
* });
|
|
96
126
|
* ```
|
|
97
127
|
*/
|
|
98
|
-
interface WebMCPConfig<TInputSchema extends
|
|
128
|
+
interface WebMCPConfig<TInputSchema extends ReactWebMCPInputSchema = InputSchema, TOutputSchema extends JsonSchemaObject | undefined = undefined> {
|
|
99
129
|
/**
|
|
100
130
|
* Unique identifier for the tool (e.g., 'posts_like', 'graph_navigate').
|
|
101
131
|
* Must follow naming conventions: lowercase with underscores.
|
|
@@ -107,20 +137,24 @@ interface WebMCPConfig<TInputSchema extends Record<string, z.ZodTypeAny> = Recor
|
|
|
107
137
|
*/
|
|
108
138
|
description: string;
|
|
109
139
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
140
|
+
* JSON Schema defining the input parameters for the tool.
|
|
141
|
+
* Use `as const` to enable type inference for the handler's input.
|
|
112
142
|
*
|
|
113
143
|
* @example
|
|
114
144
|
* ```typescript
|
|
115
145
|
* inputSchema: {
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* }
|
|
146
|
+
* type: 'object',
|
|
147
|
+
* properties: {
|
|
148
|
+
* postId: { type: 'string', description: 'The ID of the post to like' },
|
|
149
|
+
* userId: { type: 'string' },
|
|
150
|
+
* },
|
|
151
|
+
* required: ['postId'],
|
|
152
|
+
* } as const
|
|
119
153
|
* ```
|
|
120
154
|
*/
|
|
121
155
|
inputSchema?: TInputSchema;
|
|
122
156
|
/**
|
|
123
|
-
* **Recommended:**
|
|
157
|
+
* **Recommended:** JSON Schema object defining the expected output structure.
|
|
124
158
|
*
|
|
125
159
|
* When provided, this enables three key features:
|
|
126
160
|
* 1. **Type Safety**: The handler's return type is inferred from this schema
|
|
@@ -128,20 +162,17 @@ interface WebMCPConfig<TInputSchema extends Record<string, z.ZodTypeAny> = Recor
|
|
|
128
162
|
* containing the typed output per the MCP specification
|
|
129
163
|
* 3. **AI Understanding**: AI models can better understand and use the tool's output
|
|
130
164
|
*
|
|
131
|
-
* This is the recommended way to define tool outputs. The schema provides
|
|
132
|
-
* both compile-time type checking and runtime structure for MCP clients.
|
|
133
|
-
*
|
|
134
165
|
* @see {@link https://spec.modelcontextprotocol.io/specification/server/tools/#output-schemas}
|
|
135
166
|
*
|
|
136
167
|
* @example
|
|
137
168
|
* ```typescript
|
|
138
169
|
* outputSchema: {
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* }
|
|
144
|
-
* }
|
|
170
|
+
* type: 'object',
|
|
171
|
+
* properties: {
|
|
172
|
+
* counter: { type: 'number', description: 'The current counter value' },
|
|
173
|
+
* timestamp: { type: 'string', description: 'ISO timestamp' },
|
|
174
|
+
* },
|
|
175
|
+
* } as const
|
|
145
176
|
* ```
|
|
146
177
|
*/
|
|
147
178
|
outputSchema?: TOutputSchema;
|
|
@@ -160,7 +191,7 @@ interface WebMCPConfig<TInputSchema extends Record<string, z.ZodTypeAny> = Recor
|
|
|
160
191
|
* @param input - Validated input parameters matching the inputSchema
|
|
161
192
|
* @returns The result data or a Promise resolving to the result
|
|
162
193
|
*/
|
|
163
|
-
handler: (input:
|
|
194
|
+
handler: (input: InferToolInput<TInputSchema>) => Promise<InferOutput<TOutputSchema>> | InferOutput<TOutputSchema>;
|
|
164
195
|
/**
|
|
165
196
|
* Custom formatter for the MCP text response.
|
|
166
197
|
*
|
|
@@ -193,10 +224,10 @@ interface WebMCPConfig<TInputSchema extends Record<string, z.ZodTypeAny> = Recor
|
|
|
193
224
|
* Return value from the `useWebMCP` hook.
|
|
194
225
|
* Provides access to execution state and methods for manual tool control.
|
|
195
226
|
*
|
|
196
|
-
* @template TOutputSchema -
|
|
227
|
+
* @template TOutputSchema - JSON Schema object defining output structure
|
|
197
228
|
* @public
|
|
198
229
|
*/
|
|
199
|
-
interface WebMCPReturn<TOutputSchema extends
|
|
230
|
+
interface WebMCPReturn<TOutputSchema extends JsonSchemaObject | undefined = undefined> {
|
|
200
231
|
/**
|
|
201
232
|
* Current execution state including loading status, results, and errors.
|
|
202
233
|
* See {@link ToolExecutionState} for details.
|
|
@@ -221,7 +252,7 @@ interface WebMCPReturn<TOutputSchema extends Record<string, z.ZodTypeAny> = Reco
|
|
|
221
252
|
* Configuration options for the `useWebMCPPrompt` hook.
|
|
222
253
|
* Defines a prompt's metadata, argument schema, and message generator.
|
|
223
254
|
*
|
|
224
|
-
* @template TArgsSchema -
|
|
255
|
+
* @template TArgsSchema - JSON Schema defining prompt arguments
|
|
225
256
|
* @public
|
|
226
257
|
*
|
|
227
258
|
* @example
|
|
@@ -230,9 +261,13 @@ interface WebMCPReturn<TOutputSchema extends Record<string, z.ZodTypeAny> = Reco
|
|
|
230
261
|
* name: 'review_code',
|
|
231
262
|
* description: 'Review code for best practices',
|
|
232
263
|
* argsSchema: {
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
264
|
+
* type: 'object',
|
|
265
|
+
* properties: {
|
|
266
|
+
* code: { type: 'string', description: 'The code to review' },
|
|
267
|
+
* language: { type: 'string', description: 'Programming language' },
|
|
268
|
+
* },
|
|
269
|
+
* required: ['code'],
|
|
270
|
+
* } as const,
|
|
236
271
|
* get: async ({ code, language }) => ({
|
|
237
272
|
* messages: [{
|
|
238
273
|
* role: 'user',
|
|
@@ -242,7 +277,7 @@ interface WebMCPReturn<TOutputSchema extends Record<string, z.ZodTypeAny> = Reco
|
|
|
242
277
|
* };
|
|
243
278
|
* ```
|
|
244
279
|
*/
|
|
245
|
-
interface WebMCPPromptConfig<TArgsSchema extends
|
|
280
|
+
interface WebMCPPromptConfig<TArgsSchema extends ReactWebMCPInputSchema = InputSchema> {
|
|
246
281
|
/**
|
|
247
282
|
* Unique identifier for the prompt (e.g., 'review_code', 'summarize_text').
|
|
248
283
|
*/
|
|
@@ -253,8 +288,8 @@ interface WebMCPPromptConfig<TArgsSchema extends Record<string, z.ZodTypeAny> =
|
|
|
253
288
|
*/
|
|
254
289
|
description?: string;
|
|
255
290
|
/**
|
|
256
|
-
* Optional
|
|
257
|
-
*
|
|
291
|
+
* Optional JSON Schema defining the arguments for the prompt.
|
|
292
|
+
* Use `as const` to enable type inference for the `get` function's arguments.
|
|
258
293
|
*/
|
|
259
294
|
argsSchema?: TArgsSchema;
|
|
260
295
|
/**
|
|
@@ -264,7 +299,7 @@ interface WebMCPPromptConfig<TArgsSchema extends Record<string, z.ZodTypeAny> =
|
|
|
264
299
|
* @param args - Validated arguments matching the argsSchema
|
|
265
300
|
* @returns Object containing the prompt messages
|
|
266
301
|
*/
|
|
267
|
-
get: (args:
|
|
302
|
+
get: (args: InferToolInput<TArgsSchema>) => Promise<{
|
|
268
303
|
messages: PromptMessage[];
|
|
269
304
|
}> | {
|
|
270
305
|
messages: PromptMessage[];
|
|
@@ -366,7 +401,6 @@ interface WebMCPResourceReturn {
|
|
|
366
401
|
* This hook handles the complete lifecycle of an MCP tool:
|
|
367
402
|
* - Registers the tool with `window.navigator.modelContext`
|
|
368
403
|
* - Manages execution state (loading, results, errors)
|
|
369
|
-
* - Validates input using Zod schemas
|
|
370
404
|
* - Handles tool execution and lifecycle callbacks
|
|
371
405
|
* - Automatically unregisters on component unmount
|
|
372
406
|
* - Returns `structuredContent` when `outputSchema` is defined
|
|
@@ -382,12 +416,19 @@ interface WebMCPResourceReturn {
|
|
|
382
416
|
* useWebMCP({
|
|
383
417
|
* name: 'get_user',
|
|
384
418
|
* description: 'Get user by ID',
|
|
385
|
-
* inputSchema: {
|
|
419
|
+
* inputSchema: {
|
|
420
|
+
* type: 'object',
|
|
421
|
+
* properties: { userId: { type: 'string' } },
|
|
422
|
+
* required: ['userId'],
|
|
423
|
+
* } as const,
|
|
386
424
|
* outputSchema: {
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
425
|
+
* type: 'object',
|
|
426
|
+
* properties: {
|
|
427
|
+
* id: { type: 'string' },
|
|
428
|
+
* name: { type: 'string' },
|
|
429
|
+
* email: { type: 'string' },
|
|
430
|
+
* },
|
|
431
|
+
* } as const,
|
|
391
432
|
* handler: async ({ userId }) => {
|
|
392
433
|
* const user = await fetchUser(userId);
|
|
393
434
|
* return { id: user.id, name: user.name, email: user.email };
|
|
@@ -395,185 +436,17 @@ interface WebMCPResourceReturn {
|
|
|
395
436
|
* });
|
|
396
437
|
* ```
|
|
397
438
|
*
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
* This hook is optimized to minimize unnecessary tool re-registrations:
|
|
401
|
-
*
|
|
402
|
-
* - **Memoized JSON conversion**: Zod-to-JSON schema conversions are memoized to
|
|
403
|
-
* avoid recomputation on every render.
|
|
404
|
-
*
|
|
405
|
-
* - **Ref-based callbacks**: `handler`, `onSuccess`, `onError`, and `formatOutput`
|
|
406
|
-
* are stored in refs, so changing these functions won't trigger re-registration.
|
|
407
|
-
*
|
|
408
|
-
* **IMPORTANT**: If `inputSchema`, `outputSchema`, or `annotations` are defined inline
|
|
409
|
-
* or change on every render, the tool will re-register unnecessarily. To avoid this,
|
|
410
|
-
* memoize these values using `useMemo` or define them outside your component:
|
|
411
|
-
*
|
|
412
|
-
* ```tsx
|
|
413
|
-
* // Good: Memoized schema (won't change unless deps change)
|
|
414
|
-
* const outputSchema = useMemo(() => ({
|
|
415
|
-
* count: z.number(),
|
|
416
|
-
* items: z.array(z.string()),
|
|
417
|
-
* }), []);
|
|
418
|
-
*
|
|
419
|
-
* // Good: Static schema defined outside component
|
|
420
|
-
* const OUTPUT_SCHEMA = {
|
|
421
|
-
* count: z.number(),
|
|
422
|
-
* items: z.array(z.string()),
|
|
423
|
-
* };
|
|
424
|
-
*
|
|
425
|
-
* // Bad: Inline schema (creates new object every render)
|
|
426
|
-
* useWebMCP({
|
|
427
|
-
* outputSchema: { count: z.number() }, // Re-registers every render!
|
|
428
|
-
* });
|
|
429
|
-
* ```
|
|
430
|
-
*
|
|
431
|
-
* **What triggers re-registration:**
|
|
432
|
-
* - Changes to `name` or `description`
|
|
433
|
-
* - Changes to `inputSchema`, `outputSchema`, or `annotations` (reference comparison)
|
|
434
|
-
* - Changes to any value in the `deps` argument (reference comparison)
|
|
435
|
-
*
|
|
436
|
-
* **What does NOT trigger re-registration:**
|
|
437
|
-
* - Changes to `handler`, `onSuccess`, `onError`, or `formatOutput` functions
|
|
438
|
-
*
|
|
439
|
-
* @template TInputSchema - Zod schema object defining input parameter types
|
|
440
|
-
* @template TOutputSchema - Zod schema object defining output structure (enables structuredContent)
|
|
439
|
+
* @template TInputSchema - JSON Schema defining input parameter types (use `as const` for inference)
|
|
440
|
+
* @template TOutputSchema - JSON Schema object defining output structure (enables structuredContent)
|
|
441
441
|
*
|
|
442
442
|
* @param config - Configuration object for the tool
|
|
443
443
|
* @param deps - Optional dependency array that triggers tool re-registration when values change.
|
|
444
|
-
* Similar to React's `useEffect` dependencies. When any value changes (by reference),
|
|
445
|
-
* the tool will be unregistered and re-registered. Prefer primitive values over
|
|
446
|
-
* objects/arrays to minimize re-registrations.
|
|
447
444
|
*
|
|
448
445
|
* @returns Object containing execution state and control methods
|
|
449
446
|
*
|
|
450
|
-
* @remarks
|
|
451
|
-
* The hook uses React refs to store callbacks (`handler`, `onSuccess`, `onError`, `formatOutput`)
|
|
452
|
-
* which prevents re-registration when these functions change. This is a performance optimization
|
|
453
|
-
* that follows the "latest ref" pattern.
|
|
454
|
-
*
|
|
455
|
-
* When `outputSchema` is provided, the MCP response includes both text content and
|
|
456
|
-
* `structuredContent` per the MCP specification. The type system ensures that the handler's
|
|
457
|
-
* return type matches the output schema through Zod's type inference.
|
|
458
|
-
*
|
|
459
447
|
* @public
|
|
460
|
-
*
|
|
461
|
-
* @example
|
|
462
|
-
* Basic tool with outputSchema (recommended):
|
|
463
|
-
* ```tsx
|
|
464
|
-
* function PostActions() {
|
|
465
|
-
* const likeTool = useWebMCP({
|
|
466
|
-
* name: 'posts_like',
|
|
467
|
-
* description: 'Like a post by ID',
|
|
468
|
-
* inputSchema: {
|
|
469
|
-
* postId: z.string().uuid().describe('The ID of the post to like'),
|
|
470
|
-
* },
|
|
471
|
-
* outputSchema: {
|
|
472
|
-
* success: z.boolean().describe('Whether the like was successful'),
|
|
473
|
-
* likeCount: z.number().describe('Updated like count'),
|
|
474
|
-
* },
|
|
475
|
-
* handler: async ({ postId }) => {
|
|
476
|
-
* const result = await api.posts.like(postId);
|
|
477
|
-
* return { success: true, likeCount: result.likes };
|
|
478
|
-
* },
|
|
479
|
-
* });
|
|
480
|
-
*
|
|
481
|
-
* // likeTool.state.lastResult is typed as { success: boolean; likeCount: number } | null
|
|
482
|
-
* if (likeTool.state.isExecuting) {
|
|
483
|
-
* return <Spinner />;
|
|
484
|
-
* }
|
|
485
|
-
*
|
|
486
|
-
* return <div>Likes: {likeTool.state.lastResult?.likeCount ?? 0}</div>;
|
|
487
|
-
* }
|
|
488
|
-
* ```
|
|
489
|
-
*
|
|
490
|
-
* @example
|
|
491
|
-
* Tool with annotations and callbacks:
|
|
492
|
-
* ```tsx
|
|
493
|
-
* const deleteTool = useWebMCP({
|
|
494
|
-
* name: 'posts_delete',
|
|
495
|
-
* description: 'Delete a post permanently',
|
|
496
|
-
* inputSchema: {
|
|
497
|
-
* postId: z.string().uuid(),
|
|
498
|
-
* },
|
|
499
|
-
* outputSchema: {
|
|
500
|
-
* deleted: z.boolean(),
|
|
501
|
-
* deletedAt: z.string().describe('ISO timestamp of deletion'),
|
|
502
|
-
* },
|
|
503
|
-
* annotations: {
|
|
504
|
-
* destructiveHint: true,
|
|
505
|
-
* idempotentHint: false,
|
|
506
|
-
* },
|
|
507
|
-
* handler: async ({ postId }) => {
|
|
508
|
-
* await api.posts.delete(postId);
|
|
509
|
-
* return { deleted: true, deletedAt: new Date().toISOString() };
|
|
510
|
-
* },
|
|
511
|
-
* onSuccess: () => {
|
|
512
|
-
* navigate('/posts');
|
|
513
|
-
* toast.success('Post deleted');
|
|
514
|
-
* },
|
|
515
|
-
* onError: (error) => {
|
|
516
|
-
* toast.error(`Failed to delete: ${error.message}`);
|
|
517
|
-
* },
|
|
518
|
-
* });
|
|
519
|
-
* ```
|
|
520
|
-
*
|
|
521
|
-
* @example
|
|
522
|
-
* Tool with deps for automatic re-registration:
|
|
523
|
-
* ```tsx
|
|
524
|
-
* function SitesManager({ sites }: { sites: Site[] }) {
|
|
525
|
-
* // Without deps, you'd need getter functions like: getSiteCount: () => sites.length
|
|
526
|
-
* // With deps, values can be used directly in description and handler
|
|
527
|
-
* const sitesTool = useWebMCP(
|
|
528
|
-
* {
|
|
529
|
-
* name: 'sites_query',
|
|
530
|
-
* description: `Query available sites. Current count: ${sites.length}`,
|
|
531
|
-
* outputSchema: {
|
|
532
|
-
* count: z.number(),
|
|
533
|
-
* sites: z.array(z.object({ id: z.string(), name: z.string() })),
|
|
534
|
-
* },
|
|
535
|
-
* handler: async () => ({
|
|
536
|
-
* count: sites.length,
|
|
537
|
-
* sites: sites.map(s => ({ id: s.id, name: s.name })),
|
|
538
|
-
* }),
|
|
539
|
-
* },
|
|
540
|
-
* [sites] // Re-register tool when sites array changes (by reference)
|
|
541
|
-
* );
|
|
542
|
-
*
|
|
543
|
-
* return <SitesList sites={sites} />;
|
|
544
|
-
* }
|
|
545
|
-
* ```
|
|
546
|
-
*
|
|
547
|
-
* @example
|
|
548
|
-
* Optimizing with memoization and deps:
|
|
549
|
-
* ```tsx
|
|
550
|
-
* function OptimizedSites({ sites }: { sites: Site[] }) {
|
|
551
|
-
* // Memoize schema to prevent re-registration on every render
|
|
552
|
-
* const outputSchema = useMemo(() => ({
|
|
553
|
-
* sites: z.array(z.object({ id: z.string(), name: z.string() })),
|
|
554
|
-
* }), []);
|
|
555
|
-
*
|
|
556
|
-
* // Use primitive values in deps for better control
|
|
557
|
-
* const siteIds = sites.map(s => s.id).join(',');
|
|
558
|
-
* const siteCount = sites.length;
|
|
559
|
-
*
|
|
560
|
-
* const sitesTool = useWebMCP(
|
|
561
|
-
* {
|
|
562
|
-
* name: 'sites_query',
|
|
563
|
-
* description: `Query ${siteCount} available sites`,
|
|
564
|
-
* outputSchema,
|
|
565
|
-
* handler: async () => ({
|
|
566
|
-
* sites: sites.map(s => ({ id: s.id, name: s.name })),
|
|
567
|
-
* }),
|
|
568
|
-
* },
|
|
569
|
-
* [siteIds, siteCount] // Only re-register when IDs or count actually change
|
|
570
|
-
* );
|
|
571
|
-
*
|
|
572
|
-
* return <SitesList sites={sites} />;
|
|
573
|
-
* }
|
|
574
|
-
* ```
|
|
575
448
|
*/
|
|
576
|
-
declare function useWebMCP<TInputSchema extends
|
|
449
|
+
declare function useWebMCP<TInputSchema extends ReactWebMCPInputSchema = InputSchema, TOutputSchema extends JsonSchemaObject | undefined = undefined>(config: WebMCPConfig<TInputSchema, TOutputSchema>, deps?: DependencyList): WebMCPReturn<TOutputSchema>;
|
|
577
450
|
//#endregion
|
|
578
451
|
//#region src/useWebMCPContext.d.ts
|
|
579
452
|
/**
|
|
@@ -686,9 +559,13 @@ declare function useWebMCPContext<T>(name: string, description: string, getValue
|
|
|
686
559
|
* name: 'review_code',
|
|
687
560
|
* description: 'Review code for best practices',
|
|
688
561
|
* argsSchema: {
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
*
|
|
562
|
+
* type: 'object',
|
|
563
|
+
* properties: {
|
|
564
|
+
* code: { type: 'string', description: 'The code to review' },
|
|
565
|
+
* language: { type: 'string', description: 'Programming language' },
|
|
566
|
+
* },
|
|
567
|
+
* required: ['code'],
|
|
568
|
+
* } as const,
|
|
692
569
|
* get: async ({ code, language }) => ({
|
|
693
570
|
* messages: [{
|
|
694
571
|
* role: 'user',
|
|
@@ -704,7 +581,7 @@ declare function useWebMCPContext<T>(name: string, description: string, getValue
|
|
|
704
581
|
* }
|
|
705
582
|
* ```
|
|
706
583
|
*/
|
|
707
|
-
declare function useWebMCPPrompt<TArgsSchema extends
|
|
584
|
+
declare function useWebMCPPrompt<TArgsSchema extends ReactWebMCPInputSchema = InputSchema>(config: WebMCPPromptConfig<TArgsSchema>): WebMCPPromptReturn;
|
|
708
585
|
//#endregion
|
|
709
586
|
//#region src/useWebMCPResource.d.ts
|
|
710
587
|
/**
|
|
@@ -776,7 +653,7 @@ interface ElicitationState {
|
|
|
776
653
|
/** Whether an elicitation request is currently in progress */
|
|
777
654
|
isLoading: boolean;
|
|
778
655
|
/** The last elicitation result received */
|
|
779
|
-
result: ElicitationResult
|
|
656
|
+
result: ElicitationResult | null;
|
|
780
657
|
/** Any error that occurred during the last request */
|
|
781
658
|
error: Error | null;
|
|
782
659
|
/** Total number of requests made */
|
|
@@ -789,7 +666,7 @@ interface UseElicitationConfig {
|
|
|
789
666
|
/**
|
|
790
667
|
* Optional callback invoked when an elicitation request completes successfully.
|
|
791
668
|
*/
|
|
792
|
-
onSuccess?: (result: ElicitationResult
|
|
669
|
+
onSuccess?: (result: ElicitationResult) => void;
|
|
793
670
|
/**
|
|
794
671
|
* Optional callback invoked when an elicitation request fails.
|
|
795
672
|
*/
|
|
@@ -802,7 +679,7 @@ interface UseElicitationReturn {
|
|
|
802
679
|
/** Current state of elicitation */
|
|
803
680
|
state: ElicitationState;
|
|
804
681
|
/** Function to request user input from the connected client */
|
|
805
|
-
elicitInput: (params: ElicitationParams
|
|
682
|
+
elicitInput: (params: ElicitationParams) => Promise<ElicitationResult>;
|
|
806
683
|
/** Reset the state */
|
|
807
684
|
reset: () => void;
|
|
808
685
|
}
|
|
@@ -882,7 +759,7 @@ interface SamplingState {
|
|
|
882
759
|
/** Whether a sampling request is currently in progress */
|
|
883
760
|
isLoading: boolean;
|
|
884
761
|
/** The last sampling result received */
|
|
885
|
-
result: SamplingResult
|
|
762
|
+
result: SamplingResult | null;
|
|
886
763
|
/** Any error that occurred during the last request */
|
|
887
764
|
error: Error | null;
|
|
888
765
|
/** Total number of requests made */
|
|
@@ -895,7 +772,7 @@ interface UseSamplingConfig {
|
|
|
895
772
|
/**
|
|
896
773
|
* Optional callback invoked when a sampling request completes successfully.
|
|
897
774
|
*/
|
|
898
|
-
onSuccess?: (result: SamplingResult
|
|
775
|
+
onSuccess?: (result: SamplingResult) => void;
|
|
899
776
|
/**
|
|
900
777
|
* Optional callback invoked when a sampling request fails.
|
|
901
778
|
*/
|
|
@@ -908,7 +785,7 @@ interface UseSamplingReturn {
|
|
|
908
785
|
/** Current state of sampling */
|
|
909
786
|
state: SamplingState;
|
|
910
787
|
/** Function to request LLM completion from the connected client */
|
|
911
|
-
createMessage: (params: SamplingRequestParams
|
|
788
|
+
createMessage: (params: SamplingRequestParams) => Promise<SamplingResult>;
|
|
912
789
|
/** Reset the state */
|
|
913
790
|
reset: () => void;
|
|
914
791
|
}
|
|
@@ -961,12 +838,12 @@ declare function useSampling(config?: UseSamplingConfig): UseSamplingReturn;
|
|
|
961
838
|
*/
|
|
962
839
|
interface McpClientContextValue {
|
|
963
840
|
client: Client;
|
|
964
|
-
tools: Tool
|
|
965
|
-
resources: Resource
|
|
841
|
+
tools: Tool[];
|
|
842
|
+
resources: Resource[];
|
|
966
843
|
isConnected: boolean;
|
|
967
844
|
isLoading: boolean;
|
|
968
845
|
error: Error | null;
|
|
969
|
-
capabilities: ServerCapabilities
|
|
846
|
+
capabilities: ServerCapabilities | null;
|
|
970
847
|
reconnect: () => Promise<void>;
|
|
971
848
|
}
|
|
972
849
|
/**
|
|
@@ -1105,5 +982,5 @@ declare function McpClientProvider({
|
|
|
1105
982
|
*/
|
|
1106
983
|
declare function useMcpClient(): McpClientContextValue;
|
|
1107
984
|
//#endregion
|
|
1108
|
-
export { type CallToolResult, type
|
|
985
|
+
export { type CallToolResult, type ElicitationState as ElicitationHandlerState, type ElicitationState, type InferOutput, type InferToolInput, McpClientProvider, type McpClientProviderProps, type ModelContextProtocol, type PromptDescriptor, type PromptMessage, type ReactWebMCPInputSchema, type ResourceContents, type ResourceDescriptor, type SamplingState as SamplingHandlerState, type SamplingState, type ToolAnnotations, type ToolDescriptor, type ToolExecutionState, type ToolInputSchema, type UseElicitationConfig, type UseElicitationConfig as UseElicitationHandlerConfig, type UseElicitationReturn as UseElicitationHandlerReturn, type UseElicitationReturn, type UseSamplingConfig, type UseSamplingConfig as UseSamplingHandlerConfig, type UseSamplingReturn as UseSamplingHandlerReturn, type UseSamplingReturn, type WebMCPConfig, type WebMCPPromptConfig, type WebMCPPromptReturn, type WebMCPResourceConfig, type WebMCPResourceReturn, type WebMCPReturn, type ZodSchemaObject, useElicitation, useElicitation as useElicitationHandler, useMcpClient, useSampling, useSampling as useSamplingHandler, useWebMCP, useWebMCPContext, useWebMCPPrompt, useWebMCPResource };
|
|
1109
986
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/useWebMCP.ts","../src/useWebMCPContext.ts","../src/useWebMCPPrompt.ts","../src/useWebMCPResource.ts","../src/useElicitationHandler.ts","../src/useSamplingHandler.ts","../src/client/McpClientProvider.tsx"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/zod-utils.ts","../src/types.ts","../src/useWebMCP.ts","../src/useWebMCPContext.ts","../src/useWebMCPPrompt.ts","../src/useWebMCPResource.ts","../src/useElicitationHandler.ts","../src/useSamplingHandler.ts","../src/client/McpClientProvider.tsx"],"sourcesContent":[],"mappings":";;;;;;;KAIY,eAAA,GAAkB,eAAe,CAAA,CAAE;;;;;ACwB/C;AAaA;;AAGM,KAhBM,sBAAA,GAAyB,iBAgB/B,GAhBiD,eAgBjD;;;;;;;;;;;;AAQI,KAXE,cAWF,CAAA,CAAA,CAAA,GATR,CASQ,SAAA;EACA,SAAA,WAAA,EAAA;IAAM,SAAA,KAAA,CAAA,EAAA,KAAA,MAAA;EAaJ,CAAA;CACY,GAvBlB,KAuBkB,SAAA;EAEpB,SAAA,KAAA,EAAA,KAAA,EAAA;CAAsB,GAxBlB,CAwBkB,GAvBlB,MAuBkB,CAAA,MAAA,EAAA,OAAA,CAAA,GArBpB,CAqBoB,SArBV,MAqBU,CAAA,MAAA,EArBK,CAAA,CAAE,UAqBP,CAAA,GApBlB,CAAA,CAAE,KAoBgB,CApBV,CAAA,CAAE,SAoBQ,CApBE,CAoBF,CAAA,CAAA,GAlBlB,CAkBkB,SAlBR,WAkBQ,GAjBhB,wBAiBgB,CAjBS,CAiBT,CAAA,GAhBhB,MAgBgB,CAAA,MAAA,EAAA,OAAA,CAAA;;;;;AAS1B;AA8EA;;;;;;AA8DgB,KAxJJ,WAwJI,CAAA,sBAvJQ,gBAuJR,GAAA,SAAA,GAAA,SAAA,EAAA,YAAA,OAAA,CAAA,GArJZ,aAqJY,SArJU,gBAqJV,GArJ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