@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":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AACpF,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAGnE,OAAO,EACL,oBAAoB,EAEpB,mBAAmB,EACnB,aAAa,EAEb,YAAY,EACb,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,oBAAY,YAAY;IACtB,GAAG,QAAQ;IACX,IAAI,SAAS;CACd;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,UAAU,WAAW;IACnB,WAAW,CAAC,EAAE,CAAC,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;IAC1D,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,oBAAoB,KAAK,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACtE;AAED;;;;;;;;;GASG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAqD;IACtE,OAAO,CAAC,aAAa,CAAe;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;IAC1B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,YAAY,CAAC,CAAsB;IAC3C,OAAO,CAAC,QAAQ,CAAc;IAC9B;;;;;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;IAEpC,WAAW,EAAE,WAAW,CAAqB;IACpD;;;;;OAKG;IACH,OAAO;IAiBP;;;;;;;;;;;;OAYG;WACU,MAAM,CACjB,QAAQ,EAAE,MAAM,EAChB,aAAa,EAAE,YAAY,YAAoB,EAC/C,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EAC3C,YAAY,EAAE,mBAAmB,GAAG,SAAS,EAC7C,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,QAAQ,GAAE,WAAgB,GACzB,OAAO,CAAC,SAAS,CAAC;IAerB;;;;;;;;;;;;;;;;;;OAkBG;IACG,SAAS,CAAC,UAAU,UAAQ,EAAE,kBAAkB,UAAO;IAuD7D;;;;OAIG;IACH,OAAO,CAAC,OAAO;IAIf;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAY7B;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAoC7B,OAAO,CAAC,mBAAmB;IAe3B;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IA8BxB;;;;;OAKG;IACG,SAAS,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAmCzC,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;IAIrB,gBAAgB;;;;;;;;;;;;;IAIhB,eAAe;IAIf;;;;;;OAMG;IACG,QAAQ,CACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,OAAO,CAAC,iBAAiB,CAAC;IAQ7B,wBAAwB,CACtB,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC,GAAG,SAAS;IAmBpE,qBAAqB,CACnB,OAAO,EACH,CAAC,CAAC,CAAC,EAAE,oBAAoB,KAAK,OAAO,CAAC,mBAAmB,CAAC,CAAC,GAC3D,SAAS;CAkBhB;AAED;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG,OAAO,CACrC,UAAU,CAAC,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,CAC7C,CAAC;AASF,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B"}
@@ -4,6 +4,7 @@ exports.MCPClient = exports.MCPTransport = void 0;
4
4
  const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
5
5
  const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
6
6
  const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
7
+ const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
7
8
  var MCPTransport;
