@yourgpt/copilot-sdk 2.0.2-beta.2 → 2.1.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/README.md +0 -42
- package/dist/{chunk-UOWLKFXK.cjs → chunk-33JSFVGH.cjs} +23 -23
- package/dist/{chunk-UOWLKFXK.cjs.map → chunk-33JSFVGH.cjs.map} +1 -1
- package/dist/{chunk-IXFV6AW6.js → chunk-3SP6V22Y.js} +246 -219
- package/dist/chunk-3SP6V22Y.js.map +1 -0
- package/dist/{chunk-7XFFRV7D.js → chunk-7PJ4GAJR.js} +4 -4
- package/dist/{chunk-7XFFRV7D.js.map → chunk-7PJ4GAJR.js.map} +1 -1
- package/dist/{chunk-ZPYQDMUX.js → chunk-7YZVSG62.js} +3 -3
- package/dist/{chunk-ZPYQDMUX.js.map → chunk-7YZVSG62.js.map} +1 -1
- package/dist/{chunk-BKO7DSPU.js → chunk-BRUDSHCV.js} +2 -2
- package/dist/chunk-BRUDSHCV.js.map +1 -0
- package/dist/{chunk-BH7MNDWW.js → chunk-EWVQWTNV.js} +13 -3
- package/dist/chunk-EWVQWTNV.js.map +1 -0
- package/dist/{chunk-CEKAYA2Q.cjs → chunk-HNRFRNMY.cjs} +2 -2
- package/dist/chunk-HNRFRNMY.cjs.map +1 -0
- package/dist/{chunk-7K7HZMP4.cjs → chunk-JGPDQDY4.cjs} +13 -3
- package/dist/chunk-JGPDQDY4.cjs.map +1 -0
- package/dist/{chunk-H5XMKBBA.cjs → chunk-MUZ2RYS2.cjs} +268 -241
- package/dist/chunk-MUZ2RYS2.cjs.map +1 -0
- package/dist/{chunk-UIWFYMAO.cjs → chunk-O33I4HBL.cjs} +5 -5
- package/dist/{chunk-UIWFYMAO.cjs.map → chunk-O33I4HBL.cjs.map} +1 -1
- package/dist/core/index.cjs +80 -80
- package/dist/core/index.d.cts +3 -3
- package/dist/core/index.d.ts +3 -3
- package/dist/core/index.js +3 -3
- package/dist/{index-pWEH7pUE.d.ts → index-25qIYJ21.d.ts} +1 -1
- package/dist/{index-2VtgKM8S.d.cts → index-C261xY-B.d.cts} +1 -1
- package/dist/mcp/index.cjs +47 -47
- package/dist/mcp/index.d.cts +13 -3
- package/dist/mcp/index.d.ts +13 -3
- package/dist/mcp/index.js +2 -2
- package/dist/react/index.cjs +50 -50
- package/dist/react/index.d.cts +18 -5
- package/dist/react/index.d.ts +18 -5
- package/dist/react/index.js +5 -5
- package/dist/tools/anthropic/index.d.cts +1 -1
- package/dist/tools/anthropic/index.d.ts +1 -1
- package/dist/tools/brave/index.cjs +2 -2
- package/dist/tools/brave/index.d.cts +1 -1
- package/dist/tools/brave/index.d.ts +1 -1
- package/dist/tools/brave/index.js +1 -1
- package/dist/tools/exa/index.cjs +2 -2
- package/dist/tools/exa/index.d.cts +1 -1
- package/dist/tools/exa/index.d.ts +1 -1
- package/dist/tools/exa/index.js +1 -1
- package/dist/tools/google/index.cjs +2 -2
- package/dist/tools/google/index.d.cts +1 -1
- package/dist/tools/google/index.d.ts +1 -1
- package/dist/tools/google/index.js +1 -1
- package/dist/tools/openai/index.cjs +2 -2
- package/dist/tools/openai/index.d.cts +1 -1
- package/dist/tools/openai/index.d.ts +1 -1
- package/dist/tools/openai/index.js +1 -1
- package/dist/tools/searxng/index.cjs +2 -2
- package/dist/tools/searxng/index.d.cts +1 -1
- package/dist/tools/searxng/index.d.ts +1 -1
- package/dist/tools/searxng/index.js +1 -1
- package/dist/tools/serper/index.cjs +2 -2
- package/dist/tools/serper/index.d.cts +1 -1
- package/dist/tools/serper/index.d.ts +1 -1
- package/dist/tools/serper/index.js +1 -1
- package/dist/tools/tavily/index.cjs +2 -2
- package/dist/tools/tavily/index.d.cts +1 -1
- package/dist/tools/tavily/index.d.ts +1 -1
- package/dist/tools/tavily/index.js +1 -1
- package/dist/tools/web-search/index.cjs +3 -3
- package/dist/tools/web-search/index.d.cts +2 -2
- package/dist/tools/web-search/index.d.ts +2 -2
- package/dist/tools/web-search/index.js +2 -2
- package/dist/{tools-DDWrco4h.d.cts → tools-Clyufshc.d.cts} +14 -1
- package/dist/{tools-DDWrco4h.d.ts → tools-Clyufshc.d.ts} +14 -1
- package/dist/{types-DjSfYNKj.d.cts → types-DM6M_pv_.d.cts} +43 -2
- package/dist/{types-Cizh9K_f.d.ts → types-wudOhedT.d.ts} +43 -2
- package/dist/ui/index.cjs +56 -26
- package/dist/ui/index.cjs.map +1 -1
- package/dist/ui/index.d.cts +37 -3
- package/dist/ui/index.d.ts +37 -3
- package/dist/ui/index.js +52 -22
- package/dist/ui/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-7K7HZMP4.cjs.map +0 -1
- package/dist/chunk-BH7MNDWW.js.map +0 -1
- package/dist/chunk-BKO7DSPU.js.map +0 -1
- package/dist/chunk-CEKAYA2Q.cjs.map +0 -1
- package/dist/chunk-H5XMKBBA.cjs.map +0 -1
- package/dist/chunk-IXFV6AW6.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ThreadManager, isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-
|
|
2
|
-
import { createMCPClient, MCPToolAdapter } from './chunk-
|
|
3
|
-
import { createContext,
|
|
4
|
-
import { jsx } from 'react/jsx-runtime';
|
|
1
|
+
import { ThreadManager, isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-7PJ4GAJR.js';
|
|
2
|
+
import { createMCPClient, MCPToolAdapter } from './chunk-EWVQWTNV.js';
|
|
3
|
+
import { createContext, useRef, useState, useCallback, useEffect, useMemo, useContext, useSyncExternalStore } from 'react';
|
|
4
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
5
5
|
import * as z from 'zod';
|
|
6
6
|
|
|
7
7
|
// src/chat/types/tool.ts
|
|
@@ -1948,6 +1948,238 @@ function printTree(tree) {
|
|
|
1948
1948
|
});
|
|
1949
1949
|
return output.trim();
|
|
1950
1950
|
}
|
|
1951
|
+
function useMCPClient(config) {
|
|
1952
|
+
const {
|
|
1953
|
+
autoConnect = true,
|
|
1954
|
+
onConnectionStateChange,
|
|
1955
|
+
onToolsChange,
|
|
1956
|
+
onElicitationRequest,
|
|
1957
|
+
onError,
|
|
1958
|
+
onNotification,
|
|
1959
|
+
...clientConfig
|
|
1960
|
+
} = config;
|
|
1961
|
+
const clientRef = useRef(null);
|
|
1962
|
+
const mountedRef = useRef(true);
|
|
1963
|
+
const [state, setState] = useState({
|
|
1964
|
+
connectionState: "disconnected",
|
|
1965
|
+
tools: []
|
|
1966
|
+
});
|
|
1967
|
+
const getClient = useCallback(() => {
|
|
1968
|
+
if (!clientRef.current) {
|
|
1969
|
+
clientRef.current = createMCPClient(clientConfig, {
|
|
1970
|
+
onConnectionStateChange: (newState) => {
|
|
1971
|
+
if (mountedRef.current) {
|
|
1972
|
+
setState((prev) => ({ ...prev, connectionState: newState }));
|
|
1973
|
+
onConnectionStateChange?.(newState);
|
|
1974
|
+
}
|
|
1975
|
+
},
|
|
1976
|
+
onToolsChange: (tools) => {
|
|
1977
|
+
if (mountedRef.current) {
|
|
1978
|
+
setState((prev) => ({ ...prev, tools }));
|
|
1979
|
+
onToolsChange?.(tools);
|
|
1980
|
+
}
|
|
1981
|
+
},
|
|
1982
|
+
onElicitationRequest,
|
|
1983
|
+
onError: (error) => {
|
|
1984
|
+
if (mountedRef.current) {
|
|
1985
|
+
setState((prev) => ({ ...prev, error: error.message }));
|
|
1986
|
+
onError?.(error);
|
|
1987
|
+
}
|
|
1988
|
+
},
|
|
1989
|
+
onNotification
|
|
1990
|
+
});
|
|
1991
|
+
}
|
|
1992
|
+
return clientRef.current;
|
|
1993
|
+
}, [
|
|
1994
|
+
clientConfig,
|
|
1995
|
+
onConnectionStateChange,
|
|
1996
|
+
onToolsChange,
|
|
1997
|
+
onElicitationRequest,
|
|
1998
|
+
onError,
|
|
1999
|
+
onNotification
|
|
2000
|
+
]);
|
|
2001
|
+
const connect = useCallback(async () => {
|
|
2002
|
+
const client = getClient();
|
|
2003
|
+
try {
|
|
2004
|
+
setState((prev) => ({
|
|
2005
|
+
...prev,
|
|
2006
|
+
connectionState: "connecting",
|
|
2007
|
+
error: void 0
|
|
2008
|
+
}));
|
|
2009
|
+
await client.connect();
|
|
2010
|
+
if (mountedRef.current) {
|
|
2011
|
+
const clientState = client.getState();
|
|
2012
|
+
setState({
|
|
2013
|
+
connectionState: "connected",
|
|
2014
|
+
tools: clientState.tools,
|
|
2015
|
+
serverInfo: clientState.serverInfo,
|
|
2016
|
+
serverCapabilities: clientState.serverCapabilities,
|
|
2017
|
+
lastActivity: Date.now()
|
|
2018
|
+
});
|
|
2019
|
+
}
|
|
2020
|
+
} catch (error) {
|
|
2021
|
+
if (mountedRef.current) {
|
|
2022
|
+
const errorMessage = error instanceof Error ? error.message : "Connection failed";
|
|
2023
|
+
setState((prev) => ({
|
|
2024
|
+
...prev,
|
|
2025
|
+
connectionState: "error",
|
|
2026
|
+
error: errorMessage
|
|
2027
|
+
}));
|
|
2028
|
+
}
|
|
2029
|
+
throw error;
|
|
2030
|
+
}
|
|
2031
|
+
}, [getClient]);
|
|
2032
|
+
const disconnect = useCallback(async () => {
|
|
2033
|
+
const client = clientRef.current;
|
|
2034
|
+
if (client) {
|
|
2035
|
+
await client.disconnect();
|
|
2036
|
+
if (mountedRef.current) {
|
|
2037
|
+
setState({
|
|
2038
|
+
connectionState: "disconnected",
|
|
2039
|
+
tools: []
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
2043
|
+
}, []);
|
|
2044
|
+
const callTool = useCallback(
|
|
2045
|
+
async (name, args) => {
|
|
2046
|
+
const client = clientRef.current;
|
|
2047
|
+
if (!client || !client.isConnected()) {
|
|
2048
|
+
throw new Error("MCP client not connected");
|
|
2049
|
+
}
|
|
2050
|
+
const result = await client.callTool(name, args);
|
|
2051
|
+
if (mountedRef.current) {
|
|
2052
|
+
setState((prev) => ({ ...prev, lastActivity: Date.now() }));
|
|
2053
|
+
}
|
|
2054
|
+
return result;
|
|
2055
|
+
},
|
|
2056
|
+
[]
|
|
2057
|
+
);
|
|
2058
|
+
const refreshTools = useCallback(async () => {
|
|
2059
|
+
const client = clientRef.current;
|
|
2060
|
+
if (!client || !client.isConnected()) {
|
|
2061
|
+
throw new Error("MCP client not connected");
|
|
2062
|
+
}
|
|
2063
|
+
const tools = await client.refreshTools();
|
|
2064
|
+
if (mountedRef.current) {
|
|
2065
|
+
setState((prev) => ({ ...prev, tools, lastActivity: Date.now() }));
|
|
2066
|
+
}
|
|
2067
|
+
return tools;
|
|
2068
|
+
}, []);
|
|
2069
|
+
useEffect(() => {
|
|
2070
|
+
mountedRef.current = true;
|
|
2071
|
+
if (autoConnect) {
|
|
2072
|
+
connect().catch(() => {
|
|
2073
|
+
});
|
|
2074
|
+
}
|
|
2075
|
+
return () => {
|
|
2076
|
+
mountedRef.current = false;
|
|
2077
|
+
const client = clientRef.current;
|
|
2078
|
+
if (client) {
|
|
2079
|
+
client.disconnect().catch(() => {
|
|
2080
|
+
});
|
|
2081
|
+
clientRef.current = null;
|
|
2082
|
+
}
|
|
2083
|
+
};
|
|
2084
|
+
}, []);
|
|
2085
|
+
const isConnected = state.connectionState === "connected";
|
|
2086
|
+
const isLoading = state.connectionState === "connecting";
|
|
2087
|
+
return {
|
|
2088
|
+
state,
|
|
2089
|
+
connect,
|
|
2090
|
+
disconnect,
|
|
2091
|
+
callTool,
|
|
2092
|
+
refreshTools,
|
|
2093
|
+
isConnected,
|
|
2094
|
+
isLoading
|
|
2095
|
+
};
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
// src/react/hooks/useMCPTools.ts
|
|
2099
|
+
function useMCPTools(config) {
|
|
2100
|
+
const {
|
|
2101
|
+
prefixToolNames = true,
|
|
2102
|
+
autoRegister = true,
|
|
2103
|
+
hidden = false,
|
|
2104
|
+
source = "mcp",
|
|
2105
|
+
...clientConfig
|
|
2106
|
+
} = config;
|
|
2107
|
+
const { registerTool, unregisterTool } = useCopilot();
|
|
2108
|
+
const registeredToolsRef = useRef([]);
|
|
2109
|
+
const mcpClient = useMCPClient(clientConfig);
|
|
2110
|
+
const toolAdapter = useMemo(
|
|
2111
|
+
() => new MCPToolAdapter(clientConfig.name),
|
|
2112
|
+
[clientConfig.name]
|
|
2113
|
+
);
|
|
2114
|
+
const toolDefinitions = useMemo(() => {
|
|
2115
|
+
if (!mcpClient.isConnected || mcpClient.state.tools.length === 0) {
|
|
2116
|
+
return [];
|
|
2117
|
+
}
|
|
2118
|
+
return mcpClient.state.tools.map(
|
|
2119
|
+
(tool) => toolAdapter.toToolDefinition(tool, {
|
|
2120
|
+
prefix: prefixToolNames,
|
|
2121
|
+
asServerTool: true,
|
|
2122
|
+
// MCP tools execute remotely
|
|
2123
|
+
callTool: mcpClient.callTool,
|
|
2124
|
+
hidden,
|
|
2125
|
+
// Hide from chat UI
|
|
2126
|
+
source
|
|
2127
|
+
// Tool source for UI differentiation
|
|
2128
|
+
})
|
|
2129
|
+
);
|
|
2130
|
+
}, [
|
|
2131
|
+
mcpClient.isConnected,
|
|
2132
|
+
mcpClient.state.tools,
|
|
2133
|
+
mcpClient.callTool,
|
|
2134
|
+
toolAdapter,
|
|
2135
|
+
prefixToolNames,
|
|
2136
|
+
hidden,
|
|
2137
|
+
source
|
|
2138
|
+
]);
|
|
2139
|
+
useEffect(() => {
|
|
2140
|
+
if (!autoRegister) {
|
|
2141
|
+
return;
|
|
2142
|
+
}
|
|
2143
|
+
for (const toolName of registeredToolsRef.current) {
|
|
2144
|
+
unregisterTool(toolName);
|
|
2145
|
+
}
|
|
2146
|
+
registeredToolsRef.current = [];
|
|
2147
|
+
if (mcpClient.isConnected && toolDefinitions.length > 0) {
|
|
2148
|
+
for (const tool of toolDefinitions) {
|
|
2149
|
+
registerTool(tool);
|
|
2150
|
+
registeredToolsRef.current.push(tool.name);
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
return () => {
|
|
2154
|
+
for (const toolName of registeredToolsRef.current) {
|
|
2155
|
+
unregisterTool(toolName);
|
|
2156
|
+
}
|
|
2157
|
+
registeredToolsRef.current = [];
|
|
2158
|
+
};
|
|
2159
|
+
}, [
|
|
2160
|
+
autoRegister,
|
|
2161
|
+
mcpClient.isConnected,
|
|
2162
|
+
toolDefinitions,
|
|
2163
|
+
registerTool,
|
|
2164
|
+
unregisterTool
|
|
2165
|
+
]);
|
|
2166
|
+
return {
|
|
2167
|
+
...mcpClient,
|
|
2168
|
+
toolDefinitions
|
|
2169
|
+
};
|
|
2170
|
+
}
|
|
2171
|
+
function MCPConnection({ config }) {
|
|
2172
|
+
useMCPTools({
|
|
2173
|
+
name: config.name,
|
|
2174
|
+
transport: config.transport,
|
|
2175
|
+
url: config.url,
|
|
2176
|
+
headers: config.headers,
|
|
2177
|
+
autoConnect: true,
|
|
2178
|
+
prefixToolNames: config.prefixToolNames ?? true,
|
|
2179
|
+
timeout: config.timeout
|
|
2180
|
+
});
|
|
2181
|
+
return null;
|
|
2182
|
+
}
|
|
1951
2183
|
var CopilotContext = createContext(null);
|
|
1952
2184
|
function useCopilot() {
|
|
1953
2185
|
const context = useContext(CopilotContext);
|
|
@@ -1969,7 +2201,8 @@ function CopilotProvider({
|
|
|
1969
2201
|
headers,
|
|
1970
2202
|
debug = false,
|
|
1971
2203
|
maxIterations,
|
|
1972
|
-
maxIterationsMessage
|
|
2204
|
+
maxIterationsMessage,
|
|
2205
|
+
mcpServers
|
|
1973
2206
|
}) {
|
|
1974
2207
|
const debugLog = useCallback(
|
|
1975
2208
|
(...args) => {
|
|
@@ -2229,7 +2462,10 @@ function CopilotProvider({
|
|
|
2229
2462
|
toolsConfig
|
|
2230
2463
|
]
|
|
2231
2464
|
);
|
|
2232
|
-
return /* @__PURE__ */
|
|
2465
|
+
return /* @__PURE__ */ jsxs(CopilotContext.Provider, { value: contextValue, children: [
|
|
2466
|
+
mcpServers?.map((config) => /* @__PURE__ */ jsx(MCPConnection, { config }, config.name)),
|
|
2467
|
+
children
|
|
2468
|
+
] });
|
|
2233
2469
|
}
|
|
2234
2470
|
function useAIActions(actions) {
|
|
2235
2471
|
const { registerAction, unregisterAction } = useCopilot();
|
|
@@ -2562,7 +2798,8 @@ function useTool(config, dependencies = []) {
|
|
|
2562
2798
|
render: config.render,
|
|
2563
2799
|
available: config.available ?? true,
|
|
2564
2800
|
needsApproval: config.needsApproval,
|
|
2565
|
-
approvalMessage: config.approvalMessage
|
|
2801
|
+
approvalMessage: config.approvalMessage,
|
|
2802
|
+
hidden: config.hidden
|
|
2566
2803
|
};
|
|
2567
2804
|
registerTool(tool);
|
|
2568
2805
|
return () => {
|
|
@@ -3690,216 +3927,6 @@ function useThreadManager(config) {
|
|
|
3690
3927
|
hasPendingChanges
|
|
3691
3928
|
};
|
|
3692
3929
|
}
|
|
3693
|
-
function useMCPClient(config) {
|
|
3694
|
-
const {
|
|
3695
|
-
autoConnect = true,
|
|
3696
|
-
onConnectionStateChange,
|
|
3697
|
-
onToolsChange,
|
|
3698
|
-
onElicitationRequest,
|
|
3699
|
-
onError,
|
|
3700
|
-
onNotification,
|
|
3701
|
-
...clientConfig
|
|
3702
|
-
} = config;
|
|
3703
|
-
const clientRef = useRef(null);
|
|
3704
|
-
const mountedRef = useRef(true);
|
|
3705
|
-
const [state, setState] = useState({
|
|
3706
|
-
connectionState: "disconnected",
|
|
3707
|
-
tools: []
|
|
3708
|
-
});
|
|
3709
|
-
const getClient = useCallback(() => {
|
|
3710
|
-
if (!clientRef.current) {
|
|
3711
|
-
clientRef.current = createMCPClient(clientConfig, {
|
|
3712
|
-
onConnectionStateChange: (newState) => {
|
|
3713
|
-
if (mountedRef.current) {
|
|
3714
|
-
setState((prev) => ({ ...prev, connectionState: newState }));
|
|
3715
|
-
onConnectionStateChange?.(newState);
|
|
3716
|
-
}
|
|
3717
|
-
},
|
|
3718
|
-
onToolsChange: (tools) => {
|
|
3719
|
-
if (mountedRef.current) {
|
|
3720
|
-
setState((prev) => ({ ...prev, tools }));
|
|
3721
|
-
onToolsChange?.(tools);
|
|
3722
|
-
}
|
|
3723
|
-
},
|
|
3724
|
-
onElicitationRequest,
|
|
3725
|
-
onError: (error) => {
|
|
3726
|
-
if (mountedRef.current) {
|
|
3727
|
-
setState((prev) => ({ ...prev, error: error.message }));
|
|
3728
|
-
onError?.(error);
|
|
3729
|
-
}
|
|
3730
|
-
},
|
|
3731
|
-
onNotification
|
|
3732
|
-
});
|
|
3733
|
-
}
|
|
3734
|
-
return clientRef.current;
|
|
3735
|
-
}, [
|
|
3736
|
-
clientConfig,
|
|
3737
|
-
onConnectionStateChange,
|
|
3738
|
-
onToolsChange,
|
|
3739
|
-
onElicitationRequest,
|
|
3740
|
-
onError,
|
|
3741
|
-
onNotification
|
|
3742
|
-
]);
|
|
3743
|
-
const connect = useCallback(async () => {
|
|
3744
|
-
const client = getClient();
|
|
3745
|
-
try {
|
|
3746
|
-
setState((prev) => ({
|
|
3747
|
-
...prev,
|
|
3748
|
-
connectionState: "connecting",
|
|
3749
|
-
error: void 0
|
|
3750
|
-
}));
|
|
3751
|
-
await client.connect();
|
|
3752
|
-
if (mountedRef.current) {
|
|
3753
|
-
const clientState = client.getState();
|
|
3754
|
-
setState({
|
|
3755
|
-
connectionState: "connected",
|
|
3756
|
-
tools: clientState.tools,
|
|
3757
|
-
serverInfo: clientState.serverInfo,
|
|
3758
|
-
serverCapabilities: clientState.serverCapabilities,
|
|
3759
|
-
lastActivity: Date.now()
|
|
3760
|
-
});
|
|
3761
|
-
}
|
|
3762
|
-
} catch (error) {
|
|
3763
|
-
if (mountedRef.current) {
|
|
3764
|
-
const errorMessage = error instanceof Error ? error.message : "Connection failed";
|
|
3765
|
-
setState((prev) => ({
|
|
3766
|
-
...prev,
|
|
3767
|
-
connectionState: "error",
|
|
3768
|
-
error: errorMessage
|
|
3769
|
-
}));
|
|
3770
|
-
}
|
|
3771
|
-
throw error;
|
|
3772
|
-
}
|
|
3773
|
-
}, [getClient]);
|
|
3774
|
-
const disconnect = useCallback(async () => {
|
|
3775
|
-
const client = clientRef.current;
|
|
3776
|
-
if (client) {
|
|
3777
|
-
await client.disconnect();
|
|
3778
|
-
if (mountedRef.current) {
|
|
3779
|
-
setState({
|
|
3780
|
-
connectionState: "disconnected",
|
|
3781
|
-
tools: []
|
|
3782
|
-
});
|
|
3783
|
-
}
|
|
3784
|
-
}
|
|
3785
|
-
}, []);
|
|
3786
|
-
const callTool = useCallback(
|
|
3787
|
-
async (name, args) => {
|
|
3788
|
-
const client = clientRef.current;
|
|
3789
|
-
if (!client || !client.isConnected()) {
|
|
3790
|
-
throw new Error("MCP client not connected");
|
|
3791
|
-
}
|
|
3792
|
-
const result = await client.callTool(name, args);
|
|
3793
|
-
if (mountedRef.current) {
|
|
3794
|
-
setState((prev) => ({ ...prev, lastActivity: Date.now() }));
|
|
3795
|
-
}
|
|
3796
|
-
return result;
|
|
3797
|
-
},
|
|
3798
|
-
[]
|
|
3799
|
-
);
|
|
3800
|
-
const refreshTools = useCallback(async () => {
|
|
3801
|
-
const client = clientRef.current;
|
|
3802
|
-
if (!client || !client.isConnected()) {
|
|
3803
|
-
throw new Error("MCP client not connected");
|
|
3804
|
-
}
|
|
3805
|
-
const tools = await client.refreshTools();
|
|
3806
|
-
if (mountedRef.current) {
|
|
3807
|
-
setState((prev) => ({ ...prev, tools, lastActivity: Date.now() }));
|
|
3808
|
-
}
|
|
3809
|
-
return tools;
|
|
3810
|
-
}, []);
|
|
3811
|
-
useEffect(() => {
|
|
3812
|
-
mountedRef.current = true;
|
|
3813
|
-
if (autoConnect) {
|
|
3814
|
-
connect().catch(() => {
|
|
3815
|
-
});
|
|
3816
|
-
}
|
|
3817
|
-
return () => {
|
|
3818
|
-
mountedRef.current = false;
|
|
3819
|
-
const client = clientRef.current;
|
|
3820
|
-
if (client) {
|
|
3821
|
-
client.disconnect().catch(() => {
|
|
3822
|
-
});
|
|
3823
|
-
clientRef.current = null;
|
|
3824
|
-
}
|
|
3825
|
-
};
|
|
3826
|
-
}, []);
|
|
3827
|
-
const isConnected = state.connectionState === "connected";
|
|
3828
|
-
const isLoading = state.connectionState === "connecting";
|
|
3829
|
-
return {
|
|
3830
|
-
state,
|
|
3831
|
-
connect,
|
|
3832
|
-
disconnect,
|
|
3833
|
-
callTool,
|
|
3834
|
-
refreshTools,
|
|
3835
|
-
isConnected,
|
|
3836
|
-
isLoading
|
|
3837
|
-
};
|
|
3838
|
-
}
|
|
3839
|
-
function useMCPTools(config) {
|
|
3840
|
-
const {
|
|
3841
|
-
prefixToolNames = true,
|
|
3842
|
-
autoRegister = true,
|
|
3843
|
-
...clientConfig
|
|
3844
|
-
} = config;
|
|
3845
|
-
const { registerTool, unregisterTool } = useCopilot();
|
|
3846
|
-
const registeredToolsRef = useRef([]);
|
|
3847
|
-
const mcpClient = useMCPClient(clientConfig);
|
|
3848
|
-
const toolAdapter = useMemo(
|
|
3849
|
-
() => new MCPToolAdapter(clientConfig.name),
|
|
3850
|
-
[clientConfig.name]
|
|
3851
|
-
);
|
|
3852
|
-
const toolDefinitions = useMemo(() => {
|
|
3853
|
-
if (!mcpClient.isConnected || mcpClient.state.tools.length === 0) {
|
|
3854
|
-
return [];
|
|
3855
|
-
}
|
|
3856
|
-
return mcpClient.state.tools.map(
|
|
3857
|
-
(tool) => toolAdapter.toToolDefinition(tool, {
|
|
3858
|
-
prefix: prefixToolNames,
|
|
3859
|
-
asServerTool: true,
|
|
3860
|
-
// MCP tools execute remotely
|
|
3861
|
-
callTool: mcpClient.callTool
|
|
3862
|
-
})
|
|
3863
|
-
);
|
|
3864
|
-
}, [
|
|
3865
|
-
mcpClient.isConnected,
|
|
3866
|
-
mcpClient.state.tools,
|
|
3867
|
-
mcpClient.callTool,
|
|
3868
|
-
toolAdapter,
|
|
3869
|
-
prefixToolNames
|
|
3870
|
-
]);
|
|
3871
|
-
useEffect(() => {
|
|
3872
|
-
if (!autoRegister) {
|
|
3873
|
-
return;
|
|
3874
|
-
}
|
|
3875
|
-
for (const toolName of registeredToolsRef.current) {
|
|
3876
|
-
unregisterTool(toolName);
|
|
3877
|
-
}
|
|
3878
|
-
registeredToolsRef.current = [];
|
|
3879
|
-
if (mcpClient.isConnected && toolDefinitions.length > 0) {
|
|
3880
|
-
for (const tool of toolDefinitions) {
|
|
3881
|
-
registerTool(tool);
|
|
3882
|
-
registeredToolsRef.current.push(tool.name);
|
|
3883
|
-
}
|
|
3884
|
-
}
|
|
3885
|
-
return () => {
|
|
3886
|
-
for (const toolName of registeredToolsRef.current) {
|
|
3887
|
-
unregisterTool(toolName);
|
|
3888
|
-
}
|
|
3889
|
-
registeredToolsRef.current = [];
|
|
3890
|
-
};
|
|
3891
|
-
}, [
|
|
3892
|
-
autoRegister,
|
|
3893
|
-
mcpClient.isConnected,
|
|
3894
|
-
toolDefinitions,
|
|
3895
|
-
registerTool,
|
|
3896
|
-
unregisterTool
|
|
3897
|
-
]);
|
|
3898
|
-
return {
|
|
3899
|
-
...mcpClient,
|
|
3900
|
-
toolDefinitions
|
|
3901
|
-
};
|
|
3902
|
-
}
|
|
3903
3930
|
function useMCPUIIntents(config = {}) {
|
|
3904
3931
|
const {
|
|
3905
3932
|
onToolCall,
|
|
@@ -4253,5 +4280,5 @@ function useChat(config) {
|
|
|
4253
4280
|
}
|
|
4254
4281
|
|
|
4255
4282
|
export { AbstractAgentLoop, AbstractChat, CopilotProvider, ReactChat, ReactChatState, ReactThreadManager, ReactThreadManagerState, createMessageIntentHandler, createPermissionStorage, createReactChat, createReactChatState, createReactThreadManager, createReactThreadManagerState, createSessionPermissionCache, createToolIntentHandler, formatKnowledgeResultsForAI, initialAgentLoopState, searchKnowledgeBase, useAIAction, useAIActions, useAIContext, useAIContexts, useAITools, useAgent, useCapabilities, useChat, useCopilot, useDevLogger, useFeatureSupport, useKnowledgeBase, useMCPClient, useMCPTools, useMCPUIIntents, useSuggestions, useSupportedMediaTypes, useThreadManager, useTool, useToolExecutor, useToolWithSchema, useTools, useToolsWithSchema };
|
|
4256
|
-
//# sourceMappingURL=chunk-
|
|
4257
|
-
//# sourceMappingURL=chunk-
|
|
4283
|
+
//# sourceMappingURL=chunk-3SP6V22Y.js.map
|
|
4284
|
+
//# sourceMappingURL=chunk-3SP6V22Y.js.map
|