@tambo-ai/react 0.54.1 → 0.56.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.
Files changed (60) hide show
  1. package/README.md +49 -14
  2. package/dist/hooks/__tests__/use-tambo-threads.test.js +1 -9
  3. package/dist/hooks/__tests__/use-tambo-threads.test.js.map +1 -1
  4. package/dist/mcp/__tests__/mcp-client.test.js +73 -27
  5. package/dist/mcp/__tests__/mcp-client.test.js.map +1 -1
  6. package/dist/mcp/mcp-client.d.ts +93 -210
  7. package/dist/mcp/mcp-client.d.ts.map +1 -1
  8. package/dist/mcp/mcp-client.js +103 -18
  9. package/dist/mcp/mcp-client.js.map +1 -1
  10. package/dist/mcp/tambo-mcp-provider.d.ts.map +1 -1
  11. package/dist/mcp/tambo-mcp-provider.js +2 -1
  12. package/dist/mcp/tambo-mcp-provider.js.map +1 -1
  13. package/dist/providers/__tests__/tambo-thread-provider.test.js +2 -6
  14. package/dist/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
  15. package/dist/providers/hooks/use-tambo-session-token.d.ts +2 -2
  16. package/dist/providers/hooks/use-tambo-session-token.js +2 -2
  17. package/dist/providers/hooks/use-tambo-session-token.js.map +1 -1
  18. package/dist/providers/tambo-context-helpers-provider.d.ts +7 -2
  19. package/dist/providers/tambo-context-helpers-provider.d.ts.map +1 -1
  20. package/dist/providers/tambo-context-helpers-provider.js +12 -7
  21. package/dist/providers/tambo-context-helpers-provider.js.map +1 -1
  22. package/dist/providers/tambo-prop-stream-provider/index.d.ts +1 -1
  23. package/dist/providers/tambo-prop-stream-provider/index.js +1 -1
  24. package/dist/providers/tambo-prop-stream-provider/index.js.map +1 -1
  25. package/dist/providers/tambo-provider.d.ts +1 -1
  26. package/dist/providers/tambo-provider.js +1 -1
  27. package/dist/providers/tambo-provider.js.map +1 -1
  28. package/dist/providers/tambo-thread-provider.d.ts.map +1 -1
  29. package/dist/providers/tambo-thread-provider.js +2 -4
  30. package/dist/providers/tambo-thread-provider.js.map +1 -1
  31. package/esm/hooks/__tests__/use-tambo-threads.test.js +1 -9
  32. package/esm/hooks/__tests__/use-tambo-threads.test.js.map +1 -1
  33. package/esm/mcp/__tests__/mcp-client.test.js +73 -27
  34. package/esm/mcp/__tests__/mcp-client.test.js.map +1 -1
  35. package/esm/mcp/mcp-client.d.ts +93 -210
  36. package/esm/mcp/mcp-client.d.ts.map +1 -1
  37. package/esm/mcp/mcp-client.js +103 -18
  38. package/esm/mcp/mcp-client.js.map +1 -1
  39. package/esm/mcp/tambo-mcp-provider.d.ts.map +1 -1
  40. package/esm/mcp/tambo-mcp-provider.js +2 -1
  41. package/esm/mcp/tambo-mcp-provider.js.map +1 -1
  42. package/esm/providers/__tests__/tambo-thread-provider.test.js +2 -6
  43. package/esm/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
  44. package/esm/providers/hooks/use-tambo-session-token.d.ts +2 -2
  45. package/esm/providers/hooks/use-tambo-session-token.js +2 -2
  46. package/esm/providers/hooks/use-tambo-session-token.js.map +1 -1
  47. package/esm/providers/tambo-context-helpers-provider.d.ts +7 -2
  48. package/esm/providers/tambo-context-helpers-provider.d.ts.map +1 -1
  49. package/esm/providers/tambo-context-helpers-provider.js +12 -7
  50. package/esm/providers/tambo-context-helpers-provider.js.map +1 -1
  51. package/esm/providers/tambo-prop-stream-provider/index.d.ts +1 -1
  52. package/esm/providers/tambo-prop-stream-provider/index.js +1 -1
  53. package/esm/providers/tambo-prop-stream-provider/index.js.map +1 -1
  54. package/esm/providers/tambo-provider.d.ts +1 -1
  55. package/esm/providers/tambo-provider.js +1 -1
  56. package/esm/providers/tambo-provider.js.map +1 -1
  57. package/esm/providers/tambo-thread-provider.d.ts.map +1 -1
  58. package/esm/providers/tambo-thread-provider.js +2 -4
  59. package/esm/providers/tambo-thread-provider.js.map +1 -1
  60. package/package.json +16 -19
