mcp-use 1.2.2-canary.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/{chunk-VPPILX7B.js → chunk-3RJENWH4.js} +15 -6
- package/dist/{chunk-UVUM35MV.js → chunk-7UX634PO.js} +307 -112
- package/dist/{chunk-CPV4QNHD.js → chunk-KLIBVJ3Z.js} +145 -79
- package/dist/{chunk-EYAIJPBH.js → chunk-MZLETWQQ.js} +55 -17
- package/dist/{chunk-C3SRZK7H.js → chunk-RSGKBEHH.js} +144 -72
- package/dist/index.cjs +719 -300
- package/dist/index.d.ts +24 -24
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +86 -28
- package/dist/{langfuse-MO3AMDBE.js → langfuse-LCJ6VJEP.js} +2 -2
- package/dist/src/adapters/base.d.ts +2 -2
- package/dist/src/adapters/base.d.ts.map +1 -1
- package/dist/src/adapters/index.d.ts +2 -2
- package/dist/src/adapters/index.d.ts.map +1 -1
- package/dist/src/adapters/langchain_adapter.d.ts +4 -4
- package/dist/src/adapters/langchain_adapter.d.ts.map +1 -1
- package/dist/src/agents/base.d.ts +1 -1
- package/dist/src/agents/base.d.ts.map +1 -1
- package/dist/src/agents/index.d.ts +3 -3
- package/dist/src/agents/index.d.ts.map +1 -1
- package/dist/src/agents/mcp_agent.d.ts +12 -12
- package/dist/src/agents/mcp_agent.d.ts.map +1 -1
- package/dist/src/agents/prompts/system_prompt_builder.d.ts +2 -2
- package/dist/src/agents/prompts/system_prompt_builder.d.ts.map +1 -1
- package/dist/src/agents/prompts/templates.d.ts.map +1 -1
- package/dist/src/agents/remote.d.ts +2 -2
- package/dist/src/agents/remote.d.ts.map +1 -1
- package/dist/src/agents/types.d.ts +1 -1
- package/dist/src/agents/types.d.ts.map +1 -1
- package/dist/src/agents/utils/ai_sdk.d.ts +1 -1
- package/dist/src/agents/utils/ai_sdk.d.ts.map +1 -1
- package/dist/src/agents/utils/index.d.ts +1 -1
- package/dist/src/agents/utils/index.d.ts.map +1 -1
- package/dist/src/auth/browser-provider.d.ts +2 -2
- package/dist/src/auth/browser-provider.d.ts.map +1 -1
- package/dist/src/auth/callback.d.ts.map +1 -1
- package/dist/src/auth/index.d.ts +3 -3
- package/dist/src/auth/index.d.ts.map +1 -1
- package/dist/src/auth/types.d.ts +1 -1
- package/dist/src/auth/types.d.ts.map +1 -1
- package/dist/src/browser.cjs +508 -201
- package/dist/src/browser.d.ts +17 -17
- package/dist/src/browser.d.ts.map +1 -1
- package/dist/src/browser.js +11 -5
- package/dist/src/client/base.d.ts +2 -2
- package/dist/src/client/base.d.ts.map +1 -1
- package/dist/src/client/browser.d.ts +2 -2
- package/dist/src/client/browser.d.ts.map +1 -1
- package/dist/src/client.d.ts +2 -2
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/config.d.ts +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/connectors/base.d.ts +5 -5
- package/dist/src/connectors/base.d.ts.map +1 -1
- package/dist/src/connectors/http.d.ts +3 -3
- package/dist/src/connectors/http.d.ts.map +1 -1
- package/dist/src/connectors/index.d.ts +4 -4
- package/dist/src/connectors/index.d.ts.map +1 -1
- package/dist/src/connectors/stdio.d.ts +3 -3
- package/dist/src/connectors/stdio.d.ts.map +1 -1
- package/dist/src/connectors/websocket.d.ts +3 -3
- package/dist/src/connectors/websocket.d.ts.map +1 -1
- package/dist/src/logging.d.ts +4 -4
- package/dist/src/logging.d.ts.map +1 -1
- package/dist/src/managers/index.d.ts +2 -2
- package/dist/src/managers/index.d.ts.map +1 -1
- package/dist/src/managers/server_manager.d.ts +4 -4
- package/dist/src/managers/server_manager.d.ts.map +1 -1
- package/dist/src/managers/tools/acquire_active_mcp_server.d.ts +3 -3
- package/dist/src/managers/tools/acquire_active_mcp_server.d.ts.map +1 -1
- package/dist/src/managers/tools/add_server_from_config.d.ts +3 -3
- package/dist/src/managers/tools/add_server_from_config.d.ts.map +1 -1
- package/dist/src/managers/tools/base.d.ts +6 -6
- package/dist/src/managers/tools/base.d.ts.map +1 -1
- package/dist/src/managers/tools/connect_mcp_server.d.ts +4 -4
- package/dist/src/managers/tools/connect_mcp_server.d.ts.map +1 -1
- package/dist/src/managers/tools/index.d.ts +5 -5
- package/dist/src/managers/tools/index.d.ts.map +1 -1
- package/dist/src/managers/tools/list_mcp_servers.d.ts +3 -3
- package/dist/src/managers/tools/list_mcp_servers.d.ts.map +1 -1
- package/dist/src/managers/tools/release_mcp_server_connection.d.ts +3 -3
- package/dist/src/managers/tools/release_mcp_server_connection.d.ts.map +1 -1
- package/dist/src/managers/types.d.ts +3 -3
- package/dist/src/managers/types.d.ts.map +1 -1
- package/dist/src/oauth-helper.d.ts.map +1 -1
- package/dist/src/observability/index.d.ts +3 -3
- package/dist/src/observability/index.d.ts.map +1 -1
- package/dist/src/observability/langfuse.d.ts +1 -1
- package/dist/src/observability/langfuse.d.ts.map +1 -1
- package/dist/src/observability/manager.d.ts +1 -1
- package/dist/src/observability/manager.d.ts.map +1 -1
- package/dist/src/observability/types.d.ts +1 -1
- package/dist/src/observability/types.d.ts.map +1 -1
- package/dist/src/react/index.cjs +295 -154
- package/dist/src/react/index.d.ts +6 -6
- package/dist/src/react/index.d.ts.map +1 -1
- package/dist/src/react/index.js +3 -3
- package/dist/src/react/types.d.ts +6 -6
- package/dist/src/react/types.d.ts.map +1 -1
- package/dist/src/react/useMcp.d.ts +1 -1
- package/dist/src/react/useMcp.d.ts.map +1 -1
- package/dist/src/react/useWidget.d.ts +5 -2
- package/dist/src/react/useWidget.d.ts.map +1 -1
- package/dist/src/react/widget-types.d.ts +3 -3
- package/dist/src/react/widget-types.d.ts.map +1 -1
- package/dist/src/server/adapters/mcp-ui-adapter.d.ts +3 -3
- package/dist/src/server/adapters/mcp-ui-adapter.d.ts.map +1 -1
- package/dist/src/server/index.cjs +170 -50
- package/dist/src/server/index.d.ts +4 -4
- package/dist/src/server/index.d.ts.map +1 -1
- package/dist/src/server/index.js +174 -51
- package/dist/src/server/logging.d.ts +1 -1
- package/dist/src/server/logging.d.ts.map +1 -1
- package/dist/src/server/mcp-server.d.ts +29 -29
- package/dist/src/server/mcp-server.d.ts.map +1 -1
- package/dist/src/server/types/common.d.ts +2 -2
- package/dist/src/server/types/common.d.ts.map +1 -1
- package/dist/src/server/types/index.d.ts +4 -4
- package/dist/src/server/types/index.d.ts.map +1 -1
- package/dist/src/server/types/prompt.d.ts +2 -2
- package/dist/src/server/types/prompt.d.ts.map +1 -1
- package/dist/src/server/types/resource.d.ts +24 -24
- package/dist/src/server/types/resource.d.ts.map +1 -1
- package/dist/src/server/types/tool.d.ts +3 -3
- package/dist/src/server/types/tool.d.ts.map +1 -1
- package/dist/src/server/types/widget.d.ts +1 -1
- package/dist/src/server/types.d.ts +1 -1
- package/dist/src/server/types.d.ts.map +1 -1
- package/dist/src/session.d.ts +1 -1
- package/dist/src/session.d.ts.map +1 -1
- package/dist/src/task_managers/base.d.ts.map +1 -1
- package/dist/src/task_managers/index.d.ts +5 -5
- package/dist/src/task_managers/index.d.ts.map +1 -1
- package/dist/src/task_managers/sse.d.ts +3 -3
- package/dist/src/task_managers/sse.d.ts.map +1 -1
- package/dist/src/task_managers/stdio.d.ts +4 -4
- package/dist/src/task_managers/stdio.d.ts.map +1 -1
- package/dist/src/task_managers/streamable_http.d.ts +3 -3
- package/dist/src/task_managers/streamable_http.d.ts.map +1 -1
- package/dist/src/task_managers/websocket.d.ts +2 -2
- package/dist/src/task_managers/websocket.d.ts.map +1 -1
- package/dist/src/telemetry/events.d.ts.map +1 -1
- package/dist/src/telemetry/index.d.ts +4 -4
- package/dist/src/telemetry/index.d.ts.map +1 -1
- package/dist/src/telemetry/telemetry.d.ts +1 -1
- package/dist/src/telemetry/telemetry.d.ts.map +1 -1
- package/dist/src/telemetry/utils.d.ts +1 -1
- package/dist/src/telemetry/utils.d.ts.map +1 -1
- package/dist/tsup.config.d.ts.map +1 -1
- package/dist/vitest.config.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/src/react/index.cjs
CHANGED
|
@@ -92,7 +92,15 @@ var SimpleConsoleLogger = class {
|
|
|
92
92
|
this._level = level;
|
|
93
93
|
}
|
|
94
94
|
shouldLog(level) {
|
|
95
|
-
const levels = [
|
|
95
|
+
const levels = [
|
|
96
|
+
"error",
|
|
97
|
+
"warn",
|
|
98
|
+
"info",
|
|
99
|
+
"http",
|
|
100
|
+
"verbose",
|
|
101
|
+
"debug",
|
|
102
|
+
"silly"
|
|
103
|
+
];
|
|
96
104
|
const currentIndex = levels.indexOf(this._level);
|
|
97
105
|
const messageIndex = levels.indexOf(level);
|
|
98
106
|
return messageIndex <= currentIndex;
|
|
@@ -176,7 +184,10 @@ var Logger = class {
|
|
|
176
184
|
if (!isNodeJSEnvironment()) {
|
|
177
185
|
if (!this.simpleInstances[name]) {
|
|
178
186
|
const debugEnv = typeof process !== "undefined" && process.env?.DEBUG || void 0;
|
|
179
|
-
this.simpleInstances[name] = new SimpleConsoleLogger(
|
|
187
|
+
this.simpleInstances[name] = new SimpleConsoleLogger(
|
|
188
|
+
name,
|
|
189
|
+
resolveLevel(debugEnv)
|
|
190
|
+
);
|
|
180
191
|
}
|
|
181
192
|
return this.simpleInstances[name];
|
|
182
193
|
}
|
|
@@ -250,10 +261,8 @@ var Logger = class {
|
|
|
250
261
|
}
|
|
251
262
|
static setDebug(enabled) {
|
|
252
263
|
let level;
|
|
253
|
-
if (enabled === 2 || enabled === true)
|
|
254
|
-
|
|
255
|
-
else if (enabled === 1)
|
|
256
|
-
level = "info";
|
|
264
|
+
if (enabled === 2 || enabled === true) level = "debug";
|
|
265
|
+
else if (enabled === 1) level = "info";
|
|
257
266
|
else level = "info";
|
|
258
267
|
Object.values(this.simpleInstances).forEach((logger2) => {
|
|
259
268
|
logger2.level = level;
|
|
@@ -368,7 +377,9 @@ var ConnectionManager = class {
|
|
|
368
377
|
try {
|
|
369
378
|
await this.closeConnection(this._connection);
|
|
370
379
|
} catch (closeErr) {
|
|
371
|
-
logger.warn(
|
|
380
|
+
logger.warn(
|
|
381
|
+
`Error closing connection in ${this.constructor.name}: ${closeErr}`
|
|
382
|
+
);
|
|
372
383
|
}
|
|
373
384
|
this._connection = null;
|
|
374
385
|
}
|
|
@@ -534,7 +545,10 @@ var BaseConnector = class {
|
|
|
534
545
|
logger.debug("Caching server capabilities & tools");
|
|
535
546
|
const capabilities = this.client.getServerCapabilities();
|
|
536
547
|
this.capabilitiesCache = capabilities;
|
|
537
|
-
const listToolsRes = await this.client.listTools(
|
|
548
|
+
const listToolsRes = await this.client.listTools(
|
|
549
|
+
void 0,
|
|
550
|
+
defaultRequestOptions
|
|
551
|
+
);
|
|
538
552
|
this.toolsCache = listToolsRes.tools ?? [];
|
|
539
553
|
logger.debug(`Fetched ${this.toolsCache.length} tools from server`);
|
|
540
554
|
logger.debug("Server capabilities:", capabilities);
|
|
@@ -553,7 +567,11 @@ var BaseConnector = class {
|
|
|
553
567
|
throw new Error("MCP client is not connected");
|
|
554
568
|
}
|
|
555
569
|
logger.debug(`Calling tool '${name}' with args`, args);
|
|
556
|
-
const res = await this.client.callTool(
|
|
570
|
+
const res = await this.client.callTool(
|
|
571
|
+
{ name, arguments: args },
|
|
572
|
+
void 0,
|
|
573
|
+
options
|
|
574
|
+
);
|
|
557
575
|
logger.debug(`Tool '${name}' returned`, res);
|
|
558
576
|
return res;
|
|
559
577
|
}
|
|
@@ -683,7 +701,11 @@ var BaseConnector = class {
|
|
|
683
701
|
throw new Error("MCP client is not connected");
|
|
684
702
|
}
|
|
685
703
|
logger.debug(`Sending raw request '${method}' with params`, params);
|
|
686
|
-
return await this.client.request(
|
|
704
|
+
return await this.client.request(
|
|
705
|
+
{ method, params: params ?? {} },
|
|
706
|
+
void 0,
|
|
707
|
+
options
|
|
708
|
+
);
|
|
687
709
|
}
|
|
688
710
|
/**
|
|
689
711
|
* Helper to tear down the client & connection manager safely.
|
|
@@ -742,7 +764,10 @@ var HttpConnector = class extends BaseConnector {
|
|
|
742
764
|
}
|
|
743
765
|
this.timeout = opts.timeout ?? 3e4;
|
|
744
766
|
this.sseReadTimeout = opts.sseReadTimeout ?? 3e5;
|
|
745
|
-
this.clientInfo = opts.clientInfo ?? {
|
|
767
|
+
this.clientInfo = opts.clientInfo ?? {
|
|
768
|
+
name: "http-connector",
|
|
769
|
+
version: "1.0.0"
|
|
770
|
+
};
|
|
746
771
|
this.preferSse = opts.preferSse ?? false;
|
|
747
772
|
}
|
|
748
773
|
/** Establish connection to the MCP implementation via HTTP (streamable or SSE). */
|
|
@@ -813,29 +838,28 @@ var HttpConnector = class extends BaseConnector {
|
|
|
813
838
|
authError.code = 401;
|
|
814
839
|
throw authError;
|
|
815
840
|
}
|
|
816
|
-
throw new Error(
|
|
841
|
+
throw new Error(
|
|
842
|
+
"Could not connect to server with any available transport"
|
|
843
|
+
);
|
|
817
844
|
}
|
|
818
845
|
}
|
|
819
846
|
}
|
|
820
847
|
async connectWithStreamableHttp(baseUrl) {
|
|
821
848
|
try {
|
|
822
|
-
this.connectionManager = new StreamableHttpConnectionManager(
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
reconnectionDelayGrowFactor: 1.5,
|
|
835
|
-
maxRetries: 2
|
|
836
|
-
}
|
|
849
|
+
this.connectionManager = new StreamableHttpConnectionManager(baseUrl, {
|
|
850
|
+
authProvider: this.opts.authProvider,
|
|
851
|
+
// ← Pass OAuth provider to SDK
|
|
852
|
+
requestInit: {
|
|
853
|
+
headers: this.headers
|
|
854
|
+
},
|
|
855
|
+
// Pass through timeout and other options
|
|
856
|
+
reconnectionOptions: {
|
|
857
|
+
maxReconnectionDelay: 3e4,
|
|
858
|
+
initialReconnectionDelay: 1e3,
|
|
859
|
+
reconnectionDelayGrowFactor: 1.5,
|
|
860
|
+
maxRetries: 2
|
|
837
861
|
}
|
|
838
|
-
);
|
|
862
|
+
});
|
|
839
863
|
const transport = await this.connectionManager.start();
|
|
840
864
|
this.client = new import_client.Client(this.clientInfo, this.opts.clientOptions);
|
|
841
865
|
try {
|
|
@@ -844,7 +868,9 @@ var HttpConnector = class extends BaseConnector {
|
|
|
844
868
|
if (connectErr instanceof Error) {
|
|
845
869
|
const errMsg = connectErr.message || connectErr.toString();
|
|
846
870
|
if (errMsg.includes("Missing session ID") || errMsg.includes("Bad Request: Missing session ID")) {
|
|
847
|
-
const wrappedError = new Error(
|
|
871
|
+
const wrappedError = new Error(
|
|
872
|
+
`FastMCP session ID error: ${errMsg}`
|
|
873
|
+
);
|
|
848
874
|
wrappedError.cause = connectErr;
|
|
849
875
|
throw wrappedError;
|
|
850
876
|
}
|
|
@@ -853,7 +879,9 @@ var HttpConnector = class extends BaseConnector {
|
|
|
853
879
|
}
|
|
854
880
|
this.connected = true;
|
|
855
881
|
this.transportType = "streamable-http";
|
|
856
|
-
logger.debug(
|
|
882
|
+
logger.debug(
|
|
883
|
+
`Successfully connected to MCP implementation via streamable HTTP: ${baseUrl}`
|
|
884
|
+
);
|
|
857
885
|
} catch (err) {
|
|
858
886
|
await this.cleanupResources();
|
|
859
887
|
throw err;
|
|
@@ -861,20 +889,19 @@ var HttpConnector = class extends BaseConnector {
|
|
|
861
889
|
}
|
|
862
890
|
async connectWithSse(baseUrl) {
|
|
863
891
|
try {
|
|
864
|
-
this.connectionManager = new SseConnectionManager(
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
requestInit: {
|
|
868
|
-
headers: this.headers
|
|
869
|
-
}
|
|
892
|
+
this.connectionManager = new SseConnectionManager(baseUrl, {
|
|
893
|
+
requestInit: {
|
|
894
|
+
headers: this.headers
|
|
870
895
|
}
|
|
871
|
-
);
|
|
896
|
+
});
|
|
872
897
|
const transport = await this.connectionManager.start();
|
|
873
898
|
this.client = new import_client.Client(this.clientInfo, this.opts.clientOptions);
|
|
874
899
|
await this.client.connect(transport);
|
|
875
900
|
this.connected = true;
|
|
876
901
|
this.transportType = "sse";
|
|
877
|
-
logger.debug(
|
|
902
|
+
logger.debug(
|
|
903
|
+
`Successfully connected to MCP implementation via HTTP/SSE: ${baseUrl}`
|
|
904
|
+
);
|
|
878
905
|
} catch (err) {
|
|
879
906
|
await this.cleanupResources();
|
|
880
907
|
throw err;
|
|
@@ -920,7 +947,9 @@ var WebSocketConnectionManager = class extends ConnectionManager {
|
|
|
920
947
|
async establishConnection() {
|
|
921
948
|
logger.debug(`Connecting to WebSocket: ${this.url}`);
|
|
922
949
|
return new Promise((resolve, reject) => {
|
|
923
|
-
const ws = new import_ws.default(this.url, {
|
|
950
|
+
const ws = new import_ws.default(this.url, {
|
|
951
|
+
headers: this.headers
|
|
952
|
+
});
|
|
924
953
|
this._ws = ws;
|
|
925
954
|
const onOpen = /* @__PURE__ */ __name(() => {
|
|
926
955
|
cleanup();
|
|
@@ -980,8 +1009,7 @@ var WebSocketConnector = class extends BaseConnector {
|
|
|
980
1009
|
super();
|
|
981
1010
|
this.url = url;
|
|
982
1011
|
this.headers = { ...opts.headers ?? {} };
|
|
983
|
-
if (opts.authToken)
|
|
984
|
-
this.headers.Authorization = `Bearer ${opts.authToken}`;
|
|
1012
|
+
if (opts.authToken) this.headers.Authorization = `Bearer ${opts.authToken}`;
|
|
985
1013
|
}
|
|
986
1014
|
async connect() {
|
|
987
1015
|
if (this.connected) {
|
|
@@ -990,7 +1018,10 @@ var WebSocketConnector = class extends BaseConnector {
|
|
|
990
1018
|
}
|
|
991
1019
|
logger.debug(`Connecting via WebSocket: ${this.url}`);
|
|
992
1020
|
try {
|
|
993
|
-
this.connectionManager = new WebSocketConnectionManager(
|
|
1021
|
+
this.connectionManager = new WebSocketConnectionManager(
|
|
1022
|
+
this.url,
|
|
1023
|
+
this.headers
|
|
1024
|
+
);
|
|
994
1025
|
this.ws = await this.connectionManager.start();
|
|
995
1026
|
this.receiverTask = this.receiveLoop();
|
|
996
1027
|
this.connected = true;
|
|
@@ -1011,8 +1042,7 @@ var WebSocketConnector = class extends BaseConnector {
|
|
|
1011
1042
|
this.connected = false;
|
|
1012
1043
|
}
|
|
1013
1044
|
sendRequest(method, params = null) {
|
|
1014
|
-
if (!this.ws)
|
|
1015
|
-
throw new Error("WebSocket is not connected");
|
|
1045
|
+
if (!this.ws) throw new Error("WebSocket is not connected");
|
|
1016
1046
|
const id = (0, import_uuid.v4)();
|
|
1017
1047
|
const payload = JSON.stringify({ id, method, params: params ?? {} });
|
|
1018
1048
|
return new Promise((resolve, reject) => {
|
|
@@ -1026,8 +1056,7 @@ var WebSocketConnector = class extends BaseConnector {
|
|
|
1026
1056
|
});
|
|
1027
1057
|
}
|
|
1028
1058
|
async receiveLoop() {
|
|
1029
|
-
if (!this.ws)
|
|
1030
|
-
return;
|
|
1059
|
+
if (!this.ws) return;
|
|
1031
1060
|
const socket = this.ws;
|
|
1032
1061
|
const onMessage = /* @__PURE__ */ __name((msg) => {
|
|
1033
1062
|
let data;
|
|
@@ -1041,10 +1070,8 @@ var WebSocketConnector = class extends BaseConnector {
|
|
|
1041
1070
|
if (id && this.pending.has(id)) {
|
|
1042
1071
|
const { resolve, reject } = this.pending.get(id);
|
|
1043
1072
|
this.pending.delete(id);
|
|
1044
|
-
if ("result" in data)
|
|
1045
|
-
|
|
1046
|
-
else if ("error" in data)
|
|
1047
|
-
reject(data.error);
|
|
1073
|
+
if ("result" in data) resolve(data.result);
|
|
1074
|
+
else if ("error" in data) reject(data.error);
|
|
1048
1075
|
} else {
|
|
1049
1076
|
logger.debug("Received unsolicited message", data);
|
|
1050
1077
|
}
|
|
@@ -1102,14 +1129,12 @@ var WebSocketConnector = class extends BaseConnector {
|
|
|
1102
1129
|
return await this.sendRequest(method, params);
|
|
1103
1130
|
}
|
|
1104
1131
|
get tools() {
|
|
1105
|
-
if (!this.toolsCache)
|
|
1106
|
-
throw new Error("MCP client is not initialized");
|
|
1132
|
+
if (!this.toolsCache) throw new Error("MCP client is not initialized");
|
|
1107
1133
|
return this.toolsCache;
|
|
1108
1134
|
}
|
|
1109
1135
|
async cleanupResources() {
|
|
1110
|
-
if (this.receiverTask)
|
|
1111
|
-
|
|
1112
|
-
});
|
|
1136
|
+
if (this.receiverTask) await this.receiverTask.catch(() => {
|
|
1137
|
+
});
|
|
1113
1138
|
this.receiverTask = null;
|
|
1114
1139
|
this.rejectAll(new Error("WebSocket disconnected"));
|
|
1115
1140
|
if (this.connectionManager) {
|
|
@@ -1234,7 +1259,9 @@ var BaseMCPClient = class {
|
|
|
1234
1259
|
async closeSession(serverName) {
|
|
1235
1260
|
const session = this.sessions[serverName];
|
|
1236
1261
|
if (!session) {
|
|
1237
|
-
logger.warn(
|
|
1262
|
+
logger.warn(
|
|
1263
|
+
`No session exists for server ${serverName}, nothing to close`
|
|
1264
|
+
);
|
|
1238
1265
|
return;
|
|
1239
1266
|
}
|
|
1240
1267
|
try {
|
|
@@ -1261,7 +1288,9 @@ var BaseMCPClient = class {
|
|
|
1261
1288
|
}
|
|
1262
1289
|
}
|
|
1263
1290
|
if (errors.length) {
|
|
1264
|
-
logger.error(
|
|
1291
|
+
logger.error(
|
|
1292
|
+
`Encountered ${errors.length} errors while closing sessions`
|
|
1293
|
+
);
|
|
1265
1294
|
} else {
|
|
1266
1295
|
logger.debug("All sessions closed successfully");
|
|
1267
1296
|
}
|
|
@@ -1353,7 +1382,10 @@ var BrowserOAuthClientProvider = class {
|
|
|
1353
1382
|
try {
|
|
1354
1383
|
return JSON.parse(data);
|
|
1355
1384
|
} catch (e) {
|
|
1356
|
-
console.warn(
|
|
1385
|
+
console.warn(
|
|
1386
|
+
`[${this.storageKeyPrefix}] Failed to parse client information:`,
|
|
1387
|
+
e
|
|
1388
|
+
);
|
|
1357
1389
|
localStorage.removeItem(key);
|
|
1358
1390
|
return void 0;
|
|
1359
1391
|
}
|
|
@@ -1435,7 +1467,11 @@ var BrowserOAuthClientProvider = class {
|
|
|
1435
1467
|
const sanitizedAuthUrl = await this.prepareAuthorizationUrl(authorizationUrl);
|
|
1436
1468
|
const popupFeatures = "width=600,height=700,resizable=yes,scrollbars=yes,status=yes";
|
|
1437
1469
|
try {
|
|
1438
|
-
const popup = window.open(
|
|
1470
|
+
const popup = window.open(
|
|
1471
|
+
sanitizedAuthUrl,
|
|
1472
|
+
`mcp_auth_${this.serverUrlHash}`,
|
|
1473
|
+
popupFeatures
|
|
1474
|
+
);
|
|
1439
1475
|
if (this.onPopupWindow) {
|
|
1440
1476
|
this.onPopupWindow(sanitizedAuthUrl, popupFeatures, popup);
|
|
1441
1477
|
}
|
|
@@ -1445,10 +1481,15 @@ var BrowserOAuthClientProvider = class {
|
|
|
1445
1481
|
);
|
|
1446
1482
|
} else {
|
|
1447
1483
|
popup.focus();
|
|
1448
|
-
console.info(
|
|
1484
|
+
console.info(
|
|
1485
|
+
`[${this.storageKeyPrefix}] Redirecting to authorization URL in popup.`
|
|
1486
|
+
);
|
|
1449
1487
|
}
|
|
1450
1488
|
} catch (e) {
|
|
1451
|
-
console.error(
|
|
1489
|
+
console.error(
|
|
1490
|
+
`[${this.storageKeyPrefix}] Error opening popup window:`,
|
|
1491
|
+
e
|
|
1492
|
+
);
|
|
1452
1493
|
}
|
|
1453
1494
|
}
|
|
1454
1495
|
// --- Helper Methods ---
|
|
@@ -1479,7 +1520,10 @@ var BrowserOAuthClientProvider = class {
|
|
|
1479
1520
|
}
|
|
1480
1521
|
}
|
|
1481
1522
|
} catch (e) {
|
|
1482
|
-
console.warn(
|
|
1523
|
+
console.warn(
|
|
1524
|
+
`[${this.storageKeyPrefix}] Error parsing state key ${key} during clearStorage:`,
|
|
1525
|
+
e
|
|
1526
|
+
);
|
|
1483
1527
|
}
|
|
1484
1528
|
}
|
|
1485
1529
|
}
|
|
@@ -1522,7 +1566,9 @@ function useMcp(options) {
|
|
|
1522
1566
|
enabled = true,
|
|
1523
1567
|
clientName,
|
|
1524
1568
|
clientUri,
|
|
1525
|
-
callbackUrl = typeof window !== "undefined" ? (0, import_strict_url_sanitise2.sanitizeUrl)(
|
|
1569
|
+
callbackUrl = typeof window !== "undefined" ? (0, import_strict_url_sanitise2.sanitizeUrl)(
|
|
1570
|
+
new URL("/oauth/callback", window.location.origin).toString()
|
|
1571
|
+
) : "/oauth/callback",
|
|
1526
1572
|
storageKeyPrefix = "mcp:auth",
|
|
1527
1573
|
clientConfig = {},
|
|
1528
1574
|
customHeaders = {},
|
|
@@ -1563,7 +1609,10 @@ function useMcp(options) {
|
|
|
1563
1609
|
const fullMessage = args.length > 0 ? `${message} ${args.map((arg) => JSON.stringify(arg)).join(" ")}` : message;
|
|
1564
1610
|
console[level](`[useMcp] ${fullMessage}`);
|
|
1565
1611
|
if (isMountedRef.current) {
|
|
1566
|
-
setLog((prevLog) => [
|
|
1612
|
+
setLog((prevLog) => [
|
|
1613
|
+
...prevLog.slice(-100),
|
|
1614
|
+
{ level, message: fullMessage, timestamp: Date.now() }
|
|
1615
|
+
]);
|
|
1567
1616
|
}
|
|
1568
1617
|
},
|
|
1569
1618
|
[]
|
|
@@ -1604,7 +1653,11 @@ function useMcp(options) {
|
|
|
1604
1653
|
const manualUrl = authProviderRef.current?.getLastAttemptedAuthUrl();
|
|
1605
1654
|
if (manualUrl) {
|
|
1606
1655
|
setAuthUrl(manualUrl);
|
|
1607
|
-
addLog(
|
|
1656
|
+
addLog(
|
|
1657
|
+
"info",
|
|
1658
|
+
"Manual authentication URL may be available.",
|
|
1659
|
+
manualUrl
|
|
1660
|
+
);
|
|
1608
1661
|
}
|
|
1609
1662
|
}
|
|
1610
1663
|
connectingRef.current = false;
|
|
@@ -1613,7 +1666,10 @@ function useMcp(options) {
|
|
|
1613
1666
|
);
|
|
1614
1667
|
const connect = (0, import_react.useCallback)(async () => {
|
|
1615
1668
|
if (!enabled || !url) {
|
|
1616
|
-
addLog(
|
|
1669
|
+
addLog(
|
|
1670
|
+
"debug",
|
|
1671
|
+
enabled ? "No server URL provided, skipping connection." : "Connection disabled via enabled flag."
|
|
1672
|
+
);
|
|
1617
1673
|
return;
|
|
1618
1674
|
}
|
|
1619
1675
|
if (connectingRef.current) {
|
|
@@ -1630,7 +1686,10 @@ function useMcp(options) {
|
|
|
1630
1686
|
setAuthUrl(void 0);
|
|
1631
1687
|
successfulTransportRef.current = null;
|
|
1632
1688
|
setState("discovering");
|
|
1633
|
-
addLog(
|
|
1689
|
+
addLog(
|
|
1690
|
+
"info",
|
|
1691
|
+
`Connecting attempt #${connectAttemptRef.current} to ${url}...`
|
|
1692
|
+
);
|
|
1634
1693
|
if (!authProviderRef.current) {
|
|
1635
1694
|
authProviderRef.current = new BrowserOAuthClientProvider(url, {
|
|
1636
1695
|
storageKeyPrefix,
|
|
@@ -1647,7 +1706,10 @@ function useMcp(options) {
|
|
|
1647
1706
|
addLog("debug", "BrowserMCPClient initialized in connect.");
|
|
1648
1707
|
}
|
|
1649
1708
|
const tryConnectWithTransport = /* @__PURE__ */ __name(async (transportTypeParam, isAuthRetry = false) => {
|
|
1650
|
-
addLog(
|
|
1709
|
+
addLog(
|
|
1710
|
+
"info",
|
|
1711
|
+
`Attempting connection with transport: ${transportTypeParam}`
|
|
1712
|
+
);
|
|
1651
1713
|
try {
|
|
1652
1714
|
const serverName = "inspector-server";
|
|
1653
1715
|
const serverConfig = {
|
|
@@ -1744,7 +1806,9 @@ function useMcp(options) {
|
|
|
1744
1806
|
const callTool = (0, import_react.useCallback)(
|
|
1745
1807
|
async (name, args) => {
|
|
1746
1808
|
if (stateRef.current !== "ready" || !clientRef.current) {
|
|
1747
|
-
throw new Error(
|
|
1809
|
+
throw new Error(
|
|
1810
|
+
`MCP client is not ready (current state: ${state}). Cannot call tool "${name}".`
|
|
1811
|
+
);
|
|
1748
1812
|
}
|
|
1749
1813
|
addLog("info", `Calling tool: ${name}`, args);
|
|
1750
1814
|
try {
|
|
@@ -1768,7 +1832,10 @@ function useMcp(options) {
|
|
|
1768
1832
|
addLog("info", "Retry requested...");
|
|
1769
1833
|
connect();
|
|
1770
1834
|
} else {
|
|
1771
|
-
addLog(
|
|
1835
|
+
addLog(
|
|
1836
|
+
"warn",
|
|
1837
|
+
`Retry called but state is not 'failed' (state: ${stateRef.current}). Ignoring.`
|
|
1838
|
+
);
|
|
1772
1839
|
}
|
|
1773
1840
|
}, [addLog, connect]);
|
|
1774
1841
|
const authenticate = (0, import_react.useCallback)(async () => {
|
|
@@ -1790,9 +1857,15 @@ function useMcp(options) {
|
|
|
1790
1857
|
}
|
|
1791
1858
|
}, AUTH_TIMEOUT);
|
|
1792
1859
|
try {
|
|
1793
|
-
assert(
|
|
1860
|
+
assert(
|
|
1861
|
+
authProviderRef.current,
|
|
1862
|
+
"Auth Provider not available for manual auth"
|
|
1863
|
+
);
|
|
1794
1864
|
assert(url, "Server URL is required for authentication");
|
|
1795
|
-
addLog(
|
|
1865
|
+
addLog(
|
|
1866
|
+
"info",
|
|
1867
|
+
"Redirecting for manual authentication. Waiting for callback..."
|
|
1868
|
+
);
|
|
1796
1869
|
} catch (authError) {
|
|
1797
1870
|
if (!isMountedRef.current) return;
|
|
1798
1871
|
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
|
|
@@ -1802,7 +1875,10 @@ function useMcp(options) {
|
|
|
1802
1875
|
);
|
|
1803
1876
|
}
|
|
1804
1877
|
} else if (currentState === "authenticating") {
|
|
1805
|
-
addLog(
|
|
1878
|
+
addLog(
|
|
1879
|
+
"warn",
|
|
1880
|
+
"Already attempting authentication. Check for blocked popups or wait for timeout."
|
|
1881
|
+
);
|
|
1806
1882
|
const manualUrl = authProviderRef.current?.getLastAttemptedAuthUrl();
|
|
1807
1883
|
if (manualUrl && !authUrl) {
|
|
1808
1884
|
setAuthUrl(manualUrl);
|
|
@@ -1825,32 +1901,33 @@ function useMcp(options) {
|
|
|
1825
1901
|
addLog("warn", "Auth provider not initialized, cannot clear storage.");
|
|
1826
1902
|
}
|
|
1827
1903
|
}, [url, addLog, disconnect]);
|
|
1828
|
-
const listResources = (0, import_react.useCallback)(
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
const resourcesResult = await session.connector.listAllResources();
|
|
1841
|
-
setResources(resourcesResult.resources || []);
|
|
1842
|
-
addLog("info", "Resources listed successfully");
|
|
1843
|
-
} catch (err) {
|
|
1844
|
-
addLog("error", "List resources failed:", err);
|
|
1845
|
-
throw err;
|
|
1904
|
+
const listResources = (0, import_react.useCallback)(async () => {
|
|
1905
|
+
if (stateRef.current !== "ready" || !clientRef.current) {
|
|
1906
|
+
throw new Error(
|
|
1907
|
+
`MCP client is not ready (current state: ${state}). Cannot list resources.`
|
|
1908
|
+
);
|
|
1909
|
+
}
|
|
1910
|
+
addLog("info", "Listing resources");
|
|
1911
|
+
try {
|
|
1912
|
+
const serverName = "inspector-server";
|
|
1913
|
+
const session = clientRef.current.getSession(serverName);
|
|
1914
|
+
if (!session) {
|
|
1915
|
+
throw new Error("No active session found");
|
|
1846
1916
|
}
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1917
|
+
const resourcesResult = await session.connector.listAllResources();
|
|
1918
|
+
setResources(resourcesResult.resources || []);
|
|
1919
|
+
addLog("info", "Resources listed successfully");
|
|
1920
|
+
} catch (err) {
|
|
1921
|
+
addLog("error", "List resources failed:", err);
|
|
1922
|
+
throw err;
|
|
1923
|
+
}
|
|
1924
|
+
}, [state]);
|
|
1850
1925
|
const readResource = (0, import_react.useCallback)(
|
|
1851
1926
|
async (uri) => {
|
|
1852
1927
|
if (stateRef.current !== "ready" || !clientRef.current) {
|
|
1853
|
-
throw new Error(
|
|
1928
|
+
throw new Error(
|
|
1929
|
+
`MCP client is not ready (current state: ${state}). Cannot read resource.`
|
|
1930
|
+
);
|
|
1854
1931
|
}
|
|
1855
1932
|
addLog("info", `Reading resource: ${uri}`);
|
|
1856
1933
|
try {
|
|
@@ -1869,32 +1946,33 @@ function useMcp(options) {
|
|
|
1869
1946
|
},
|
|
1870
1947
|
[state]
|
|
1871
1948
|
);
|
|
1872
|
-
const listPrompts = (0, import_react.useCallback)(
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
const promptsResult = await session.connector.listPrompts();
|
|
1885
|
-
setPrompts(promptsResult.prompts || []);
|
|
1886
|
-
addLog("info", "Prompts listed successfully");
|
|
1887
|
-
} catch (err) {
|
|
1888
|
-
addLog("error", "List prompts failed:", err);
|
|
1889
|
-
throw err;
|
|
1949
|
+
const listPrompts = (0, import_react.useCallback)(async () => {
|
|
1950
|
+
if (stateRef.current !== "ready" || !clientRef.current) {
|
|
1951
|
+
throw new Error(
|
|
1952
|
+
`MCP client is not ready (current state: ${state}). Cannot list prompts.`
|
|
1953
|
+
);
|
|
1954
|
+
}
|
|
1955
|
+
addLog("info", "Listing prompts");
|
|
1956
|
+
try {
|
|
1957
|
+
const serverName = "inspector-server";
|
|
1958
|
+
const session = clientRef.current.getSession(serverName);
|
|
1959
|
+
if (!session) {
|
|
1960
|
+
throw new Error("No active session found");
|
|
1890
1961
|
}
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1962
|
+
const promptsResult = await session.connector.listPrompts();
|
|
1963
|
+
setPrompts(promptsResult.prompts || []);
|
|
1964
|
+
addLog("info", "Prompts listed successfully");
|
|
1965
|
+
} catch (err) {
|
|
1966
|
+
addLog("error", "List prompts failed:", err);
|
|
1967
|
+
throw err;
|
|
1968
|
+
}
|
|
1969
|
+
}, [state]);
|
|
1894
1970
|
const getPrompt = (0, import_react.useCallback)(
|
|
1895
1971
|
async (name, args) => {
|
|
1896
1972
|
if (stateRef.current !== "ready" || !clientRef.current) {
|
|
1897
|
-
throw new Error(
|
|
1973
|
+
throw new Error(
|
|
1974
|
+
`MCP client is not ready (current state: ${state}). Cannot get prompt.`
|
|
1975
|
+
);
|
|
1898
1976
|
}
|
|
1899
1977
|
addLog("info", `Getting prompt: ${name}`, args);
|
|
1900
1978
|
try {
|
|
@@ -1927,19 +2005,30 @@ function useMcp(options) {
|
|
|
1927
2005
|
if (authTimeoutRef.current) clearTimeout(authTimeoutRef.current);
|
|
1928
2006
|
authTimeoutRef.current = null;
|
|
1929
2007
|
if (event.data.success) {
|
|
1930
|
-
addLog(
|
|
2008
|
+
addLog(
|
|
2009
|
+
"info",
|
|
2010
|
+
"Authentication successful via popup. Reconnecting client..."
|
|
2011
|
+
);
|
|
1931
2012
|
if (connectingRef.current) {
|
|
1932
|
-
addLog(
|
|
2013
|
+
addLog(
|
|
2014
|
+
"debug",
|
|
2015
|
+
"Connection attempt already in progress, resetting flag to allow reconnection."
|
|
2016
|
+
);
|
|
1933
2017
|
}
|
|
1934
2018
|
connectingRef.current = false;
|
|
1935
2019
|
setTimeout(() => {
|
|
1936
2020
|
if (isMountedRef.current) {
|
|
1937
|
-
addLog(
|
|
2021
|
+
addLog(
|
|
2022
|
+
"debug",
|
|
2023
|
+
"Initiating reconnection after successful auth callback."
|
|
2024
|
+
);
|
|
1938
2025
|
connectRef.current();
|
|
1939
2026
|
}
|
|
1940
2027
|
}, 100);
|
|
1941
2028
|
} else {
|
|
1942
|
-
failConnectionRef.current(
|
|
2029
|
+
failConnectionRef.current(
|
|
2030
|
+
`Authentication failed in callback: ${event.data.error || "Unknown reason."}`
|
|
2031
|
+
);
|
|
1943
2032
|
}
|
|
1944
2033
|
}
|
|
1945
2034
|
}, "messageHandler");
|
|
@@ -1954,7 +2043,10 @@ function useMcp(options) {
|
|
|
1954
2043
|
(0, import_react.useEffect)(() => {
|
|
1955
2044
|
isMountedRef.current = true;
|
|
1956
2045
|
if (!enabled || !url) {
|
|
1957
|
-
addLog(
|
|
2046
|
+
addLog(
|
|
2047
|
+
"debug",
|
|
2048
|
+
enabled ? "No server URL provided, skipping connection." : "Connection disabled via enabled flag."
|
|
2049
|
+
);
|
|
1958
2050
|
setState("discovering");
|
|
1959
2051
|
return () => {
|
|
1960
2052
|
isMountedRef.current = false;
|
|
@@ -1971,7 +2063,10 @@ function useMcp(options) {
|
|
|
1971
2063
|
preventAutoAuth,
|
|
1972
2064
|
onPopupWindow
|
|
1973
2065
|
});
|
|
1974
|
-
addLog(
|
|
2066
|
+
addLog(
|
|
2067
|
+
"debug",
|
|
2068
|
+
"BrowserOAuthClientProvider initialized/updated on mount/option change."
|
|
2069
|
+
);
|
|
1975
2070
|
}
|
|
1976
2071
|
connect();
|
|
1977
2072
|
return () => {
|
|
@@ -1979,7 +2074,16 @@ function useMcp(options) {
|
|
|
1979
2074
|
addLog("debug", "useMcp unmounting, disconnecting.");
|
|
1980
2075
|
disconnect(true);
|
|
1981
2076
|
};
|
|
1982
|
-
}, [
|
|
2077
|
+
}, [
|
|
2078
|
+
url,
|
|
2079
|
+
enabled,
|
|
2080
|
+
storageKeyPrefix,
|
|
2081
|
+
callbackUrl,
|
|
2082
|
+
clientName,
|
|
2083
|
+
clientUri,
|
|
2084
|
+
clientConfig.name,
|
|
2085
|
+
clientConfig.version
|
|
2086
|
+
]);
|
|
1983
2087
|
(0, import_react.useEffect)(() => {
|
|
1984
2088
|
let retryTimeoutId = null;
|
|
1985
2089
|
if (state === "failed" && autoRetry && connectAttemptRef.current > 0) {
|
|
@@ -2027,23 +2131,36 @@ async function onMcpAuthorization() {
|
|
|
2027
2131
|
const error = queryParams.get("error");
|
|
2028
2132
|
const errorDescription = queryParams.get("error_description");
|
|
2029
2133
|
const logPrefix = "[mcp-callback]";
|
|
2030
|
-
console.log(`${logPrefix} Handling callback...`, {
|
|
2134
|
+
console.log(`${logPrefix} Handling callback...`, {
|
|
2135
|
+
code,
|
|
2136
|
+
state,
|
|
2137
|
+
error,
|
|
2138
|
+
errorDescription
|
|
2139
|
+
});
|
|
2031
2140
|
let provider = null;
|
|
2032
2141
|
let storedStateData = null;
|
|
2033
2142
|
const stateKey = state ? `mcp:auth:state_${state}` : null;
|
|
2034
2143
|
try {
|
|
2035
2144
|
if (error) {
|
|
2036
|
-
throw new Error(
|
|
2145
|
+
throw new Error(
|
|
2146
|
+
`OAuth error: ${error} - ${errorDescription || "No description provided."}`
|
|
2147
|
+
);
|
|
2037
2148
|
}
|
|
2038
2149
|
if (!code) {
|
|
2039
|
-
throw new Error(
|
|
2150
|
+
throw new Error(
|
|
2151
|
+
"Authorization code not found in callback query parameters."
|
|
2152
|
+
);
|
|
2040
2153
|
}
|
|
2041
2154
|
if (!state || !stateKey) {
|
|
2042
|
-
throw new Error(
|
|
2155
|
+
throw new Error(
|
|
2156
|
+
"State parameter not found or invalid in callback query parameters."
|
|
2157
|
+
);
|
|
2043
2158
|
}
|
|
2044
2159
|
const storedStateJSON = localStorage.getItem(stateKey);
|
|
2045
2160
|
if (!storedStateJSON) {
|
|
2046
|
-
throw new Error(
|
|
2161
|
+
throw new Error(
|
|
2162
|
+
`Invalid or expired state parameter "${state}". No matching state found in storage.`
|
|
2163
|
+
);
|
|
2047
2164
|
}
|
|
2048
2165
|
try {
|
|
2049
2166
|
storedStateData = JSON.parse(storedStateJSON);
|
|
@@ -2052,38 +2169,59 @@ async function onMcpAuthorization() {
|
|
|
2052
2169
|
}
|
|
2053
2170
|
if (!storedStateData.expiry || storedStateData.expiry < Date.now()) {
|
|
2054
2171
|
localStorage.removeItem(stateKey);
|
|
2055
|
-
throw new Error(
|
|
2172
|
+
throw new Error(
|
|
2173
|
+
"OAuth state has expired. Please try initiating authentication again."
|
|
2174
|
+
);
|
|
2056
2175
|
}
|
|
2057
2176
|
if (!storedStateData.providerOptions) {
|
|
2058
2177
|
throw new Error("Stored state is missing required provider options.");
|
|
2059
2178
|
}
|
|
2060
2179
|
const { serverUrl, ...providerOptions } = storedStateData.providerOptions;
|
|
2061
|
-
console.log(
|
|
2180
|
+
console.log(
|
|
2181
|
+
`${logPrefix} Re-instantiating provider for server: ${serverUrl}`
|
|
2182
|
+
);
|
|
2062
2183
|
provider = new BrowserOAuthClientProvider(serverUrl, providerOptions);
|
|
2063
2184
|
console.log(`${logPrefix} Calling SDK auth() to exchange code...`);
|
|
2064
2185
|
const baseUrl = new URL(serverUrl).origin;
|
|
2065
|
-
const authResult = await (0, import_auth.auth)(provider, {
|
|
2186
|
+
const authResult = await (0, import_auth.auth)(provider, {
|
|
2187
|
+
serverUrl: baseUrl,
|
|
2188
|
+
authorizationCode: code
|
|
2189
|
+
});
|
|
2066
2190
|
if (authResult === "AUTHORIZED") {
|
|
2067
|
-
console.log(
|
|
2191
|
+
console.log(
|
|
2192
|
+
`${logPrefix} Authorization successful via SDK auth(). Notifying opener...`
|
|
2193
|
+
);
|
|
2068
2194
|
if (window.opener && !window.opener.closed) {
|
|
2069
|
-
window.opener.postMessage(
|
|
2195
|
+
window.opener.postMessage(
|
|
2196
|
+
{ type: "mcp_auth_callback", success: true },
|
|
2197
|
+
window.location.origin
|
|
2198
|
+
);
|
|
2070
2199
|
window.close();
|
|
2071
2200
|
} else {
|
|
2072
|
-
console.warn(
|
|
2201
|
+
console.warn(
|
|
2202
|
+
`${logPrefix} No opener window detected. Redirecting to root.`
|
|
2203
|
+
);
|
|
2073
2204
|
const pathParts = window.location.pathname.split("/").filter(Boolean);
|
|
2074
2205
|
const basePath = pathParts.length > 0 && pathParts[pathParts.length - 1] === "callback" ? "/" + pathParts.slice(0, -2).join("/") : "/";
|
|
2075
2206
|
window.location.href = basePath || "/";
|
|
2076
2207
|
}
|
|
2077
2208
|
localStorage.removeItem(stateKey);
|
|
2078
2209
|
} else {
|
|
2079
|
-
console.warn(
|
|
2080
|
-
|
|
2210
|
+
console.warn(
|
|
2211
|
+
`${logPrefix} SDK auth() returned unexpected status: ${authResult}`
|
|
2212
|
+
);
|
|
2213
|
+
throw new Error(
|
|
2214
|
+
`Unexpected result from authentication library: ${authResult}`
|
|
2215
|
+
);
|
|
2081
2216
|
}
|
|
2082
2217
|
} catch (err) {
|
|
2083
2218
|
console.error(`${logPrefix} Error during OAuth callback handling:`, err);
|
|
2084
2219
|
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
2085
2220
|
if (window.opener && !window.opener.closed) {
|
|
2086
|
-
window.opener.postMessage(
|
|
2221
|
+
window.opener.postMessage(
|
|
2222
|
+
{ type: "mcp_auth_callback", success: false, error: errorMessage },
|
|
2223
|
+
window.location.origin
|
|
2224
|
+
);
|
|
2087
2225
|
}
|
|
2088
2226
|
try {
|
|
2089
2227
|
document.body.innerHTML = `
|
|
@@ -2097,7 +2235,10 @@ async function onMcpAuthorization() {
|
|
|
2097
2235
|
</div>
|
|
2098
2236
|
`;
|
|
2099
2237
|
} catch (displayError) {
|
|
2100
|
-
console.error(
|
|
2238
|
+
console.error(
|
|
2239
|
+
`${logPrefix} Could not display error in callback window:`,
|
|
2240
|
+
displayError
|
|
2241
|
+
);
|
|
2101
2242
|
}
|
|
2102
2243
|
if (stateKey) {
|
|
2103
2244
|
localStorage.removeItem(stateKey);
|
|
@@ -2129,17 +2270,11 @@ function useOpenAiGlobal(key) {
|
|
|
2129
2270
|
onChange();
|
|
2130
2271
|
}, "handleSetGlobal");
|
|
2131
2272
|
if (typeof window !== "undefined") {
|
|
2132
|
-
window.addEventListener(
|
|
2133
|
-
SET_GLOBALS_EVENT_TYPE,
|
|
2134
|
-
handleSetGlobal
|
|
2135
|
-
);
|
|
2273
|
+
window.addEventListener(SET_GLOBALS_EVENT_TYPE, handleSetGlobal);
|
|
2136
2274
|
}
|
|
2137
2275
|
return () => {
|
|
2138
2276
|
if (typeof window !== "undefined") {
|
|
2139
|
-
window.removeEventListener(
|
|
2140
|
-
SET_GLOBALS_EVENT_TYPE,
|
|
2141
|
-
handleSetGlobal
|
|
2142
|
-
);
|
|
2277
|
+
window.removeEventListener(SET_GLOBALS_EVENT_TYPE, handleSetGlobal);
|
|
2143
2278
|
}
|
|
2144
2279
|
};
|
|
2145
2280
|
},
|
|
@@ -2149,7 +2284,10 @@ function useOpenAiGlobal(key) {
|
|
|
2149
2284
|
__name(useOpenAiGlobal, "useOpenAiGlobal");
|
|
2150
2285
|
function useWidget(defaultProps) {
|
|
2151
2286
|
console.log(window?.location?.search, window.openai);
|
|
2152
|
-
const isOpenAiAvailable = (0, import_react2.useMemo)(
|
|
2287
|
+
const isOpenAiAvailable = (0, import_react2.useMemo)(
|
|
2288
|
+
() => typeof window !== "undefined" && !!window.openai,
|
|
2289
|
+
[]
|
|
2290
|
+
);
|
|
2153
2291
|
const provider = (0, import_react2.useMemo)(() => {
|
|
2154
2292
|
return isOpenAiAvailable ? "openai" : "mcp-ui";
|
|
2155
2293
|
}, [isOpenAiAvailable]);
|
|
@@ -2190,12 +2328,15 @@ function useWidget(defaultProps) {
|
|
|
2190
2328
|
},
|
|
2191
2329
|
[]
|
|
2192
2330
|
);
|
|
2193
|
-
const sendFollowUpMessage = (0, import_react2.useCallback)(
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2331
|
+
const sendFollowUpMessage = (0, import_react2.useCallback)(
|
|
2332
|
+
async (prompt) => {
|
|
2333
|
+
if (!window.openai?.sendFollowUpMessage) {
|
|
2334
|
+
throw new Error("window.openai.sendFollowUpMessage is not available");
|
|
2335
|
+
}
|
|
2336
|
+
return window.openai.sendFollowUpMessage({ prompt });
|
|
2337
|
+
},
|
|
2338
|
+
[]
|
|
2339
|
+
);
|
|
2199
2340
|
const openExternal = (0, import_react2.useCallback)((href) => {
|
|
2200
2341
|
if (!window.openai?.openExternal) {
|
|
2201
2342
|
throw new Error("window.openai.openExternal is not available");
|