@tambo-ai/react 0.64.1 → 0.65.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +304 -327
- package/dist/hooks/use-tambo-threads.d.ts +0 -12
- package/dist/hooks/use-tambo-threads.d.ts.map +1 -1
- package/dist/hooks/use-tambo-threads.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp/__tests__/elicitation.test.js +7 -3
- package/dist/mcp/__tests__/elicitation.test.js.map +1 -1
- package/dist/mcp/__tests__/mcp-hooks.test.js +149 -123
- package/dist/mcp/__tests__/mcp-hooks.test.js.map +1 -1
- package/dist/mcp/__tests__/tambo-mcp-provider.test.js +176 -120
- package/dist/mcp/__tests__/tambo-mcp-provider.test.js.map +1 -1
- package/dist/mcp/__tests__/use-mcp-servers.test.js +12 -9
- package/dist/mcp/__tests__/use-mcp-servers.test.js.map +1 -1
- package/dist/mcp/elicitation.d.ts +4 -40
- package/dist/mcp/elicitation.d.ts.map +1 -1
- package/dist/mcp/elicitation.js +1 -1
- package/dist/mcp/elicitation.js.map +1 -1
- package/dist/mcp/index.d.ts +2 -1
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +2 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/mcp-client.d.ts +14 -26
- package/dist/mcp/mcp-client.d.ts.map +1 -1
- package/dist/mcp/mcp-client.js +4 -7
- package/dist/mcp/mcp-client.js.map +1 -1
- package/dist/mcp/mcp-hooks.d.ts +27 -78
- package/dist/mcp/mcp-hooks.d.ts.map +1 -1
- package/dist/mcp/tambo-mcp-provider.d.ts +27 -45
- package/dist/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/dist/mcp/tambo-mcp-provider.js +36 -87
- package/dist/mcp/tambo-mcp-provider.js.map +1 -1
- package/dist/model/mcp-server-info.d.ts +74 -0
- package/dist/model/mcp-server-info.d.ts.map +1 -0
- package/dist/model/mcp-server-info.js +29 -0
- package/dist/model/mcp-server-info.js.map +1 -0
- package/dist/providers/__tests__/tambo-thread-provider-initial-messages.test.js +22 -8
- package/dist/providers/__tests__/tambo-thread-provider-initial-messages.test.js.map +1 -1
- package/dist/providers/__tests__/tambo-thread-provider.test.js +318 -129
- package/dist/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +2 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/tambo-client-provider.d.ts +4 -0
- package/dist/providers/tambo-client-provider.d.ts.map +1 -1
- package/dist/providers/tambo-client-provider.js +3 -0
- package/dist/providers/tambo-client-provider.js.map +1 -1
- package/dist/providers/tambo-mcp-token-provider.d.ts +3 -0
- package/dist/providers/tambo-mcp-token-provider.d.ts.map +1 -1
- package/dist/providers/tambo-mcp-token-provider.js +11 -3
- package/dist/providers/tambo-mcp-token-provider.js.map +1 -1
- package/dist/providers/tambo-provider.d.ts +1 -0
- package/dist/providers/tambo-provider.d.ts.map +1 -1
- package/dist/providers/tambo-provider.js +10 -5
- package/dist/providers/tambo-provider.js.map +1 -1
- package/dist/providers/tambo-registry-provider.d.ts +37 -0
- package/dist/providers/tambo-registry-provider.d.ts.map +1 -1
- package/dist/providers/tambo-registry-provider.js +162 -2
- package/dist/providers/tambo-registry-provider.js.map +1 -1
- package/dist/providers/tambo-stubs.d.ts.map +1 -1
- package/dist/providers/tambo-stubs.js +10 -1
- package/dist/providers/tambo-stubs.js.map +1 -1
- package/esm/hooks/use-tambo-threads.d.ts +0 -12
- package/esm/hooks/use-tambo-threads.d.ts.map +1 -1
- package/esm/hooks/use-tambo-threads.js.map +1 -1
- package/esm/index.d.ts +3 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +3 -2
- package/esm/index.js.map +1 -1
- package/esm/mcp/__tests__/elicitation.test.js +8 -4
- package/esm/mcp/__tests__/elicitation.test.js.map +1 -1
- package/esm/mcp/__tests__/mcp-hooks.test.js +149 -123
- package/esm/mcp/__tests__/mcp-hooks.test.js.map +1 -1
- package/esm/mcp/__tests__/tambo-mcp-provider.test.js +178 -122
- package/esm/mcp/__tests__/tambo-mcp-provider.test.js.map +1 -1
- package/esm/mcp/__tests__/use-mcp-servers.test.js +12 -9
- package/esm/mcp/__tests__/use-mcp-servers.test.js.map +1 -1
- package/esm/mcp/elicitation.d.ts +4 -40
- package/esm/mcp/elicitation.d.ts.map +1 -1
- package/esm/mcp/elicitation.js +2 -2
- package/esm/mcp/elicitation.js.map +1 -1
- package/esm/mcp/index.d.ts +2 -1
- package/esm/mcp/index.d.ts.map +1 -1
- package/esm/mcp/index.js +1 -1
- package/esm/mcp/index.js.map +1 -1
- package/esm/mcp/mcp-client.d.ts +14 -26
- package/esm/mcp/mcp-client.d.ts.map +1 -1
- package/esm/mcp/mcp-client.js +3 -5
- package/esm/mcp/mcp-client.js.map +1 -1
- package/esm/mcp/mcp-hooks.d.ts +27 -78
- package/esm/mcp/mcp-hooks.d.ts.map +1 -1
- package/esm/mcp/tambo-mcp-provider.d.ts +27 -45
- package/esm/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/esm/mcp/tambo-mcp-provider.js +35 -86
- package/esm/mcp/tambo-mcp-provider.js.map +1 -1
- package/esm/model/mcp-server-info.d.ts +74 -0
- package/esm/model/mcp-server-info.d.ts.map +1 -0
- package/esm/model/mcp-server-info.js +25 -0
- package/esm/model/mcp-server-info.js.map +1 -0
- package/esm/providers/__tests__/tambo-thread-provider-initial-messages.test.js +23 -9
- package/esm/providers/__tests__/tambo-thread-provider-initial-messages.test.js.map +1 -1
- package/esm/providers/__tests__/tambo-thread-provider.test.js +319 -130
- package/esm/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
- package/esm/providers/index.d.ts +1 -1
- package/esm/providers/index.d.ts.map +1 -1
- package/esm/providers/index.js +1 -1
- package/esm/providers/index.js.map +1 -1
- package/esm/providers/tambo-client-provider.d.ts +4 -0
- package/esm/providers/tambo-client-provider.d.ts.map +1 -1
- package/esm/providers/tambo-client-provider.js +3 -0
- package/esm/providers/tambo-client-provider.js.map +1 -1
- package/esm/providers/tambo-mcp-token-provider.d.ts +3 -0
- package/esm/providers/tambo-mcp-token-provider.d.ts.map +1 -1
- package/esm/providers/tambo-mcp-token-provider.js +13 -5
- package/esm/providers/tambo-mcp-token-provider.js.map +1 -1
- package/esm/providers/tambo-provider.d.ts +1 -0
- package/esm/providers/tambo-provider.d.ts.map +1 -1
- package/esm/providers/tambo-provider.js +11 -6
- package/esm/providers/tambo-provider.js.map +1 -1
- package/esm/providers/tambo-registry-provider.d.ts +37 -0
- package/esm/providers/tambo-registry-provider.d.ts.map +1 -1
- package/esm/providers/tambo-registry-provider.js +161 -2
- package/esm/providers/tambo-registry-provider.js.map +1 -1
- package/esm/providers/tambo-stubs.d.ts.map +1 -1
- package/esm/providers/tambo-stubs.js +10 -1
- package/esm/providers/tambo-stubs.js.map +1 -1
- package/package.json +6 -6
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React, { createContext, useContext, useEffect, useMemo, useRef, useState, } from "react";
|
|
2
|
+
import { getMcpServerUniqueKey, } from "../model/mcp-server-info";
|
|
2
3
|
import { useTamboMcpToken } from "../providers/tambo-mcp-token-provider";
|
|
3
|
-
import { useTamboRegistry } from "../providers/tambo-registry-provider";
|
|
4
|
+
import { useTamboMcpServerInfos, useTamboRegistry, } from "../providers/tambo-registry-provider";
|
|
4
5
|
import { isContentPartArray, toText } from "../util/content-parts";
|
|
5
6
|
import { useElicitation } from "./elicitation";
|
|
6
7
|
import { MCPClient, MCPTransport, } from "./mcp-client";
|
|
@@ -37,15 +38,19 @@ const TAMBO_INTERNAL_MCP_SERVER_NAME = "__tambo_internal_mcp_server__";
|
|
|
37
38
|
/**
|
|
38
39
|
* This provider is used to register tools from MCP servers.
|
|
39
40
|
* It automatically includes an internal Tambo MCP server when an MCP access token is available.
|
|
41
|
+
*
|
|
42
|
+
* **BREAKING CHANGE**: This provider no longer accepts `mcpServers` as a prop.
|
|
43
|
+
* Instead, pass `mcpServers` to `TamboProvider` or `TamboRegistryProvider`.
|
|
44
|
+
* This provider must be wrapped inside `TamboProvider` to access the MCP server registry.
|
|
40
45
|
* @param props - The provider props
|
|
41
|
-
* @param props.mcpServers - Array of MCP server configurations
|
|
42
46
|
* @param props.handlers - Optional handlers applied to all MCP servers unless overridden per-server
|
|
43
47
|
* @param props.children - The children to wrap
|
|
44
48
|
* @returns The TamboMcpProvider component
|
|
45
49
|
*/
|
|
46
|
-
export const TamboMcpProvider = ({
|
|
50
|
+
export const TamboMcpProvider = ({ handlers, children }) => {
|
|
47
51
|
const { registerTool } = useTamboRegistry();
|
|
48
52
|
const { mcpAccessToken, tamboBaseUrl } = useTamboMcpToken();
|
|
53
|
+
const mcpServers = useTamboMcpServerInfos();
|
|
49
54
|
const providerSamplingHandler = handlers?.sampling;
|
|
50
55
|
// Elicitation state and default handler
|
|
51
56
|
const { elicitation, resolveElicitation, defaultElicitationHandler } = useElicitation();
|
|
@@ -71,6 +76,7 @@ export const TamboMcpProvider = ({ mcpServers, handlers, children }) => {
|
|
|
71
76
|
name: TAMBO_INTERNAL_MCP_SERVER_NAME,
|
|
72
77
|
url: tamboMcpUrl,
|
|
73
78
|
transport: MCPTransport.HTTP,
|
|
79
|
+
serverKey: "tambo", // Internal server always uses 'tambo' as serverKey
|
|
74
80
|
customHeaders: {
|
|
75
81
|
Authorization: `Bearer ${mcpAccessToken}`,
|
|
76
82
|
},
|
|
@@ -207,8 +213,12 @@ export const TamboMcpProvider = ({ mcpServers, handlers, children }) => {
|
|
|
207
213
|
if (keysToRemove.length > 0) {
|
|
208
214
|
setConnectedMcpServers(Array.from(clientMap.values()));
|
|
209
215
|
}
|
|
210
|
-
|
|
211
|
-
|
|
216
|
+
}, [
|
|
217
|
+
currentServersMap,
|
|
218
|
+
providerElicitationHandler,
|
|
219
|
+
providerSamplingHandler,
|
|
220
|
+
registerTool,
|
|
221
|
+
]);
|
|
212
222
|
// Update handlers when they change (without recreating clients)
|
|
213
223
|
useEffect(() => {
|
|
214
224
|
const clientMap = clientMapRef.current;
|
|
@@ -290,16 +300,16 @@ export const useTamboMcpServers = () => {
|
|
|
290
300
|
return useContext(McpProviderContext).servers;
|
|
291
301
|
};
|
|
292
302
|
/**
|
|
293
|
-
* Hook to access elicitation
|
|
303
|
+
* Hook to access MCP elicitation state from TamboMcpProvider.
|
|
294
304
|
* This provides access to the current elicitation request and methods to respond to it.
|
|
295
305
|
*
|
|
296
306
|
* The elicitation state is automatically managed by TamboMcpProvider when MCP servers
|
|
297
307
|
* request user input through the elicitation protocol.
|
|
298
|
-
* @returns The elicitation
|
|
308
|
+
* @returns The elicitation state with current request and response handler
|
|
299
309
|
* @example
|
|
300
310
|
* ```tsx
|
|
301
311
|
* function ElicitationUI() {
|
|
302
|
-
* const { elicitation, resolveElicitation } =
|
|
312
|
+
* const { elicitation, resolveElicitation } = useTamboMcpElicitation();
|
|
303
313
|
*
|
|
304
314
|
* if (!elicitation) return null;
|
|
305
315
|
*
|
|
@@ -314,7 +324,7 @@ export const useTamboMcpServers = () => {
|
|
|
314
324
|
* }
|
|
315
325
|
* ```
|
|
316
326
|
*/
|
|
317
|
-
export const
|
|
327
|
+
export const useTamboMcpElicitation = () => {
|
|
318
328
|
const context = useContext(McpProviderContext);
|
|
319
329
|
return {
|
|
320
330
|
elicitation: context.elicitation,
|
|
@@ -322,86 +332,25 @@ export const useTamboElicitationContext = () => {
|
|
|
322
332
|
};
|
|
323
333
|
};
|
|
324
334
|
/**
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* Examples:
|
|
329
|
-
* - "https://mcp.linear.app/mcp" -> "linear"
|
|
330
|
-
* - "https://api.github.com" -> "github"
|
|
331
|
-
* - "https://google.com" -> "google"
|
|
332
|
-
* - "https://google.co.uk" -> "google"
|
|
333
|
-
* - "https://mcp.company.co.uk" -> "company"
|
|
334
|
-
* @param url - The URL to derive a server key from
|
|
335
|
-
* @returns A short server key derived from the hostname
|
|
335
|
+
* @deprecated Use `useTamboMcpElicitation` instead.
|
|
336
|
+
* This hook will be removed in a future version.
|
|
336
337
|
*/
|
|
337
|
-
|
|
338
|
-
try {
|
|
339
|
-
const parsed = new URL(url);
|
|
340
|
-
const hostname = parsed.hostname;
|
|
341
|
-
// Split hostname into parts
|
|
342
|
-
const parts = hostname.split(".");
|
|
343
|
-
// Remove common TLD patterns
|
|
344
|
-
// Handle cases like: .com, .org, .co.uk, .com.au, etc.
|
|
345
|
-
let relevantParts = [...parts];
|
|
346
|
-
// If we have 3+ parts and the last two are short (likely TLD like .co.uk)
|
|
347
|
-
if (relevantParts.length >= 3 &&
|
|
348
|
-
relevantParts[relevantParts.length - 1].length <= 3 &&
|
|
349
|
-
relevantParts[relevantParts.length - 2].length <= 3) {
|
|
350
|
-
relevantParts = relevantParts.slice(0, -2);
|
|
351
|
-
}
|
|
352
|
-
// Otherwise just remove the last part (TLD like .com)
|
|
353
|
-
else if (relevantParts.length >= 2) {
|
|
354
|
-
relevantParts = relevantParts.slice(0, -1);
|
|
355
|
-
}
|
|
356
|
-
// From what's left, prefer the rightmost part that's not a common prefix
|
|
357
|
-
// Common prefixes: www, api, mcp, app, etc.
|
|
358
|
-
const commonPrefixes = new Set([
|
|
359
|
-
"www",
|
|
360
|
-
"api",
|
|
361
|
-
"mcp",
|
|
362
|
-
"app",
|
|
363
|
-
"staging",
|
|
364
|
-
"dev",
|
|
365
|
-
"prod",
|
|
366
|
-
]);
|
|
367
|
-
// Work backwards through the parts to find a meaningful name
|
|
368
|
-
for (let i = relevantParts.length - 1; i >= 0; i--) {
|
|
369
|
-
const part = relevantParts[i];
|
|
370
|
-
if (part && !commonPrefixes.has(part.toLowerCase())) {
|
|
371
|
-
return part.toLowerCase();
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
// Fallback: use the last relevant part even if it's a common prefix
|
|
375
|
-
return relevantParts[relevantParts.length - 1]?.toLowerCase() || hostname;
|
|
376
|
-
}
|
|
377
|
-
catch {
|
|
378
|
-
// If URL parsing fails, just return a sanitized version of the input
|
|
379
|
-
return url.replace(/[^a-zA-Z0-9]/g, "_").toLowerCase();
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
/**
|
|
383
|
-
* Creates a stable identifier for an MCP server based on its connection properties.
|
|
384
|
-
* Two servers with the same URL, transport, and headers will have the same key.
|
|
385
|
-
* @returns A stable string key identifying the server
|
|
386
|
-
*/
|
|
387
|
-
function getServerKey(serverInfo) {
|
|
388
|
-
const headerStr = serverInfo.customHeaders
|
|
389
|
-
? JSON.stringify(Object.entries(serverInfo.customHeaders)
|
|
390
|
-
.map(([k, v]) => [k.toLowerCase(), v])
|
|
391
|
-
.sort(([a], [b]) => a.localeCompare(b)))
|
|
392
|
-
: "";
|
|
393
|
-
return `${serverInfo.url}|${serverInfo.transport ?? MCPTransport.HTTP}|${headerStr}`;
|
|
394
|
-
}
|
|
338
|
+
export const useTamboElicitationContext = useTamboMcpElicitation;
|
|
395
339
|
/**
|
|
396
|
-
* Normalizes
|
|
397
|
-
*
|
|
340
|
+
* Normalizes registry server metadata into a `McpServerConfig`.
|
|
341
|
+
*
|
|
342
|
+
* Accepts a `NormalizedMcpServerInfo`, which already guarantees a concrete
|
|
343
|
+
* `transport` and a `serverKey` derived by the registry, and narrows the
|
|
344
|
+
* opaque `handlers` field to `Partial<MCPHandlers>`.
|
|
398
345
|
*/
|
|
399
346
|
function normalizeServerInfo(server) {
|
|
400
|
-
const
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
347
|
+
const key = getMcpServerUniqueKey(server);
|
|
348
|
+
// Cast handlers to proper type if present
|
|
349
|
+
const handlers = server.handlers;
|
|
350
|
+
return {
|
|
351
|
+
...server,
|
|
352
|
+
handlers,
|
|
353
|
+
key,
|
|
354
|
+
};
|
|
406
355
|
}
|
|
407
356
|
//# sourceMappingURL=tambo-mcp-provider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-mcp-provider.js","sourceRoot":"","sources":["../../src/mcp/tambo-mcp-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EACZ,aAAa,EAEb,UAAU,EACV,SAAS,EACT,OAAO,EACP,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAC;AAEf,OAAO,EAAE,gBAAgB,EAAE,MAAM,uCAAuC,CAAC;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AACxE,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAgC,cAAc,EAAE,MAAM,eAAe,CAAC;AAC7E,OAAO,EACL,SAAS,EAIT,YAAY,GACb,MAAM,cAAc,CAAC;AAEtB;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAgB;IAClD,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QAC9C,OAAO,wBAAwB,CAAC;IAClC,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,MAAM,SAAS,GAAG,OAAO;aACtB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,MAAM,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,CAAC;aACxE,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE5B,OAAO,SAAS,CAAC,MAAM,GAAG,CAAC;YACzB,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC;YACrB,CAAC,CAAC,wCAAwC,CAAC;IAC/C,CAAC;IAED,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,IAAI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CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAC1C;QACH,CAAC,CAAC,EAAE,CAAC;IACP,OAAO,GAAG,UAAU,CAAC,GAAG,IAAI,UAAU,CAAC,SAAS,IAAI,YAAY,CAAC,IAAI,IAAI,SAAS,EAAE,CAAC;AACvF,CAAC;AAED;;;GAGG;AACH,SAAS,mBAAmB,CAAC,MAA8B;IACzD,MAAM,CAAC,GACL,OAAO,MAAM,KAAK,QAAQ;QACxB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,CAAC,IAAI,EAAE;QAC/C,CAAC,CAAC,MAAM,CAAC;IACb,MAAM,GAAG,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;IAC5B,MAAM,SAAS,GAAG,CAAC,CAAC,SAAS,IAAI,eAAe,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACxD,OAAO,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC;AAClC,CAAC","sourcesContent":["import React, {\n createContext,\n FC,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport { TamboTool } from \"../model/component-metadata\";\nimport { useTamboMcpToken } from \"../providers/tambo-mcp-token-provider\";\nimport { useTamboRegistry } from \"../providers/tambo-registry-provider\";\nimport { isContentPartArray, toText } from \"../util/content-parts\";\nimport { type ElicitationContextState, useElicitation } from \"./elicitation\";\nimport {\n MCPClient,\n MCPElicitationHandler,\n MCPHandlers,\n MCPSamplingHandler,\n MCPTransport,\n} from \"./mcp-client\";\n\n/**\n * Extracts error message from MCP tool result content.\n * Handles both array and string content formats.\n * Always returns a string, even for invalid/null inputs.\n * @returns The extracted error message as a string\n */\nexport function extractErrorMessage(content: unknown): string {\n if (content === undefined || content === null) {\n return \"Unknown error occurred\";\n }\n\n if (Array.isArray(content)) {\n const textItems = content\n .filter((item) => item?.type === \"text\" && typeof item.text === \"string\")\n .map((item) => item.text);\n\n return textItems.length > 0\n ? textItems.join(\" \")\n : \"Error occurred but no details provided\";\n }\n\n if (typeof content === \"object\") {\n return JSON.stringify(content);\n }\n\n return `${content}`;\n}\n\n/**\n * User-provided configuration for an MCP server.\n */\nexport interface McpServerInfo {\n /** Optional name for the MCP server */\n name?: string;\n /** The URL of the MCP server to connect to */\n url: string;\n /** Optional description of the MCP server */\n description?: string;\n /** The transport type to use (SSE or HTTP). Defaults to HTTP for string URLs */\n transport?: MCPTransport;\n /** Optional custom headers to include in requests */\n customHeaders?: Record<string, string>;\n /**\n * Optional short name for namespacing MCP resources, prompts, and tools.\n * When multiple MCP servers are configured, this key is used to prefix:\n * - prompts: `<serverKey>:<promptName>`\n * - resources: `<serverKey>:<resourceUrl>`\n * - tools: `<serverKey>__<toolName>`\n *\n * If not provided, a key will be derived from the URL hostname.\n * For example, \"https://mcp.linear.app/mcp\" becomes \"linear\".\n */\n serverKey?: string;\n /**\n * Optional handlers for elicitation and sampling requests from the server.\n * Note: These callbacks should be stable (e.g., wrapped in useCallback or defined outside the component)\n * to avoid constant re-registration of the MCP server on every render.\n */\n handlers?: Partial<MCPHandlers>;\n}\n\n/**\n * Normalized server information with a stable derived key.\n */\ninterface McpServerConfig extends McpServerInfo {\n /**\n * Stable identity for this server derived from its URL/transport/headers.\n * Present for all server states (connected or failed).\n */\n key: string;\n /**\n * Short name for namespacing, either provided by user or derived from URL.\n * Used to prefix tools, prompts, and resources when multiple servers are present.\n */\n serverKey: string;\n}\n\n/**\n * Connected MCP server with an active client.\n */\nexport interface ConnectedMcpServer extends McpServerConfig {\n client: MCPClient;\n}\n\n/**\n * Failed MCP server with a connection error.\n */\nexport interface FailedMcpServer extends McpServerConfig {\n client?: never;\n connectionError: Error;\n}\n\n/**\n * An active or failed MCP server, with access to the MCP client.\n */\nexport type McpServer = ConnectedMcpServer | FailedMcpServer;\n\n/**\n * Provider-level MCP handlers that receive the McpServerInfo as context in addition to the request.\n * These handlers are applied to all MCP servers unless overridden by per-server handlers.\n *\n * Handlers receive three parameters:\n * 1. request - The MCP request\n * 2. extra - RequestHandlerExtra containing AbortSignal and other metadata\n * 3. serverInfo - Configuration of the MCP server that triggered this request\n */\nexport interface ProviderMCPHandlers {\n elicitation?: (\n request: Parameters<MCPElicitationHandler>[0],\n extra: Parameters<MCPElicitationHandler>[1],\n serverInfo: McpServerConfig,\n ) => ReturnType<MCPElicitationHandler>;\n sampling?: (\n request: Parameters<MCPSamplingHandler>[0],\n extra: Parameters<MCPSamplingHandler>[1],\n serverInfo: McpServerConfig,\n ) => ReturnType<MCPSamplingHandler>;\n}\n\n/**\n * Context value for MCP provider including server list and elicitation state\n */\ninterface McpProviderContextValue extends ElicitationContextState {\n servers: McpServer[];\n}\n\nconst McpProviderContext = createContext<McpProviderContextValue>({\n servers: [],\n elicitation: null,\n resolveElicitation: null,\n});\n\n// Constant for the internal Tambo MCP server name\nconst TAMBO_INTERNAL_MCP_SERVER_NAME = \"__tambo_internal_mcp_server__\";\n\n/**\n * This provider is used to register tools from MCP servers.\n * It automatically includes an internal Tambo MCP server when an MCP access token is available.\n * @param props - The provider props\n * @param props.mcpServers - Array of MCP server configurations\n * @param props.handlers - Optional handlers applied to all MCP servers unless overridden per-server\n * @param props.children - The children to wrap\n * @returns The TamboMcpProvider component\n */\nexport const TamboMcpProvider: FC<{\n mcpServers: (McpServerInfo | string)[];\n handlers?: ProviderMCPHandlers;\n children: React.ReactNode;\n}> = ({ mcpServers, handlers, children }) => {\n const { registerTool } = useTamboRegistry();\n const { mcpAccessToken, tamboBaseUrl } = useTamboMcpToken();\n const providerSamplingHandler = handlers?.sampling;\n\n // Elicitation state and default handler\n const { elicitation, resolveElicitation, defaultElicitationHandler } =\n useElicitation();\n\n // Use provided handler or fall back to default\n const providerElicitationHandler =\n handlers?.elicitation ?? defaultElicitationHandler;\n\n // Stable reference to track active clients by server key\n const clientMapRef = useRef<Map<string, McpServer>>(new Map());\n // Track tool ownership to prevent duplicate registrations across servers\n // toolOwnerRef: tool name -> server key; keyToToolsRef: server key -> set of tool names\n const toolOwnerRef = useRef<Map<string, string>>(new Map());\n const keyToToolsRef = useRef<Map<string, Set<string>>>(new Map());\n\n // State for exposing connected servers to consumers\n const [connectedMcpServers, setConnectedMcpServers] = useState<McpServer[]>(\n [],\n );\n\n // Stable map of current server configurations keyed by server key\n const currentServersMap = useMemo(() => {\n const servers = [...mcpServers];\n\n // Add internal Tambo MCP server if we have an access token and a base URL\n if (mcpAccessToken && tamboBaseUrl) {\n const base = new URL(tamboBaseUrl);\n base.pathname = `${base.pathname.replace(/\\/+$/, \"\")}/mcp`;\n const tamboMcpUrl = base.toString();\n servers.push({\n name: TAMBO_INTERNAL_MCP_SERVER_NAME,\n url: tamboMcpUrl,\n transport: MCPTransport.HTTP,\n customHeaders: {\n Authorization: `Bearer ${mcpAccessToken}`,\n },\n });\n }\n\n // Create a map of server key -> server info for efficient lookups\n const serverMap = new Map<string, McpServerConfig>();\n servers.forEach((server) => {\n const serverInfo = normalizeServerInfo(server);\n // Store without cloning to avoid unnecessary allocation\n serverMap.set(serverInfo.key, serverInfo);\n });\n\n return serverMap;\n }, [mcpServers, mcpAccessToken, tamboBaseUrl]);\n\n // Main effect: manage client lifecycle (create/remove)\n useEffect(() => {\n const clientMap = clientMapRef.current;\n const currentKeys = new Set(currentServersMap.keys());\n const existingKeys = new Set(clientMap.keys());\n\n // 1. Remove clients that are no longer in the current server list\n const keysToRemove = Array.from(existingKeys).filter(\n (key) => !currentKeys.has(key),\n );\n keysToRemove.forEach((key) => {\n const server = clientMap.get(key);\n if (server?.client) {\n try {\n server.client.close();\n } catch (error) {\n // Avoid logging sensitive data embedded in the key (headers)\n const url = (server as McpServer).url ?? \"(unknown url)\";\n console.error(`Error closing MCP client for ${url}:`, error);\n }\n }\n // Release tool ownership for this server\n const owned = keyToToolsRef.current.get(key);\n if (owned) {\n for (const name of owned) toolOwnerRef.current.delete(name);\n keyToToolsRef.current.delete(key);\n }\n clientMap.delete(key);\n });\n\n // 2. Add new clients for servers that don't exist yet\n const keysToAdd = Array.from(currentKeys).filter(\n (key) => !existingKeys.has(key),\n );\n\n if (keysToAdd.length > 0) {\n // Async initialization of new clients\n Promise.allSettled(\n keysToAdd.map(async (key) => {\n const serverInfo = currentServersMap.get(key)!;\n\n try {\n // Build effective handlers (per-server overrides provider)\n const effectiveHandlers: Partial<MCPHandlers> = {};\n\n if (serverInfo.handlers?.elicitation) {\n effectiveHandlers.elicitation = serverInfo.handlers.elicitation;\n } else if (providerElicitationHandler) {\n effectiveHandlers.elicitation = async (request, extra) =>\n await providerElicitationHandler(request, extra, serverInfo);\n }\n\n if (serverInfo.handlers?.sampling) {\n effectiveHandlers.sampling = serverInfo.handlers.sampling;\n } else if (providerSamplingHandler) {\n effectiveHandlers.sampling = async (request, extra) =>\n await providerSamplingHandler(request, extra, serverInfo);\n }\n\n const client = await MCPClient.create(\n serverInfo.url,\n serverInfo.transport,\n serverInfo.customHeaders,\n undefined,\n undefined,\n effectiveHandlers,\n );\n\n const connectedServer: ConnectedMcpServer = {\n ...serverInfo,\n client,\n };\n\n clientMap.set(key, connectedServer);\n setConnectedMcpServers(Array.from(clientMap.values()));\n\n // Register tools from this server (deduplicated by ownership)\n try {\n const tools = await client.listTools();\n const shouldPrefix = currentServersMap.size > 1;\n\n tools.forEach((tool) => {\n // Prefix tool name with serverKey if multiple servers are present\n const toolName = shouldPrefix\n ? `${serverInfo.serverKey}__${tool.name}`\n : tool.name;\n\n // Skip if another server already owns this tool (using final name for ownership)\n const currentOwner = toolOwnerRef.current.get(toolName);\n if (currentOwner && currentOwner !== key) {\n return;\n }\n\n // Record ownership for this server key (using final name)\n if (!currentOwner) {\n toolOwnerRef.current.set(toolName, key);\n if (!keyToToolsRef.current.has(key)) {\n keyToToolsRef.current.set(key, new Set());\n }\n keyToToolsRef.current.get(key)!.add(toolName);\n }\n\n registerTool({\n description: tool.description ?? \"\",\n name: toolName,\n tool: async (args: Record<string, unknown> = {}) => {\n const server = clientMap.get(key);\n if (!server?.client) {\n throw new Error(\n `MCP server for tool ${tool.name} is not connected`,\n );\n }\n const result = await server.client.callTool(\n tool.name,\n args,\n );\n if (result.isError) {\n const errorMessage = extractErrorMessage(result.content);\n throw new Error(errorMessage);\n }\n return result.content;\n },\n toolSchema: tool.inputSchema as TamboTool[\"toolSchema\"],\n transformToContent: (content: unknown) => {\n if (isContentPartArray(content)) {\n return content;\n }\n return [{ type: \"text\", text: toText(content) }];\n },\n });\n });\n } catch (error) {\n console.error(\n `Failed to register tools from MCP server ${serverInfo.url}:`,\n error,\n );\n }\n } catch (error) {\n const failedServer: FailedMcpServer = {\n ...serverInfo,\n connectionError: error as Error,\n };\n clientMap.set(key, failedServer);\n setConnectedMcpServers(Array.from(clientMap.values()));\n console.error(\n `Failed to connect to MCP server ${serverInfo.url}:`,\n error,\n );\n }\n }),\n );\n }\n\n // Update state after removals (additions update state asynchronously above)\n if (keysToRemove.length > 0) {\n setConnectedMcpServers(Array.from(clientMap.values()));\n }\n\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [currentServersMap, registerTool]);\n\n // Update handlers when they change (without recreating clients)\n useEffect(() => {\n const clientMap = clientMapRef.current;\n\n clientMap.forEach((server, key) => {\n if (!server.client) {\n return; // Skip failed servers\n }\n\n const serverInfo = currentServersMap.get(key);\n if (!serverInfo) {\n return; // Server was removed, handled by main effect\n }\n\n // Determine effective handlers\n const effectiveElicitationHandler =\n serverInfo.handlers?.elicitation ??\n (providerElicitationHandler\n ? async (request, extra) =>\n await providerElicitationHandler(request, extra, serverInfo)\n : undefined);\n\n const effectiveSamplingHandler =\n serverInfo.handlers?.sampling ??\n (providerSamplingHandler\n ? async (request, extra) =>\n await providerSamplingHandler(request, extra, serverInfo)\n : undefined);\n\n // Update handlers unconditionally (allows removal by passing undefined)\n server.client.updateElicitationHandler?.(effectiveElicitationHandler);\n server.client.updateSamplingHandler?.(effectiveSamplingHandler);\n });\n }, [currentServersMap, providerElicitationHandler, providerSamplingHandler]);\n\n // Cleanup on unmount: close all clients\n useEffect(() => {\n const clientMap = clientMapRef.current;\n const ownerMapAtMount = toolOwnerRef.current;\n const keyToToolsAtMount = keyToToolsRef.current;\n return () => {\n clientMap.forEach((server, _key) => {\n if (server.client) {\n try {\n server.client.close();\n } catch (error) {\n const url = (server as McpServer).url ?? \"(unknown url)\";\n console.error(\n `Error closing MCP client on unmount for ${url}:`,\n error,\n );\n }\n }\n });\n clientMap.clear();\n ownerMapAtMount.clear();\n keyToToolsAtMount.clear();\n };\n }, []);\n\n const contextValue = useMemo(\n () => ({\n servers: connectedMcpServers,\n elicitation,\n resolveElicitation,\n }),\n [connectedMcpServers, elicitation, resolveElicitation],\n );\n\n return (\n <McpProviderContext.Provider value={contextValue}>\n {children}\n </McpProviderContext.Provider>\n );\n};\n\n/**\n * Hook to access the actual MCP servers, as they are connected (or fail to\n * connect).\n *\n * You can call methods on the MCP client that is included in the MCP server\n * object.\n *\n * If the server fails to connect, the `client` property will be `undefined` and\n * the `connectionError` property will be set.\n *\n * For example, to forcibly disconnect and reconnect all MCP servers:\n *\n * ```tsx\n * const mcpServers = useTamboMcpServers();\n * mcpServers.forEach((mcpServer) => {\n * mcpServer.client?.reconnect();\n * });\n * ```\n *\n * Note that the MCP servers are not guaranteed to be in the same order as the\n * input array, because they are added as they are connected.\n * @returns The MCP servers\n */\nexport const useTamboMcpServers = () => {\n return useContext(McpProviderContext).servers;\n};\n\n/**\n * Hook to access elicitation context from TamboMcpProvider.\n * This provides access to the current elicitation request and methods to respond to it.\n *\n * The elicitation state is automatically managed by TamboMcpProvider when MCP servers\n * request user input through the elicitation protocol.\n * @returns The elicitation context with current request and response handlers\n * @example\n * ```tsx\n * function ElicitationUI() {\n * const { elicitation, resolveElicitation } = useTamboElicitationContext();\n *\n * if (!elicitation) return null;\n *\n * return (\n * <div>\n * <p>{elicitation.message}</p>\n * <button onClick={() => resolveElicitation?.({ action: \"accept\", content: {} })}>\n * Accept\n * </button>\n * </div>\n * );\n * }\n * ```\n */\nexport const useTamboElicitationContext = () => {\n const context = useContext(McpProviderContext);\n return {\n elicitation: context.elicitation,\n resolveElicitation: context.resolveElicitation,\n };\n};\n\n/**\n * Derives a short server key from a URL hostname using heuristics.\n * Attempts to extract the \"meaningful\" part of the domain name.\n *\n * Examples:\n * - \"https://mcp.linear.app/mcp\" -> \"linear\"\n * - \"https://api.github.com\" -> \"github\"\n * - \"https://google.com\" -> \"google\"\n * - \"https://google.co.uk\" -> \"google\"\n * - \"https://mcp.company.co.uk\" -> \"company\"\n * @param url - The URL to derive a server key from\n * @returns A short server key derived from the hostname\n */\n\nfunction deriveServerKey(url: string): string {\n try {\n const parsed = new URL(url);\n const hostname = parsed.hostname;\n\n // Split hostname into parts\n const parts = hostname.split(\".\");\n\n // Remove common TLD patterns\n // Handle cases like: .com, .org, .co.uk, .com.au, etc.\n let relevantParts = [...parts];\n\n // If we have 3+ parts and the last two are short (likely TLD like .co.uk)\n if (\n relevantParts.length >= 3 &&\n relevantParts[relevantParts.length - 1].length <= 3 &&\n relevantParts[relevantParts.length - 2].length <= 3\n ) {\n relevantParts = relevantParts.slice(0, -2);\n }\n // Otherwise just remove the last part (TLD like .com)\n else if (relevantParts.length >= 2) {\n relevantParts = relevantParts.slice(0, -1);\n }\n\n // From what's left, prefer the rightmost part that's not a common prefix\n // Common prefixes: www, api, mcp, app, etc.\n const commonPrefixes = new Set([\n \"www\",\n \"api\",\n \"mcp\",\n \"app\",\n \"staging\",\n \"dev\",\n \"prod\",\n ]);\n\n // Work backwards through the parts to find a meaningful name\n for (let i = relevantParts.length - 1; i >= 0; i--) {\n const part = relevantParts[i];\n if (part && !commonPrefixes.has(part.toLowerCase())) {\n return part.toLowerCase();\n }\n }\n\n // Fallback: use the last relevant part even if it's a common prefix\n return relevantParts[relevantParts.length - 1]?.toLowerCase() || hostname;\n } catch {\n // If URL parsing fails, just return a sanitized version of the input\n return url.replace(/[^a-zA-Z0-9]/g, \"_\").toLowerCase();\n }\n}\n\n/**\n * Creates a stable identifier for an MCP server based on its connection properties.\n * Two servers with the same URL, transport, and headers will have the same key.\n * @returns A stable string key identifying the server\n */\nfunction getServerKey(\n serverInfo: Pick<McpServerInfo, \"url\" | \"transport\" | \"customHeaders\">,\n): string {\n const headerStr = serverInfo.customHeaders\n ? JSON.stringify(\n Object.entries(serverInfo.customHeaders)\n .map(([k, v]) => [k.toLowerCase(), v] as const)\n .sort(([a], [b]) => a.localeCompare(b)),\n )\n : \"\";\n return `${serverInfo.url}|${serverInfo.transport ?? MCPTransport.HTTP}|${headerStr}`;\n}\n\n/**\n * Normalizes a server definition (string or object) into a McpServerInfo.\n * @returns The normalized McpServerInfo object\n */\nfunction normalizeServerInfo(server: McpServerInfo | string): McpServerConfig {\n const s =\n typeof server === \"string\"\n ? { url: server, transport: MCPTransport.HTTP }\n : server;\n const key = getServerKey(s);\n const serverKey = s.serverKey ?? deriveServerKey(s.url);\n return { ...s, key, serverKey };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"tambo-mcp-provider.js","sourceRoot":"","sources":["../../src/mcp/tambo-mcp-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EACZ,aAAa,EAEb,UAAU,EACV,SAAS,EACT,OAAO,EACP,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAC;AAEf,OAAO,EACL,qBAAqB,GAEtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uCAAuC,CAAC;AACzE,OAAO,EACL,sBAAsB,EACtB,gBAAgB,GACjB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAgC,cAAc,EAAE,MAAM,eAAe,CAAC;AAC7E,OAAO,EACL,SAAS,EAIT,YAAY,GACb,MAAM,cAAc,CAAC;AAEtB;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAgB;IAClD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GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,sBAAsB,CAAC;AAEjE;;;;;;GAMG;AACH,SAAS,mBAAmB,CAAC,MAA+B;IAC1D,MAAM,GAAG,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAC1C,0CAA0C;IAC1C,MAAM,QAAQ,GAAG,MAAM,CAAC,QAA4C,CAAC;IACrE,OAAO;QACL,GAAG,MAAM;QACT,QAAQ;QACR,GAAG;KACJ,CAAC;AACJ,CAAC","sourcesContent":["import React, {\n createContext,\n FC,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport { TamboTool } from \"../model/component-metadata\";\nimport {\n getMcpServerUniqueKey,\n type NormalizedMcpServerInfo,\n} from \"../model/mcp-server-info\";\nimport { useTamboMcpToken } from \"../providers/tambo-mcp-token-provider\";\nimport {\n useTamboMcpServerInfos,\n useTamboRegistry,\n} from \"../providers/tambo-registry-provider\";\nimport { isContentPartArray, toText } from \"../util/content-parts\";\nimport { type ElicitationContextState, useElicitation } from \"./elicitation\";\nimport {\n MCPClient,\n MCPElicitationHandler,\n MCPHandlers,\n MCPSamplingHandler,\n MCPTransport,\n} from \"./mcp-client\";\n\n/**\n * Extracts error message from MCP tool result content.\n * Handles both array and string content formats.\n * Always returns a string, even for invalid/null inputs.\n * @returns The extracted error message as a string\n */\nexport function extractErrorMessage(content: unknown): string {\n if (content === undefined || content === null) {\n return \"Unknown error occurred\";\n }\n\n if (Array.isArray(content)) {\n const textItems = content\n .filter((item) => item?.type === \"text\" && typeof item.text === \"string\")\n .map((item) => item.text);\n\n return textItems.length > 0\n ? textItems.join(\" \")\n : \"Error occurred but no details provided\";\n }\n\n if (typeof content === \"object\") {\n return JSON.stringify(content);\n }\n\n return `${content}`;\n}\n\n/**\n * Normalized MCP server information as consumed by the provider.\n *\n * Extends `NormalizedMcpServerInfo` from the core model by:\n * - narrowing `handlers` to `Partial<MCPHandlers>`\n * - adding a stable `key` derived from URL/transport/headers\n *\n * The registry is responsible for producing `NormalizedMcpServerInfo`\n * instances; this type adds the MCP-specific wiring needed to connect and\n * track clients.\n */\ninterface McpServerConfig extends NormalizedMcpServerInfo {\n /**\n * Optional handlers for elicitation and sampling requests from the server.\n * Interpreted as a partial set of MCP handlers.\n */\n handlers?: Partial<MCPHandlers>;\n /**\n * Stable identity for this server derived from its URL/transport/headers.\n * Present for all server states (connected or failed).\n */\n key: string;\n}\n\n/**\n * Connected MCP server with an active client.\n */\nexport interface ConnectedMcpServer extends McpServerConfig {\n client: MCPClient;\n}\n\n/**\n * Failed MCP server with a connection error.\n */\nexport interface FailedMcpServer extends McpServerConfig {\n client?: never;\n connectionError: Error;\n}\n\n/**\n * An active or failed MCP server, with access to the MCP client.\n */\nexport type McpServer = ConnectedMcpServer | FailedMcpServer;\n\n/**\n * Provider-level MCP handlers that receive the McpServerInfo as context in addition to the request.\n * These handlers are applied to all MCP servers unless overridden by per-server handlers.\n *\n * Handlers receive three parameters:\n * 1. request - The MCP request\n * 2. extra - RequestHandlerExtra containing AbortSignal and other metadata\n * 3. serverInfo - Configuration of the MCP server that triggered this request\n */\nexport interface ProviderMCPHandlers {\n elicitation?: (\n request: Parameters<MCPElicitationHandler>[0],\n extra: Parameters<MCPElicitationHandler>[1],\n serverInfo: McpServerConfig,\n ) => ReturnType<MCPElicitationHandler>;\n sampling?: (\n request: Parameters<MCPSamplingHandler>[0],\n extra: Parameters<MCPSamplingHandler>[1],\n serverInfo: McpServerConfig,\n ) => ReturnType<MCPSamplingHandler>;\n}\n\n/**\n * Context value for MCP provider including server list and elicitation state\n */\ninterface McpProviderContextValue extends ElicitationContextState {\n servers: McpServer[];\n}\n\nconst McpProviderContext = createContext<McpProviderContextValue>({\n servers: [],\n elicitation: null,\n resolveElicitation: null,\n});\n\n// Constant for the internal Tambo MCP server name\nconst TAMBO_INTERNAL_MCP_SERVER_NAME = \"__tambo_internal_mcp_server__\";\n\n/**\n * This provider is used to register tools from MCP servers.\n * It automatically includes an internal Tambo MCP server when an MCP access token is available.\n *\n * **BREAKING CHANGE**: This provider no longer accepts `mcpServers` as a prop.\n * Instead, pass `mcpServers` to `TamboProvider` or `TamboRegistryProvider`.\n * This provider must be wrapped inside `TamboProvider` to access the MCP server registry.\n * @param props - The provider props\n * @param props.handlers - Optional handlers applied to all MCP servers unless overridden per-server\n * @param props.children - The children to wrap\n * @returns The TamboMcpProvider component\n */\nexport const TamboMcpProvider: FC<{\n handlers?: ProviderMCPHandlers;\n children: React.ReactNode;\n}> = ({ handlers, children }) => {\n const { registerTool } = useTamboRegistry();\n const { mcpAccessToken, tamboBaseUrl } = useTamboMcpToken();\n const mcpServers = useTamboMcpServerInfos();\n const providerSamplingHandler = handlers?.sampling;\n\n // Elicitation state and default handler\n const { elicitation, resolveElicitation, defaultElicitationHandler } =\n useElicitation();\n\n // Use provided handler or fall back to default\n const providerElicitationHandler =\n handlers?.elicitation ?? defaultElicitationHandler;\n\n // Stable reference to track active clients by server key\n const clientMapRef = useRef<Map<string, McpServer>>(new Map());\n // Track tool ownership to prevent duplicate registrations across servers\n // toolOwnerRef: tool name -> server key; keyToToolsRef: server key -> set of tool names\n const toolOwnerRef = useRef<Map<string, string>>(new Map());\n const keyToToolsRef = useRef<Map<string, Set<string>>>(new Map());\n\n // State for exposing connected servers to consumers\n const [connectedMcpServers, setConnectedMcpServers] = useState<McpServer[]>(\n [],\n );\n\n // Stable map of current server configurations keyed by server key\n const currentServersMap = useMemo(() => {\n const servers = [...mcpServers];\n\n // Add internal Tambo MCP server if we have an access token and a base URL\n if (mcpAccessToken && tamboBaseUrl) {\n const base = new URL(tamboBaseUrl);\n base.pathname = `${base.pathname.replace(/\\/+$/, \"\")}/mcp`;\n const tamboMcpUrl = base.toString();\n servers.push({\n name: TAMBO_INTERNAL_MCP_SERVER_NAME,\n url: tamboMcpUrl,\n transport: MCPTransport.HTTP,\n serverKey: \"tambo\", // Internal server always uses 'tambo' as serverKey\n customHeaders: {\n Authorization: `Bearer ${mcpAccessToken}`,\n },\n });\n }\n\n // Create a map of server key -> server info for efficient lookups\n const serverMap = new Map<string, McpServerConfig>();\n servers.forEach((server) => {\n const serverInfo = normalizeServerInfo(server);\n // Store without cloning to avoid unnecessary allocation\n serverMap.set(serverInfo.key, serverInfo);\n });\n\n return serverMap;\n }, [mcpServers, mcpAccessToken, tamboBaseUrl]);\n\n // Main effect: manage client lifecycle (create/remove)\n useEffect(() => {\n const clientMap = clientMapRef.current;\n const currentKeys = new Set(currentServersMap.keys());\n const existingKeys = new Set(clientMap.keys());\n\n // 1. Remove clients that are no longer in the current server list\n const keysToRemove = Array.from(existingKeys).filter(\n (key) => !currentKeys.has(key),\n );\n keysToRemove.forEach((key) => {\n const server = clientMap.get(key);\n if (server?.client) {\n try {\n server.client.close();\n } catch (error) {\n // Avoid logging sensitive data embedded in the key (headers)\n const url = (server as McpServer).url ?? \"(unknown url)\";\n console.error(`Error closing MCP client for ${url}:`, error);\n }\n }\n // Release tool ownership for this server\n const owned = keyToToolsRef.current.get(key);\n if (owned) {\n for (const name of owned) toolOwnerRef.current.delete(name);\n keyToToolsRef.current.delete(key);\n }\n clientMap.delete(key);\n });\n\n // 2. Add new clients for servers that don't exist yet\n const keysToAdd = Array.from(currentKeys).filter(\n (key) => !existingKeys.has(key),\n );\n\n if (keysToAdd.length > 0) {\n // Async initialization of new clients\n Promise.allSettled(\n keysToAdd.map(async (key) => {\n const serverInfo = currentServersMap.get(key)!;\n\n try {\n // Build effective handlers (per-server overrides provider)\n const effectiveHandlers: Partial<MCPHandlers> = {};\n\n if (serverInfo.handlers?.elicitation) {\n effectiveHandlers.elicitation = serverInfo.handlers.elicitation;\n } else if (providerElicitationHandler) {\n effectiveHandlers.elicitation = async (\n request: Parameters<MCPElicitationHandler>[0],\n extra: Parameters<MCPElicitationHandler>[1],\n ) => await providerElicitationHandler(request, extra, serverInfo);\n }\n\n if (serverInfo.handlers?.sampling) {\n effectiveHandlers.sampling = serverInfo.handlers.sampling;\n } else if (providerSamplingHandler) {\n effectiveHandlers.sampling = async (\n request: Parameters<MCPSamplingHandler>[0],\n extra: Parameters<MCPSamplingHandler>[1],\n ) => await providerSamplingHandler(request, extra, serverInfo);\n }\n\n const client = await MCPClient.create(\n serverInfo.url,\n serverInfo.transport,\n serverInfo.customHeaders,\n undefined,\n undefined,\n effectiveHandlers,\n );\n\n const connectedServer: ConnectedMcpServer = {\n ...serverInfo,\n client,\n };\n\n clientMap.set(key, connectedServer);\n setConnectedMcpServers(Array.from(clientMap.values()));\n\n // Register tools from this server (deduplicated by ownership)\n try {\n const tools = await client.listTools();\n const shouldPrefix = currentServersMap.size > 1;\n\n tools.forEach((tool) => {\n // Prefix tool name with serverKey if multiple servers are present\n const toolName = shouldPrefix\n ? `${serverInfo.serverKey}__${tool.name}`\n : tool.name;\n\n // Skip if another server already owns this tool (using final name for ownership)\n const currentOwner = toolOwnerRef.current.get(toolName);\n if (currentOwner && currentOwner !== key) {\n return;\n }\n\n // Record ownership for this server key (using final name)\n if (!currentOwner) {\n toolOwnerRef.current.set(toolName, key);\n if (!keyToToolsRef.current.has(key)) {\n keyToToolsRef.current.set(key, new Set());\n }\n keyToToolsRef.current.get(key)!.add(toolName);\n }\n\n registerTool({\n description: tool.description ?? \"\",\n name: toolName,\n tool: async (args: Record<string, unknown> = {}) => {\n const server = clientMap.get(key);\n if (!server?.client) {\n throw new Error(\n `MCP server for tool ${tool.name} is not connected`,\n );\n }\n const result = await server.client.callTool(\n tool.name,\n args,\n );\n if (result.isError) {\n const errorMessage = extractErrorMessage(result.content);\n throw new Error(errorMessage);\n }\n return result.content;\n },\n toolSchema: tool.inputSchema as TamboTool[\"toolSchema\"],\n transformToContent: (content: unknown) => {\n if (isContentPartArray(content)) {\n return content;\n }\n return [{ type: \"text\", text: toText(content) }];\n },\n });\n });\n } catch (error) {\n console.error(\n `Failed to register tools from MCP server ${serverInfo.url}:`,\n error,\n );\n }\n } catch (error) {\n const failedServer: FailedMcpServer = {\n ...serverInfo,\n connectionError: error as Error,\n };\n clientMap.set(key, failedServer);\n setConnectedMcpServers(Array.from(clientMap.values()));\n console.error(\n `Failed to connect to MCP server ${serverInfo.url}:`,\n error,\n );\n }\n }),\n );\n }\n\n // Update state after removals (additions update state asynchronously above)\n if (keysToRemove.length > 0) {\n setConnectedMcpServers(Array.from(clientMap.values()));\n }\n }, [\n currentServersMap,\n providerElicitationHandler,\n providerSamplingHandler,\n registerTool,\n ]);\n\n // Update handlers when they change (without recreating clients)\n useEffect(() => {\n const clientMap = clientMapRef.current;\n\n clientMap.forEach((server, key) => {\n if (!server.client) {\n return; // Skip failed servers\n }\n\n const serverInfo = currentServersMap.get(key);\n if (!serverInfo) {\n return; // Server was removed, handled by main effect\n }\n\n // Determine effective handlers\n const effectiveElicitationHandler =\n serverInfo.handlers?.elicitation ??\n (providerElicitationHandler\n ? async (\n request: Parameters<MCPElicitationHandler>[0],\n extra: Parameters<MCPElicitationHandler>[1],\n ) => await providerElicitationHandler(request, extra, serverInfo)\n : undefined);\n\n const effectiveSamplingHandler =\n serverInfo.handlers?.sampling ??\n (providerSamplingHandler\n ? async (\n request: Parameters<MCPSamplingHandler>[0],\n extra: Parameters<MCPSamplingHandler>[1],\n ) => await providerSamplingHandler(request, extra, serverInfo)\n : undefined);\n\n // Update handlers unconditionally (allows removal by passing undefined)\n server.client.updateElicitationHandler?.(effectiveElicitationHandler);\n server.client.updateSamplingHandler?.(effectiveSamplingHandler);\n });\n }, [currentServersMap, providerElicitationHandler, providerSamplingHandler]);\n\n // Cleanup on unmount: close all clients\n useEffect(() => {\n const clientMap = clientMapRef.current;\n const ownerMapAtMount = toolOwnerRef.current;\n const keyToToolsAtMount = keyToToolsRef.current;\n return () => {\n clientMap.forEach((server, _key) => {\n if (server.client) {\n try {\n server.client.close();\n } catch (error) {\n const url = (server as McpServer).url ?? \"(unknown url)\";\n console.error(\n `Error closing MCP client on unmount for ${url}:`,\n error,\n );\n }\n }\n });\n clientMap.clear();\n ownerMapAtMount.clear();\n keyToToolsAtMount.clear();\n };\n }, []);\n\n const contextValue = useMemo(\n () => ({\n servers: connectedMcpServers,\n elicitation,\n resolveElicitation,\n }),\n [connectedMcpServers, elicitation, resolveElicitation],\n );\n\n return (\n <McpProviderContext.Provider value={contextValue}>\n {children}\n </McpProviderContext.Provider>\n );\n};\n\n/**\n * Hook to access the actual MCP servers, as they are connected (or fail to\n * connect).\n *\n * You can call methods on the MCP client that is included in the MCP server\n * object.\n *\n * If the server fails to connect, the `client` property will be `undefined` and\n * the `connectionError` property will be set.\n *\n * For example, to forcibly disconnect and reconnect all MCP servers:\n *\n * ```tsx\n * const mcpServers = useTamboMcpServers();\n * mcpServers.forEach((mcpServer) => {\n * mcpServer.client?.reconnect();\n * });\n * ```\n *\n * Note that the MCP servers are not guaranteed to be in the same order as the\n * input array, because they are added as they are connected.\n * @returns The MCP servers\n */\nexport const useTamboMcpServers = () => {\n return useContext(McpProviderContext).servers;\n};\n\n/**\n * Hook to access MCP elicitation state from TamboMcpProvider.\n * This provides access to the current elicitation request and methods to respond to it.\n *\n * The elicitation state is automatically managed by TamboMcpProvider when MCP servers\n * request user input through the elicitation protocol.\n * @returns The elicitation state with current request and response handler\n * @example\n * ```tsx\n * function ElicitationUI() {\n * const { elicitation, resolveElicitation } = useTamboMcpElicitation();\n *\n * if (!elicitation) return null;\n *\n * return (\n * <div>\n * <p>{elicitation.message}</p>\n * <button onClick={() => resolveElicitation?.({ action: \"accept\", content: {} })}>\n * Accept\n * </button>\n * </div>\n * );\n * }\n * ```\n */\nexport const useTamboMcpElicitation = (): ElicitationContextState => {\n const context = useContext(McpProviderContext);\n return {\n elicitation: context.elicitation,\n resolveElicitation: context.resolveElicitation,\n };\n};\n\n/**\n * @deprecated Use `useTamboMcpElicitation` instead.\n * This hook will be removed in a future version.\n */\nexport const useTamboElicitationContext = useTamboMcpElicitation;\n\n/**\n * Normalizes registry server metadata into a `McpServerConfig`.\n *\n * Accepts a `NormalizedMcpServerInfo`, which already guarantees a concrete\n * `transport` and a `serverKey` derived by the registry, and narrows the\n * opaque `handlers` field to `Partial<MCPHandlers>`.\n */\nfunction normalizeServerInfo(server: NormalizedMcpServerInfo): McpServerConfig {\n const key = getMcpServerUniqueKey(server);\n // Cast handlers to proper type if present\n const handlers = server.handlers as Partial<MCPHandlers> | undefined;\n return {\n ...server,\n handlers,\n key,\n };\n}\n"]}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transport protocol to use for MCP connections.
|
|
3
|
+
*/
|
|
4
|
+
export declare enum MCPTransport {
|
|
5
|
+
SSE = "sse",
|
|
6
|
+
HTTP = "http"
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* User-provided configuration for an MCP server.
|
|
10
|
+
*
|
|
11
|
+
* This is the type accepted by `TamboProvider` / `TamboRegistryProvider` in
|
|
12
|
+
* the `mcpServers` prop.
|
|
13
|
+
*
|
|
14
|
+
* The `handlers` field is intentionally typed as `unknown` here so the core
|
|
15
|
+
* SDK does not depend on the MCP subpackage. In the `@tambo-ai/react/mcp`
|
|
16
|
+
* subpackage this is treated as `Partial<MCPHandlers>` (with
|
|
17
|
+
* `elicitation` / `sampling` callbacks).
|
|
18
|
+
*/
|
|
19
|
+
export interface McpServerInfo {
|
|
20
|
+
/** Optional name for the MCP server */
|
|
21
|
+
name?: string;
|
|
22
|
+
/** The URL of the MCP server to connect to */
|
|
23
|
+
url: string;
|
|
24
|
+
/** Optional description of the MCP server */
|
|
25
|
+
description?: string;
|
|
26
|
+
/** The transport type to use (SSE or HTTP). Defaults to HTTP for string URLs */
|
|
27
|
+
transport?: MCPTransport;
|
|
28
|
+
/** Optional custom headers to include in requests */
|
|
29
|
+
customHeaders?: Record<string, string>;
|
|
30
|
+
/**
|
|
31
|
+
* Optional short name for namespacing MCP resources, prompts, and tools.
|
|
32
|
+
* When multiple MCP servers are configured, this key is used to prefix:
|
|
33
|
+
* - prompts: `<serverKey>:<promptName>`
|
|
34
|
+
* - resources: `<serverKey>:<resourceUrl>`
|
|
35
|
+
* - tools: `<serverKey>__<toolName>`
|
|
36
|
+
*
|
|
37
|
+
* If not provided, a key will be derived from the URL hostname.
|
|
38
|
+
* For example, "https://mcp.linear.app/mcp" becomes "linear".
|
|
39
|
+
*/
|
|
40
|
+
serverKey?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Optional handlers for elicitation and sampling requests from the server.
|
|
43
|
+
*
|
|
44
|
+
* In the MCP subpackage this is interpreted as `Partial<MCPHandlers>`,
|
|
45
|
+
* i.e. `{ elicitation?: MCPElicitationHandler; sampling?: MCPSamplingHandler }`.
|
|
46
|
+
*
|
|
47
|
+
* Note: These callbacks should be stable (e.g., wrapped in useCallback or
|
|
48
|
+
* defined outside the component) to avoid constant re-registration of the
|
|
49
|
+
* MCP server on every render.
|
|
50
|
+
*/
|
|
51
|
+
handlers?: unknown;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Normalized MCP server metadata used internally by the registry and MCP
|
|
55
|
+
* provider.
|
|
56
|
+
*
|
|
57
|
+
* This is equivalent to `McpServerInfo` except that:
|
|
58
|
+
* - `serverKey` is guaranteed to be present
|
|
59
|
+
* - `transport` is resolved to a concrete value (defaults to HTTP)
|
|
60
|
+
*/
|
|
61
|
+
export interface NormalizedMcpServerInfo extends McpServerInfo {
|
|
62
|
+
transport: MCPTransport;
|
|
63
|
+
serverKey: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates a stable identifier for an MCP server based on its connection properties.
|
|
67
|
+
* Two servers with the same URL, transport, and headers will have the same key.
|
|
68
|
+
*
|
|
69
|
+
* This is used by both the registry and MCP provider to deduplicate servers,
|
|
70
|
+
* so it lives alongside the shared server metadata type.
|
|
71
|
+
* @returns A stable string key identifying the server
|
|
72
|
+
*/
|
|
73
|
+
export declare function getMcpServerUniqueKey(serverInfo: Pick<McpServerInfo, "url" | "transport" | "customHeaders">): string;
|
|
74
|
+
//# sourceMappingURL=mcp-server-info.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-server-info.d.ts","sourceRoot":"","sources":["../../src/model/mcp-server-info.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,YAAY;IACtB,GAAG,QAAQ;IACX,IAAI,SAAS;CACd;AAED;;;;;;;;;;GAUG;AACH,MAAM,WAAW,aAAa;IAC5B,uCAAuC;IACvC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8CAA8C;IAC9C,GAAG,EAAE,MAAM,CAAC;IACZ,6CAA6C;IAC7C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gFAAgF;IAChF,SAAS,CAAC,EAAE,YAAY,CAAC;IACzB,qDAAqD;IACrD,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvC;;;;;;;;;OASG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;;;;OASG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,uBAAwB,SAAQ,aAAa;IAC5D,SAAS,EAAE,YAAY,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,IAAI,CAAC,aAAa,EAAE,KAAK,GAAG,WAAW,GAAG,eAAe,CAAC,GACrE,MAAM,CAUR"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transport protocol to use for MCP connections.
|
|
3
|
+
*/
|
|
4
|
+
export var MCPTransport;
|
|
5
|
+
(function (MCPTransport) {
|
|
6
|
+
MCPTransport["SSE"] = "sse";
|
|
7
|
+
MCPTransport["HTTP"] = "http";
|
|
8
|
+
})(MCPTransport || (MCPTransport = {}));
|
|
9
|
+
/**
|
|
10
|
+
* Creates a stable identifier for an MCP server based on its connection properties.
|
|
11
|
+
* Two servers with the same URL, transport, and headers will have the same key.
|
|
12
|
+
*
|
|
13
|
+
* This is used by both the registry and MCP provider to deduplicate servers,
|
|
14
|
+
* so it lives alongside the shared server metadata type.
|
|
15
|
+
* @returns A stable string key identifying the server
|
|
16
|
+
*/
|
|
17
|
+
export function getMcpServerUniqueKey(serverInfo) {
|
|
18
|
+
const headerStr = serverInfo.customHeaders
|
|
19
|
+
? JSON.stringify(Object.entries(serverInfo.customHeaders)
|
|
20
|
+
.map(([k, v]) => [k.toLowerCase(), v])
|
|
21
|
+
.sort(([a], [b]) => a.localeCompare(b)))
|
|
22
|
+
: "";
|
|
23
|
+
return `${serverInfo.url}|${serverInfo.transport ?? MCPTransport.HTTP}|${headerStr}`;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=mcp-server-info.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-server-info.js","sourceRoot":"","sources":["../../src/model/mcp-server-info.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAN,IAAY,YAGX;AAHD,WAAY,YAAY;IACtB,2BAAW,CAAA;IACX,6BAAa,CAAA;AACf,CAAC,EAHW,YAAY,KAAZ,YAAY,QAGvB;AA6DD;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CACnC,UAAsE;IAEtE,MAAM,SAAS,GAAG,UAAU,CAAC,aAAa;QACxC,CAAC,CAAC,IAAI,CAAC,SAAS,CACZ,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC;aACrC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAU,CAAC;aAC9C,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAC1C;QACH,CAAC,CAAC,EAAE,CAAC;IAEP,OAAO,GAAG,UAAU,CAAC,GAAG,IAAI,UAAU,CAAC,SAAS,IAAI,YAAY,CAAC,IAAI,IAAI,SAAS,EAAE,CAAC;AACvF,CAAC","sourcesContent":["/**\n * The transport protocol to use for MCP connections.\n */\nexport enum MCPTransport {\n SSE = \"sse\",\n HTTP = \"http\",\n}\n\n/**\n * User-provided configuration for an MCP server.\n *\n * This is the type accepted by `TamboProvider` / `TamboRegistryProvider` in\n * the `mcpServers` prop.\n *\n * The `handlers` field is intentionally typed as `unknown` here so the core\n * SDK does not depend on the MCP subpackage. In the `@tambo-ai/react/mcp`\n * subpackage this is treated as `Partial<MCPHandlers>` (with\n * `elicitation` / `sampling` callbacks).\n */\nexport interface McpServerInfo {\n /** Optional name for the MCP server */\n name?: string;\n /** The URL of the MCP server to connect to */\n url: string;\n /** Optional description of the MCP server */\n description?: string;\n /** The transport type to use (SSE or HTTP). Defaults to HTTP for string URLs */\n transport?: MCPTransport;\n /** Optional custom headers to include in requests */\n customHeaders?: Record<string, string>;\n /**\n * Optional short name for namespacing MCP resources, prompts, and tools.\n * When multiple MCP servers are configured, this key is used to prefix:\n * - prompts: `<serverKey>:<promptName>`\n * - resources: `<serverKey>:<resourceUrl>`\n * - tools: `<serverKey>__<toolName>`\n *\n * If not provided, a key will be derived from the URL hostname.\n * For example, \"https://mcp.linear.app/mcp\" becomes \"linear\".\n */\n serverKey?: string;\n /**\n * Optional handlers for elicitation and sampling requests from the server.\n *\n * In the MCP subpackage this is interpreted as `Partial<MCPHandlers>`,\n * i.e. `{ elicitation?: MCPElicitationHandler; sampling?: MCPSamplingHandler }`.\n *\n * Note: These callbacks should be stable (e.g., wrapped in useCallback or\n * defined outside the component) to avoid constant re-registration of the\n * MCP server on every render.\n */\n handlers?: unknown;\n}\n\n/**\n * Normalized MCP server metadata used internally by the registry and MCP\n * provider.\n *\n * This is equivalent to `McpServerInfo` except that:\n * - `serverKey` is guaranteed to be present\n * - `transport` is resolved to a concrete value (defaults to HTTP)\n */\nexport interface NormalizedMcpServerInfo extends McpServerInfo {\n transport: MCPTransport;\n serverKey: string;\n}\n\n/**\n * Creates a stable identifier for an MCP server based on its connection properties.\n * Two servers with the same URL, transport, and headers will have the same key.\n *\n * This is used by both the registry and MCP provider to deduplicate servers,\n * so it lives alongside the shared server metadata type.\n * @returns A stable string key identifying the server\n */\nexport function getMcpServerUniqueKey(\n serverInfo: Pick<McpServerInfo, \"url\" | \"transport\" | \"customHeaders\">,\n): string {\n const headerStr = serverInfo.customHeaders\n ? JSON.stringify(\n Object.entries(serverInfo.customHeaders)\n .map(([k, v]) => [k.toLowerCase(), v] as const)\n .sort(([a], [b]) => a.localeCompare(b)),\n )\n : \"\";\n\n return `${serverInfo.url}|${serverInfo.transport ?? MCPTransport.HTTP}|${headerStr}`;\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import { advanceStream } from "@tambo-ai/typescript-sdk";
|
|
|
2
2
|
import { act, renderHook } from "@testing-library/react";
|
|
3
3
|
import React from "react";
|
|
4
4
|
import { GenerationStage, } from "../../model/generate-component-response";
|
|
5
|
-
import { useTamboClient, useTamboQueryClient } from "../tambo-client-provider";
|
|
5
|
+
import { TamboClientContext, useTamboClient, useTamboQueryClient, } from "../tambo-client-provider";
|
|
6
6
|
import { TamboContextHelpersProvider } from "../tambo-context-helpers-provider";
|
|
7
7
|
import { TamboMcpTokenProvider } from "../tambo-mcp-token-provider";
|
|
8
8
|
import { TamboRegistryProvider } from "../tambo-registry-provider";
|
|
@@ -14,10 +14,13 @@ Object.defineProperty(global, "crypto", {
|
|
|
14
14
|
},
|
|
15
15
|
});
|
|
16
16
|
// Mock the required providers
|
|
17
|
-
jest.mock("../tambo-client-provider", () =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
jest.mock("../tambo-client-provider", () => {
|
|
18
|
+
return {
|
|
19
|
+
useTamboClient: jest.fn(),
|
|
20
|
+
useTamboQueryClient: jest.fn(),
|
|
21
|
+
TamboClientContext: React.createContext(undefined),
|
|
22
|
+
};
|
|
23
|
+
});
|
|
21
24
|
jest.mock("@tambo-ai/typescript-sdk", () => ({
|
|
22
25
|
advanceStream: jest.fn(),
|
|
23
26
|
}));
|
|
@@ -33,10 +36,21 @@ const createMockMessage = (overrides = {}) => ({
|
|
|
33
36
|
});
|
|
34
37
|
// Test wrapper
|
|
35
38
|
const createWrapper = (initialMessages = []) => {
|
|
36
|
-
const TestWrapper = ({ children }) =>
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
const TestWrapper = ({ children }) => {
|
|
40
|
+
const client = useTamboClient();
|
|
41
|
+
const queryClient = useTamboQueryClient();
|
|
42
|
+
return (React.createElement(TamboClientContext.Provider, { value: {
|
|
43
|
+
client,
|
|
44
|
+
queryClient,
|
|
45
|
+
isUpdatingToken: false,
|
|
46
|
+
mcpAccessToken: null,
|
|
47
|
+
setMcpAccessToken: () => { },
|
|
48
|
+
} },
|
|
49
|
+
React.createElement(TamboRegistryProvider, { components: [], tools: [] },
|
|
50
|
+
React.createElement(TamboContextHelpersProvider, null,
|
|
51
|
+
React.createElement(TamboMcpTokenProvider, null,
|
|
52
|
+
React.createElement(TamboThreadProvider, { initialMessages: initialMessages, autoGenerateThreadName: false }, children))))));
|
|
53
|
+
};
|
|
40
54
|
TestWrapper.displayName = "TestWrapper";
|
|
41
55
|
return TestWrapper;
|
|
42
56
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-thread-provider-initial-messages.test.js","sourceRoot":"","sources":["../../../src/providers/__tests__/tambo-thread-provider-initial-messages.test.tsx"],"names":[],"mappings":"AAAA,OAAgB,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EACL,eAAe,GAEhB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CA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AAG,CAAC,GAAG,EAAE;YACP,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,oBAAoB,EAAE,KAAK,CAAC,CAAC;QAClE,CAAC,CAAC,CAAC;QAEH,qBAAqB;QACrB,GAAG,CAAC,GAAG,EAAE;YACP,MAAM,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;QAClC,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;QACvD,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAC5D,8BAA8B,CAC/B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import TamboAI, { advanceStream } from \"@tambo-ai/typescript-sdk\";\nimport { act, renderHook } from \"@testing-library/react\";\nimport React from \"react\";\nimport { DeepPartial } from \"ts-essentials\";\nimport {\n GenerationStage,\n TamboThreadMessage,\n} from \"../../model/generate-component-response\";\nimport { useTamboClient, useTamboQueryClient } from \"../tambo-client-provider\";\nimport { TamboContextHelpersProvider } from \"../tambo-context-helpers-provider\";\nimport { TamboMcpTokenProvider } from \"../tambo-mcp-token-provider\";\nimport { TamboRegistryProvider } from \"../tambo-registry-provider\";\nimport { TamboThreadProvider, useTamboThread } from \"../tambo-thread-provider\";\n\ntype PartialTamboAI = DeepPartial<TamboAI>;\n\n// Mock crypto.randomUUID\nObject.defineProperty(global, \"crypto\", {\n value: {\n randomUUID: jest.fn().mockReturnValue(\"test-uuid\"),\n },\n});\n\n// Mock the required providers\njest.mock(\"../tambo-client-provider\", () => ({\n useTamboClient: jest.fn(),\n useTamboQueryClient: jest.fn(),\n}));\njest.mock(\"@tambo-ai/typescript-sdk\", () => ({\n advanceStream: jest.fn(),\n}));\n\n// Test utilities\nconst createMockMessage = (\n overrides: Partial<TamboThreadMessage> = {},\n): TamboThreadMessage => ({\n id: \"test-message-1\",\n content: [{ type: \"text\", text: \"Hello\" }],\n role: \"user\",\n threadId: \"test-thread-1\",\n createdAt: new Date().toISOString(),\n componentState: {},\n ...overrides,\n});\n\n// Test wrapper\nconst createWrapper = (initialMessages: TamboThreadMessage[] = []) => {\n const TestWrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboRegistryProvider components={[]} tools={[]}>\n <TamboContextHelpersProvider>\n <TamboMcpTokenProvider>\n <TamboThreadProvider\n initialMessages={initialMessages}\n autoGenerateThreadName={false}\n >\n {children}\n </TamboThreadProvider>\n </TamboMcpTokenProvider>\n </TamboContextHelpersProvider>\n </TamboRegistryProvider>\n );\n TestWrapper.displayName = \"TestWrapper\";\n return TestWrapper;\n};\n\ndescribe(\"TamboThreadProvider with initial messages\", () => {\n const mockClient: PartialTamboAI = {\n beta: {\n threads: {\n advance: jest.fn(),\n advanceByID: jest.fn(),\n cancel: jest.fn(),\n messages: {\n create: jest.fn(),\n },\n },\n },\n };\n\n beforeEach(() => {\n jest.clearAllMocks();\n (useTamboClient as jest.Mock).mockReturnValue(mockClient);\n // Provide a minimal mock for the query client used by the provider\n const mockQueryClient = {\n setQueryData: jest.fn(),\n invalidateQueries: jest.fn(),\n };\n (useTamboQueryClient as jest.Mock).mockReturnValue(mockQueryClient);\n (advanceStream as jest.Mock).mockImplementation(async function* () {\n yield {\n responseMessageDto: {\n id: \"response-1\",\n role: \"assistant\",\n content: [{ type: \"text\", text: \"Hello back!\" }],\n threadId: \"new-thread-id\",\n componentState: {},\n createdAt: new Date().toISOString(),\n },\n generationStage: GenerationStage.COMPLETE,\n };\n });\n });\n\n it(\"should initialize with empty messages when no initial messages provided\", () => {\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(),\n });\n\n expect(result.current.thread.messages).toEqual([]);\n });\n\n it(\"should initialize with provided initial messages\", () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n createMockMessage({\n id: \"initial-2\",\n role: \"user\",\n content: [{ type: \"text\", text: \"Hello!\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n expect(result.current.thread.messages).toHaveLength(2);\n expect(result.current.thread.messages[0].content[0].text).toBe(\n \"You are a helpful assistant.\",\n );\n expect(result.current.thread.messages[1].content[0].text).toBe(\"Hello!\");\n });\n\n it(\"should include initial messages when sending a message to a new thread\", async () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n await act(async () => {\n await result.current.sendThreadMessage(\"Test message\");\n });\n\n // Check that advanceStream was called with initial messages\n expect(advanceStream).toHaveBeenCalledWith(\n mockClient,\n expect.objectContaining({\n initialMessages: [\n {\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n role: \"system\",\n additionalContext: undefined,\n },\n ],\n }),\n undefined,\n );\n });\n\n it(\"should not include initial messages when sending to an existing thread\", async () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n // Switch to an existing thread first\n await act(async () => {\n result.current.switchCurrentThread(\"existing-thread-id\", false);\n });\n\n await act(async () => {\n await result.current.sendThreadMessage(\"Test message\");\n });\n\n // Check that advanceStream was called without initial messages\n expect(advanceStream).toHaveBeenCalledWith(\n mockClient,\n expect.not.objectContaining({\n initialMessages: expect.anything(),\n }),\n \"existing-thread-id\",\n );\n });\n\n it(\"should reset to initial messages when starting a new thread\", () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n // Switch to an existing thread\n act(() => {\n result.current.switchCurrentThread(\"existing-thread-id\", false);\n });\n\n // Start a new thread\n act(() => {\n result.current.startNewThread();\n });\n\n expect(result.current.thread.messages).toHaveLength(1);\n expect(result.current.thread.messages[0].content[0].text).toBe(\n \"You are a helpful assistant.\",\n );\n });\n});\n"]}
|
|
1
|
+
{"version":3,"file":"tambo-thread-provider-initial-messages.test.js","sourceRoot":"","sources":["../../../src/providers/__tests__/tambo-thread-provider-initial-messages.test.tsx"],"names":[],"mappings":"AAAA,OAAgB,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EACL,eAAe,GAEhB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EACL,kBAAkB,EAClB,cAAc,EACd,mBAAmB,GACpB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,2BAA2B,EAAE,MAAM,mCAAmC,CAAC;AAChF,OAAO,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAI/E,yBAAyB;AACzB,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE;IACtC,KAAK,EAAE;QACL,UAAU,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC,WAAW,CAAC;KACnD;CACF,CAAC,CAAC;AAEH,8BAA8B;AAC9B,IAAI,CAAC,IAA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,CAAC,CAAC;QAClE,CAAC,CAAC,CAAC;QAEH,qBAAqB;QACrB,GAAG,CAAC,GAAG,EAAE;YACP,MAAM,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;QAClC,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;QACvD,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAC5D,8BAA8B,CAC/B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import TamboAI, { advanceStream } from \"@tambo-ai/typescript-sdk\";\nimport { act, renderHook } from \"@testing-library/react\";\nimport React from \"react\";\nimport { DeepPartial } from \"ts-essentials\";\nimport {\n GenerationStage,\n TamboThreadMessage,\n} from \"../../model/generate-component-response\";\nimport {\n TamboClientContext,\n useTamboClient,\n useTamboQueryClient,\n} from \"../tambo-client-provider\";\nimport { TamboContextHelpersProvider } from \"../tambo-context-helpers-provider\";\nimport { TamboMcpTokenProvider } from \"../tambo-mcp-token-provider\";\nimport { TamboRegistryProvider } from \"../tambo-registry-provider\";\nimport { TamboThreadProvider, useTamboThread } from \"../tambo-thread-provider\";\n\ntype PartialTamboAI = DeepPartial<TamboAI>;\n\n// Mock crypto.randomUUID\nObject.defineProperty(global, \"crypto\", {\n value: {\n randomUUID: jest.fn().mockReturnValue(\"test-uuid\"),\n },\n});\n\n// Mock the required providers\njest.mock(\"../tambo-client-provider\", () => {\n return {\n useTamboClient: jest.fn(),\n useTamboQueryClient: jest.fn(),\n TamboClientContext: React.createContext(undefined),\n };\n});\njest.mock(\"@tambo-ai/typescript-sdk\", () => ({\n advanceStream: jest.fn(),\n}));\n\n// Test utilities\nconst createMockMessage = (\n overrides: Partial<TamboThreadMessage> = {},\n): TamboThreadMessage => ({\n id: \"test-message-1\",\n content: [{ type: \"text\", text: \"Hello\" }],\n role: \"user\",\n threadId: \"test-thread-1\",\n createdAt: new Date().toISOString(),\n componentState: {},\n ...overrides,\n});\n\n// Test wrapper\nconst createWrapper = (initialMessages: TamboThreadMessage[] = []) => {\n const TestWrapper = ({ children }: { children: React.ReactNode }) => {\n const client = useTamboClient();\n const queryClient = useTamboQueryClient();\n\n return (\n <TamboClientContext.Provider\n value={{\n client,\n queryClient,\n isUpdatingToken: false,\n mcpAccessToken: null,\n setMcpAccessToken: () => {},\n }}\n >\n <TamboRegistryProvider components={[]} tools={[]}>\n <TamboContextHelpersProvider>\n <TamboMcpTokenProvider>\n <TamboThreadProvider\n initialMessages={initialMessages}\n autoGenerateThreadName={false}\n >\n {children}\n </TamboThreadProvider>\n </TamboMcpTokenProvider>\n </TamboContextHelpersProvider>\n </TamboRegistryProvider>\n </TamboClientContext.Provider>\n );\n };\n TestWrapper.displayName = \"TestWrapper\";\n return TestWrapper;\n};\n\ndescribe(\"TamboThreadProvider with initial messages\", () => {\n const mockClient: PartialTamboAI = {\n beta: {\n threads: {\n advance: jest.fn(),\n advanceByID: jest.fn(),\n cancel: jest.fn(),\n messages: {\n create: jest.fn(),\n },\n },\n },\n };\n\n beforeEach(() => {\n jest.clearAllMocks();\n (useTamboClient as jest.Mock).mockReturnValue(mockClient);\n // Provide a minimal mock for the query client used by the provider\n const mockQueryClient = {\n setQueryData: jest.fn(),\n invalidateQueries: jest.fn(),\n };\n (useTamboQueryClient as jest.Mock).mockReturnValue(mockQueryClient);\n (advanceStream as jest.Mock).mockImplementation(async function* () {\n yield {\n responseMessageDto: {\n id: \"response-1\",\n role: \"assistant\",\n content: [{ type: \"text\", text: \"Hello back!\" }],\n threadId: \"new-thread-id\",\n componentState: {},\n createdAt: new Date().toISOString(),\n },\n generationStage: GenerationStage.COMPLETE,\n };\n });\n });\n\n it(\"should initialize with empty messages when no initial messages provided\", () => {\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(),\n });\n\n expect(result.current.thread.messages).toEqual([]);\n });\n\n it(\"should initialize with provided initial messages\", () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n createMockMessage({\n id: \"initial-2\",\n role: \"user\",\n content: [{ type: \"text\", text: \"Hello!\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n expect(result.current.thread.messages).toHaveLength(2);\n expect(result.current.thread.messages[0].content[0].text).toBe(\n \"You are a helpful assistant.\",\n );\n expect(result.current.thread.messages[1].content[0].text).toBe(\"Hello!\");\n });\n\n it(\"should include initial messages when sending a message to a new thread\", async () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n await act(async () => {\n await result.current.sendThreadMessage(\"Test message\");\n });\n\n // Check that advanceStream was called with initial messages\n expect(advanceStream).toHaveBeenCalledWith(\n mockClient,\n expect.objectContaining({\n initialMessages: [\n {\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n role: \"system\",\n additionalContext: undefined,\n },\n ],\n }),\n undefined,\n );\n });\n\n it(\"should not include initial messages when sending to an existing thread\", async () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n // Switch to an existing thread first\n await act(async () => {\n result.current.switchCurrentThread(\"existing-thread-id\", false);\n });\n\n await act(async () => {\n await result.current.sendThreadMessage(\"Test message\");\n });\n\n // Check that advanceStream was called without initial messages\n expect(advanceStream).toHaveBeenCalledWith(\n mockClient,\n expect.not.objectContaining({\n initialMessages: expect.anything(),\n }),\n \"existing-thread-id\",\n );\n });\n\n it(\"should reset to initial messages when starting a new thread\", () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n // Switch to an existing thread\n act(() => {\n result.current.switchCurrentThread(\"existing-thread-id\", false);\n });\n\n // Start a new thread\n act(() => {\n result.current.startNewThread();\n });\n\n expect(result.current.thread.messages).toHaveLength(1);\n expect(result.current.thread.messages[0].content[0].text).toBe(\n \"You are a helpful assistant.\",\n );\n });\n});\n"]}
|