@@ -1,8 +1,33 @@
1
+ import { type OAuthClientProvider } from "@modelcontextprotocol/sdk/client/auth.js";
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3
+ import { CreateMessageRequest, CreateMessageResult, ElicitRequest, ElicitResult } from "@modelcontextprotocol/sdk/types.js";
1
4
  import { JSONSchema7 } from "json-schema";
2
5
  export declare enum MCPTransport {
3
6
  SSE = "sse",
4
7
  HTTP = "http"
5
8
  }
9
+ /**
10
+ * Handlers for MCP requests - these are only used if the server supports the corresponding capabilities
11
+ * @param elicitation - Handler for elicitation requests
12
+ * @param sampling - Handler for sampling requests
13
+ * @example
14
+ * ```typescript
15
+ * const mcp = await MCPClient.create(
16
+ * 'https://api.example.com/mcp',
17
+ * MCPTransport.HTTP,
18
+ * {},
19
+ * undefined,
20
+ * undefined,
21
+ * {
22
+ * elicitation: (e: ElicitRequest) => Promise.resolve({...}),
23
+ * },
24
+ * });
25
+ * ```
26
+ */
27
+ interface MCPHandlers {
28
+ elicitation?: (e: ElicitRequest) => Promise<ElicitResult>;
29
+ sampling?: (e: CreateMessageRequest) => Promise<CreateMessageResult>;
30
+ }
6
31
  /**
7
32
  * A client for interacting with MCP (Model Context Protocol) servers.
8
33
  * Provides a simple interface for listing and calling tools exposed by the server.
@@ -17,8 +42,11 @@ export declare class MCPClient {
17
42
  private client;
18
43
  private transport;
19
44
  private transportType;
45
+ sessionId?: string;
20
46
  private endpoint;
21
47
  private headers;
48
+ private authProvider?;
49
+ private handlers;
22
50
  /**
23
51
  * Tracks an in-flight reconnect so concurrent triggers coalesce
24
52
  * (single-flight). When set, additional calls to `reconnect()` or
@@ -54,6 +82,7 @@ export declare class MCPClient {
54
82
  static readonly BACKOFF_MULTIPLIER = 2;
55
83
  static readonly BACKOFF_MAX_MS = 30000;
56
84
  static readonly BACKOFF_JITTER_RATIO = 0.2;
85
+ elicitation: EventTarget;
57
86
  /**
58
87
  * Private constructor to enforce using the static create method.
59
88
  * @param endpoint - The URL of the MCP server to connect to
@@ -62,16 +91,19 @@ export declare class MCPClient {
62
91
  */
63
92
  private constructor();
64
93
  /**
65
- * Creates and initializes a new MCPClient instance.
66
- * This is the recommended way to create an MCPClient as it handles both
67
- * instantiation and connection setup.
94
+ * Creates and initializes a new MCPClient instance. This is the recommended
95
+ * way to create an MCPClient as it handles both instantiation and connection
96
+ * setup.
68
97
  * @param endpoint - The URL of the MCP server to connect to
69
98
  * @param transportType - The transport type to use for the MCP client. Defaults to HTTP.
70
99
  * @param headers - Optional custom headers to include in requests
100
+ * @param authProvider - Optional auth provider to use for authentication
101
+ * @param sessionId - Optional session id to use for the MCP client - if not
102
+ * provided, a new session will be created
71
103
  * @returns A connected MCPClient instance ready for use
72
- * @throws Will throw an error if connection fails
104
+ * @throws {Error} Will throw an error if connection fails
73
105
  */
