@tambo-ai/react 0.58.0 → 0.59.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/dist/hooks/use-tambo-stream-status.js +1 -1
- package/dist/hooks/use-tambo-stream-status.js.map +1 -1
- package/dist/mcp/__tests__/mcp-client.test.js +0 -266
- package/dist/mcp/__tests__/mcp-client.test.js.map +1 -1
- package/dist/mcp/__tests__/tambo-mcp-provider.test.js +218 -12
- package/dist/mcp/__tests__/tambo-mcp-provider.test.js.map +1 -1
- package/dist/mcp/__tests__/use-mcp-servers.test.js +34 -9
- package/dist/mcp/__tests__/use-mcp-servers.test.js.map +1 -1
- package/dist/mcp/index.d.ts +3 -1
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +4 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/mcp-client.d.ts +12 -79
- package/dist/mcp/mcp-client.d.ts.map +1 -1
- package/dist/mcp/mcp-client.js +22 -159
- package/dist/mcp/mcp-client.js.map +1 -1
- package/dist/mcp/mcp-hooks.d.ts +93 -0
- package/dist/mcp/mcp-hooks.d.ts.map +1 -0
- package/dist/mcp/mcp-hooks.js +56 -0
- package/dist/mcp/mcp-hooks.js.map +1 -0
- package/dist/mcp/tambo-mcp-provider.d.ts +30 -2
- package/dist/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/dist/mcp/tambo-mcp-provider.js +127 -17
- package/dist/mcp/tambo-mcp-provider.js.map +1 -1
- package/dist/model/component-metadata.d.ts +1 -1
- package/dist/model/component-metadata.d.ts.map +1 -1
- package/dist/model/component-metadata.js.map +1 -1
- package/dist/providers/__tests__/tambo-thread-provider-initial-messages.test.js +3 -1
- package/dist/providers/__tests__/tambo-thread-provider-initial-messages.test.js.map +1 -1
- package/dist/providers/__tests__/tambo-thread-provider.test.js +395 -8
- package/dist/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/dist/providers/tambo-mcp-token-provider.d.ts +34 -0
- package/dist/providers/tambo-mcp-token-provider.d.ts.map +1 -0
- package/dist/providers/tambo-mcp-token-provider.js +69 -0
- package/dist/providers/tambo-mcp-token-provider.js.map +1 -0
- package/dist/providers/tambo-provider.d.ts.map +1 -1
- package/dist/providers/tambo-provider.js +7 -5
- package/dist/providers/tambo-provider.js.map +1 -1
- package/dist/providers/tambo-thread-provider.d.ts.map +1 -1
- package/dist/providers/tambo-thread-provider.js +20 -8
- package/dist/providers/tambo-thread-provider.js.map +1 -1
- package/dist/testing/tools.d.ts +1 -1
- package/esm/hooks/use-tambo-stream-status.js +1 -1
- package/esm/hooks/use-tambo-stream-status.js.map +1 -1
- package/esm/mcp/__tests__/mcp-client.test.js +0 -266
- package/esm/mcp/__tests__/mcp-client.test.js.map +1 -1
- package/esm/mcp/__tests__/tambo-mcp-provider.test.js +218 -12
- package/esm/mcp/__tests__/tambo-mcp-provider.test.js.map +1 -1
- package/esm/mcp/__tests__/use-mcp-servers.test.js +34 -9
- package/esm/mcp/__tests__/use-mcp-servers.test.js.map +1 -1
- package/esm/mcp/index.d.ts +3 -1
- package/esm/mcp/index.d.ts.map +1 -1
- package/esm/mcp/index.js +1 -0
- package/esm/mcp/index.js.map +1 -1
- package/esm/mcp/mcp-client.d.ts +12 -79
- package/esm/mcp/mcp-client.d.ts.map +1 -1
- package/esm/mcp/mcp-client.js +22 -159
- package/esm/mcp/mcp-client.js.map +1 -1
- package/esm/mcp/mcp-hooks.d.ts +93 -0
- package/esm/mcp/mcp-hooks.d.ts.map +1 -0
- package/esm/mcp/mcp-hooks.js +52 -0
- package/esm/mcp/mcp-hooks.js.map +1 -0
- package/esm/mcp/tambo-mcp-provider.d.ts +30 -2
- package/esm/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/esm/mcp/tambo-mcp-provider.js +128 -18
- package/esm/mcp/tambo-mcp-provider.js.map +1 -1
- package/esm/model/component-metadata.d.ts +1 -1
- package/esm/model/component-metadata.d.ts.map +1 -1
- package/esm/model/component-metadata.js.map +1 -1
- package/esm/providers/__tests__/tambo-thread-provider-initial-messages.test.js +3 -1
- package/esm/providers/__tests__/tambo-thread-provider-initial-messages.test.js.map +1 -1
- package/esm/providers/__tests__/tambo-thread-provider.test.js +395 -8
- package/esm/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/esm/providers/tambo-mcp-token-provider.d.ts +34 -0
- package/esm/providers/tambo-mcp-token-provider.d.ts.map +1 -0
- package/esm/providers/tambo-mcp-token-provider.js +31 -0
- package/esm/providers/tambo-mcp-token-provider.js.map +1 -0
- package/esm/providers/tambo-provider.d.ts.map +1 -1
- package/esm/providers/tambo-provider.js +7 -5
- package/esm/providers/tambo-provider.js.map +1 -1
- package/esm/providers/tambo-thread-provider.d.ts.map +1 -1
- package/esm/providers/tambo-thread-provider.js +20 -8
- package/esm/providers/tambo-thread-provider.js.map +1 -1
- package/esm/testing/tools.d.ts +1 -1
- package/package.json +1 -1
package/dist/mcp/mcp-client.d.ts
CHANGED
|
@@ -24,9 +24,9 @@ export declare enum MCPTransport {
|
|
|
24
24
|
* });
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
|
-
interface MCPHandlers {
|
|
28
|
-
elicitation
|
|
29
|
-
sampling
|
|
27
|
+
export interface MCPHandlers {
|
|
28
|
+
elicitation: (e: ElicitRequest) => Promise<ElicitResult>;
|
|
29
|
+
sampling: (e: CreateMessageRequest) => Promise<CreateMessageResult>;
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
32
|
* A client for interacting with MCP (Model Context Protocol) servers.
|
|
@@ -39,7 +39,12 @@ interface MCPHandlers {
|
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
41
|
export declare class MCPClient {
|
|
42
|
-
|
|
42
|
+
/**
|
|
43
|
+
* The underlying MCP client
|
|
44
|
+
*
|
|
45
|
+
* Be careful not to mutate the client directly, use the methods provided instead.
|
|
46
|
+
*/
|
|
47
|
+
client: Client;
|
|
43
48
|
private transport;
|
|
44
49
|
private transportType;
|
|
45
50
|
sessionId?: string;
|
|
@@ -47,42 +52,6 @@ export declare class MCPClient {
|
|
|
47
52
|
private headers;
|
|
48
53
|
private authProvider?;
|
|
49
54
|
private handlers;
|
|
50
|
-
/**
|
|
51
|
-
* Tracks an in-flight reconnect so concurrent triggers coalesce
|
|
52
|
-
* (single-flight). When set, additional calls to `reconnect()` or
|
|
53
|
-
* the automatic `onclose` handler will await the same Promise instead of
|
|
54
|
-
* starting another reconnect sequence.
|
|
55
|
-
*/
|
|
56
|
-
private reconnecting?;
|
|
57
|
-
/**
|
|
58
|
-
* Timer id for a scheduled automatic reconnect (used by `onclose`).
|
|
59
|
-
* Present only while waiting for the backoff delay to elapse.
|
|
60
|
-
*/
|
|
61
|
-
private reconnectTimer?;
|
|
62
|
-
/**
|
|
63
|
-
* Count of consecutive automatic reconnect failures used to compute
|
|
64
|
-
* exponential backoff. Reset to 0 after a successful connection.
|
|
65
|
-
*/
|
|
66
|
-
private backoffAttempts;
|
|
67
|
-
/**
|
|
68
|
-
* Backoff policy (discoverable constants)
|
|
69
|
-
* - BACKOFF_INITIAL_MS: initial delay for the first automatic retry
|
|
70
|
-
* - BACKOFF_MULTIPLIER: exponential growth factor for each failed attempt
|
|
71
|
-
* - BACKOFF_MAX_MS: upper bound for the delay
|
|
72
|
-
* - BACKOFF_JITTER_RATIO: jitter range as a fraction of the base delay
|
|
73
|
-
*
|
|
74
|
-
* Jitter is applied symmetrically in [-ratio, +ratio]. For example, with a
|
|
75
|
-
* 500ms base delay and 0.2 ratio, the actual delay is in [400ms, 600ms].
|
|
76
|
-
*
|
|
77
|
-
* The backoff applies only to automatic reconnects started from the
|
|
78
|
-
* `onclose` handler. Explicit/manual calls to `reconnect()` run immediately
|
|
79
|
-
* (no backoff), and will preempt any scheduled automatic attempt.
|
|
80
|
-
*/
|
|
81
|
-
static readonly BACKOFF_INITIAL_MS = 500;
|
|
82
|
-
static readonly BACKOFF_MULTIPLIER = 2;
|
|
83
|
-
static readonly BACKOFF_MAX_MS = 30000;
|
|
84
|
-
static readonly BACKOFF_JITTER_RATIO = 0.2;
|
|
85
|
-
elicitation: EventTarget;
|
|
86
55
|
/**
|
|
87
56
|
* Private constructor to enforce using the static create method.
|
|
88
57
|
* @param endpoint - The URL of the MCP server to connect to
|
|
@@ -103,43 +72,7 @@ export declare class MCPClient {
|
|
|
103
72
|
* @returns A connected MCPClient instance ready for use
|
|
104
73
|
* @throws {Error} Will throw an error if connection fails
|
|
105
74
|
*/
|
|
106
|
-
static create(endpoint: string, transportType: MCPTransport | undefined, headers: Record<string, string> | undefined, authProvider: OAuthClientProvider | undefined, sessionId: string | undefined, handlers?: MCPHandlers): Promise<MCPClient>;
|
|
107
|
-
/**
|
|
108
|
-
* Reconnects to the MCP server, optionally retaining the same session ID.
|
|
109
|
-
*
|
|
110
|
-
* Single‑flight semantics:
|
|
111
|
-
* - If a reconnect is already in progress (triggered either manually or by
|
|
112
|
-
* the automatic `onclose` handler), additional calls will await the
|
|
113
|
-
* in-flight reconnect rather than start another one.
|
|
114
|
-
* - If an automatic reconnect has been scheduled but not yet started (i.e.,
|
|
115
|
-
* we are waiting in a backoff delay), calling `reconnect()` manually will
|
|
116
|
-
* cancel the scheduled attempt and perform an immediate reconnect.
|
|
117
|
-
*
|
|
118
|
-
* Backoff policy:
|
|
119
|
-
* - Backoff delays with jitter are applied only for automatic reconnects
|
|
120
|
-
* (via `onclose`). Manual calls to `reconnect()` do not use backoff.
|
|
121
|
-
* @param newSession - Whether to create a new session (true) or reuse existing session ID (false)
|
|
122
|
-
* @param reportErrorOnClose - Whether to report errors when closing the client
|
|
123
|
-
* Note that only StreamableHTTPClientTransport supports session IDs.
|
|
124
|
-
* @returns A promise that resolves when the reconnect is complete
|
|
125
|
-
*/
|
|
126
|
-
reconnect(newSession?: boolean, reportErrorOnClose?: boolean): Promise<void>;
|
|
127
|
-
/**
|
|
128
|
-
* Called by the underlying MCP SDK when the connection closes.
|
|
129
|
-
* Schedules an automatic reconnect with bounded exponential backoff and
|
|
130
|
-
* jitter. If a reconnect is already scheduled or running, this is a no-op.
|
|
131
|
-
*/
|
|
132
|
-
private onclose;
|
|
133
|
-
/**
|
|
134
|
-
* Compute the next backoff delay with symmetric jitter.
|
|
135
|
-
* @returns The next backoff delay in milliseconds
|
|
136
|
-
*/
|
|
137
|
-
private computeBackoffDelayMs;
|
|
138
|
-
/**
|
|
139
|
-
* Schedule an automatic reconnect attempt if one is not already scheduled
|
|
140
|
-
* or running. Uses the backoff policy and self-reschedules on failure.
|
|
141
|
-
*/
|
|
142
|
-
private scheduleAutoReconnect;
|
|
75
|
+
static create(endpoint: string, transportType: MCPTransport | undefined, headers: Record<string, string> | undefined, authProvider: OAuthClientProvider | undefined, sessionId: string | undefined, handlers?: Partial<MCPHandlers>): Promise<MCPClient>;
|
|
143
76
|
private initializeTransport;
|
|
144
77
|
/**
|
|
145
78
|
* Initializes the MCP client with the appropriate capabilities and handlers
|
|
@@ -199,9 +132,10 @@ export declare class MCPClient {
|
|
|
199
132
|
* @returns The result from the tool execution
|
|
200
133
|
* @throws {Error} Will throw an error if the tool call fails or if arguments are invalid
|
|
201
134
|
*/
|
|
202
|
-
callTool(name: string, args: Record<string, unknown>): Promise<MCPToolCallResult>;
|
|
135
|
+
callTool(name: string, args: Record<string, unknown>, _meta?: Record<string, unknown>): Promise<MCPToolCallResult>;
|
|
203
136
|
updateElicitationHandler(handler: ((e: ElicitRequest) => Promise<ElicitResult>) | undefined): void;
|
|
204
137
|
updateSamplingHandler(handler: ((e: CreateMessageRequest) => Promise<CreateMessageResult>) | undefined): void;
|
|
138
|
+
close(): Promise<void>;
|
|
205
139
|
}
|
|
206
140
|
/**
|
|
207
141
|
* The result of a tool call.
|
|
@@ -213,5 +147,4 @@ export interface MCPToolSpec {
|
|
|
213
147
|
description?: string;
|
|
214
148
|
inputSchema?: JSONSchema7;
|
|
215
149
|
}
|
|
216
|
-
export {};
|
|
217
150
|
//# 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":"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,
|
|
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,MAAM,WAAW,WAAW;IAC1B,WAAW,EAAE,CAAC,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;IACzD,QAAQ,EAAE,CAAC,CAAC,EAAE,oBAAoB,KAAK,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACrE;AAED;;;;;;;;;GASG;AACH,qBAAa,SAAS;IACpB;;;;OAIG;IACH,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,SAAS,CAAqD;IACtE,OAAO,CAAC,aAAa,CAAe;IAC7B,SAAS,CAAC,EAAE,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AC,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"}
|
package/dist/mcp/mcp-client.js
CHANGED
|
@@ -21,6 +21,11 @@ var MCPTransport;
|
|
|
21
21
|
* ```
|
|
22
22
|
*/
|
|
23
23
|
class MCPClient {
|
|
24
|
+
/**
|
|
25
|
+
* The underlying MCP client
|
|
26
|
+
*
|
|
27
|
+
* Be careful not to mutate the client directly, use the methods provided instead.
|
|
28
|
+
*/
|
|
24
29
|
client;
|
|
25
30
|
transport;
|
|
26
31
|
transportType;
|
|
@@ -29,42 +34,6 @@ class MCPClient {
|
|
|
29
34
|
headers;
|
|
30
35
|
authProvider;
|
|
31
36
|
handlers;
|
|
32
|
-
/**
|
|
33
|
-
* Tracks an in-flight reconnect so concurrent triggers coalesce
|
|
34
|
-
* (single-flight). When set, additional calls to `reconnect()` or
|
|
35
|
-
* the automatic `onclose` handler will await the same Promise instead of
|
|
36
|
-
* starting another reconnect sequence.
|
|
37
|
-
*/
|
|
38
|
-
reconnecting;
|
|
39
|
-
/**
|
|
40
|
-
* Timer id for a scheduled automatic reconnect (used by `onclose`).
|
|
41
|
-
* Present only while waiting for the backoff delay to elapse.
|
|
42
|
-
*/
|
|
43
|
-
reconnectTimer;
|
|
44
|
-
/**
|
|
45
|
-
* Count of consecutive automatic reconnect failures used to compute
|
|
46
|
-
* exponential backoff. Reset to 0 after a successful connection.
|
|
47
|
-
*/
|
|
48
|
-
backoffAttempts = 0;
|
|
49
|
-
/**
|
|
50
|
-
* Backoff policy (discoverable constants)
|
|
51
|
-
* - BACKOFF_INITIAL_MS: initial delay for the first automatic retry
|
|
52
|
-
* - BACKOFF_MULTIPLIER: exponential growth factor for each failed attempt
|
|
53
|
-
* - BACKOFF_MAX_MS: upper bound for the delay
|
|
54
|
-
* - BACKOFF_JITTER_RATIO: jitter range as a fraction of the base delay
|
|
55
|
-
*
|
|
56
|
-
* Jitter is applied symmetrically in [-ratio, +ratio]. For example, with a
|
|
57
|
-
* 500ms base delay and 0.2 ratio, the actual delay is in [400ms, 600ms].
|
|
58
|
-
*
|
|
59
|
-
* The backoff applies only to automatic reconnects started from the
|
|
60
|
-
* `onclose` handler. Explicit/manual calls to `reconnect()` run immediately
|
|
61
|
-
* (no backoff), and will preempt any scheduled automatic attempt.
|
|
62
|
-
*/
|
|
63
|
-
static BACKOFF_INITIAL_MS = 500;
|
|
64
|
-
static BACKOFF_MULTIPLIER = 2;
|
|
65
|
-
static BACKOFF_MAX_MS = 30_000;
|
|
66
|
-
static BACKOFF_JITTER_RATIO = 0.2;
|
|
67
|
-
elicitation = new EventTarget();
|
|
68
37
|
/**
|
|
69
38
|
* Private constructor to enforce using the static create method.
|
|
70
39
|
* @param endpoint - The URL of the MCP server to connect to
|
|
@@ -101,127 +70,6 @@ class MCPClient {
|
|
|
101
70
|
}
|
|
102
71
|
return mcpClient;
|
|
103
72
|
}
|
|
104
|
-
/**
|
|
105
|
-
* Reconnects to the MCP server, optionally retaining the same session ID.
|
|
106
|
-
*
|
|
107
|
-
* Single‑flight semantics:
|
|
108
|
-
* - If a reconnect is already in progress (triggered either manually or by
|
|
109
|
-
* the automatic `onclose` handler), additional calls will await the
|
|
110
|
-
* in-flight reconnect rather than start another one.
|
|
111
|
-
* - If an automatic reconnect has been scheduled but not yet started (i.e.,
|
|
112
|
-
* we are waiting in a backoff delay), calling `reconnect()` manually will
|
|
113
|
-
* cancel the scheduled attempt and perform an immediate reconnect.
|
|
114
|
-
*
|
|
115
|
-
* Backoff policy:
|
|
116
|
-
* - Backoff delays with jitter are applied only for automatic reconnects
|
|
117
|
-
* (via `onclose`). Manual calls to `reconnect()` do not use backoff.
|
|
118
|
-
* @param newSession - Whether to create a new session (true) or reuse existing session ID (false)
|
|
119
|
-
* @param reportErrorOnClose - Whether to report errors when closing the client
|
|
120
|
-
* Note that only StreamableHTTPClientTransport supports session IDs.
|
|
121
|
-
* @returns A promise that resolves when the reconnect is complete
|
|
122
|
-
*/
|
|
123
|
-
async reconnect(newSession = false, reportErrorOnClose = true) {
|
|
124
|
-
// If a reconnect is already running, coalesce into it.
|
|
125
|
-
if (this.reconnecting) {
|
|
126
|
-
return await this.reconnecting;
|
|
127
|
-
}
|
|
128
|
-
// Manual reconnect preempts any scheduled automatic attempt.
|
|
129
|
-
if (this.reconnectTimer) {
|
|
130
|
-
clearTimeout(this.reconnectTimer);
|
|
131
|
-
this.reconnectTimer = undefined;
|
|
132
|
-
}
|
|
133
|
-
const doReconnect = async () => {
|
|
134
|
-
const sessionId = newSession ? undefined : this.sessionId;
|
|
135
|
-
// Prevent re-entrant onclose during deliberate close by detaching
|
|
136
|
-
// the handler from the previous client instance.
|
|
137
|
-
const prevClient = this.client;
|
|
138
|
-
// Prevent re-entrant onclose callbacks from the previous client
|
|
139
|
-
prevClient.onclose = undefined;
|
|
140
|
-
try {
|
|
141
|
-
await prevClient.close();
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
if (reportErrorOnClose) {
|
|
145
|
-
console.error("Error closing Tambo MCP Client:", error);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
this.transport = this.initializeTransport(sessionId);
|
|
149
|
-
this.client = this.initializeClient();
|
|
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
|
-
}
|
|
159
|
-
};
|
|
160
|
-
this.reconnecting = (async () => {
|
|
161
|
-
try {
|
|
162
|
-
await doReconnect();
|
|
163
|
-
// Successful manual reconnect: reset backoff.
|
|
164
|
-
this.backoffAttempts = 0;
|
|
165
|
-
}
|
|
166
|
-
finally {
|
|
167
|
-
this.reconnecting = undefined;
|
|
168
|
-
}
|
|
169
|
-
})();
|
|
170
|
-
return await this.reconnecting;
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Called by the underlying MCP SDK when the connection closes.
|
|
174
|
-
* Schedules an automatic reconnect with bounded exponential backoff and
|
|
175
|
-
* jitter. If a reconnect is already scheduled or running, this is a no-op.
|
|
176
|
-
*/
|
|
177
|
-
onclose() {
|
|
178
|
-
this.scheduleAutoReconnect();
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Compute the next backoff delay with symmetric jitter.
|
|
182
|
-
* @returns The next backoff delay in milliseconds
|
|
183
|
-
*/
|
|
184
|
-
computeBackoffDelayMs() {
|
|
185
|
-
const base = Math.min(MCPClient.BACKOFF_MAX_MS, MCPClient.BACKOFF_INITIAL_MS *
|
|
186
|
-
Math.pow(MCPClient.BACKOFF_MULTIPLIER, this.backoffAttempts));
|
|
187
|
-
const jitterRange = MCPClient.BACKOFF_JITTER_RATIO * base;
|
|
188
|
-
const jitter = (Math.random() * 2 - 1) * jitterRange; // [-range, +range]
|
|
189
|
-
const ms = Math.max(0, Math.round(base + jitter));
|
|
190
|
-
return ms;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Schedule an automatic reconnect attempt if one is not already scheduled
|
|
194
|
-
* or running. Uses the backoff policy and self-reschedules on failure.
|
|
195
|
-
*/
|
|
196
|
-
scheduleAutoReconnect() {
|
|
197
|
-
if (this.reconnecting || this.reconnectTimer) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
const delayMs = this.computeBackoffDelayMs();
|
|
201
|
-
console.warn("Tambo MCP Client closed; attempting automatic reconnect in", `${delayMs}ms`);
|
|
202
|
-
this.reconnectTimer = setTimeout(async () => {
|
|
203
|
-
this.reconnectTimer = undefined;
|
|
204
|
-
// Start the actual reconnect (single-flight)
|
|
205
|
-
const inFlight = (this.reconnecting = this.reconnect(false, false));
|
|
206
|
-
try {
|
|
207
|
-
await inFlight;
|
|
208
|
-
// Success: reset attempts
|
|
209
|
-
this.backoffAttempts = 0;
|
|
210
|
-
}
|
|
211
|
-
catch (err) {
|
|
212
|
-
// Failure: increase attempts; scheduling occurs in finally below so the
|
|
213
|
-
// new timer isn't blocked by `this.reconnecting` being truthy.
|
|
214
|
-
this.backoffAttempts += 1;
|
|
215
|
-
console.warn("Automatic reconnect failed; will retry with backoff.", err);
|
|
216
|
-
}
|
|
217
|
-
finally {
|
|
218
|
-
this.reconnecting = undefined;
|
|
219
|
-
if (this.backoffAttempts > 0) {
|
|
220
|
-
this.scheduleAutoReconnect();
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}, delayMs);
|
|
224
|
-
}
|
|
225
73
|
initializeTransport(sessionId) {
|
|
226
74
|
if (this.transportType === MCPTransport.SSE) {
|
|
227
75
|
return new sse_js_1.SSEClientTransport(new URL(this.endpoint), {
|
|
@@ -255,7 +103,6 @@ class MCPClient {
|
|
|
255
103
|
...samplingCapability,
|
|
256
104
|
},
|
|
257
105
|
});
|
|
258
|
-
client.onclose = this.onclose.bind(this);
|
|
259
106
|
if (this.handlers.elicitation) {
|
|
260
107
|
client.setRequestHandler(types_js_1.ElicitRequestSchema, this.handlers.elicitation);
|
|
261
108
|
}
|
|
@@ -313,14 +160,19 @@ class MCPClient {
|
|
|
313
160
|
* @returns The result from the tool execution
|
|
314
161
|
* @throws {Error} Will throw an error if the tool call fails or if arguments are invalid
|
|
315
162
|
*/
|
|
316
|
-
async callTool(name, args) {
|
|
163
|
+
async callTool(name, args, _meta) {
|
|
317
164
|
const result = await this.client.callTool({
|
|
318
165
|
name,
|
|
319
166
|
arguments: args,
|
|
167
|
+
_meta,
|
|
320
168
|
});
|
|
321
169
|
return result;
|
|
322
170
|
}
|
|
323
171
|
updateElicitationHandler(handler) {
|
|
172
|
+
// Skip if handler hasn't changed
|
|
173
|
+
if (handler === this.handlers.elicitation) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
324
176
|
// Because we advertise the elicitation capability on initial connection, we can only update
|
|
325
177
|
// an existing handler, not add it if we haven't set it yet.
|
|
326
178
|
if (handler && !this.handlers.elicitation) {
|
|
@@ -338,6 +190,10 @@ class MCPClient {
|
|
|
338
190
|
this.client.setRequestHandler(types_js_1.ElicitRequestSchema, handler);
|
|
339
191
|
}
|
|
340
192
|
updateSamplingHandler(handler) {
|
|
193
|
+
// Skip if handler hasn't changed
|
|
194
|
+
if (handler === this.handlers.sampling) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
341
197
|
// Because we advertise the sampling capability on initial connection, we can only update
|
|
342
198
|
// an existing handler, not add it if we haven't set it yet.
|
|
343
199
|
if (handler && !this.handlers.sampling) {
|
|
@@ -354,6 +210,13 @@ class MCPClient {
|
|
|
354
210
|
}
|
|
355
211
|
this.client.setRequestHandler(types_js_1.CreateMessageRequestSchema, handler);
|
|
356
212
|
}
|
|
213
|
+
async close() {
|
|
214
|
+
// Not really sure which one of these to close first, but we'll close the
|
|
215
|
+
// transport first so that no requests can come in and hit closing/closed
|
|
216
|
+
// clients
|
|
217
|
+
await this.transport.close();
|
|
218
|
+
await this.client.close();
|
|
219
|
+
}
|
|
357
220
|
}
|
|
358
221
|
exports.MCPClient = MCPClient;
|
|
359
222
|
//# sourceMappingURL=mcp-client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-client.js","sourceRoot":"","sources":["../../src/mcp/mcp-client.ts"],"names":[],"mappings":";;;AACA,wEAAmE;AACnE,oEAA6E;AAC7E,0FAAmG;AACnG,iEAO4C;AAG5C,IAAY,YAGX;AAHD,WAAY,YAAY;IACtB,2BAAW,CAAA;IACX,6BAAa,CAAA;AACf,CAAC,EAHW,YAAY,4BAAZ,YAAY,QAGvB;AAyBD;;;;;;;;;GASG;AACH,MAAa,SAAS;I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d,GAAG,IAAI,CAAC,QAAQ;YAChB,QAAQ,EAAE,OAAO;SAClB,CAAC;QACF,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,MAAM,GAAG,qCAA0B,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;YAC7D,IAAI,CAAC,MAAM,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;YACzC,OAAO;QACT,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,qCAA0B,EAAE,OAAO,CAAC,CAAC;IACrE,CAAC;;AApZH,8BAqZC","sourcesContent":["import { type OAuthClientProvider } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { SSEClientTransport } from \"@modelcontextprotocol/sdk/client/sse.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport {\n CreateMessageRequest,\n CreateMessageRequestSchema,\n CreateMessageResult,\n ElicitRequest,\n ElicitRequestSchema,\n ElicitResult,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { JSONSchema7 } from \"json-schema\";\n\nexport enum MCPTransport {\n SSE = \"sse\",\n HTTP = \"http\",\n}\n\n/**\n * Handlers for MCP requests - these are only used if the server supports the corresponding capabilities\n * @param elicitation - Handler for elicitation requests\n * @param sampling - Handler for sampling requests\n * @example\n * ```typescript\n * const mcp = await MCPClient.create(\n * 'https://api.example.com/mcp',\n * MCPTransport.HTTP,\n * {},\n * undefined,\n * undefined,\n * {\n * elicitation: (e: ElicitRequest) => Promise.resolve({...}),\n * },\n * });\n * ```\n */\ninterface MCPHandlers {\n elicitation?: (e: ElicitRequest) => Promise<ElicitResult>;\n sampling?: (e: CreateMessageRequest) => Promise<CreateMessageResult>;\n}\n\n/**\n * A client for interacting with MCP (Model Context Protocol) servers.\n * Provides a simple interface for listing and calling tools exposed by the server.\n * @example\n * ```typescript\n * const mcp = await MCPClient.create('https://api.example.com/mcp');\n * const tools = await mcp.listTools();\n * const result = await mcp.callTool('toolName', { arg1: 'value1' });\n * ```\n */\nexport class MCPClient {\n private client: Client;\n private transport: SSEClientTransport | StreamableHTTPClientTransport;\n private transportType: MCPTransport;\n public sessionId?: string;\n private endpoint: string;\n private headers: Record<string, string>;\n private authProvider?: OAuthClientProvider;\n private handlers: MCPHandlers;\n /**\n * Tracks an in-flight reconnect so concurrent triggers coalesce\n * (single-flight). When set, additional calls to `reconnect()` or\n * the automatic `onclose` handler will await the same Promise instead of\n * starting another reconnect sequence.\n */\n private reconnecting?: Promise<void>;\n /**\n * Timer id for a scheduled automatic reconnect (used by `onclose`).\n * Present only while waiting for the backoff delay to elapse.\n */\n private reconnectTimer?: ReturnType<typeof setTimeout>;\n /**\n * Count of consecutive automatic reconnect failures used to compute\n * exponential backoff. Reset to 0 after a successful connection.\n */\n private backoffAttempts = 0;\n\n /**\n * Backoff policy (discoverable constants)\n * - BACKOFF_INITIAL_MS: initial delay for the first automatic retry\n * - BACKOFF_MULTIPLIER: exponential growth factor for each failed attempt\n * - BACKOFF_MAX_MS: upper bound for the delay\n * - BACKOFF_JITTER_RATIO: jitter range as a fraction of the base delay\n *\n * Jitter is applied symmetrically in [-ratio, +ratio]. For example, with a\n * 500ms base delay and 0.2 ratio, the actual delay is in [400ms, 600ms].\n *\n * The backoff applies only to automatic reconnects started from the\n * `onclose` handler. Explicit/manual calls to `reconnect()` run immediately\n * (no backoff), and will preempt any scheduled automatic attempt.\n */\n static readonly BACKOFF_INITIAL_MS = 500;\n static readonly BACKOFF_MULTIPLIER = 2;\n static readonly BACKOFF_MAX_MS = 30_000;\n static readonly BACKOFF_JITTER_RATIO = 0.2;\n\n public elicitation: EventTarget = new EventTarget();\n /**\n * Private constructor to enforce using the static create method.\n * @param endpoint - The URL of the MCP server to connect to\n * @param transportType - The transport to use for the MCP client\n * @param headers - Optional custom headers to include in requests\n */\n private constructor(\n endpoint: string,\n transportType: MCPTransport,\n headers?: Record<string, string>,\n authProvider?: OAuthClientProvider,\n sessionId?: string,\n handlers: MCPHandlers = {},\n ) {\n this.endpoint = endpoint;\n this.headers = headers ?? {};\n this.authProvider = authProvider;\n this.transportType = transportType;\n this.handlers = handlers;\n this.transport = this.initializeTransport(sessionId);\n this.client = this.initializeClient();\n }\n\n /**\n * Creates and initializes a new MCPClient instance. This is the recommended\n * way to create an MCPClient as it handles both instantiation and connection\n * setup.\n * @param endpoint - The URL of the MCP server to connect to\n * @param transportType - The transport type to use for the MCP client. Defaults to HTTP.\n * @param headers - Optional custom headers to include in requests\n * @param authProvider - Optional auth provider to use for authentication\n * @param sessionId - Optional session id to use for the MCP client - if not\n * provided, a new session will be created\n * @returns A connected MCPClient instance ready for use\n * @throws {Error} Will throw an error if connection fails\n */\n static async create(\n endpoint: string,\n transportType: MCPTransport = MCPTransport.HTTP,\n headers: Record<string, string> | undefined,\n authProvider: OAuthClientProvider | undefined,\n sessionId: string | undefined,\n handlers: MCPHandlers = {},\n ): Promise<MCPClient> {\n const mcpClient = new MCPClient(\n endpoint,\n transportType,\n headers,\n authProvider,\n sessionId,\n handlers,\n );\n await mcpClient.client.connect(mcpClient.transport);\n if (\"sessionId\" in mcpClient.transport) {\n mcpClient.sessionId = mcpClient.transport.sessionId;\n }\n return mcpClient;\n }\n /**\n * Reconnects to the MCP server, optionally retaining the same session ID.\n *\n * Single‑flight semantics:\n * - If a reconnect is already in progress (triggered either manually or by\n * the automatic `onclose` handler), additional calls will await the\n * in-flight reconnect rather than start another one.\n * - If an automatic reconnect has been scheduled but not yet started (i.e.,\n * we are waiting in a backoff delay), calling `reconnect()` manually will\n * cancel the scheduled attempt and perform an immediate reconnect.\n *\n * Backoff policy:\n * - Backoff delays with jitter are applied only for automatic reconnects\n * (via `onclose`). Manual calls to `reconnect()` do not use backoff.\n * @param newSession - Whether to create a new session (true) or reuse existing session ID (false)\n * @param reportErrorOnClose - Whether to report errors when closing the client\n * Note that only StreamableHTTPClientTransport supports session IDs.\n * @returns A promise that resolves when the reconnect is complete\n */\n async reconnect(newSession = false, reportErrorOnClose = true) {\n // If a reconnect is already running, coalesce into it.\n if (this.reconnecting) {\n return await this.reconnecting;\n }\n\n // Manual reconnect preempts any scheduled automatic attempt.\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = undefined;\n }\n\n const doReconnect = async () => {\n const sessionId = newSession ? undefined : this.sessionId;\n\n // Prevent re-entrant onclose during deliberate close by detaching\n // the handler from the previous client instance.\n const prevClient = this.client;\n // Prevent re-entrant onclose callbacks from the previous client\n prevClient.onclose = undefined;\n\n try {\n await prevClient.close();\n } catch (error) {\n if (reportErrorOnClose) {\n console.error(\"Error closing Tambo MCP Client:\", error);\n }\n }\n\n this.transport = this.initializeTransport(sessionId);\n this.client = this.initializeClient();\n await this.client.connect(this.transport);\n // We may have gotten a session id from the server, so we need to set it\n if (\"sessionId\" in this.transport) {\n this.sessionId = this.transport.sessionId;\n if (sessionId !== this.sessionId) {\n // This is a pretty unusual thing to happen, but it might be possible?\n console.warn(\"Session id mismatch\", sessionId, this.sessionId);\n }\n }\n };\n\n this.reconnecting = (async () => {\n try {\n await doReconnect();\n // Successful manual reconnect: reset backoff.\n this.backoffAttempts = 0;\n } finally {\n this.reconnecting = undefined;\n }\n })();\n\n return await this.reconnecting;\n }\n\n /**\n * Called by the underlying MCP SDK when the connection closes.\n * Schedules an automatic reconnect with bounded exponential backoff and\n * jitter. If a reconnect is already scheduled or running, this is a no-op.\n */\n private onclose() {\n this.scheduleAutoReconnect();\n }\n\n /**\n * Compute the next backoff delay with symmetric jitter.\n * @returns The next backoff delay in milliseconds\n */\n private computeBackoffDelayMs(): number {\n const base = Math.min(\n MCPClient.BACKOFF_MAX_MS,\n MCPClient.BACKOFF_INITIAL_MS *\n Math.pow(MCPClient.BACKOFF_MULTIPLIER, this.backoffAttempts),\n );\n const jitterRange = MCPClient.BACKOFF_JITTER_RATIO * base;\n const jitter = (Math.random() * 2 - 1) * jitterRange; // [-range, +range]\n const ms = Math.max(0, Math.round(base + jitter));\n return ms;\n }\n\n /**\n * Schedule an automatic reconnect attempt if one is not already scheduled\n * or running. Uses the backoff policy and self-reschedules on failure.\n */\n private scheduleAutoReconnect() {\n if (this.reconnecting || this.reconnectTimer) {\n return;\n }\n\n const delayMs = this.computeBackoffDelayMs();\n console.warn(\n \"Tambo MCP Client closed; attempting automatic reconnect in\",\n `${delayMs}ms`,\n );\n\n this.reconnectTimer = setTimeout(async () => {\n this.reconnectTimer = undefined;\n // Start the actual reconnect (single-flight)\n const inFlight = (this.reconnecting = this.reconnect(false, false));\n try {\n await inFlight;\n // Success: reset attempts\n this.backoffAttempts = 0;\n } catch (err) {\n // Failure: increase attempts; scheduling occurs in finally below so the\n // new timer isn't blocked by `this.reconnecting` being truthy.\n this.backoffAttempts += 1;\n console.warn(\n \"Automatic reconnect failed; will retry with backoff.\",\n err,\n );\n } finally {\n this.reconnecting = undefined;\n if (this.backoffAttempts > 0) {\n this.scheduleAutoReconnect();\n }\n }\n }, delayMs);\n }\n\n private initializeTransport(sessionId: string | undefined) {\n if (this.transportType === MCPTransport.SSE) {\n return new SSEClientTransport(new URL(this.endpoint), {\n authProvider: this.authProvider,\n requestInit: { headers: this.headers },\n });\n } else {\n return new StreamableHTTPClientTransport(new URL(this.endpoint), {\n authProvider: this.authProvider,\n requestInit: { headers: this.headers },\n sessionId,\n });\n }\n }\n\n /**\n * Initializes the MCP client with the appropriate capabilities and handlers\n * @returns The initialized MCP client\n */\n private initializeClient() {\n const elicitationCapability = this.handlers.elicitation\n ? { elicitation: {} }\n : {};\n const samplingCapability = this.handlers.sampling ? { sampling: {} } : {};\n const client = new Client(\n {\n name: \"tambo-mcp-client\",\n version: \"1.0.0\",\n },\n {\n capabilities: {\n ...elicitationCapability,\n ...samplingCapability,\n },\n },\n );\n client.onclose = this.onclose.bind(this);\n if (this.handlers.elicitation) {\n client.setRequestHandler(ElicitRequestSchema, this.handlers.elicitation);\n }\n if (this.handlers.sampling) {\n client.setRequestHandler(\n CreateMessageRequestSchema,\n this.handlers.sampling,\n );\n }\n return client;\n }\n\n /**\n * Retrieves a complete list of all available tools from the MCP server.\n * Handles pagination automatically by following cursors until all tools are fetched.\n * @returns A complete list of all available tools and their descriptions\n * @throws {Error} Will throw an error if any server request fails during pagination\n */\n async listTools(): Promise<MCPToolSpec[]> {\n const allTools: MCPToolSpec[] = [];\n let hasMore = true;\n let cursor: string | undefined = undefined;\n\n while (hasMore) {\n const response = await this.client.listTools({ cursor }, {});\n allTools.push(\n ...response.tools.map((tool): MCPToolSpec => {\n // make sure the right type is used\n const inputSchemaType: string = tool.inputSchema.type;\n if (inputSchemaType !== \"object\") {\n throw new Error(\n `Input schema for tool ${tool.name} is not an object`,\n );\n }\n\n return {\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema as JSONSchema7,\n };\n }),\n );\n\n if (response.nextCursor) {\n cursor = response.nextCursor;\n } else {\n hasMore = false;\n }\n }\n\n return allTools;\n }\n\n getServerCapabilities() {\n return this.client.getServerCapabilities();\n }\n\n getServerVersion() {\n return this.client.getServerVersion();\n }\n\n getInstructions() {\n return this.client.getInstructions();\n }\n\n /**\n * Calls a specific tool on the MCP server with the provided arguments.\n * @param name - The name of the tool to call\n * @param args - Arguments to pass to the tool, must match the tool's expected schema\n * @returns The result from the tool execution\n * @throws {Error} Will throw an error if the tool call fails or if arguments are invalid\n */\n async callTool(\n name: string,\n args: Record<string, unknown>,\n ): Promise<MCPToolCallResult> {\n const result = await this.client.callTool({\n name,\n arguments: args,\n });\n return result;\n }\n\n updateElicitationHandler(\n handler: ((e: ElicitRequest) => Promise<ElicitResult>) | undefined,\n ) {\n // Because we advertise the elicitation capability on initial connection, we can only update\n // an existing handler, not add it if we haven't set it yet.\n if (handler && !this.handlers.elicitation) {\n throw new Error(\"Elicitation handler must be set on create\");\n }\n this.handlers = {\n ...this.handlers,\n elicitation: handler,\n };\n if (!handler) {\n const method = ElicitRequestSchema.shape.method.value;\n this.client.removeRequestHandler(method);\n return;\n }\n this.client.setRequestHandler(ElicitRequestSchema, handler);\n }\n\n updateSamplingHandler(\n handler:\n | ((e: CreateMessageRequest) => Promise<CreateMessageResult>)\n | undefined,\n ) {\n // Because we advertise the sampling capability on initial connection, we can only update\n // an existing handler, not add it if we haven't set it yet.\n if (handler && !this.handlers.sampling) {\n throw new Error(\"Sampling handler must be set on create\");\n }\n this.handlers = {\n ...this.handlers,\n sampling: handler,\n };\n if (!handler) {\n const method = CreateMessageRequestSchema.shape.method.value;\n this.client.removeRequestHandler(method);\n return;\n }\n this.client.setRequestHandler(CreateMessageRequestSchema, handler);\n }\n}\n\n/**\n * The result of a tool call.\n * This is the same as the result of a tool call in the OpenAI SDK, but is reified here\n */\nexport type MCPToolCallResult = Awaited<\n ReturnType<typeof Client.prototype.callTool>\n>;\n\n// Example usage:\n/*\nconst mcp = await MCPClient.create('https://api.example.com/mcp', MCPTransport.HTTP);\nconst tools = await mcp.listTools();\nconst result = await mcp.callTool('toolName', { arg1: 'value1' });\n*/\n\nexport interface MCPToolSpec {\n name: string;\n description?: string;\n inputSchema?: JSONSchema7;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"mcp-client.js","sourceRoot":"","sources":["../../src/mcp/mcp-client.ts"],"names":[],"mappings":";;;AACA,wEAAmE;AACnE,oEAA6E;AAC7E,0FAAmG;AACnG,iEAO4C;AAG5C,IAAY,YAGX;AAHD,WAAY,YAAY;IACtB,2BAAW,CAAA;IACX,6BAAa,CAAA;AACf,CAAC,EAHW,YAAY,4BAAZ,YAAY,QAGvB;AAyBD;;;;;;;;;GASG;AACH,MAAa,SAAS;IACpB;;;;OAIG;IACH,MAAM,CAAS;IACP,SAAS,CAAqD;IAC9D,aAAa,CAAe;IAC7B,SAAS,CAAU;IAClB,QAAQ,CAAS;IACjB,OAAO,CAAyB;IAChC,YAAY,CAAuB;IACnC,QAAQ,CAAuB;IAEvC;;;;;OAKG;IACH,YACE,QAAgB,EAChB,aAA2B,EAC3B,OAAgC,EA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AAM,GAAG,qCAA0B,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;YAC7D,IAAI,CAAC,MAAM,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;YACzC,OAAO;QACT,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,qCAA0B,EAAE,OAAO,CAAC,CAAC;IACrE,CAAC;IAED,KAAK,CAAC,KAAK;QACT,yEAAyE;QACzE,yEAAyE;QACzE,UAAU;QACV,MAAM,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QAC7B,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC5B,CAAC;CACF;AA/PD,8BA+PC","sourcesContent":["import { type OAuthClientProvider } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { SSEClientTransport } from \"@modelcontextprotocol/sdk/client/sse.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport {\n CreateMessageRequest,\n CreateMessageRequestSchema,\n CreateMessageResult,\n ElicitRequest,\n ElicitRequestSchema,\n ElicitResult,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { JSONSchema7 } from \"json-schema\";\n\nexport enum MCPTransport {\n SSE = \"sse\",\n HTTP = \"http\",\n}\n\n/**\n * Handlers for MCP requests - these are only used if the server supports the corresponding capabilities\n * @param elicitation - Handler for elicitation requests\n * @param sampling - Handler for sampling requests\n * @example\n * ```typescript\n * const mcp = await MCPClient.create(\n * 'https://api.example.com/mcp',\n * MCPTransport.HTTP,\n * {},\n * undefined,\n * undefined,\n * {\n * elicitation: (e: ElicitRequest) => Promise.resolve({...}),\n * },\n * });\n * ```\n */\nexport interface MCPHandlers {\n elicitation: (e: ElicitRequest) => Promise<ElicitResult>;\n sampling: (e: CreateMessageRequest) => Promise<CreateMessageResult>;\n}\n\n/**\n * A client for interacting with MCP (Model Context Protocol) servers.\n * Provides a simple interface for listing and calling tools exposed by the server.\n * @example\n * ```typescript\n * const mcp = await MCPClient.create('https://api.example.com/mcp');\n * const tools = await mcp.listTools();\n * const result = await mcp.callTool('toolName', { arg1: 'value1' });\n * ```\n */\nexport class MCPClient {\n /**\n * The underlying MCP client\n *\n * Be careful not to mutate the client directly, use the methods provided instead.\n */\n client: Client;\n private transport: SSEClientTransport | StreamableHTTPClientTransport;\n private transportType: MCPTransport;\n public sessionId?: string;\n private endpoint: string;\n private headers: Record<string, string>;\n private authProvider?: OAuthClientProvider;\n private handlers: Partial<MCPHandlers>;\n\n /**\n * Private constructor to enforce using the static create method.\n * @param endpoint - The URL of the MCP server to connect to\n * @param transportType - The transport to use for the MCP client\n * @param headers - Optional custom headers to include in requests\n */\n private constructor(\n endpoint: string,\n transportType: MCPTransport,\n headers?: Record<string, string>,\n authProvider?: OAuthClientProvider,\n sessionId?: string,\n handlers: Partial<MCPHandlers> = {},\n ) {\n this.endpoint = endpoint;\n this.headers = headers ?? {};\n this.authProvider = authProvider;\n this.transportType = transportType;\n this.handlers = handlers;\n this.transport = this.initializeTransport(sessionId);\n this.client = this.initializeClient();\n }\n\n /**\n * Creates and initializes a new MCPClient instance. This is the recommended\n * way to create an MCPClient as it handles both instantiation and connection\n * setup.\n * @param endpoint - The URL of the MCP server to connect to\n * @param transportType - The transport type to use for the MCP client. Defaults to HTTP.\n * @param headers - Optional custom headers to include in requests\n * @param authProvider - Optional auth provider to use for authentication\n * @param sessionId - Optional session id to use for the MCP client - if not\n * provided, a new session will be created\n * @returns A connected MCPClient instance ready for use\n * @throws {Error} Will throw an error if connection fails\n */\n static async create(\n endpoint: string,\n transportType: MCPTransport = MCPTransport.HTTP,\n headers: Record<string, string> | undefined,\n authProvider: OAuthClientProvider | undefined,\n sessionId: string | undefined,\n handlers: Partial<MCPHandlers> = {},\n ): Promise<MCPClient> {\n const mcpClient = new MCPClient(\n endpoint,\n transportType,\n headers,\n authProvider,\n sessionId,\n handlers,\n );\n await mcpClient.client.connect(mcpClient.transport);\n if (\"sessionId\" in mcpClient.transport) {\n mcpClient.sessionId = mcpClient.transport.sessionId;\n }\n return mcpClient;\n }\n\n private initializeTransport(sessionId: string | undefined) {\n if (this.transportType === MCPTransport.SSE) {\n return new SSEClientTransport(new URL(this.endpoint), {\n authProvider: this.authProvider,\n requestInit: { headers: this.headers },\n });\n } else {\n return new StreamableHTTPClientTransport(new URL(this.endpoint), {\n authProvider: this.authProvider,\n requestInit: { headers: this.headers },\n sessionId,\n });\n }\n }\n\n /**\n * Initializes the MCP client with the appropriate capabilities and handlers\n * @returns The initialized MCP client\n */\n private initializeClient() {\n const elicitationCapability = this.handlers.elicitation\n ? { elicitation: {} }\n : {};\n const samplingCapability = this.handlers.sampling ? { sampling: {} } : {};\n const client = new Client(\n {\n name: \"tambo-mcp-client\",\n version: \"1.0.0\",\n },\n {\n capabilities: {\n ...elicitationCapability,\n ...samplingCapability,\n },\n },\n );\n\n if (this.handlers.elicitation) {\n client.setRequestHandler(ElicitRequestSchema, this.handlers.elicitation);\n }\n if (this.handlers.sampling) {\n client.setRequestHandler(\n CreateMessageRequestSchema,\n this.handlers.sampling,\n );\n }\n return client;\n }\n\n /**\n * Retrieves a complete list of all available tools from the MCP server.\n * Handles pagination automatically by following cursors until all tools are fetched.\n * @returns A complete list of all available tools and their descriptions\n * @throws {Error} Will throw an error if any server request fails during pagination\n */\n async listTools(): Promise<MCPToolSpec[]> {\n const allTools: MCPToolSpec[] = [];\n let hasMore = true;\n let cursor: string | undefined = undefined;\n\n while (hasMore) {\n const response = await this.client.listTools({ cursor }, {});\n allTools.push(\n ...response.tools.map((tool): MCPToolSpec => {\n // make sure the right type is used\n const inputSchemaType: string = tool.inputSchema.type;\n if (inputSchemaType !== \"object\") {\n throw new Error(\n `Input schema for tool ${tool.name} is not an object`,\n );\n }\n\n return {\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema as JSONSchema7,\n };\n }),\n );\n\n if (response.nextCursor) {\n cursor = response.nextCursor;\n } else {\n hasMore = false;\n }\n }\n\n return allTools;\n }\n\n getServerCapabilities() {\n return this.client.getServerCapabilities();\n }\n\n getServerVersion() {\n return this.client.getServerVersion();\n }\n\n getInstructions() {\n return this.client.getInstructions();\n }\n\n /**\n * Calls a specific tool on the MCP server with the provided arguments.\n * @param name - The name of the tool to call\n * @param args - Arguments to pass to the tool, must match the tool's expected schema\n * @returns The result from the tool execution\n * @throws {Error} Will throw an error if the tool call fails or if arguments are invalid\n */\n async callTool(\n name: string,\n args: Record<string, unknown>,\n _meta?: Record<string, unknown>,\n ): Promise<MCPToolCallResult> {\n const result = await this.client.callTool({\n name,\n arguments: args,\n _meta,\n });\n return result;\n }\n\n updateElicitationHandler(\n handler: ((e: ElicitRequest) => Promise<ElicitResult>) | undefined,\n ) {\n // Skip if handler hasn't changed\n if (handler === this.handlers.elicitation) {\n return;\n }\n\n // Because we advertise the elicitation capability on initial connection, we can only update\n // an existing handler, not add it if we haven't set it yet.\n if (handler && !this.handlers.elicitation) {\n throw new Error(\"Elicitation handler must be set on create\");\n }\n this.handlers = {\n ...this.handlers,\n elicitation: handler,\n };\n if (!handler) {\n const method = ElicitRequestSchema.shape.method.value;\n this.client.removeRequestHandler(method);\n return;\n }\n this.client.setRequestHandler(ElicitRequestSchema, handler);\n }\n\n updateSamplingHandler(\n handler:\n | ((e: CreateMessageRequest) => Promise<CreateMessageResult>)\n | undefined,\n ) {\n // Skip if handler hasn't changed\n if (handler === this.handlers.sampling) {\n return;\n }\n\n // Because we advertise the sampling capability on initial connection, we can only update\n // an existing handler, not add it if we haven't set it yet.\n if (handler && !this.handlers.sampling) {\n throw new Error(\"Sampling handler must be set on create\");\n }\n this.handlers = {\n ...this.handlers,\n sampling: handler,\n };\n if (!handler) {\n const method = CreateMessageRequestSchema.shape.method.value;\n this.client.removeRequestHandler(method);\n return;\n }\n this.client.setRequestHandler(CreateMessageRequestSchema, handler);\n }\n\n async close() {\n // Not really sure which one of these to close first, but we'll close the\n // transport first so that no requests can come in and hit closing/closed\n // clients\n await this.transport.close();\n await this.client.close();\n }\n}\n\n/**\n * The result of a tool call.\n * This is the same as the result of a tool call in the OpenAI SDK, but is reified here\n */\nexport type MCPToolCallResult = Awaited<\n ReturnType<typeof Client.prototype.callTool>\n>;\n\n// Example usage:\n/*\nconst mcp = await MCPClient.create('https://api.example.com/mcp', MCPTransport.HTTP);\nconst tools = await mcp.listTools();\nconst result = await mcp.callTool('toolName', { arg1: 'value1' });\n*/\n\nexport interface MCPToolSpec {\n name: string;\n description?: string;\n inputSchema?: JSONSchema7;\n}\n"]}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type ListPromptsResult } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
import { McpServerInfo } from "./tambo-mcp-provider";
|
|
3
|
+
export type ListPromptItem = ListPromptsResult["prompts"][number];
|
|
4
|
+
export interface ListPromptEntry {
|
|
5
|
+
server: McpServerInfo;
|
|
6
|
+
prompt: ListPromptItem;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Hook to get the prompts for all the registered MCP servers.
|
|
10
|
+
* @returns The prompts for the MCP servers, including the server that the prompt was found on.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useTamboMcpPromptList(): import("@tanstack/react-query").UseQueryResult<ListPromptEntry[], Error>;
|
|
13
|
+
/**
|
|
14
|
+
* Hook to get the prompt for the specified name.
|
|
15
|
+
* @param promptName - The name of the prompt to get.
|
|
16
|
+
* @returns The prompt for the specified name.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useTamboMcpPrompt(promptName: string): import("@tanstack/react-query").UseQueryResult<{
|
|
19
|
+
[x: string]: unknown;
|
|
20
|
+
messages: {
|
|
21
|
+
[x: string]: unknown;
|
|
22
|
+
content: {
|
|
23
|
+
[x: string]: unknown;
|
|
24
|
+
text: string;
|
|
25
|
+
type: "text";
|
|
26
|
+
_meta?: {
|
|
27
|
+
[x: string]: unknown;
|
|
28
|
+
} | undefined;
|
|
29
|
+
} | {
|
|
30
|
+
[x: string]: unknown;
|
|
31
|
+
data: string;
|
|
32
|
+
type: "image";
|
|
33
|
+
mimeType: string;
|
|
34
|
+
_meta?: {
|
|
35
|
+
[x: string]: unknown;
|
|
36
|
+
} | undefined;
|
|
37
|
+
} | {
|
|
38
|
+
[x: string]: unknown;
|
|
39
|
+
data: string;
|
|
40
|
+
type: "audio";
|
|
41
|
+
mimeType: string;
|
|
42
|
+
_meta?: {
|
|
43
|
+
[x: string]: unknown;
|
|
44
|
+
} | undefined;
|
|
45
|
+
} | {
|
|
46
|
+
[x: string]: unknown;
|
|
47
|
+
name: string;
|
|
48
|
+
type: "resource_link";
|
|
49
|
+
uri: string;
|
|
50
|
+
title?: string | undefined;
|
|
51
|
+
description?: string | undefined;
|
|
52
|
+
_meta?: {
|
|
53
|
+
[x: string]: unknown;
|
|
54
|
+
} | undefined;
|
|
55
|
+
mimeType?: string | undefined;
|
|
56
|
+
icons?: {
|
|
57
|
+
[x: string]: unknown;
|
|
58
|
+
src: string;
|
|
59
|
+
mimeType?: string | undefined;
|
|
60
|
+
sizes?: string[] | undefined;
|
|
61
|
+
}[] | undefined;
|
|
62
|
+
} | {
|
|
63
|
+
[x: string]: unknown;
|
|
64
|
+
type: "resource";
|
|
65
|
+
resource: {
|
|
66
|
+
[x: string]: unknown;
|
|
67
|
+
text: string;
|
|
68
|
+
uri: string;
|
|
69
|
+
_meta?: {
|
|
70
|
+
[x: string]: unknown;
|
|
71
|
+
} | undefined;
|
|
72
|
+
mimeType?: string | undefined;
|
|
73
|
+
} | {
|
|
74
|
+
[x: string]: unknown;
|
|
75
|
+
uri: string;
|
|
76
|
+
blob: string;
|
|
77
|
+
_meta?: {
|
|
78
|
+
[x: string]: unknown;
|
|
79
|
+
} | undefined;
|
|
80
|
+
mimeType?: string | undefined;
|
|
81
|
+
};
|
|
82
|
+
_meta?: {
|
|
83
|
+
[x: string]: unknown;
|
|
84
|
+
} | undefined;
|
|
85
|
+
};
|
|
86
|
+
role: "user" | "assistant";
|
|
87
|
+
}[];
|
|
88
|
+
description?: string | undefined;
|
|
89
|
+
_meta?: {
|
|
90
|
+
[x: string]: unknown;
|
|
91
|
+
} | undefined;
|
|
92
|
+
} | undefined, Error>;
|
|
93
|
+
//# sourceMappingURL=mcp-hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-hooks.d.ts","sourceRoot":"","sources":["../../src/mcp/mcp-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,iBAAiB,EACvB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAAE,aAAa,EAAsB,MAAM,sBAAsB,CAAC;AAEzE,MAAM,MAAM,cAAc,GAAG,iBAAiB,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC;AAClE,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,aAAa,CAAC;IACtB,MAAM,EAAE,cAAc,CAAC;CACxB;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,6EAyBpC;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sBAsBnD"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useTamboMcpPromptList = useTamboMcpPromptList;
|
|
4
|
+
exports.useTamboMcpPrompt = useTamboMcpPrompt;
|
|
5
|
+
const hooks_1 = require("../hooks");
|
|
6
|
+
const tambo_mcp_provider_1 = require("./tambo-mcp-provider");
|
|
7
|
+
/**
|
|
8
|
+
* Hook to get the prompts for all the registered MCP servers.
|
|
9
|
+
* @returns The prompts for the MCP servers, including the server that the prompt was found on.
|
|
10
|
+
*/
|
|
11
|
+
function useTamboMcpPromptList() {
|
|
12
|
+
const mcpServers = (0, tambo_mcp_provider_1.useTamboMcpServers)();
|
|
13
|
+
return (0, hooks_1.useTamboQuery)({
|
|
14
|
+
queryKey: ["mcp-prompts"],
|
|
15
|
+
queryFn: async () => {
|
|
16
|
+
const promptResults = await Promise.allSettled(mcpServers.map(async (mcpServer) => ({
|
|
17
|
+
server: mcpServer,
|
|
18
|
+
response: await mcpServer.client?.client.listPrompts(),
|
|
19
|
+
})));
|
|
20
|
+
const prompts = promptResults
|
|
21
|
+
.filter((result) => result.status === "fulfilled")
|
|
22
|
+
.flatMap((result) => {
|
|
23
|
+
const { server, response } = result.value;
|
|
24
|
+
return response?.prompts?.map((prompt) => ({
|
|
25
|
+
server,
|
|
26
|
+
prompt,
|
|
27
|
+
}));
|
|
28
|
+
})
|
|
29
|
+
.filter((prompt) => prompt !== undefined);
|
|
30
|
+
return prompts;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Hook to get the prompt for the specified name.
|
|
36
|
+
* @param promptName - The name of the prompt to get.
|
|
37
|
+
* @returns The prompt for the specified name.
|
|
38
|
+
*/
|
|
39
|
+
function useTamboMcpPrompt(promptName) {
|
|
40
|
+
// figure out which server has the prompt
|
|
41
|
+
const { data: promptEntries } = useTamboMcpPromptList();
|
|
42
|
+
const promptEntry = promptEntries?.find((prompt) => prompt.prompt.name === promptName);
|
|
43
|
+
const mcpServers = (0, tambo_mcp_provider_1.useTamboMcpServers)();
|
|
44
|
+
const mcpServer = mcpServers.find((mcpServer) => mcpServer.name === promptEntry?.server.name &&
|
|
45
|
+
mcpServer.url === promptEntry?.server.url &&
|
|
46
|
+
mcpServer.transport === promptEntry?.server.transport);
|
|
47
|
+
return (0, hooks_1.useTamboQuery)({
|
|
48
|
+
queryKey: ["mcp-prompt", promptName],
|
|
49
|
+
queryFn: async () => {
|
|
50
|
+
return await mcpServer?.client?.client.getPrompt({
|
|
51
|
+
name: promptName,
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
});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B,iBAAiB,CAAC,UAAkB;IAClD,yCAAyC;IACzC,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,qBAAqB,EAAE,CAAC;IACxD,MAAM,WAAW,GAAG,aAAa,EAAE,IAAI,CACrC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAC9C,CAAC;IACF,MAAM,UAAU,GAAG,IAAA,uCAAkB,GAAE,CAAC;IACxC,MAAM,SAAS,GAAG,UAAU,CAAC,IAAI,CAC/B,CAAC,SAAS,EAAE,EAAE,CACZ,SAAS,CAAC,IAAI,KAAK,WAAW,EAAE,MAAM,CAAC,IAAI;QAC3C,SAAS,CAAC,GAAG,KAAK,WAAW,EAAE,MAAM,CAAC,GAAG;QACzC,SAAS,CAAC,SAAS,KAAK,WAAW,EAAE,MAAM,CAAC,SAAS,CACxD,CAAC;IAEF,OAAO,IAAA,qBAAa,EAAC;QACnB,QAAQ,EAAE,CAAC,YAAY,EAAE,UAAU,CAAC;QACpC,OAAO,EAAE,KAAK,IAA0C,EAAE;YACxD,OAAO,MAAM,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC;gBAC/C,IAAI,EAAE,UAAU;aACjB,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n GetPromptResult,\n type ListPromptsResult,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { useTamboQuery } from \"../hooks\";\nimport { McpServerInfo, useTamboMcpServers } from \"./tambo-mcp-provider\";\n\nexport type ListPromptItem = ListPromptsResult[\"prompts\"][number];\nexport interface ListPromptEntry {\n server: McpServerInfo;\n prompt: ListPromptItem;\n}\n\n/**\n * Hook to get the prompts for all the registered MCP servers.\n * @returns The prompts for the MCP servers, including the server that the prompt was found on.\n */\nexport function useTamboMcpPromptList() {\n const mcpServers = useTamboMcpServers();\n return useTamboQuery({\n queryKey: [\"mcp-prompts\"],\n queryFn: async (): Promise<ListPromptEntry[]> => {\n const promptResults = await Promise.allSettled(\n mcpServers.map(async (mcpServer) => ({\n server: mcpServer,\n response: await mcpServer.client?.client.listPrompts(),\n })),\n );\n\n const prompts: ListPromptEntry[] = promptResults\n .filter((result) => result.status === \"fulfilled\")\n .flatMap((result) => {\n const { server, response } = result.value;\n return response?.prompts?.map((prompt: ListPromptItem) => ({\n server,\n prompt,\n }));\n })\n .filter((prompt) => prompt !== undefined);\n return prompts;\n },\n });\n}\n\n/**\n * Hook to get the prompt for the specified name.\n * @param promptName - The name of the prompt to get.\n * @returns The prompt for the specified name.\n */\nexport function useTamboMcpPrompt(promptName: string) {\n // figure out which server has the prompt\n const { data: promptEntries } = useTamboMcpPromptList();\n const promptEntry = promptEntries?.find(\n (prompt) => prompt.prompt.name === promptName,\n );\n const mcpServers = useTamboMcpServers();\n const mcpServer = mcpServers.find(\n (mcpServer) =>\n mcpServer.name === promptEntry?.server.name &&\n mcpServer.url === promptEntry?.server.url &&\n mcpServer.transport === promptEntry?.server.transport,\n );\n\n return useTamboQuery({\n queryKey: [\"mcp-prompt\", promptName],\n queryFn: async (): Promise<GetPromptResult | undefined> => {\n return await mcpServer?.client?.client.getPrompt({\n name: promptName,\n });\n },\n });\n}\n"]}
|