8
9
  (function (MCPTransport) {
9
10
  MCPTransport["SSE"] = "sse";
@@ -23,8 +24,11 @@ class MCPClient {
23
24
  client;
24
25
  transport;
25
26
  transportType;
27
+ sessionId;
26
28
  endpoint;
27
29
  headers;
30
+ authProvider;
31
+ handlers;
28
32
  /**
29
33
  * Tracks an in-flight reconnect so concurrent triggers coalesce
30
34
  * (single-flight). When set, additional calls to `reconnect()` or
@@ -60,32 +64,41 @@ class MCPClient {
60
64
  static BACKOFF_MULTIPLIER = 2;
61
65
  static BACKOFF_MAX_MS = 30_000;
62
66
  static BACKOFF_JITTER_RATIO = 0.2;
67
+ elicitation = new EventTarget();
63
68
  /**
64
69
  * Private constructor to enforce using the static create method.
65
70
  * @param endpoint - The URL of the MCP server to connect to
66
71
  * @param transportType - The transport to use for the MCP client
67
72
  * @param headers - Optional custom headers to include in requests
68
73
  */
69
- constructor(endpoint, transportType, headers = {}) {
74
+ constructor(endpoint, transportType, headers, authProvider, sessionId, handlers = {}) {
70
75
  this.endpoint = endpoint;
71
- this.headers = headers;
76
+ this.headers = headers ?? {};
77
+ this.authProvider = authProvider;
72
78
  this.transportType = transportType;
73
- this.transport = this.initializeTransport(undefined);
79
+ this.handlers = handlers;
80
+ this.transport = this.initializeTransport(sessionId);
74
81
  this.client = this.initializeClient();
75
82
  }
76
83
  /**
77
- * Creates and initializes a new MCPClient instance.
78
- * This is the recommended way to create an MCPClient as it handles both
79
- * instantiation and connection setup.
84
+ * Creates and initializes a new MCPClient instance. This is the recommended
85
+ * way to create an MCPClient as it handles both instantiation and connection
86
+ * setup.
80
87
  * @param endpoint - The URL of the MCP server to connect to
81
88
  * @param transportType - The transport type to use for the MCP client. Defaults to HTTP.
82
89
  * @param headers - Optional custom headers to include in requests
90
+ * @param authProvider - Optional auth provider to use for authentication
91
+ * @param sessionId - Optional session id to use for the MCP client - if not
92
+ * provided, a new session will be created
83
93
  * @returns A connected MCPClient instance ready for use
84
- * @throws Will throw an error if connection fails
94
+ * @throws {Error} Will throw an error if connection fails
85
95
  */
86
- static async create(endpoint, transportType = MCPTransport.HTTP, headers) {
87
- const mcpClient = new MCPClient(endpoint, transportType, headers);
96
+ static async create(endpoint, transportType = MCPTransport.HTTP, headers, authProvider, sessionId, handlers = {}) {
97
+ const mcpClient = new MCPClient(endpoint, transportType, headers, authProvider, sessionId, handlers);
88
98
  await mcpClient.client.connect(mcpClient.transport);
99
+ if ("sessionId" in mcpClient.transport) {
100
+ mcpClient.sessionId = mcpClient.transport.sessionId;
101
+ }
89
102
  return mcpClient;
90
103
  }
91
104
  /**
@@ -105,6 +118,7 @@ class MCPClient {
105
118
  * @param newSession - Whether to create a new session (true) or reuse existing session ID (false)
106
119
  * @param reportErrorOnClose - Whether to report errors when closing the client
107
120
  * Note that only StreamableHTTPClientTransport supports session IDs.
121
+ * @returns A promise that resolves when the reconnect is complete
108
122
  */
109
123
  async reconnect(newSession = false, reportErrorOnClose = true) {
110
124
  // If a reconnect is already running, coalesce into it.
@@ -117,11 +131,7 @@ class MCPClient {
117
131
  this.reconnectTimer = undefined;
118
132
  }
119
133
  const doReconnect = async () => {
120
- const sessionId = newSession
121
- ? undefined
122
- : "sessionId" in this.transport
123
- ? this.transport.sessionId
124
- : undefined;
134
+ const sessionId = newSession ? undefined : this.sessionId;
125
135
  // Prevent re-entrant onclose during deliberate close by detaching
126
136
  // the handler from the previous client instance.
127
137
  const prevClient = this.client;
@@ -138,6 +148,14 @@ class MCPClient {
138
148
  this.transport = this.initializeTransport(sessionId);
139
149
  this.client = this.initializeClient();
140
150
  await this.client.connect(this.transport);
151
+ // We may have gotten a session id from the server, so we need to set it
152
+ if ("sessionId" in this.transport) {
153
+ this.sessionId = this.transport.sessionId;
154
+ if (sessionId !== this.sessionId) {
155
+ // This is a pretty unusual thing to happen, but it might be possible?
156
+ console.warn("Session id mismatch", sessionId, this.sessionId);
157
+ }
158
+ }
141
159
  };
142
160
  this.reconnecting = (async () => {
143
161
  try {
@@ -161,6 +179,7 @@ class MCPClient {
161
179
  }
162
180
  /**
163
181
  * Compute the next backoff delay with symmetric jitter.
182
+ * @returns The next backoff delay in milliseconds
164
183
  */
165
184
  computeBackoffDelayMs() {
166
185
  const base = Math.min(MCPClient.BACKOFF_MAX_MS, MCPClient.BACKOFF_INITIAL_MS *
@@ -206,29 +225,50 @@ class MCPClient {
206
225
  initializeTransport(sessionId) {
207
226
  if (this.transportType === MCPTransport.SSE) {
208
227
  return new sse_js_1.SSEClientTransport(new URL(this.endpoint), {
228
+ authProvider: this.authProvider,
209
229
  requestInit: { headers: this.headers },
210
230
  });
211
231
  }
212
232
  else {
213
233
  return new streamableHttp_js_1.StreamableHTTPClientTransport(new URL(this.endpoint), {
214
- sessionId,
234
+ authProvider: this.authProvider,
215
235
  requestInit: { headers: this.headers },
236
+ sessionId,
216
237
  });
217
238
  }
218
239
  }
240
+ /**
241
+ * Initializes the MCP client with the appropriate capabilities and handlers
242
+ * @returns The initialized MCP client
243
+ */
219
244
  initializeClient() {
245
+ const elicitationCapability = this.handlers.elicitation
246
+ ? { elicitation: {} }
247
+ : {};
248
+ const samplingCapability = this.handlers.sampling ? { sampling: {} } : {};
220
249
  const client = new index_js_1.Client({
221
250
  name: "tambo-mcp-client",
222
251
  version: "1.0.0",
252
+ }, {
253
+ capabilities: {
254
+ ...elicitationCapability,
255
+ ...samplingCapability,
256
+ },
223
257
  });
224
258
  client.onclose = this.onclose.bind(this);
259
+ if (this.handlers.elicitation) {
260
+ client.setRequestHandler(types_js_1.ElicitRequestSchema, this.handlers.elicitation);
261
+ }
262
+ if (this.handlers.sampling) {
263
+ client.setRequestHandler(types_js_1.CreateMessageRequestSchema, this.handlers.sampling);
264
+ }
225
265
  return client;
226
266
  }
227
267
  /**
228
268
  * Retrieves a complete list of all available tools from the MCP server.
229
269
  * Handles pagination automatically by following cursors until all tools are fetched.
230
270
  * @returns A complete list of all available tools and their descriptions
231
- * @throws Will throw an error if any server request fails during pagination
271
+ * @throws {Error} Will throw an error if any server request fails during pagination
232
272
  */
233
273
  async listTools() {
234
274
  const allTools = [];
@@ -237,7 +277,9 @@ class MCPClient {
237
277
  while (hasMore) {
238
278
  const response = await this.client.listTools({ cursor }, {});
239
279
  allTools.push(...response.tools.map((tool) => {
240
- if (tool.inputSchema.type !== "object") {
280
+ // make sure the right type is used
281
+ const inputSchemaType = tool.inputSchema.type;
282
+ if (inputSchemaType !== "object") {
241
283
  throw new Error(`Input schema for tool ${tool.name} is not an object`);
242
284
  }
243
285
  return {
@@ -255,12 +297,21 @@ class MCPClient {
255
297
  }
256
298
  return allTools;
257
299
  }
300
+ getServerCapabilities() {
301
+ return this.client.getServerCapabilities();
302
+ }
303
+ getServerVersion() {
304
+ return this.client.getServerVersion();
305
+ }
306
+ getInstructions() {
307
+ return this.client.getInstructions();
308
+ }
258
309
  /**
259
310
  * Calls a specific tool on the MCP server with the provided arguments.
260
311
  * @param name - The name of the tool to call
261
312
  * @param args - Arguments to pass to the tool, must match the tool's expected schema
262
313
  * @returns The result from the tool execution
263
- * @throws Will throw an error if the tool call fails or if arguments are invalid
314
+ * @throws {Error} Will throw an error if the tool call fails or if arguments are invalid
264
315
  */
265
316
  async callTool(name, args) {
266
317
  const result = await this.client.callTool({
@@ -269,6 +320,40 @@ class MCPClient {
269
320
  });
270
321
  return result;
271
322
  }
323
+ updateElicitationHandler(handler) {
324
+ // Because we advertise the elicitation capability on initial connection, we can only update
325
+ // an existing handler, not add it if we haven't set it yet.
326
+ if (handler && !this.handlers.elicitation) {
327
+ throw new Error("Elicitation handler must be set on create");
328
+ }
329
+ this.handlers = {
330
+ ...this.handlers,
331
+ elicitation: handler,
332
+ };
333
+ if (!handler) {
334
+ const method = types_js_1.ElicitRequestSchema.shape.method.value;
335
+ this.client.removeRequestHandler(method);
336
+ return;
337
+ }
338
+ this.client.setRequestHandler(types_js_1.ElicitRequestSchema, handler);
339
+ }
340
+ updateSamplingHandler(handler) {
341
+ // Because we advertise the sampling capability on initial connection, we can only update
342
+ // an existing handler, not add it if we haven't set it yet.
343
+ if (handler && !this.handlers.sampling) {
344
+ throw new Error("Sampling handler must be set on create");
345
+ }
346
+ this.handlers = {
347
+ ...this.handlers,
348
+ sampling: handler,
349
+ };
350
+ if (!handler) {
351
+ const method = types_js_1.CreateMessageRequestSchema.shape.method.value;
352
+ this.client.removeRequestHandler(method);
353
+ return;
354
+ }
355
+ this.client.setRequestHandler(types_js_1.CreateMessageRequestSchema, handler);
356
+ }
272
357
  }
273
358
  exports.MCPClient = MCPClient;
274
359
  //# sourceMappingURL=mcp-client.js.map