74
- static create(endpoint: string, transportType?: MCPTransport, headers?: Record<string, string>): Promise<MCPClient>;
106
+ static create(endpoint: string, transportType: MCPTransport | undefined, headers: Record<string, string> | undefined, authProvider: OAuthClientProvider | undefined, sessionId: string | undefined, handlers?: MCPHandlers): Promise<MCPClient>;
75
107
  /**
76
108
  * Reconnects to the MCP server, optionally retaining the same session ID.
77
109
  *
@@ -89,6 +121,7 @@ export declare class MCPClient {
89
121
  * @param newSession - Whether to create a new session (true) or reuse existing session ID (false)
90
122
  * @param reportErrorOnClose - Whether to report errors when closing the client
91
123
  * Note that only StreamableHTTPClientTransport supports session IDs.
124
+ * @returns A promise that resolves when the reconnect is complete
92
125
  */
93
126
  reconnect(newSession?: boolean, reportErrorOnClose?: boolean): Promise<void>;
94
127
  /**
@@ -99,6 +132,7 @@ export declare class MCPClient {
99
132
  private onclose;
100
133
  /**
101
134
  * Compute the next backoff delay with symmetric jitter.
135
+ * @returns The next backoff delay in milliseconds
102
136
  */
103
137
  private computeBackoffDelayMs;
104
138
  /**
@@ -107,228 +141,77 @@ export declare class MCPClient {
107
141
  */
108
142
  private scheduleAutoReconnect;
109
143
  private initializeTransport;
144
+ /**
145
+ * Initializes the MCP client with the appropriate capabilities and handlers
146
+ * @returns The initialized MCP client
147
+ */
110
148
  private initializeClient;
111
149
  /**
112
150
  * Retrieves a complete list of all available tools from the MCP server.
113
151
  * Handles pagination automatically by following cursors until all tools are fetched.
114
152
  * @returns A complete list of all available tools and their descriptions
115
- * @throws Will throw an error if any server request fails during pagination
153
+ * @throws {Error} Will throw an error if any server request fails during pagination
116
154
  */
117
155
  listTools(): Promise<MCPToolSpec[]>;
156
+ getServerCapabilities(): {
157
+ [x: string]: unknown;
158
+ tools?: {
159
+ [x: string]: unknown;
160
+ listChanged?: boolean | undefined;
161
+ } | undefined;
162
+ experimental?: {
163
+ [x: string]: unknown;
164
+ } | undefined;
165
+ logging?: {
166
+ [x: string]: unknown;
167
+ } | undefined;
168
+ completions?: {
169
+ [x: string]: unknown;
170
+ } | undefined;
171
+ prompts?: {
172
+ [x: string]: unknown;
173
+ listChanged?: boolean | undefined;
174
+ } | undefined;
175
+ resources?: {
176
+ [x: string]: unknown;
177
+ listChanged?: boolean | undefined;
178
+ subscribe?: boolean | undefined;
179
+ } | undefined;
180
+ } | undefined;
181
+ getServerVersion(): {
182
+ [x: string]: unknown;
183
+ name: string;
184
+ version: string;
185
+ title?: string | undefined;
186
+ websiteUrl?: string | undefined;
187
+ icons?: {
188
+ [x: string]: unknown;
189
+ src: string;
190
+ mimeType?: string | undefined;
191
+ sizes?: string[] | undefined;
192
+ }[] | undefined;
193
+ } | undefined;
194
+ getInstructions(): string | undefined;
118
195
  /**
119
196
  * Calls a specific tool on the MCP server with the provided arguments.
120
197
  * @param name - The name of the tool to call
121
198
  * @param args - Arguments to pass to the tool, must match the tool's expected schema
122
199
  * @returns The result from the tool execution
123
- * @throws Will throw an error if the tool call fails or if arguments are invalid
200
+ * @throws {Error} Will throw an error if the tool call fails or if arguments are invalid
124
201
  */
