@tambo-ai/react 0.63.0 → 0.64.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-voice.js.map +1 -1
- package/dist/mcp/__tests__/mcp-hooks.test.js +479 -16
- package/dist/mcp/__tests__/mcp-hooks.test.js.map +1 -1
- package/dist/mcp/__tests__/tambo-mcp-provider.test.js +156 -0
- package/dist/mcp/__tests__/tambo-mcp-provider.test.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 +3 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/mcp-hooks.d.ts +93 -3
- package/dist/mcp/mcp-hooks.d.ts.map +1 -1
- package/dist/mcp/mcp-hooks.js +111 -4
- package/dist/mcp/mcp-hooks.js.map +1 -1
- package/dist/mcp/tambo-mcp-provider.d.ts +16 -0
- package/dist/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/dist/mcp/tambo-mcp-provider.js +71 -7
- package/dist/mcp/tambo-mcp-provider.js.map +1 -1
- package/dist/providers/tambo-context-attachment-provider.js +2 -2
- package/dist/providers/tambo-context-attachment-provider.js.map +1 -1
- package/esm/hooks/use-tambo-voice.js.map +1 -1
- package/esm/mcp/__tests__/mcp-hooks.test.js +480 -17
- package/esm/mcp/__tests__/mcp-hooks.test.js.map +1 -1
- package/esm/mcp/__tests__/tambo-mcp-provider.test.js +156 -0
- package/esm/mcp/__tests__/tambo-mcp-provider.test.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-hooks.d.ts +93 -3
- package/esm/mcp/mcp-hooks.d.ts.map +1 -1
- package/esm/mcp/mcp-hooks.js +109 -4
- package/esm/mcp/mcp-hooks.js.map +1 -1
- package/esm/mcp/tambo-mcp-provider.d.ts +16 -0
- package/esm/mcp/tambo-mcp-provider.d.ts.map +1 -1
- package/esm/mcp/tambo-mcp-provider.js +71 -7
- package/esm/mcp/tambo-mcp-provider.js.map +1 -1
- package/esm/providers/tambo-context-attachment-provider.js +2 -2
- package/esm/providers/tambo-context-attachment-provider.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-hooks.js","sourceRoot":"","sources":["../../src/mcp/mcp-hooks.ts"],"names":[],"mappings":";;AAuBA,sDA0BC;AA8DD,8CAmCC;AA7ID,oCAA0D;AAC1D,6DAI8B;AAS9B;;;GAGG;AACH,SAAgB,qBAAqB;IACnC,MAAM,UAAU,GAAG,IAAA,uCAAkB,GAAE,CAAC;IACxC,MAAM,OAAO,GAAG,IAAA,uBAAe,EAAC;QAC9B,OAAO,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;YACtC,QAAQ,EAAE,CAAC,aAAa,EAAE,SAAS,CAAC,GAAG,CAAC;YACxC,oDAAoD;YACpD,OAAO,EAAE,oBAAoB,CAAC,SAAS,CAAC;YACxC,OAAO,EAAE,KAAK,IAAgC,EAAE;gBAC9C,6DAA6D;gBAC7D,IAAI,CAAC,oBAAoB,CAAC,SAAS,CAAC;oBAAE,OAAO,EAAE,CAAC;gBAEhD,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;gBAC3D,MAAM,OAAO,GAAqB,MAAM,EAAE,OAAO,IAAI,EAAE,CAAC;gBACxD,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;oBAC9C,MAAM,EAAE,SAAS;oBACjB,MAAM;iBACP,CAAC,CAAC,CAAC;gBACJ,OAAO,cAAc,CAAC;YACxB,CAAC;SACF,CAAC,CAAC;QACH,OAAO,EAAE,CAAC,OAAO,EAAE,EAAE;YACnB,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IAAA,qBAAa,EAAC;QACnB,sEAAsE;QACtE,QAAQ,EAAE,CAAC,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,EAAE,iBAAiB,CAAC;QACvE,6DAA6D;QAC7D,OAAO,EAAE,OAAO,CACd,UAAU,IAAI,SAAS,IAAI,oBAAoB,CAAC,SAAS,CAAC,CAC3D;QACD,OAAO,EAAE,KAAK,IAAqC,EAAE;YACnD,IAAI,CAAC,UAAU,IAAI,CAAC,SAAS,IAAI,CAAC,oBAAoB,CAAC,SAAS,CAAC,EAAE,CAAC;gBAClE,OAAO,IAAI,CAAC;YACd,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC;gBACrD,IAAI,EAAE,UAAU;gBAChB,SAAS,EAAE,IAAI;aAChB,CAAC,CAAC;YACH,OAAO,MAAM,IAAI,IAAI,CAAC,CAAC,iEAAiE;QAC1F,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n GetPromptResult,\n type ListPromptsResult,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { UseQueryResult } from \"@tanstack/react-query\";\nimport { useTamboQueries, useTamboQuery } from \"../hooks\";\nimport {\n type ConnectedMcpServer,\n type McpServer,\n useTamboMcpServers,\n} from \"./tambo-mcp-provider\";\n\nexport type ListPromptItem = ListPromptsResult[\"prompts\"][number];\nexport interface ListPromptEntry {\n // Only connected servers produce prompt entries, so expose the connected type\n server: ConnectedMcpServer;\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 const queries = useTamboQueries({\n queries: mcpServers.map((mcpServer) => ({\n queryKey: [\"mcp-prompts\", mcpServer.key],\n // Only run for connected servers that have a client\n enabled: isConnectedMcpServer(mcpServer),\n queryFn: async (): Promise<ListPromptEntry[]> => {\n // Fast path: if this server doesn't have a client, skip work\n if (!isConnectedMcpServer(mcpServer)) return [];\n\n const result = await mcpServer.client.client.listPrompts();\n const prompts: ListPromptItem[] = result?.prompts ?? [];\n const promptsEntries = prompts.map((prompt) => ({\n server: mcpServer,\n prompt,\n }));\n return promptsEntries;\n },\n })),\n combine: (results) => {\n return combineArrayResults(results);\n },\n });\n\n return queries;\n}\n// TODO: find a more general place for this\nfunction combineArrayResults<T>(results: UseQueryResult<T[], Error>[]): {\n data: T[];\n error: Error | null;\n errors: Error[];\n isPending: boolean;\n isSuccess: boolean;\n isError: boolean;\n isPaused: boolean;\n isRefetching: boolean;\n isFetching: boolean;\n isLoading: boolean;\n refetch: () => Promise<void>;\n} {\n const errors = results\n .filter((result) => result.isError)\n .map((result) => result.error as Error);\n\n // Treat queries that are idle (disabled) as non-blocking for aggregate status\n const enabledish = results.filter(\n (r) => r.fetchStatus !== \"idle\" || r.isSuccess || r.isError,\n );\n\n return {\n // Prefer flatMap to avoid extra intermediate arrays\n data: results.flatMap((result) =>\n result.isSuccess && Array.isArray(result.data) ? result.data : [],\n ),\n // Preserve a single error for compatibility and expose the full list for diagnostics\n error: errors[0] ?? null,\n errors,\n isPending: enabledish.some((result) => result.isPending),\n isSuccess:\n enabledish.length > 0 && enabledish.every((result) => result.isSuccess),\n isError: errors.length > 0,\n isPaused: enabledish.some((result) => result.isPaused),\n isRefetching: enabledish.some((result) => result.isRefetching),\n isFetching: enabledish.some((result) => result.isFetching),\n isLoading: enabledish.some((result) => result.isLoading),\n // Aggregate refetch to trigger all underlying queries\n refetch: async () => {\n await Promise.all(\n results.map(async (r) => {\n await r.refetch();\n }),\n );\n },\n };\n}\n\n// Type guard for narrowing to connected servers\nfunction isConnectedMcpServer(server: McpServer): server is ConnectedMcpServer {\n return \"client\" in server && server.client != null;\n}\n\n/**\n * Hook to get the prompt for the specified name.\n * @param promptName - The name of the prompt to get. If the prompt won't return anything\n * @param args - The arguments to pass to the prompt.\n * @returns The prompt for the specified name.\n */\nexport function useTamboMcpPrompt(\n promptName: string | undefined,\n args: Record<string, string> = {},\n) {\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 // Use the stable server key (and the server instance itself) instead of brittle\n // name/url/transport matching.\n const mcpServer = promptEntry?.server;\n\n // Canonicalize args to avoid unstable cache keys from object identity/order\n const sortedArgsEntries = Object.keys(args)\n .sort()\n .map((k) => [k, args[k]] as const);\n return useTamboQuery({\n // Include server identity and sorted args to prevent stale cache hits\n queryKey: [\"mcp-prompt\", promptName, mcpServer?.key, sortedArgsEntries],\n // Only run when we have a prompt name and a connected server\n enabled: Boolean(\n promptName && mcpServer && isConnectedMcpServer(mcpServer),\n ),\n queryFn: async (): Promise<GetPromptResult | null> => {\n if (!promptName || !mcpServer || !isConnectedMcpServer(mcpServer)) {\n return null;\n }\n const result = await mcpServer.client.client.getPrompt({\n name: promptName,\n arguments: args,\n });\n return result ?? null; // return null because react-query doesn't like undefined results\n },\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"mcp-hooks.js","sourceRoot":"","sources":["../../src/mcp/mcp-hooks.ts"],"names":[],"mappings":";;AAgCA,sDA6CC;AA8DD,8CA4CC;AAMD,0DA6CC;AAOD,kDAqCC;AA/QD,oCAA0D;AAC1D,6DAI8B;AAgB9B;;;GAGG;AACH,SAAgB,qBAAqB;IACnC,MAAM,UAAU,GAAG,IAAA,uCAAkB,GAAE,CAAC;IAExC,MAAM,OAAO,GAAG,IAAA,uBAAe,EAAC;QAC9B,OAAO,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;YACtC,QAAQ,EAAE,CAAC,aAAa,EAAE,SAAS,CAAC,GAAG,CAAC;YACxC,oDAAoD;YACpD,OAAO,EAAE,oBAAoB,CAAC,SAAS,CAAC;YACxC,OAAO,EAAE,KAAK,IAAgC,EAAE;gBAC9C,6DAA6D;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AAiE;IACjE,MAAM,mBAAmB,GAAG,aAAa;QACvC,CAAC,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC,MAAM,CAAC,SAAS,GAAG,EAAE,EAAE,CAAC;QAChE,CAAC,CAAC,WAAW,CAAC;IAEhB,OAAO,IAAA,qBAAa,EAAC;QACnB,sDAAsD;QACtD,QAAQ,EAAE,CAAC,cAAc,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,CAAC;QACvD,8DAA8D;QAC9D,OAAO,EAAE,OAAO,CACd,WAAW,IAAI,SAAS,IAAI,oBAAoB,CAAC,SAAS,CAAC,CAC5D;QACD,OAAO,EAAE,KAAK,IAAwC,EAAE;YACtD,IACE,CAAC,mBAAmB;gBACpB,CAAC,SAAS;gBACV,CAAC,oBAAoB,CAAC,SAAS,CAAC,EAChC,CAAC;gBACD,OAAO,IAAI,CAAC;YACd,CAAC;YACD,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC;gBACxD,GAAG,EAAE,mBAAmB;aACzB,CAAC,CAAC;YACH,OAAO,MAAM,IAAI,IAAI,CAAC,CAAC,iEAAiE;QAC1F,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n GetPromptResult,\n type ListPromptsResult,\n type ListResourcesResult,\n type ReadResourceResult,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { UseQueryResult } from \"@tanstack/react-query\";\nimport { useTamboQueries, useTamboQuery } from \"../hooks\";\nimport {\n type ConnectedMcpServer,\n type McpServer,\n useTamboMcpServers,\n} from \"./tambo-mcp-provider\";\n\nexport type ListPromptItem = ListPromptsResult[\"prompts\"][number];\nexport interface ListPromptEntry {\n // Only connected servers produce prompt entries, so expose the connected type\n server: ConnectedMcpServer;\n prompt: ListPromptItem;\n}\n\nexport type ListResourceItem = ListResourcesResult[\"resources\"][number];\nexport interface ListResourceEntry {\n // Only connected servers produce resource entries, so expose the connected type\n server: ConnectedMcpServer;\n resource: ListResourceItem;\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\n const queries = useTamboQueries({\n queries: mcpServers.map((mcpServer) => ({\n queryKey: [\"mcp-prompts\", mcpServer.key],\n // Only run for connected servers that have a client\n enabled: isConnectedMcpServer(mcpServer),\n queryFn: async (): Promise<ListPromptEntry[]> => {\n // Fast path: if this server doesn't have a client, skip work\n if (!isConnectedMcpServer(mcpServer)) return [];\n\n const result = await mcpServer.client.client.listPrompts();\n const prompts: ListPromptItem[] = result?.prompts ?? [];\n // Return prompts without prefixes - we'll apply prefixing in combine\n const promptsEntries = prompts.map((prompt) => ({\n server: mcpServer,\n prompt,\n }));\n return promptsEntries;\n },\n })),\n combine: (results) => {\n const combined = combineArrayResults(results);\n // Apply prefixing based on current server count\n const shouldPrefix = mcpServers.length > 1;\n if (!shouldPrefix) {\n return combined;\n }\n\n // Apply prefixes to all prompts\n return {\n ...combined,\n data: combined.data.map((entry) => ({\n ...entry,\n prompt: {\n ...entry.prompt,\n name: `${entry.server.serverKey}:${entry.prompt.name}`,\n },\n })),\n };\n },\n });\n\n return queries;\n}\n// TODO: find a more general place for this\nfunction combineArrayResults<T>(results: UseQueryResult<T[], Error>[]): {\n data: T[];\n error: Error | null;\n errors: Error[];\n isPending: boolean;\n isSuccess: boolean;\n isError: boolean;\n isPaused: boolean;\n isRefetching: boolean;\n isFetching: boolean;\n isLoading: boolean;\n refetch: () => Promise<void>;\n} {\n const errors = results\n .filter((result) => result.isError)\n .map((result) => result.error as Error);\n\n // Treat queries that are idle (disabled) as non-blocking for aggregate status\n const enabledish = results.filter(\n (r) => r.fetchStatus !== \"idle\" || r.isSuccess || r.isError,\n );\n\n return {\n // Prefer flatMap to avoid extra intermediate arrays\n data: results.flatMap((result) =>\n result.isSuccess && Array.isArray(result.data) ? result.data : [],\n ),\n // Preserve a single error for compatibility and expose the full list for diagnostics\n error: errors[0] ?? null,\n errors,\n isPending: enabledish.some((result) => result.isPending),\n isSuccess:\n enabledish.length > 0 && enabledish.every((result) => result.isSuccess),\n isError: errors.length > 0,\n isPaused: enabledish.some((result) => result.isPaused),\n isRefetching: enabledish.some((result) => result.isRefetching),\n isFetching: enabledish.some((result) => result.isFetching),\n isLoading: enabledish.some((result) => result.isLoading),\n // Aggregate refetch to trigger all underlying queries\n refetch: async () => {\n await Promise.all(\n results.map(async (r) => {\n await r.refetch();\n }),\n );\n },\n };\n}\n\n// Type guard for narrowing to connected servers\nfunction isConnectedMcpServer(server: McpServer): server is ConnectedMcpServer {\n return \"client\" in server && server.client != null;\n}\n\n/**\n * Hook to get the prompt for the specified name.\n * @param promptName - The name of the prompt to get. Can be prefixed with serverKey (e.g., \"linear:issue\") or unprefixed.\n * @param args - The arguments to pass to the prompt.\n * @returns The prompt for the specified name.\n */\nexport function useTamboMcpPrompt(\n promptName: string | undefined,\n args: Record<string, string> = {},\n) {\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 // Use the stable server key (and the server instance itself) instead of brittle\n // name/url/transport matching.\n const mcpServer = promptEntry?.server;\n\n // Strip the prefix to get the original prompt name for the MCP server call\n const originalPromptName = promptName?.includes(\":\")\n ? promptName.split(\":\").slice(1).join(\":\")\n : promptName;\n\n // Canonicalize args to avoid unstable cache keys from object identity/order\n const sortedArgsEntries = Object.keys(args)\n .sort()\n .map((k) => [k, args[k]] as const);\n return useTamboQuery({\n // Include server identity and sorted args to prevent stale cache hits\n queryKey: [\"mcp-prompt\", promptName, mcpServer?.key, sortedArgsEntries],\n // Only run when we have a prompt name and a connected server\n enabled: Boolean(\n promptName && mcpServer && isConnectedMcpServer(mcpServer),\n ),\n queryFn: async (): Promise<GetPromptResult | null> => {\n if (\n !originalPromptName ||\n !mcpServer ||\n !isConnectedMcpServer(mcpServer)\n ) {\n return null;\n }\n const result = await mcpServer.client.client.getPrompt({\n name: originalPromptName,\n arguments: args,\n });\n return result ?? null; // return null because react-query doesn't like undefined results\n },\n });\n}\n\n/**\n * Hook to get the resources for all the registered MCP servers.\n * @returns The resources for the MCP servers, including the server that the resource was found on.\n */\nexport function useTamboMcpResourceList() {\n const mcpServers = useTamboMcpServers();\n\n const queries = useTamboQueries({\n queries: mcpServers.map((mcpServer) => ({\n queryKey: [\"mcp-resources\", mcpServer.key],\n // Only run for connected servers that have a client\n enabled: isConnectedMcpServer(mcpServer),\n queryFn: async (): Promise<ListResourceEntry[]> => {\n // Fast path: if this server doesn't have a client, skip work\n if (!isConnectedMcpServer(mcpServer)) return [];\n\n const result = await mcpServer.client.client.listResources();\n const resources: ListResourceItem[] = result?.resources ?? [];\n // Return resources without prefixes - we'll apply prefixing in combine\n const resourceEntries = resources.map((resource) => ({\n server: mcpServer,\n resource,\n }));\n return resourceEntries;\n },\n })),\n combine: (results) => {\n const combined = combineArrayResults(results);\n // Apply prefixing based on current server count\n const shouldPrefix = mcpServers.length > 1;\n if (!shouldPrefix) {\n return combined;\n }\n\n // Apply prefixes to all resources\n return {\n ...combined,\n data: combined.data.map((entry) => ({\n ...entry,\n resource: {\n ...entry.resource,\n uri: `${entry.server.serverKey}:${entry.resource.uri}`,\n },\n })),\n };\n },\n });\n\n return queries;\n}\n\n/**\n * Hook to get the resource for the specified URI.\n * @param resourceUri - The URI of the resource to get. Can be prefixed with serverKey (e.g., \"linear:file://foo\") or unprefixed.\n * @returns The resource for the specified URI.\n */\nexport function useTamboMcpResource(resourceUri: string | undefined) {\n // figure out which server has the resource\n const { data: resourceEntries } = useTamboMcpResourceList();\n const resourceEntry = resourceEntries?.find(\n (resource) => resource.resource.uri === resourceUri,\n );\n // Use the stable server key (and the server instance itself) instead of brittle\n // name/url/transport matching.\n const mcpServer = resourceEntry?.server;\n\n // Strip the prefix to get the original resource URI for the MCP server call\n // Only strip if we found a matching resource entry with a server\n const originalResourceUri = resourceEntry\n ? resourceUri?.replace(`${resourceEntry.server.serverKey}:`, \"\")\n : resourceUri;\n\n return useTamboQuery({\n // Include server identity to prevent stale cache hits\n queryKey: [\"mcp-resource\", resourceUri, mcpServer?.key],\n // Only run when we have a resource URI and a connected server\n enabled: Boolean(\n resourceUri && mcpServer && isConnectedMcpServer(mcpServer),\n ),\n queryFn: async (): Promise<ReadResourceResult | null> => {\n if (\n !originalResourceUri ||\n !mcpServer ||\n !isConnectedMcpServer(mcpServer)\n ) {\n return null;\n }\n const result = await mcpServer.client.client.readResource({\n uri: originalResourceUri,\n });\n return result ?? null; // return null because react-query doesn't like undefined results\n },\n });\n}\n"]}
|
|
@@ -21,6 +21,17 @@ export interface McpServerInfo {
|
|
|
21
21
|
transport?: MCPTransport;
|
|
22
22
|
/** Optional custom headers to include in requests */
|
|
23
23
|
customHeaders?: Record<string, string>;
|
|
24
|
+
/**
|
|
25
|
+
* Optional short name for namespacing MCP resources, prompts, and tools.
|
|
26
|
+
* When multiple MCP servers are configured, this key is used to prefix:
|
|
27
|
+
* - prompts: `<serverKey>:<promptName>`
|
|
28
|
+
* - resources: `<serverKey>:<resourceUrl>`
|
|
29
|
+
* - tools: `<serverKey>__<toolName>`
|
|
30
|
+
*
|
|
31
|
+
* If not provided, a key will be derived from the URL hostname.
|
|
32
|
+
* For example, "https://mcp.linear.app/mcp" becomes "linear".
|
|
33
|
+
*/
|
|
34
|
+
serverKey?: string;
|
|
24
35
|
/**
|
|
25
36
|
* Optional handlers for elicitation and sampling requests from the server.
|
|
26
37
|
* Note: These callbacks should be stable (e.g., wrapped in useCallback or defined outside the component)
|
|
@@ -37,6 +48,11 @@ interface McpServerConfig extends McpServerInfo {
|
|
|
37
48
|
* Present for all server states (connected or failed).
|
|
38
49
|
*/
|
|
39
50
|
key: string;
|
|
51
|
+
/**
|
|
52
|
+
* Short name for namespacing, either provided by user or derived from URL.
|
|
53
|
+
* Used to prefix tools, prompts, and resources when multiple servers are present.
|
|
54
|
+
*/
|
|
55
|
+
serverKey: string;
|
|
40
56
|
}
|
|
41
57
|
/**
|
|
42
58
|
* Connected MCP server with an active client.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-mcp-provider.d.ts","sourceRoot":"","sources":["../../src/mcp/tambo-mcp-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAEZ,EAAE,EAMH,MAAM,OAAO,CAAC;AAMf,OAAO,EACL,SAAS,EACT,qBAAqB,EACrB,WAAW,EACX,kBAAkB,EAClB,YAAY,EACb,MAAM,cAAc,CAAC;AAEtB;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAoB5D;AAED;;GAEG;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;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;CACjC;AAED;;GAEG;AACH,UAAU,eAAgB,SAAQ,aAAa;IAC7C;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"tambo-mcp-provider.d.ts","sourceRoot":"","sources":["../../src/mcp/tambo-mcp-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAEZ,EAAE,EAMH,MAAM,OAAO,CAAC;AAMf,OAAO,EACL,SAAS,EACT,qBAAqB,EACrB,WAAW,EACX,kBAAkB,EAClB,YAAY,EACb,MAAM,cAAc,CAAC;AAEtB;;;;;GAKG;AACH,wBAAgB,mBAAmB,CA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C,CAAC,CAAC,EAC1C,KAAK,EAAE,UAAU,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,EACxC,UAAU,EAAE,eAAe,KACxB,UAAU,CAAC,kBAAkB,CAAC,CAAC;CACrC;AAkBD;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,EAAE,EAAE,CAAC;IAChC,UAAU,EAAE,CAAC,aAAa,GAAG,MAAM,CAAC,EAAE,CAAC;IACvC,QAAQ,CAAC,EAAE,mBAAmB,CAAC;IAC/B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B,CAkSA,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,kBAAkB,mBAE9B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,0BAA0B;;;CAMtC,CAAC"}
|
|
@@ -181,23 +181,28 @@ const TamboMcpProvider = ({ mcpServers, handlers, children }) => {
|
|
|
181
181
|
// Register tools from this server (deduplicated by ownership)
|
|
182
182
|
try {
|
|
183
183
|
const tools = await client.listTools();
|
|
184
|
+
const shouldPrefix = currentServersMap.size > 1;
|
|
184
185
|
tools.forEach((tool) => {
|
|
185
|
-
//
|
|
186
|
-
const
|
|
186
|
+
// Prefix tool name with serverKey if multiple servers are present
|
|
187
|
+
const toolName = shouldPrefix
|
|
188
|
+
? `${serverInfo.serverKey}__${tool.name}`
|
|
189
|
+
: tool.name;
|
|
190
|
+
// Skip if another server already owns this tool (using final name for ownership)
|
|
191
|
+
const currentOwner = toolOwnerRef.current.get(toolName);
|
|
187
192
|
if (currentOwner && currentOwner !== key) {
|
|
188
193
|
return;
|
|
189
194
|
}
|
|
190
|
-
// Record ownership for this server key
|
|
195
|
+
// Record ownership for this server key (using final name)
|
|
191
196
|
if (!currentOwner) {
|
|
192
|
-
toolOwnerRef.current.set(
|
|
197
|
+
toolOwnerRef.current.set(toolName, key);
|
|
193
198
|
if (!keyToToolsRef.current.has(key)) {
|
|
194
199
|
keyToToolsRef.current.set(key, new Set());
|
|
195
200
|
}
|
|
196
|
-
keyToToolsRef.current.get(key).add(
|
|
201
|
+
keyToToolsRef.current.get(key).add(toolName);
|
|
197
202
|
}
|
|
198
203
|
registerTool({
|
|
199
204
|
description: tool.description ?? "",
|
|
200
|
-
name:
|
|
205
|
+
name: toolName,
|
|
201
206
|
tool: async (args = {}) => {
|
|
202
207
|
const server = clientMap.get(key);
|
|
203
208
|
if (!server?.client) {
|
|
@@ -356,6 +361,64 @@ const useTamboElicitationContext = () => {
|
|
|
356
361
|
};
|
|
357
362
|
};
|
|
358
363
|
exports.useTamboElicitationContext = useTamboElicitationContext;
|
|
364
|
+
/**
|
|
365
|
+
* Derives a short server key from a URL hostname using heuristics.
|
|
366
|
+
* Attempts to extract the "meaningful" part of the domain name.
|
|
367
|
+
*
|
|
368
|
+
* Examples:
|
|
369
|
+
* - "https://mcp.linear.app/mcp" -> "linear"
|
|
370
|
+
* - "https://api.github.com" -> "github"
|
|
371
|
+
* - "https://google.com" -> "google"
|
|
372
|
+
* - "https://google.co.uk" -> "google"
|
|
373
|
+
* - "https://mcp.company.co.uk" -> "company"
|
|
374
|
+
* @param url - The URL to derive a server key from
|
|
375
|
+
* @returns A short server key derived from the hostname
|
|
376
|
+
*/
|
|
377
|
+
function deriveServerKey(url) {
|
|
378
|
+
try {
|
|
379
|
+
const parsed = new URL(url);
|
|
380
|
+
const hostname = parsed.hostname;
|
|
381
|
+
// Split hostname into parts
|
|
382
|
+
const parts = hostname.split(".");
|
|
383
|
+
// Remove common TLD patterns
|
|
384
|
+
// Handle cases like: .com, .org, .co.uk, .com.au, etc.
|
|
385
|
+
let relevantParts = [...parts];
|
|
386
|
+
// If we have 3+ parts and the last two are short (likely TLD like .co.uk)
|
|
387
|
+
if (relevantParts.length >= 3 &&
|
|
388
|
+
relevantParts[relevantParts.length - 1].length <= 3 &&
|
|
389
|
+
relevantParts[relevantParts.length - 2].length <= 3) {
|
|
390
|
+
relevantParts = relevantParts.slice(0, -2);
|
|
391
|
+
}
|
|
392
|
+
// Otherwise just remove the last part (TLD like .com)
|
|
393
|
+
else if (relevantParts.length >= 2) {
|
|
394
|
+
relevantParts = relevantParts.slice(0, -1);
|
|
395
|
+
}
|
|
396
|
+
// From what's left, prefer the rightmost part that's not a common prefix
|
|
397
|
+
// Common prefixes: www, api, mcp, app, etc.
|
|
398
|
+
const commonPrefixes = new Set([
|
|
399
|
+
"www",
|
|
400
|
+
"api",
|
|
401
|
+
"mcp",
|
|
402
|
+
"app",
|
|
403
|
+
"staging",
|
|
404
|
+
"dev",
|
|
405
|
+
"prod",
|
|
406
|
+
]);
|
|
407
|
+
// Work backwards through the parts to find a meaningful name
|
|
408
|
+
for (let i = relevantParts.length - 1; i >= 0; i--) {
|
|
409
|
+
const part = relevantParts[i];
|
|
410
|
+
if (part && !commonPrefixes.has(part.toLowerCase())) {
|
|
411
|
+
return part.toLowerCase();
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
// Fallback: use the last relevant part even if it's a common prefix
|
|
415
|
+
return relevantParts[relevantParts.length - 1]?.toLowerCase() || hostname;
|
|
416
|
+
}
|
|
417
|
+
catch {
|
|
418
|
+
// If URL parsing fails, just return a sanitized version of the input
|
|
419
|
+
return url.replace(/[^a-zA-Z0-9]/g, "_").toLowerCase();
|
|
420
|
+
}
|
|
421
|
+
}
|
|
359
422
|
/**
|
|
360
423
|
* Creates a stable identifier for an MCP server based on its connection properties.
|
|
361
424
|
* Two servers with the same URL, transport, and headers will have the same key.
|
|
@@ -378,6 +441,7 @@ function normalizeServerInfo(server) {
|
|
|
378
441
|
? { url: server, transport: mcp_client_1.MCPTransport.HTTP }
|
|
379
442
|
: server;
|
|
380
443
|
const key = getServerKey(s);
|
|
381
|
-
|
|
444
|
+
const serverKey = s.serverKey ?? deriveServerKey(s.url);
|
|
445
|
+
return { ...s, key, serverKey };
|
|
382
446
|
}
|
|
383
447
|
//# sourceMappingURL=tambo-mcp-provider.js.map
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G,IAAA,kBAAU,EAAC,kBAAkB,CAAC,CAAC;IAC/C,OAAO;QACL,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;KAC/C,CAAC;AACJ,CAAC,CAAC;AANW,QAAA,0BAA0B,8BAMrC;AAEF;;;;GAIG;AACH,SAAS,YAAY,CACnB,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;IACP,OAAO,GAAG,UAAU,CAAC,GAAG,IAAI,UAAU,CAAC,SAAS,IAAI,yBAAY,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,yBAAY,CAAC,IAAI,EAAE;QAC/C,CAAC,CAAC,MAAM,CAAC;IACb,MAAM,GAAG,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;IAC5B,OAAO,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC;AACvB,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 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\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 tools.forEach((tool) => {\n // Skip if another server already owns this tool\n const currentOwner = toolOwnerRef.current.get(tool.name);\n if (currentOwner && currentOwner !== key) {\n return;\n }\n\n // Record ownership for this server key\n if (!currentOwner) {\n toolOwnerRef.current.set(tool.name, key);\n if (!keyToToolsRef.current.has(key)) {\n keyToToolsRef.current.set(key, new Set());\n }\n keyToToolsRef.current.get(key)!.add(tool.name);\n }\n\n registerTool({\n description: tool.description ?? \"\",\n name: tool.name,\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 * 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 return { ...s, key };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"tambo-mcp-provider.js","sourceRoot":"","sources":["../../src/mcp/tambo-mcp-provider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,kDAoBC;AAhDD,+CAQe;AAEf,oFAAyE;AACzE,kFAAwE;AACxE,yDAAmE;AACnE,+CAA6E;AAC7E,6CAMsB;AAEtB;;;;;GAKG;AACH,SAAgB,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,K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Effect,\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"]}
|
|
@@ -87,11 +87,11 @@ function TamboContextAttachmentProvider({ children, getContextHelperData, }) {
|
|
|
87
87
|
const prevGetContextHelperDataRef = (0, react_1.useRef)();
|
|
88
88
|
// Sync context helpers with attachments using useEffect
|
|
89
89
|
(0, react_1.useEffect)(() => {
|
|
90
|
-
const currentIds =
|
|
90
|
+
const currentIds = attachments.map((a) => a.id);
|
|
91
91
|
const registeredIds = registeredIdsRef.current;
|
|
92
92
|
// Remove context helpers that are no longer in attachments
|
|
93
93
|
registeredIds.forEach((id) => {
|
|
94
|
-
if (!currentIds.
|
|
94
|
+
if (!currentIds.includes(id)) {
|
|
95
95
|
removeContextHelper(id);
|
|
96
96
|
registeredIds.delete(id);
|
|
97
97
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tambo-context-attachment-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-context-attachment-provider.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgIb,wEAyHC;AAyBD,8DAQC;AAvRD,+CAQe;AACf,qFAA0E;AAgD1E,MAAM,wBAAwB,GAAG,IAAA,qBAAa,EAC5C,IAAI,CACL,CAAC;AA2BF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,SAAgB,8BAA8B,CAAC,EAC7C,QAAQ,EACR,oBAAoB,GACgB;IACpC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,IAAA,gBAAQ,EAAsB,EAAE,CAAC,CAAC;IACxE,MAAM,CAAC,iBAAiB,EAAE,oBAAoB,CAAC,GAAG,IAAA,gBAAQ,EAExD,IAAI,CAAC,CAAC;IACR,MAAM,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,GAAG,IAAA,uDAAsB,GAAE,CAAC;IAE3E,uEAAuE;IACvE,MAAM,gBAAgB,GAAG,IAAA,cAAM,EAAc,IAAI,GAAG,EAAE,CAAC,CAAC;IACxD,MAAM,2BAA2B,GAAG,IAAA,cAAM,GAA+B,CAAC;IAE1E,wDAAwD;IACxD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM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,KAAK,EAAE,CAAC,CAAC,CAAC;IAC5D,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,oEAAoE;IACpE,MAAM,uBAAuB,GAAG,IAAA,mBAAW,EAAC,GAAG,EAAE;QAC/C,cAAc,CAAC,EAAE,CAAC,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,KAAK,GAAG,IAAA,eAAO,EACnB,GAAG,EAAE,CAAC,CAAC;QACL,WAAW;QACX,oBAAoB;QACpB,uBAAuB;QACvB,uBAAuB;QACvB,iBAAiB;QACjB,oBAAoB;KACrB,CAAC,EACF;QACE,WAAW;QACX,oBAAoB;QACpB,uBAAuB;QACvB,uBAAuB;QACvB,iBAAiB;KAClB,CACF,CAAC;IAEF,OAAO,CACL,8BAAC,wBAAwB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC5C,QAAQ,CACyB,CACrC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,yBAAyB;IACvC,MAAM,OAAO,GAAG,IAAA,kBAAU,EAAC,wBAAwB,CAAC,CAAC;IACrD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,gFAAgF,CACjF,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["\"use client\";\n\nimport type { Suggestion } from \"@tambo-ai/typescript-sdk/resources/beta/threads/suggestions\";\nimport React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport { useTamboContextHelpers } from \"./tambo-context-helpers-provider\";\n\n/**\n * Represents a context attachment that can be displayed in MessageInputContexts.\n * Context attachments appear as badges above the message input and provide additional\n * information to the AI about what to focus on.\n * @property {string} name - Display name shown in the badge\n * @property {React.ReactNode} [icon] - Optional icon to display in the badge\n * @property {Record<string, unknown>} [metadata] - Additional data passed to the AI\n * @example\n * ```tsx\n * const context: ContextAttachment = {\n * name: \"Button.tsx\",\n * icon: <FileIcon />,\n * metadata: { filePath: \"/src/components/Button.tsx\" }\n * };\n * ```\n */\nexport interface ContextAttachment {\n id: string;\n name: string;\n icon?: React.ReactNode;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Represents the data structure returned by a context helper\n */\nexport type ContextHelperData = Record<string, unknown>;\n\n/**\n * Context state interface for managing context attachments and custom suggestions.\n * @property {ContextAttachment[]} attachments - Array of active context attachments (badges above message input)\n * @property {(context: Omit<ContextAttachment, \"id\">) => void} addContextAttachment - Add a new context attachment\n * @property {(id: string) => void} removeContextAttachment - Remove a context attachment by ID\n * @property {() => void} clearContextAttachments - Remove all context attachments - This is used to clear the context when the user submits a message\n * @property {Suggestion[] | null} customSuggestions - Custom suggestions to display instead of auto-generated ones\n * @property {(suggestions: Suggestion[] | null) => void} setCustomSuggestions - Set or clear custom suggestions\n */\nexport interface ContextAttachmentState {\n attachments: ContextAttachment[];\n addContextAttachment: (context: Omit<ContextAttachment, \"id\">) => void;\n removeContextAttachment: (id: string) => void;\n clearContextAttachments: () => void;\n customSuggestions: Suggestion[] | null;\n setCustomSuggestions: (suggestions: Suggestion[] | null) => void;\n}\n\nconst ContextAttachmentContext = createContext<ContextAttachmentState | null>(\n null,\n);\n\n/**\n * Props for the TamboContextAttachmentProvider.\n * @property {(context: ContextAttachment) => Promise<ContextHelperData> | ContextHelperData} [getContextHelperData] - Optional function to customize the data sent to the AI for each context. If not provided, uses a default structure with the context name and instruction.\n * @example\n * ```tsx\n * <TamboContextAttachmentProvider\n * getContextHelperData={(context) => ({\n * selectedFile: {\n * name: context.name,\n * path: context.metadata?.filePath,\n * instruction: \"Focus on this file\"\n * }\n * })}\n * >\n * {children}\n * </TamboContextAttachmentProvider>\n * ```\n */\nexport interface TamboContextAttachmentProviderProps {\n children?: React.ReactNode;\n getContextHelperData?: (\n context: ContextAttachment,\n ) => Promise<ContextHelperData> | ContextHelperData;\n}\n\n/**\n * Provider that enables context attachment features and custom suggestions in MessageInput.\n * **When to use:**\n * - **Included by default** in TamboProvider - no need to wrap separately\n * - Use `useTamboContextAttachment()` hook to manage context attachments\n * **What it does:**\n * - Manages context items that appear as badges above MessageInput\n * - Syncs context data with Tambo's AI for better responses\n * - Manages custom suggestions that replace auto-generated suggestions\n * - Allows components to add/remove contexts via `useTamboContextAttachment()`\n * - Allows components to set custom suggestions via `setCustomSuggestions()`\n * @param props - The props for the TamboContextAttachmentProvider\n * @param props.children - The children to wrap\n * @param props.getContextHelperData - The function to get the context helper data\n * @returns The TamboContextAttachmentProvider component\n * @example\n * Basic usage - already included in TamboProvider\n * ```tsx\n * <TamboProvider apiKey=\"...\">\n * <App />\n * </TamboProvider>\n * ```\n * @example\n * Using TamboProvider with custom context data\n * ```tsx\n * <TamboProvider\n * apiKey=\"...\"\n * getContextHelperData={(context) => ({\n * selectedComponent: {\n * name: context.name,\n * filePath: context.metadata?.path,\n * instruction: \"Edit this component\"\n * }\n * })}\n * >\n * <App />\n * </TamboProvider>\n * ```\n */\nexport function TamboContextAttachmentProvider({\n children,\n getContextHelperData,\n}: TamboContextAttachmentProviderProps) {\n const [attachments, setAttachments] = useState<ContextAttachment[]>([]);\n const [customSuggestions, setCustomSuggestions] = useState<\n Suggestion[] | null\n >(null);\n const { addContextHelper, removeContextHelper } = useTamboContextHelpers();\n\n // Track which context helpers have been registered to avoid duplicates\n const registeredIdsRef = useRef<Set<string>>(new Set());\n const prevGetContextHelperDataRef = useRef<typeof getContextHelperData>();\n\n // Sync context helpers with attachments using useEffect\n useEffect(() => {\n const currentIds = new Set(attachments.map((a) => a.id));\n const registeredIds = registeredIdsRef.current;\n\n // Remove context helpers that are no longer in attachments\n registeredIds.forEach((id) => {\n if (!currentIds.has(id)) {\n removeContextHelper(id);\n registeredIds.delete(id);\n }\n });\n\n const getDataChanged =\n prevGetContextHelperDataRef.current !== getContextHelperData;\n\n // Add or replace context helpers for attachments\n attachments.forEach((context) => {\n if (getDataChanged || !registeredIds.has(context.id)) {\n addContextHelper(context.id, async (): Promise<ContextHelperData> => {\n if (getContextHelperData) {\n return await getContextHelperData(context);\n }\n return {\n selectedComponent: {\n name: context.name,\n instruction:\n \"This is a Tambo interactable component that is currently selected and visible on the dashboard. You can read its current props and state, and update it by modifying its props. If multiple components are attached, you can interact with and modify any of them. Use the auto-registered interactable component tools (like get_interactable_component_by_id and update_interactable_component_<id>) to view and update the component's state.\",\n ...(context.metadata ?? {}),\n },\n };\n });\n registeredIds.add(context.id);\n }\n });\n\n prevGetContextHelperDataRef.current = getContextHelperData;\n }, [\n attachments,\n addContextHelper,\n removeContextHelper,\n getContextHelperData,\n ]);\n\n // Cleanup: remove all context helpers on unmount\n useEffect(() => {\n const registeredIds = registeredIdsRef.current;\n return () => {\n registeredIds.forEach((id) => {\n removeContextHelper(id);\n });\n registeredIds.clear();\n };\n }, [removeContextHelper]);\n\n const addContextAttachment = useCallback(\n (context: Omit<ContextAttachment, \"id\">) => {\n setAttachments((prev) => {\n if (prev.some((c) => c.name === context.name)) return prev;\n\n if (typeof crypto === \"undefined\" || !(\"randomUUID\" in crypto)) {\n throw new Error(\n \"crypto.randomUUID() is not available. This usually happens when using an IP address instead of 'localhost' in development. Use 'localhost' or a secure context (HTTPS) to enable crypto APIs.\",\n );\n }\n\n const newId = crypto.randomUUID();\n const newContext = { ...context, id: newId };\n return [...prev, newContext];\n });\n },\n [],\n );\n\n // This is used to remove a context when the user clicks the remove button\n const removeContextAttachment = useCallback((id: string) => {\n setAttachments((prev) => prev.filter((c) => c.id !== id));\n }, []);\n\n // This is used to clear the context when the user submits a message\n const clearContextAttachments = useCallback(() => {\n setAttachments([]);\n }, []);\n\n const value = useMemo(\n () => ({\n attachments,\n addContextAttachment,\n removeContextAttachment,\n clearContextAttachments,\n customSuggestions,\n setCustomSuggestions,\n }),\n [\n attachments,\n addContextAttachment,\n removeContextAttachment,\n clearContextAttachments,\n customSuggestions,\n ],\n );\n\n return (\n <ContextAttachmentContext.Provider value={value}>\n {children}\n </ContextAttachmentContext.Provider>\n );\n}\n\n/**\n * Hook to access context attachment state and methods.\n * **Must be used within a `TamboProvider`** - throws an error otherwise.\n * @throws {Error} If used outside of TamboProvider\n * @returns The context attachment state and methods\n * @example\n * ```tsx\n * const contextAttachment = useTamboContextAttachment();\n *\n * // Add a context\n * contextAttachment.addContextAttachment({\n * name: \"Button.tsx\",\n * icon: <FileIcon />,\n * metadata: { path: \"/src/Button.tsx\" }\n * });\n *\n * // Remove a context\n * contextAttachment.removeContextAttachment(contextId);\n *\n * // Set custom suggestions\n * contextAttachment.setCustomSuggestions([{ id: \"1\", title: \"Add Feature\" }]);\n * ```\n */\nexport function useTamboContextAttachment() {\n const context = useContext(ContextAttachmentContext);\n if (!context) {\n throw new Error(\n \"useTamboContextAttachment must be used within a TamboContextAttachmentProvider\",\n );\n }\n return context;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"tambo-context-attachment-provider.js","sourceRoot":"","sources":["../../src/providers/tambo-context-attachment-provider.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgIb,wEAyHC;AAyBD,8DAQC;AAvRD,+CAQe;AACf,qFAA0E;AAgD1E,MAAM,wBAAwB,GAAG,IAAA,qBAAa,EAC5C,IAAI,CACL,CAAC;AA2BF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,SAAgB,8BAA8B,CAAC,EAC7C,QAAQ,EACR,oBAAoB,GACgB;IACpC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,IAAA,gBAAQ,EAAsB,EAAE,CAAC,CAAC;IACxE,MAAM,CAAC,iBAAiB,EAAE,oBAAoB,CAAC,GAAG,IAAA,gBAAQ,EAExD,IAAI,CAAC,CAAC;IACR,MAAM,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,GAAG,IAAA,uDAAsB,GAAE,CAAC;IAE3E,uEAAuE;IACvE,MAAM,gBAAgB,GAAG,IAAA,cA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,EAAE,EAAE;QACzD,cAAc,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IAC5D,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,oEAAoE;IACpE,MAAM,uBAAuB,GAAG,IAAA,mBAAW,EAAC,GAAG,EAAE;QAC/C,cAAc,CAAC,EAAE,CAAC,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,KAAK,GAAG,IAAA,eAAO,EACnB,GAAG,EAAE,CAAC,CAAC;QACL,WAAW;QACX,oBAAoB;QACpB,uBAAuB;QACvB,uBAAuB;QACvB,iBAAiB;QACjB,oBAAoB;KACrB,CAAC,EACF;QACE,WAAW;QACX,oBAAoB;QACpB,uBAAuB;QACvB,uBAAuB;QACvB,iBAAiB;KAClB,CACF,CAAC;IAEF,OAAO,CACL,8BAAC,wBAAwB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC5C,QAAQ,CACyB,CACrC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,yBAAyB;IACvC,MAAM,OAAO,GAAG,IAAA,kBAAU,EAAC,wBAAwB,CAAC,CAAC;IACrD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,gFAAgF,CACjF,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["\"use client\";\n\nimport type { Suggestion } from \"@tambo-ai/typescript-sdk/resources/beta/threads/suggestions\";\nimport React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport { useTamboContextHelpers } from \"./tambo-context-helpers-provider\";\n\n/**\n * Represents a context attachment that can be displayed in MessageInputContexts.\n * Context attachments appear as badges above the message input and provide additional\n * information to the AI about what to focus on.\n * @property {string} name - Display name shown in the badge\n * @property {React.ReactNode} [icon] - Optional icon to display in the badge\n * @property {Record<string, unknown>} [metadata] - Additional data passed to the AI\n * @example\n * ```tsx\n * const context: ContextAttachment = {\n * name: \"Button.tsx\",\n * icon: <FileIcon />,\n * metadata: { filePath: \"/src/components/Button.tsx\" }\n * };\n * ```\n */\nexport interface ContextAttachment {\n id: string;\n name: string;\n icon?: React.ReactNode;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Represents the data structure returned by a context helper\n */\nexport type ContextHelperData = Record<string, unknown>;\n\n/**\n * Context state interface for managing context attachments and custom suggestions.\n * @property {ContextAttachment[]} attachments - Array of active context attachments (badges above message input)\n * @property {(context: Omit<ContextAttachment, \"id\">) => void} addContextAttachment - Add a new context attachment\n * @property {(id: string) => void} removeContextAttachment - Remove a context attachment by ID\n * @property {() => void} clearContextAttachments - Remove all context attachments - This is used to clear the context when the user submits a message\n * @property {Suggestion[] | null} customSuggestions - Custom suggestions to display instead of auto-generated ones\n * @property {(suggestions: Suggestion[] | null) => void} setCustomSuggestions - Set or clear custom suggestions\n */\nexport interface ContextAttachmentState {\n attachments: ContextAttachment[];\n addContextAttachment: (context: Omit<ContextAttachment, \"id\">) => void;\n removeContextAttachment: (id: string) => void;\n clearContextAttachments: () => void;\n customSuggestions: Suggestion[] | null;\n setCustomSuggestions: (suggestions: Suggestion[] | null) => void;\n}\n\nconst ContextAttachmentContext = createContext<ContextAttachmentState | null>(\n null,\n);\n\n/**\n * Props for the TamboContextAttachmentProvider.\n * @property {(context: ContextAttachment) => Promise<ContextHelperData> | ContextHelperData} [getContextHelperData] - Optional function to customize the data sent to the AI for each context. If not provided, uses a default structure with the context name and instruction.\n * @example\n * ```tsx\n * <TamboContextAttachmentProvider\n * getContextHelperData={(context) => ({\n * selectedFile: {\n * name: context.name,\n * path: context.metadata?.filePath,\n * instruction: \"Focus on this file\"\n * }\n * })}\n * >\n * {children}\n * </TamboContextAttachmentProvider>\n * ```\n */\nexport interface TamboContextAttachmentProviderProps {\n children?: React.ReactNode;\n getContextHelperData?: (\n context: ContextAttachment,\n ) => Promise<ContextHelperData> | ContextHelperData;\n}\n\n/**\n * Provider that enables context attachment features and custom suggestions in MessageInput.\n * **When to use:**\n * - **Included by default** in TamboProvider - no need to wrap separately\n * - Use `useTamboContextAttachment()` hook to manage context attachments\n * **What it does:**\n * - Manages context items that appear as badges above MessageInput\n * - Syncs context data with Tambo's AI for better responses\n * - Manages custom suggestions that replace auto-generated suggestions\n * - Allows components to add/remove contexts via `useTamboContextAttachment()`\n * - Allows components to set custom suggestions via `setCustomSuggestions()`\n * @param props - The props for the TamboContextAttachmentProvider\n * @param props.children - The children to wrap\n * @param props.getContextHelperData - The function to get the context helper data\n * @returns The TamboContextAttachmentProvider component\n * @example\n * Basic usage - already included in TamboProvider\n * ```tsx\n * <TamboProvider apiKey=\"...\">\n * <App />\n * </TamboProvider>\n * ```\n * @example\n * Using TamboProvider with custom context data\n * ```tsx\n * <TamboProvider\n * apiKey=\"...\"\n * getContextHelperData={(context) => ({\n * selectedComponent: {\n * name: context.name,\n * filePath: context.metadata?.path,\n * instruction: \"Edit this component\"\n * }\n * })}\n * >\n * <App />\n * </TamboProvider>\n * ```\n */\nexport function TamboContextAttachmentProvider({\n children,\n getContextHelperData,\n}: TamboContextAttachmentProviderProps) {\n const [attachments, setAttachments] = useState<ContextAttachment[]>([]);\n const [customSuggestions, setCustomSuggestions] = useState<\n Suggestion[] | null\n >(null);\n const { addContextHelper, removeContextHelper } = useTamboContextHelpers();\n\n // Track which context helpers have been registered to avoid duplicates\n const registeredIdsRef = useRef<Set<string>>(new Set());\n const prevGetContextHelperDataRef = useRef<typeof getContextHelperData>();\n\n // Sync context helpers with attachments using useEffect\n useEffect(() => {\n const currentIds = attachments.map((a) => a.id);\n const registeredIds = registeredIdsRef.current;\n\n // Remove context helpers that are no longer in attachments\n registeredIds.forEach((id) => {\n if (!currentIds.includes(id)) {\n removeContextHelper(id);\n registeredIds.delete(id);\n }\n });\n\n const getDataChanged =\n prevGetContextHelperDataRef.current !== getContextHelperData;\n\n // Add or replace context helpers for attachments\n attachments.forEach((context) => {\n if (getDataChanged || !registeredIds.has(context.id)) {\n addContextHelper(context.id, async (): Promise<ContextHelperData> => {\n if (getContextHelperData) {\n return await getContextHelperData(context);\n }\n return {\n selectedComponent: {\n name: context.name,\n instruction:\n \"This is a Tambo interactable component that is currently selected and visible on the dashboard. You can read its current props and state, and update it by modifying its props. If multiple components are attached, you can interact with and modify any of them. Use the auto-registered interactable component tools (like get_interactable_component_by_id and update_interactable_component_<id>) to view and update the component's state.\",\n ...(context.metadata ?? {}),\n },\n };\n });\n registeredIds.add(context.id);\n }\n });\n\n prevGetContextHelperDataRef.current = getContextHelperData;\n }, [\n attachments,\n addContextHelper,\n removeContextHelper,\n getContextHelperData,\n ]);\n\n // Cleanup: remove all context helpers on unmount\n useEffect(() => {\n const registeredIds = registeredIdsRef.current;\n return () => {\n registeredIds.forEach((id) => {\n removeContextHelper(id);\n });\n registeredIds.clear();\n };\n }, [removeContextHelper]);\n\n const addContextAttachment = useCallback(\n (context: Omit<ContextAttachment, \"id\">) => {\n setAttachments((prev) => {\n if (prev.some((c) => c.name === context.name)) return prev;\n\n if (typeof crypto === \"undefined\" || !(\"randomUUID\" in crypto)) {\n throw new Error(\n \"crypto.randomUUID() is not available. This usually happens when using an IP address instead of 'localhost' in development. Use 'localhost' or a secure context (HTTPS) to enable crypto APIs.\",\n );\n }\n\n const newId = crypto.randomUUID();\n const newContext = { ...context, id: newId };\n return [...prev, newContext];\n });\n },\n [],\n );\n\n // This is used to remove a context when the user clicks the remove button\n const removeContextAttachment = useCallback((id: string) => {\n setAttachments((prev) => prev.filter((c) => c.id !== id));\n }, []);\n\n // This is used to clear the context when the user submits a message\n const clearContextAttachments = useCallback(() => {\n setAttachments([]);\n }, []);\n\n const value = useMemo(\n () => ({\n attachments,\n addContextAttachment,\n removeContextAttachment,\n clearContextAttachments,\n customSuggestions,\n setCustomSuggestions,\n }),\n [\n attachments,\n addContextAttachment,\n removeContextAttachment,\n clearContextAttachments,\n customSuggestions,\n ],\n );\n\n return (\n <ContextAttachmentContext.Provider value={value}>\n {children}\n </ContextAttachmentContext.Provider>\n );\n}\n\n/**\n * Hook to access context attachment state and methods.\n * **Must be used within a `TamboProvider`** - throws an error otherwise.\n * @throws {Error} If used outside of TamboProvider\n * @returns The context attachment state and methods\n * @example\n * ```tsx\n * const contextAttachment = useTamboContextAttachment();\n *\n * // Add a context\n * contextAttachment.addContextAttachment({\n * name: \"Button.tsx\",\n * icon: <FileIcon />,\n * metadata: { path: \"/src/Button.tsx\" }\n * });\n *\n * // Remove a context\n * contextAttachment.removeContextAttachment(contextId);\n *\n * // Set custom suggestions\n * contextAttachment.setCustomSuggestions([{ id: \"1\", title: \"Add Feature\" }]);\n * ```\n */\nexport function useTamboContextAttachment() {\n const context = useContext(ContextAttachmentContext);\n if (!context) {\n throw new Error(\n \"useTamboContextAttachment must be used within a TamboContextAttachmentProvider\",\n );\n }\n return context;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-tambo-voice.js","sourceRoot":"","sources":["../../src/hooks/use-tambo-voice.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa;IAC3B,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAgB,IAAI,CAAC,CAAC;IAClE,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;IAChC,MAAM,EACJ,MAAM,EACN,cAAc,EAAE,mBAAmB,EACnC,aAAa,EAAE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W;YAAE,OAAO;QAExB,0CAA0C;QAC1C,aAAa,CAAC,IAAI,CAAC,CAAC;QACpB,qBAAqB,CAAC,KAAK,EAAE,CAAC,CAAC,oCAAoC;QACnE,mBAAmB,EAAE,CAAC;IACxB,CAAC,EAAE,CAAC,WAAW,EAAE,mBAAmB,EAAE,qBAAqB,CAAC,CAAC,CAAC;IAE9D,MAAM,aAAa,GAAG,WAAW,CAAC,GAAG,EAAE;QACrC,IAAI,WAAW,EAAE,CAAC;YAChB,kBAAkB,EAAE,CAAC;QACvB,CAAC;IACH,CAAC,EAAE,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC,CAAC;IAEtC,OAAO;QACL,cAAc;QACd,aAAa;QACb,WAAW;QACX,gBAAgB,EAAE,gBAAgB,IAAI,IAAI;QAC1C,cAAc,EAAE,qBAAqB,CAAC,SAAS;QAC/C,UAAU;QACV,kBAAkB,EAAE,qBAAqB,CAAC,KAAK,EAAE,OAAO,IAAI,IAAI;KACjE,CAAC;AACJ,CAAC","sourcesContent":["import { useCallback, useEffect, useState } from \"react\";\nimport { useReactMediaRecorder } from \"react-media-recorder\";\nimport { useTamboClient } from \"../providers/tambo-client-provider\";\nimport { useTamboMutation } from \"./react-query-hooks\";\n\n/**\n * Exposes functionality to record speech and transcribe it using the Tambo API.\n * @returns An object with:\n * - startRecording: A function to start recording audio and reset the current transcript.\n * - stopRecording: A function to stop recording audio and automatically kick off transcription.\n * - isRecording: A boolean indicating if the user is recording audio.\n * - isTranscribing: A boolean indicating if the audio is being transcribed.\n * - transcript: The transcript of the recorded audio.\n * - transcriptionError: An error message if the transcription fails.\n * - mediaAccessError: An error message if microphone access fails.\n */\nexport function useTamboVoice() {\n const [transcript, setTranscript] = useState<string | null>(null);\n const client = useTamboClient();\n const {\n status,\n startRecording: startMediaRecording,\n stopRecording: stopMediaRecording,\n mediaBlobUrl,\n error: mediaAccessError,\n } = useReactMediaRecorder({\n audio: true,\n video: false,\n blobPropertyBag: { type: \"audio/webm\" },\n });\n\n const isRecording = status === \"recording\";\n\n const transcriptionMutation = useTamboMutation<\n string,\n Error,\n string // blobUrl parameter\n >({\n mutationFn: async (blobUrl: string) => {\n const response = await fetch(blobUrl);\n const audioBlob = await response.blob();\n const file = new File([audioBlob], \"recording.webm\", {\n type: \"audio/webm\",\n });\n\n return await client.beta.audio.transcribe({ file });\n },\n onSuccess: (transcription: string) => {\n setTranscript(transcription);\n },\n });\n\n // Trigger transcription when recording stops and we have a blob URL\n const shouldTranscribe =\n status === \"stopped\" &&\n mediaBlobUrl &&\n !transcriptionMutation.isPending &&\n !transcriptionMutation.isSuccess;\n\n useEffect(() => {\n if (shouldTranscribe) {\n transcriptionMutation.mutate(mediaBlobUrl);\n }\n }, [shouldTranscribe, mediaBlobUrl, transcriptionMutation]);\n\n const startRecording = useCallback(() => {\n if (isRecording) return;\n\n // Reset state when starting new recording\n setTranscript(null);\n transcriptionMutation.reset(); // Clear any previous mutation state\n startMediaRecording();\n }, [isRecording, startMediaRecording, transcriptionMutation]);\n\n const stopRecording = useCallback(() => {\n if (isRecording) {\n stopMediaRecording();\n }\n }, [isRecording, stopMediaRecording]);\n\n return {\n startRecording,\n stopRecording,\n isRecording,\n mediaAccessError: mediaAccessError ?? null,\n isTranscribing: transcriptionMutation.isPending,\n transcript,\n transcriptionError: transcriptionMutation.error?.message ?? null,\n };\n}\n"]}
|