125
- callTool(name: string, args: Record<string, unknown>): Promise<import("zod").objectOutputType<{
126
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
127
- } & {
128
- content: import("zod").ZodDefault<import("zod").ZodArray<import("zod").ZodUnion<[import("zod").ZodObject<{
129
- type: import("zod").ZodLiteral<"text">;
130
- text: import("zod").ZodString;
131
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
132
- }, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{
133
- type: import("zod").ZodLiteral<"text">;
134
- text: import("zod").ZodString;
135
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
136
- }, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{
137
- type: import("zod").ZodLiteral<"text">;
138
- text: import("zod").ZodString;
139
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
140
- }, import("zod").ZodTypeAny, "passthrough">>, import("zod").ZodObject<{
141
- type: import("zod").ZodLiteral<"image">;
142
- data: import("zod").ZodEffects<import("zod").ZodString, string, string>;
143
- mimeType: import("zod").ZodString;
144
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
145
- }, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{
146
- type: import("zod").ZodLiteral<"image">;
147
- data: import("zod").ZodEffects<import("zod").ZodString, string, string>;
148
- mimeType: import("zod").ZodString;
149
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
150
- }, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{
151
- type: import("zod").ZodLiteral<"image">;
152
- data: import("zod").ZodEffects<import("zod").ZodString, string, string>;
153
- mimeType: import("zod").ZodString;
154
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
155
- }, import("zod").ZodTypeAny, "passthrough">>, import("zod").ZodObject<{
156
- type: import("zod").ZodLiteral<"audio">;
157
- data: import("zod").ZodEffects<import("zod").ZodString, string, string>;
158
- mimeType: import("zod").ZodString;
159
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
160
- }, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{
161
- type: import("zod").ZodLiteral<"audio">;
162
- data: import("zod").ZodEffects<import("zod").ZodString, string, string>;
163
- mimeType: import("zod").ZodString;
164
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
165
- }, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{
166
- type: import("zod").ZodLiteral<"audio">;
167
- data: import("zod").ZodEffects<import("zod").ZodString, string, string>;
168
- mimeType: import("zod").ZodString;
169
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
170
- }, import("zod").ZodTypeAny, "passthrough">>, import("zod").ZodObject<import("zod").objectUtil.extendShape<import("zod").objectUtil.extendShape<{
171
- name: import("zod").ZodString;
172
- title: import("zod").ZodOptional<import("zod").ZodString>;
173
- }, {
174
- uri: import("zod").ZodString;
175
- description: import("zod").ZodOptional<import("zod").ZodString>;
176
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
177
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
178
- }>, {
179
- type: import("zod").ZodLiteral<"resource_link">;
180
- }>, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<import("zod").objectUtil.extendShape<import("zod").objectUtil.extendShape<{
181
- name: import("zod").ZodString;
182
- title: import("zod").ZodOptional<import("zod").ZodString>;
183
- }, {
184
- uri: import("zod").ZodString;
185
- description: import("zod").ZodOptional<import("zod").ZodString>;
186
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
187
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
188
- }>, {
189
- type: import("zod").ZodLiteral<"resource_link">;
190
- }>, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<import("zod").objectUtil.extendShape<import("zod").objectUtil.extendShape<{
191
- name: import("zod").ZodString;
192
- title: import("zod").ZodOptional<import("zod").ZodString>;
193
- }, {
194
- uri: import("zod").ZodString;
195
- description: import("zod").ZodOptional<import("zod").ZodString>;
196
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
197
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
198
- }>, {
199
- type: import("zod").ZodLiteral<"resource_link">;
200
- }>, import("zod").ZodTypeAny, "passthrough">>, import("zod").ZodObject<{
201
- type: import("zod").ZodLiteral<"resource">;
202
- resource: import("zod").ZodUnion<[import("zod").ZodObject<import("zod").objectUtil.extendShape<{
203
- uri: import("zod").ZodString;
204
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
205
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
206
- }, {
207
- text: import("zod").ZodString;
208
- }>, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<import("zod").objectUtil.extendShape<{
209
- uri: import("zod").ZodString;
210
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
211
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
212
- }, {
213
- text: import("zod").ZodString;
214
- }>, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<import("zod").objectUtil.extendShape<{
215
- uri: import("zod").ZodString;
216
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
217
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
218
- }, {
219
- text: import("zod").ZodString;
220
- }>, import("zod").ZodTypeAny, "passthrough">>, import("zod").ZodObject<import("zod").objectUtil.extendShape<{
221
- uri: import("zod").ZodString;
222
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
223
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
224
- }, {
225
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
226
- }>, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<import("zod").objectUtil.extendShape<{
227
- uri: import("zod").ZodString;
228
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
229
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
230
- }, {
231
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
232
- }>, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<import("zod").objectUtil.extendShape<{
233
- uri: import("zod").ZodString;
234
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
235
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
236
- }, {
237
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
238
- }>, import("zod").ZodTypeAny, "passthrough">>]>;
239
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
240
- }, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{
241
- type: import("zod").ZodLiteral<"resource">;
242
- resource: import("zod").ZodUnion<[import("zod").ZodObject<import("zod").objectUtil.extendShape<{
243
- uri: import("zod").ZodString;
244
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
245
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
246
- }, {
247
- text: import("zod").ZodString;
248
- }>, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<import("zod").objectUtil.extendShape<{
249
- uri: import("zod").ZodString;
250
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
251
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
252
- }, {
253
- text: import("zod").ZodString;
254
- }>, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<import("zod").objectUtil.extendShape<{
255
- uri: import("zod").ZodString;
256
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
257
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
258
- }, {
259
- text: import("zod").ZodString;
260
- }>, import("zod").ZodTypeAny, "passthrough">>, import("zod").ZodObject<import("zod").objectUtil.extendShape<{
261
- uri: import("zod").ZodString;
262
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
263
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
264
- }, {
265
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
266
- }>, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<import("zod").objectUtil.extendShape<{
267
- uri: import("zod").ZodString;
268
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
269
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
270
- }, {
271
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
272
- }>, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<import("zod").objectUtil.extendShape<{
273
- uri: import("zod").ZodString;
274
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
275
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
276
- }, {
277
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
278
- }>, import("zod").ZodTypeAny, "passthrough">>]>;
279
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
280
- }, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{
281
- type: import("zod").ZodLiteral<"resource">;
282
- resource: import("zod").ZodUnion<[import("zod").ZodObject<import("zod").objectUtil.extendShape<{
283
- uri: import("zod").ZodString;
284
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
285
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
286
- }, {
287
- text: import("zod").ZodString;
288
- }>, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<import("zod").objectUtil.extendShape<{
289
- uri: import("zod").ZodString;
290
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
291
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
292
- }, {
293
- text: import("zod").ZodString;
294
- }>, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<import("zod").objectUtil.extendShape<{
295
- uri: import("zod").ZodString;
296
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
297
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
298
- }, {
299
- text: import("zod").ZodString;
300
- }>, import("zod").ZodTypeAny, "passthrough">>, import("zod").ZodObject<import("zod").objectUtil.extendShape<{
301
- uri: import("zod").ZodString;
302
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
303
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
304
- }, {
305
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
306
- }>, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<import("zod").objectUtil.extendShape<{
307
- uri: import("zod").ZodString;
308
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
309
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
310
- }, {
311
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
312
- }>, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<import("zod").objectUtil.extendShape<{
313
- uri: import("zod").ZodString;
314
- mimeType: import("zod").ZodOptional<import("zod").ZodString>;
315
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
316
- }, {
317
- blob: import("zod").ZodEffects<import("zod").ZodString, string, string>;
318
- }>, import("zod").ZodTypeAny, "passthrough">>]>;
319
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
320
- }, import("zod").ZodTypeAny, "passthrough">>]>, "many">>;
321
- structuredContent: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
322
- isError: import("zod").ZodOptional<import("zod").ZodBoolean>;
323
- }, import("zod").ZodTypeAny, "passthrough"> | import("zod").objectOutputType<{
324
- _meta: import("zod").ZodOptional<import("zod").ZodObject<{}, "passthrough", import("zod").ZodTypeAny, import("zod").objectOutputType<{}, import("zod").ZodTypeAny, "passthrough">, import("zod").objectInputType<{}, import("zod").ZodTypeAny, "passthrough">>>;
325
- } & {
326
- toolResult: import("zod").ZodUnknown;
327
- }, import("zod").ZodTypeAny, "passthrough">>;
202
+ callTool(name: string, args: Record<string, unknown>): Promise<MCPToolCallResult>;
203
+ updateElicitationHandler(handler: ((e: ElicitRequest) => Promise<ElicitResult>) | undefined): void;
204
+ updateSamplingHandler(handler: ((e: CreateMessageRequest) => Promise<CreateMessageResult>) | undefined): void;
328
205
  }
206
+ /**
207
+ * The result of a tool call.
208
+ * This is the same as the result of a tool call in the OpenAI SDK, but is reified here
209
+ */
210
+ export type MCPToolCallResult = Awaited<ReturnType<typeof Client.prototype.callTool>>;
329
211
  export interface MCPToolSpec {
330
212
  name: string;
331
213
  description?: string;
332
214
  inputSchema?: JSONSchema7;
333
215
  }
216
+ export {};
334
217
  //# sourceMappingURL=mcp-client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-client.d.ts","sourceRoot":"","sources":["../../src/mcp/mcp-client.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,oBAAY,YAAY;IACtB,GAAG,QAAQ;IACX,IAAI,SAAS;CACd;AACD;;;;;;;;;GASG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAqD;IACtE,OAAO,CAAC,aAAa,CAAe;IACpC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAyB;IACxC;;;;;OAKG;IACH,OAAO,CAAC,YAAY,CAAC,CAAgB;IACrC;;;OAGG;IACH,OAAO,CAAC,cAAc,CAAC,CAAgC;IACvD;;;OAGG;IACH,OAAO,CAAC,eAAe,CAAK;IAE5B;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAC,QAAQ,CAAC,kBAAkB,OAAO;IACzC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,KAAK;IACvC,MAAM,CAAC,QAAQ,CAAC,cAAc,SAAU;IACxC,MAAM,CAAC,QAAQ,CAAC,oBAAoB,OAAO;IAE3C;;;;;OAKG;IACH,OAAO;IAYP;;;;;;;;;OASG;WACU,MAAM,CACjB,QAAQ,EAAE,MAAM,EAChB,aAAa,GAAE,YAAgC,EAC/C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC/B,OAAO,CAAC,SAAS,CAAC;IAKrB;;;;;;;;;;;;;;;;;OAiBG;IACG,SAAS,CAAC,UAAU,UAAQ,EAAE,kBAAkB,UAAO;IAmD7D;;;;OAIG;IACH,OAAO,CAAC,OAAO;IAIf;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAY7B;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAoC7B,OAAO,CAAC,mBAAmB;IAa3B,OAAO,CAAC,gBAAgB;IASxB;;;;;OAKG;IACG,SAAS,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAiCzC;;;;;;OAMG;IACG,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAO3D;AASD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B"}
1
+ {"version":3,"file":"mcp-client.d.ts","sourceRoot":"","sources":["../../src/mcp/mcp-client.ts"],"names":[],"mappings":"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"}
@@ -1,6 +1,7 @@
1
1
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
2
  import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
3
3
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
4
+ import { CreateMessageRequestSchema, ElicitRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
4
5
  export var MCPTransport;
5
6
  (function (MCPTransport) {
6
7
  MCPTransport["SSE"] = "sse";
@@ -20,8 +21,11 @@ export class MCPClient {
20
21
  client;
21
22
  transport;
22
23
  transportType;
24
+ sessionId;
23
25
  endpoint;
24
26
  headers;
27
+ authProvider;
28
+ handlers;
25
29
  /**
26
30
  * Tracks an in-flight reconnect so concurrent triggers coalesce
27
31
  * (single-flight). When set, additional calls to `reconnect()` or
@@ -57,32 +61,41 @@ export class MCPClient {
57
61
  static BACKOFF_MULTIPLIER = 2;
58
62
  static BACKOFF_MAX_MS = 30_000;
59
63
  static BACKOFF_JITTER_RATIO = 0.2;
64
+ elicitation = new EventTarget();
60
65
  /**
61
66
  * Private constructor to enforce using the static create method.
62
67
  * @param endpoint - The URL of the MCP server to connect to
63
68
  * @param transportType - The transport to use for the MCP client
64
69
  * @param headers - Optional custom headers to include in requests
65
70
  */
66
- constructor(endpoint, transportType, headers = {}) {
71
+ constructor(endpoint, transportType, headers, authProvider, sessionId, handlers = {}) {
67
72
  this.endpoint = endpoint;
68
- this.headers = headers;
73
+ this.headers = headers ?? {};
74
+ this.authProvider = authProvider;
69
75
  this.transportType = transportType;
70
- this.transport = this.initializeTransport(undefined);
76
+ this.handlers = handlers;
77
+ this.transport = this.initializeTransport(sessionId);
71
78
  this.client = this.initializeClient();
72
79
  }
73
80
  /**
74
- * Creates and initializes a new MCPClient instance.
75
- * This is the recommended way to create an MCPClient as it handles both
76
- * instantiation and connection setup.
81
+ * Creates and initializes a new MCPClient instance. This is the recommended
82
+ * way to create an MCPClient as it handles both instantiation and connection
83
+ * setup.
77
84
  * @param endpoint - The URL of the MCP server to connect to
78
85
  * @param transportType - The transport type to use for the MCP client. Defaults to HTTP.
79
86
  * @param headers - Optional custom headers to include in requests
87
+ * @param authProvider - Optional auth provider to use for authentication
88
+ * @param sessionId - Optional session id to use for the MCP client - if not
89
+ * provided, a new session will be created
80
90
  * @returns A connected MCPClient instance ready for use
81
- * @throws Will throw an error if connection fails
91
+ * @throws {Error} Will throw an error if connection fails
82
92
  */
83
- static async create(endpoint, transportType = MCPTransport.HTTP, headers) {
84
- const mcpClient = new MCPClient(endpoint, transportType, headers);
93
+ static async create(endpoint, transportType = MCPTransport.HTTP, headers, authProvider, sessionId, handlers = {}) {
94
+ const mcpClient = new MCPClient(endpoint, transportType, headers, authProvider, sessionId, handlers);
85
95
  await mcpClient.client.connect(mcpClient.transport);
96
+ if ("sessionId" in mcpClient.transport) {
97
+ mcpClient.sessionId = mcpClient.transport.sessionId;
98
+ }
86
99
  return mcpClient;
87
100
  }
88
101
  /**
@@ -102,6 +115,7 @@ export class MCPClient {
102
115
  * @param newSession - Whether to create a new session (true) or reuse existing session ID (false)
103
116
  * @param reportErrorOnClose - Whether to report errors when closing the client
104
117
  * Note that only StreamableHTTPClientTransport supports session IDs.
118
+ * @returns A promise that resolves when the reconnect is complete
105
119
  */
106
120
  async reconnect(newSession = false, reportErrorOnClose = true) {
107
121
  // If a reconnect is already running, coalesce into it.
@@ -114,11 +128,7 @@ export class MCPClient {
114
128
  this.reconnectTimer = undefined;
115
129
  }
116
130
  const doReconnect = async () => {
117
- const sessionId = newSession
118
- ? undefined
119
- : "sessionId" in this.transport
120
- ? this.transport.sessionId
121
- : undefined;
131
+ const sessionId = newSession ? undefined : this.sessionId;
122
132
  // Prevent re-entrant onclose during deliberate close by detaching
123
133
  // the handler from the previous client instance.
124
134
  const prevClient = this.client;
@@ -135,6 +145,14 @@ export class MCPClient {
135
145
  this.transport = this.initializeTransport(sessionId);
136
146
  this.client = this.initializeClient();
137
147
  await this.client.connect(this.transport);
148
+ // We may have gotten a session id from the server, so we need to set it
149
+ if ("sessionId" in this.transport) {
150
+ this.sessionId = this.transport.sessionId;
151
+ if (sessionId !== this.sessionId) {
152
+ // This is a pretty unusual thing to happen, but it might be possible?
153
+ console.warn("Session id mismatch", sessionId, this.sessionId);
154
+ }
155
+ }
138
156
  };
139
157
  this.reconnecting = (async () => {
140
158
  try {
@@ -158,6 +176,7 @@ export class MCPClient {
158
176
  }
159
177
  /**
160
178
  * Compute the next backoff delay with symmetric jitter.
179
+ * @returns The next backoff delay in milliseconds
161
180
  */
162
181
  computeBackoffDelayMs() {
163
182
  const base = Math.min(MCPClient.BACKOFF_MAX_MS, MCPClient.BACKOFF_INITIAL_MS *
@@ -203,29 +222,50 @@ export class MCPClient {
203
222
  initializeTransport(sessionId) {
204
223
  if (this.transportType === MCPTransport.SSE) {
205
224
  return new SSEClientTransport(new URL(this.endpoint), {
225
+ authProvider: this.authProvider,
206
226
  requestInit: { headers: this.headers },
207
227
  });
208
228
  }
209
229
  else {
210
230
  return new StreamableHTTPClientTransport(new URL(this.endpoint), {
211
- sessionId,
231
+ authProvider: this.authProvider,
212
232
  requestInit: { headers: this.headers },
233
+ sessionId,
213
234
  });
214
235
  }
215
236
  }
237
+ /**
238
+ * Initializes the MCP client with the appropriate capabilities and handlers
239
+ * @returns The initialized MCP client
240
+ */
216
241
  initializeClient() {
242
+ const elicitationCapability = this.handlers.elicitation
243
+ ? { elicitation: {} }
244
+ : {};
245
+ const samplingCapability = this.handlers.sampling ? { sampling: {} } : {};
217
246
  const client = new Client({
218
247
  name: "tambo-mcp-client",
219
248
  version: "1.0.0",
249
+ }, {
250
+ capabilities: {
251
+ ...elicitationCapability,
252
+ ...samplingCapability,
253
+ },
220
254
  });
221
255
  client.onclose = this.onclose.bind(this);
256
+ if (this.handlers.elicitation) {
257
+ client.setRequestHandler(ElicitRequestSchema, this.handlers.elicitation);
258
+ }
259
+ if (this.handlers.sampling) {
260
+ client.setRequestHandler(CreateMessageRequestSchema, this.handlers.sampling);
261
+ }
222
262
  return client;
223
263
  }
224
264
  /**
225
265
  * Retrieves a complete list of all available tools from the MCP server.
226
266
  * Handles pagination automatically by following cursors until all tools are fetched.
227
267
  * @returns A complete list of all available tools and their descriptions
228
- * @throws Will throw an error if any server request fails during pagination
268
+ * @throws {Error} Will throw an error if any server request fails during pagination
229
269
  */
230
270
  async listTools() {
231
271
  const allTools = [];
@@ -234,7 +274,9 @@ export class MCPClient {
234
274
  while (hasMore) {
235
275
  const response = await this.client.listTools({ cursor }, {});
236
276
  allTools.push(...response.tools.map((tool) => {
237
- if (tool.inputSchema.type !== "object") {
277
+ // make sure the right type is used
278
+ const inputSchemaType = tool.inputSchema.type;
279
+ if (inputSchemaType !== "object") {
238
280
  throw new Error(`Input schema for tool ${tool.name} is not an object`);
239
281
  }
240
282
  return {
@@ -252,12 +294,21 @@ export class MCPClient {
252
294
  }
253
295
  return allTools;
254
296
  }
297
+ getServerCapabilities() {
298
+ return this.client.getServerCapabilities();
299
+ }
300
+ getServerVersion() {
301
+ return this.client.getServerVersion();
302
+ }
303
+ getInstructions() {
304
+ return this.client.getInstructions();
305
+ }
255
306
  /**
256
307
  * Calls a specific tool on the MCP server with the provided arguments.
257
308
  * @param name - The name of the tool to call
258
309
  * @param args - Arguments to pass to the tool, must match the tool's expected schema
259
310
  * @returns The result from the tool execution
260
- * @throws Will throw an error if the tool call fails or if arguments are invalid
311
+ * @throws {Error} Will throw an error if the tool call fails or if arguments are invalid
261
312
  */
262
313
  async callTool(name, args) {
263
314
  const result = await this.client.callTool({
@@ -266,5 +317,39 @@ export class MCPClient {
266
317
  });
267
318
  return result;
268
319
  }
320
+ updateElicitationHandler(handler) {
321
+ // Because we advertise the elicitation capability on initial connection, we can only update
322
+ // an existing handler, not add it if we haven't set it yet.
323
+ if (handler && !this.handlers.elicitation) {
324
+ throw new Error("Elicitation handler must be set on create");
325
+ }
326
+ this.handlers = {
327
+ ...this.handlers,
328
+ elicitation: handler,
329
+ };
330
+ if (!handler) {
331
+ const method = ElicitRequestSchema.shape.method.value;
332
+ this.client.removeRequestHandler(method);
333
+ return;
334
+ }
335
+ this.client.setRequestHandler(ElicitRequestSchema, handler);
336
+ }
337
+ updateSamplingHandler(handler) {
338
+ // Because we advertise the sampling capability on initial connection, we can only update
339
+ // an existing handler, not add it if we haven't set it yet.
340
+ if (handler && !this.handlers.sampling) {
341
+ throw new Error("Sampling handler must be set on create");
342
+ }
343
+ this.handlers = {
344
+ ...this.handlers,
345
+ sampling: handler,
346
+ };
347
+ if (!handler) {
348
+ const method = CreateMessageRequestSchema.shape.method.value;
349
+ this.client.removeRequestHandler(method);
350
+ return;
351
+ }
352
+ this.client.setRequestHandler(CreateMessageRequestSchema, handler);
353
+ }
269
354
  }
270
355
  //# sourceMappingURL=mcp-client.js.map