integrate-sdk 0.7.43 → 0.7.45
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/adapters/index.js +80 -35
- package/dist/adapters/solid-start.js +80 -35
- package/dist/adapters/svelte-kit.js +80 -35
- package/dist/ai/index.d.ts +1 -3
- package/dist/ai/index.d.ts.map +1 -1
- package/dist/ai/index.js +4 -33
- package/dist/ai/vercel-ai.d.ts +6 -2
- package/dist/ai/vercel-ai.d.ts.map +1 -1
- package/dist/ai/vercel-ai.js +4 -1
- package/dist/index.js +80 -35
- package/dist/server.js +84 -67
- package/dist/src/ai/index.d.ts +1 -3
- package/dist/src/ai/index.d.ts.map +1 -1
- package/dist/src/ai/vercel-ai.d.ts +6 -2
- package/dist/src/ai/vercel-ai.d.ts.map +1 -1
- package/dist/src/client.d.ts +9 -4
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/config/types.d.ts +105 -0
- package/dist/src/config/types.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/oauth/manager.d.ts +32 -12
- package/dist/src/oauth/manager.d.ts.map +1 -1
- package/dist/src/server.d.ts +2 -1
- package/dist/src/server.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/ai/openai-agents.d.ts +0 -99
- package/dist/ai/openai-agents.d.ts.map +0 -1
- package/dist/ai/openai-agents.js +0 -4235
- package/dist/src/ai/openai-agents.d.ts +0 -99
- package/dist/src/ai/openai-agents.d.ts.map +0 -1
package/dist/adapters/index.js
CHANGED
|
@@ -1117,7 +1117,9 @@ class OAuthManager {
|
|
|
1117
1117
|
flowConfig;
|
|
1118
1118
|
oauthApiBase;
|
|
1119
1119
|
apiBaseUrl;
|
|
1120
|
-
|
|
1120
|
+
getTokenCallback;
|
|
1121
|
+
setTokenCallback;
|
|
1122
|
+
constructor(oauthApiBase, flowConfig, apiBaseUrl, tokenCallbacks) {
|
|
1121
1123
|
this.oauthApiBase = oauthApiBase;
|
|
1122
1124
|
this.apiBaseUrl = apiBaseUrl;
|
|
1123
1125
|
this.windowManager = new OAuthWindowManager;
|
|
@@ -1126,6 +1128,8 @@ class OAuthManager {
|
|
|
1126
1128
|
popupOptions: flowConfig?.popupOptions,
|
|
1127
1129
|
onAuthCallback: flowConfig?.onAuthCallback
|
|
1128
1130
|
};
|
|
1131
|
+
this.getTokenCallback = tokenCallbacks?.getProviderToken;
|
|
1132
|
+
this.setTokenCallback = tokenCallbacks?.setProviderToken;
|
|
1129
1133
|
this.cleanupExpiredPendingAuths();
|
|
1130
1134
|
}
|
|
1131
1135
|
async initiateFlow(provider, config, returnUrl) {
|
|
@@ -1189,7 +1193,7 @@ class OAuthManager {
|
|
|
1189
1193
|
scopes: response.scopes
|
|
1190
1194
|
};
|
|
1191
1195
|
this.providerTokens.set(pendingAuth.provider, tokenData);
|
|
1192
|
-
this.saveProviderToken(pendingAuth.provider, tokenData);
|
|
1196
|
+
await this.saveProviderToken(pendingAuth.provider, tokenData);
|
|
1193
1197
|
this.pendingAuths.delete(state);
|
|
1194
1198
|
this.removePendingAuthFromStorage(state);
|
|
1195
1199
|
return { ...tokenData, provider: pendingAuth.provider };
|
|
@@ -1200,7 +1204,7 @@ class OAuthManager {
|
|
|
1200
1204
|
}
|
|
1201
1205
|
}
|
|
1202
1206
|
async checkAuthStatus(provider) {
|
|
1203
|
-
const tokenData = this.
|
|
1207
|
+
const tokenData = await this.getProviderToken(provider);
|
|
1204
1208
|
if (!tokenData) {
|
|
1205
1209
|
return {
|
|
1206
1210
|
authorized: false,
|
|
@@ -1215,26 +1219,38 @@ class OAuthManager {
|
|
|
1215
1219
|
};
|
|
1216
1220
|
}
|
|
1217
1221
|
async disconnectProvider(provider) {
|
|
1218
|
-
const tokenData = this.
|
|
1222
|
+
const tokenData = await this.getProviderToken(provider);
|
|
1219
1223
|
if (!tokenData) {
|
|
1220
1224
|
throw new Error(`No access token available for provider "${provider}". Cannot disconnect provider.`);
|
|
1221
1225
|
}
|
|
1222
1226
|
this.providerTokens.delete(provider);
|
|
1223
1227
|
this.clearProviderToken(provider);
|
|
1224
1228
|
}
|
|
1225
|
-
getProviderToken(provider) {
|
|
1229
|
+
async getProviderToken(provider, context) {
|
|
1230
|
+
if (this.getTokenCallback) {
|
|
1231
|
+
try {
|
|
1232
|
+
const tokenData = await this.getTokenCallback(provider, context);
|
|
1233
|
+
if (tokenData) {
|
|
1234
|
+
this.providerTokens.set(provider, tokenData);
|
|
1235
|
+
}
|
|
1236
|
+
return tokenData;
|
|
1237
|
+
} catch (error) {
|
|
1238
|
+
console.error(`Failed to get token for ${provider} via callback:`, error);
|
|
1239
|
+
return;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1226
1242
|
return this.providerTokens.get(provider);
|
|
1227
1243
|
}
|
|
1228
1244
|
getAllProviderTokens() {
|
|
1229
1245
|
return new Map(this.providerTokens);
|
|
1230
1246
|
}
|
|
1231
|
-
setProviderToken(provider, tokenData) {
|
|
1247
|
+
async setProviderToken(provider, tokenData, context) {
|
|
1232
1248
|
this.providerTokens.set(provider, tokenData);
|
|
1233
|
-
this.saveProviderToken(provider, tokenData);
|
|
1249
|
+
await this.saveProviderToken(provider, tokenData, context);
|
|
1234
1250
|
}
|
|
1235
1251
|
clearProviderToken(provider) {
|
|
1236
1252
|
this.providerTokens.delete(provider);
|
|
1237
|
-
if (typeof window !== "undefined" && window.localStorage) {
|
|
1253
|
+
if (!this.getTokenCallback && typeof window !== "undefined" && window.localStorage) {
|
|
1238
1254
|
try {
|
|
1239
1255
|
window.localStorage.removeItem(`integrate_token_${provider}`);
|
|
1240
1256
|
} catch (error) {
|
|
@@ -1245,7 +1261,7 @@ class OAuthManager {
|
|
|
1245
1261
|
clearAllProviderTokens() {
|
|
1246
1262
|
const providers = Array.from(this.providerTokens.keys());
|
|
1247
1263
|
this.providerTokens.clear();
|
|
1248
|
-
if (typeof window !== "undefined" && window.localStorage) {
|
|
1264
|
+
if (!this.getTokenCallback && typeof window !== "undefined" && window.localStorage) {
|
|
1249
1265
|
for (const provider of providers) {
|
|
1250
1266
|
try {
|
|
1251
1267
|
window.localStorage.removeItem(`integrate_token_${provider}`);
|
|
@@ -1273,7 +1289,16 @@ class OAuthManager {
|
|
|
1273
1289
|
}
|
|
1274
1290
|
}
|
|
1275
1291
|
}
|
|
1276
|
-
saveProviderToken(provider, tokenData) {
|
|
1292
|
+
async saveProviderToken(provider, tokenData, context) {
|
|
1293
|
+
if (this.setTokenCallback) {
|
|
1294
|
+
try {
|
|
1295
|
+
await this.setTokenCallback(provider, tokenData, context);
|
|
1296
|
+
} catch (error) {
|
|
1297
|
+
console.error(`Failed to save token for ${provider} via callback:`, error);
|
|
1298
|
+
throw error;
|
|
1299
|
+
}
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1277
1302
|
if (typeof window !== "undefined" && window.localStorage) {
|
|
1278
1303
|
try {
|
|
1279
1304
|
const key = `integrate_token_${provider}`;
|
|
@@ -1283,7 +1308,15 @@ class OAuthManager {
|
|
|
1283
1308
|
}
|
|
1284
1309
|
}
|
|
1285
1310
|
}
|
|
1286
|
-
loadProviderToken(provider) {
|
|
1311
|
+
async loadProviderToken(provider) {
|
|
1312
|
+
if (this.getTokenCallback) {
|
|
1313
|
+
try {
|
|
1314
|
+
return await this.getTokenCallback(provider);
|
|
1315
|
+
} catch (error) {
|
|
1316
|
+
console.error(`Failed to load token for ${provider} via callback:`, error);
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1287
1320
|
if (typeof window !== "undefined" && window.localStorage) {
|
|
1288
1321
|
try {
|
|
1289
1322
|
const key = `integrate_token_${provider}`;
|
|
@@ -1297,9 +1330,9 @@ class OAuthManager {
|
|
|
1297
1330
|
}
|
|
1298
1331
|
return;
|
|
1299
1332
|
}
|
|
1300
|
-
loadAllProviderTokens(providers) {
|
|
1333
|
+
async loadAllProviderTokens(providers) {
|
|
1301
1334
|
for (const provider of providers) {
|
|
1302
|
-
const tokenData = this.loadProviderToken(provider);
|
|
1335
|
+
const tokenData = await this.loadProviderToken(provider);
|
|
1303
1336
|
if (tokenData) {
|
|
1304
1337
|
this.providerTokens.set(provider, tokenData);
|
|
1305
1338
|
}
|
|
@@ -1497,16 +1530,28 @@ class MCPClientBase {
|
|
|
1497
1530
|
};
|
|
1498
1531
|
this.onReauthRequired = config.onReauthRequired;
|
|
1499
1532
|
this.maxReauthRetries = config.maxReauthRetries ?? 1;
|
|
1500
|
-
this.oauthManager = new OAuthManager(oauthApiBase, config.oauthFlow, this.apiBaseUrl
|
|
1533
|
+
this.oauthManager = new OAuthManager(oauthApiBase, config.oauthFlow, this.apiBaseUrl, {
|
|
1534
|
+
getProviderToken: config.getProviderToken,
|
|
1535
|
+
setProviderToken: config.setProviderToken
|
|
1536
|
+
});
|
|
1501
1537
|
const providers = this.integrations.filter((p) => p.oauth).map((p) => p.oauth.provider);
|
|
1502
|
-
this.oauthManager.loadAllProviderTokens(providers)
|
|
1538
|
+
this.oauthManager.loadAllProviderTokens(providers).catch((error) => {
|
|
1539
|
+
console.error("Failed to load provider tokens:", error);
|
|
1540
|
+
});
|
|
1503
1541
|
for (const integration of this.integrations) {
|
|
1504
1542
|
for (const toolName of integration.tools) {
|
|
1505
1543
|
this.enabledToolNames.add(toolName);
|
|
1506
1544
|
}
|
|
1507
1545
|
if (integration.oauth) {
|
|
1508
|
-
const
|
|
1509
|
-
this.authState.set(
|
|
1546
|
+
const provider = integration.oauth.provider;
|
|
1547
|
+
this.authState.set(provider, { authenticated: false });
|
|
1548
|
+
this.oauthManager.getProviderToken(provider).then((tokenData) => {
|
|
1549
|
+
if (tokenData) {
|
|
1550
|
+
this.authState.set(provider, { authenticated: true });
|
|
1551
|
+
}
|
|
1552
|
+
}).catch((error) => {
|
|
1553
|
+
console.error(`Failed to check token for ${provider}:`, error);
|
|
1554
|
+
});
|
|
1510
1555
|
}
|
|
1511
1556
|
}
|
|
1512
1557
|
const integrationIds = this.integrations.map((i) => i.id);
|
|
@@ -1534,9 +1579,9 @@ class MCPClientBase {
|
|
|
1534
1579
|
}
|
|
1535
1580
|
return new Proxy({}, {
|
|
1536
1581
|
get: (_target, methodName) => {
|
|
1537
|
-
return async (args) => {
|
|
1582
|
+
return async (args, options) => {
|
|
1538
1583
|
const toolName = methodToToolName(methodName, integrationId);
|
|
1539
|
-
return await this.callToolWithRetry(toolName, args, 0);
|
|
1584
|
+
return await this.callToolWithRetry(toolName, args, 0, options);
|
|
1540
1585
|
};
|
|
1541
1586
|
}
|
|
1542
1587
|
});
|
|
@@ -1544,17 +1589,17 @@ class MCPClientBase {
|
|
|
1544
1589
|
createServerProxy() {
|
|
1545
1590
|
return new Proxy({}, {
|
|
1546
1591
|
get: (_target, methodName) => {
|
|
1547
|
-
return async (args) => {
|
|
1592
|
+
return async (args, options) => {
|
|
1548
1593
|
const toolName = methodToToolName(methodName, "");
|
|
1549
1594
|
const finalToolName = toolName.startsWith("_") ? toolName.substring(1) : toolName;
|
|
1550
|
-
return await this.callServerToolInternal(finalToolName, args);
|
|
1595
|
+
return await this.callServerToolInternal(finalToolName, args, options);
|
|
1551
1596
|
};
|
|
1552
1597
|
}
|
|
1553
1598
|
});
|
|
1554
1599
|
}
|
|
1555
|
-
async callServerToolInternal(name, args) {
|
|
1600
|
+
async callServerToolInternal(name, args, options) {
|
|
1556
1601
|
try {
|
|
1557
|
-
const response = await this.callToolThroughHandler(name, args);
|
|
1602
|
+
const response = await this.callToolThroughHandler(name, args, undefined, options);
|
|
1558
1603
|
return response;
|
|
1559
1604
|
} catch (error) {
|
|
1560
1605
|
const parsedError = parseServerError(error, { toolName: name });
|
|
@@ -1603,8 +1648,8 @@ class MCPClientBase {
|
|
|
1603
1648
|
const enabledTools = response.tools.filter((tool) => this.enabledToolNames.has(tool.name));
|
|
1604
1649
|
console.log(`Discovered ${response.tools.length} tools, ${enabledTools.length} enabled by integrations`);
|
|
1605
1650
|
}
|
|
1606
|
-
async _callToolByName(name, args) {
|
|
1607
|
-
return await this.callToolWithRetry(name, args, 0);
|
|
1651
|
+
async _callToolByName(name, args, options) {
|
|
1652
|
+
return await this.callToolWithRetry(name, args, 0, options);
|
|
1608
1653
|
}
|
|
1609
1654
|
async callServerTool(name, args) {
|
|
1610
1655
|
try {
|
|
@@ -1615,12 +1660,12 @@ class MCPClientBase {
|
|
|
1615
1660
|
throw parsedError;
|
|
1616
1661
|
}
|
|
1617
1662
|
}
|
|
1618
|
-
async callToolThroughHandler(name, args, provider) {
|
|
1663
|
+
async callToolThroughHandler(name, args, provider, options) {
|
|
1619
1664
|
const transportHeaders = this.transport.headers || {};
|
|
1620
1665
|
const hasApiKey = !!transportHeaders["X-API-KEY"];
|
|
1621
1666
|
if (hasApiKey) {
|
|
1622
1667
|
if (provider) {
|
|
1623
|
-
const tokenData = this.oauthManager.getProviderToken(provider);
|
|
1668
|
+
const tokenData = await this.oauthManager.getProviderToken(provider, options?.context);
|
|
1624
1669
|
if (tokenData && this.transport.setHeader) {
|
|
1625
1670
|
const previousAuthHeader = transportHeaders["Authorization"];
|
|
1626
1671
|
try {
|
|
@@ -1650,7 +1695,7 @@ class MCPClientBase {
|
|
|
1650
1695
|
"Content-Type": "application/json"
|
|
1651
1696
|
};
|
|
1652
1697
|
if (provider) {
|
|
1653
|
-
const tokenData = this.oauthManager.getProviderToken(provider);
|
|
1698
|
+
const tokenData = await this.oauthManager.getProviderToken(provider, options?.context);
|
|
1654
1699
|
if (tokenData) {
|
|
1655
1700
|
headers["Authorization"] = `Bearer ${tokenData.accessToken}`;
|
|
1656
1701
|
}
|
|
@@ -1688,13 +1733,13 @@ class MCPClientBase {
|
|
|
1688
1733
|
const result = await response.json();
|
|
1689
1734
|
return result;
|
|
1690
1735
|
}
|
|
1691
|
-
async callToolWithRetry(name, args, retryCount = 0) {
|
|
1736
|
+
async callToolWithRetry(name, args, retryCount = 0, options) {
|
|
1692
1737
|
if (!this.enabledToolNames.has(name)) {
|
|
1693
1738
|
throw new Error(`Tool "${name}" is not enabled. Enable it by adding the appropriate integration.`);
|
|
1694
1739
|
}
|
|
1695
1740
|
const provider = this.getProviderForTool(name);
|
|
1696
1741
|
try {
|
|
1697
|
-
const response = await this.callToolThroughHandler(name, args, provider);
|
|
1742
|
+
const response = await this.callToolThroughHandler(name, args, provider, options);
|
|
1698
1743
|
if (provider) {
|
|
1699
1744
|
this.authState.set(provider, { authenticated: true });
|
|
1700
1745
|
}
|
|
@@ -1845,7 +1890,7 @@ class MCPClientBase {
|
|
|
1845
1890
|
this.eventEmitter.emit("auth:started", { provider });
|
|
1846
1891
|
try {
|
|
1847
1892
|
await this.oauthManager.initiateFlow(provider, integration.oauth, options?.returnUrl);
|
|
1848
|
-
const tokenData = this.oauthManager.getProviderToken(provider);
|
|
1893
|
+
const tokenData = await this.oauthManager.getProviderToken(provider);
|
|
1849
1894
|
if (tokenData) {
|
|
1850
1895
|
this.eventEmitter.emit("auth:complete", {
|
|
1851
1896
|
provider,
|
|
@@ -1876,11 +1921,11 @@ class MCPClientBase {
|
|
|
1876
1921
|
throw error;
|
|
1877
1922
|
}
|
|
1878
1923
|
}
|
|
1879
|
-
getProviderToken(provider) {
|
|
1880
|
-
return this.oauthManager.getProviderToken(provider);
|
|
1924
|
+
async getProviderToken(provider, context) {
|
|
1925
|
+
return await this.oauthManager.getProviderToken(provider, context);
|
|
1881
1926
|
}
|
|
1882
|
-
setProviderToken(provider, tokenData) {
|
|
1883
|
-
this.oauthManager.setProviderToken(provider, tokenData);
|
|
1927
|
+
async setProviderToken(provider, tokenData, context) {
|
|
1928
|
+
await this.oauthManager.setProviderToken(provider, tokenData, context);
|
|
1884
1929
|
this.authState.set(provider, { authenticated: true });
|
|
1885
1930
|
}
|
|
1886
1931
|
getAllProviderTokens() {
|
|
@@ -982,7 +982,9 @@ class OAuthManager {
|
|
|
982
982
|
flowConfig;
|
|
983
983
|
oauthApiBase;
|
|
984
984
|
apiBaseUrl;
|
|
985
|
-
|
|
985
|
+
getTokenCallback;
|
|
986
|
+
setTokenCallback;
|
|
987
|
+
constructor(oauthApiBase, flowConfig, apiBaseUrl, tokenCallbacks) {
|
|
986
988
|
this.oauthApiBase = oauthApiBase;
|
|
987
989
|
this.apiBaseUrl = apiBaseUrl;
|
|
988
990
|
this.windowManager = new OAuthWindowManager;
|
|
@@ -991,6 +993,8 @@ class OAuthManager {
|
|
|
991
993
|
popupOptions: flowConfig?.popupOptions,
|
|
992
994
|
onAuthCallback: flowConfig?.onAuthCallback
|
|
993
995
|
};
|
|
996
|
+
this.getTokenCallback = tokenCallbacks?.getProviderToken;
|
|
997
|
+
this.setTokenCallback = tokenCallbacks?.setProviderToken;
|
|
994
998
|
this.cleanupExpiredPendingAuths();
|
|
995
999
|
}
|
|
996
1000
|
async initiateFlow(provider, config, returnUrl) {
|
|
@@ -1054,7 +1058,7 @@ class OAuthManager {
|
|
|
1054
1058
|
scopes: response.scopes
|
|
1055
1059
|
};
|
|
1056
1060
|
this.providerTokens.set(pendingAuth.provider, tokenData);
|
|
1057
|
-
this.saveProviderToken(pendingAuth.provider, tokenData);
|
|
1061
|
+
await this.saveProviderToken(pendingAuth.provider, tokenData);
|
|
1058
1062
|
this.pendingAuths.delete(state);
|
|
1059
1063
|
this.removePendingAuthFromStorage(state);
|
|
1060
1064
|
return { ...tokenData, provider: pendingAuth.provider };
|
|
@@ -1065,7 +1069,7 @@ class OAuthManager {
|
|
|
1065
1069
|
}
|
|
1066
1070
|
}
|
|
1067
1071
|
async checkAuthStatus(provider) {
|
|
1068
|
-
const tokenData = this.
|
|
1072
|
+
const tokenData = await this.getProviderToken(provider);
|
|
1069
1073
|
if (!tokenData) {
|
|
1070
1074
|
return {
|
|
1071
1075
|
authorized: false,
|
|
@@ -1080,26 +1084,38 @@ class OAuthManager {
|
|
|
1080
1084
|
};
|
|
1081
1085
|
}
|
|
1082
1086
|
async disconnectProvider(provider) {
|
|
1083
|
-
const tokenData = this.
|
|
1087
|
+
const tokenData = await this.getProviderToken(provider);
|
|
1084
1088
|
if (!tokenData) {
|
|
1085
1089
|
throw new Error(`No access token available for provider "${provider}". Cannot disconnect provider.`);
|
|
1086
1090
|
}
|
|
1087
1091
|
this.providerTokens.delete(provider);
|
|
1088
1092
|
this.clearProviderToken(provider);
|
|
1089
1093
|
}
|
|
1090
|
-
getProviderToken(provider) {
|
|
1094
|
+
async getProviderToken(provider, context) {
|
|
1095
|
+
if (this.getTokenCallback) {
|
|
1096
|
+
try {
|
|
1097
|
+
const tokenData = await this.getTokenCallback(provider, context);
|
|
1098
|
+
if (tokenData) {
|
|
1099
|
+
this.providerTokens.set(provider, tokenData);
|
|
1100
|
+
}
|
|
1101
|
+
return tokenData;
|
|
1102
|
+
} catch (error) {
|
|
1103
|
+
console.error(`Failed to get token for ${provider} via callback:`, error);
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1091
1107
|
return this.providerTokens.get(provider);
|
|
1092
1108
|
}
|
|
1093
1109
|
getAllProviderTokens() {
|
|
1094
1110
|
return new Map(this.providerTokens);
|
|
1095
1111
|
}
|
|
1096
|
-
setProviderToken(provider, tokenData) {
|
|
1112
|
+
async setProviderToken(provider, tokenData, context) {
|
|
1097
1113
|
this.providerTokens.set(provider, tokenData);
|
|
1098
|
-
this.saveProviderToken(provider, tokenData);
|
|
1114
|
+
await this.saveProviderToken(provider, tokenData, context);
|
|
1099
1115
|
}
|
|
1100
1116
|
clearProviderToken(provider) {
|
|
1101
1117
|
this.providerTokens.delete(provider);
|
|
1102
|
-
if (typeof window !== "undefined" && window.localStorage) {
|
|
1118
|
+
if (!this.getTokenCallback && typeof window !== "undefined" && window.localStorage) {
|
|
1103
1119
|
try {
|
|
1104
1120
|
window.localStorage.removeItem(`integrate_token_${provider}`);
|
|
1105
1121
|
} catch (error) {
|
|
@@ -1110,7 +1126,7 @@ class OAuthManager {
|
|
|
1110
1126
|
clearAllProviderTokens() {
|
|
1111
1127
|
const providers = Array.from(this.providerTokens.keys());
|
|
1112
1128
|
this.providerTokens.clear();
|
|
1113
|
-
if (typeof window !== "undefined" && window.localStorage) {
|
|
1129
|
+
if (!this.getTokenCallback && typeof window !== "undefined" && window.localStorage) {
|
|
1114
1130
|
for (const provider of providers) {
|
|
1115
1131
|
try {
|
|
1116
1132
|
window.localStorage.removeItem(`integrate_token_${provider}`);
|
|
@@ -1138,7 +1154,16 @@ class OAuthManager {
|
|
|
1138
1154
|
}
|
|
1139
1155
|
}
|
|
1140
1156
|
}
|
|
1141
|
-
saveProviderToken(provider, tokenData) {
|
|
1157
|
+
async saveProviderToken(provider, tokenData, context) {
|
|
1158
|
+
if (this.setTokenCallback) {
|
|
1159
|
+
try {
|
|
1160
|
+
await this.setTokenCallback(provider, tokenData, context);
|
|
1161
|
+
} catch (error) {
|
|
1162
|
+
console.error(`Failed to save token for ${provider} via callback:`, error);
|
|
1163
|
+
throw error;
|
|
1164
|
+
}
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1142
1167
|
if (typeof window !== "undefined" && window.localStorage) {
|
|
1143
1168
|
try {
|
|
1144
1169
|
const key = `integrate_token_${provider}`;
|
|
@@ -1148,7 +1173,15 @@ class OAuthManager {
|
|
|
1148
1173
|
}
|
|
1149
1174
|
}
|
|
1150
1175
|
}
|
|
1151
|
-
loadProviderToken(provider) {
|
|
1176
|
+
async loadProviderToken(provider) {
|
|
1177
|
+
if (this.getTokenCallback) {
|
|
1178
|
+
try {
|
|
1179
|
+
return await this.getTokenCallback(provider);
|
|
1180
|
+
} catch (error) {
|
|
1181
|
+
console.error(`Failed to load token for ${provider} via callback:`, error);
|
|
1182
|
+
return;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1152
1185
|
if (typeof window !== "undefined" && window.localStorage) {
|
|
1153
1186
|
try {
|
|
1154
1187
|
const key = `integrate_token_${provider}`;
|
|
@@ -1162,9 +1195,9 @@ class OAuthManager {
|
|
|
1162
1195
|
}
|
|
1163
1196
|
return;
|
|
1164
1197
|
}
|
|
1165
|
-
loadAllProviderTokens(providers) {
|
|
1198
|
+
async loadAllProviderTokens(providers) {
|
|
1166
1199
|
for (const provider of providers) {
|
|
1167
|
-
const tokenData = this.loadProviderToken(provider);
|
|
1200
|
+
const tokenData = await this.loadProviderToken(provider);
|
|
1168
1201
|
if (tokenData) {
|
|
1169
1202
|
this.providerTokens.set(provider, tokenData);
|
|
1170
1203
|
}
|
|
@@ -1362,16 +1395,28 @@ class MCPClientBase {
|
|
|
1362
1395
|
};
|
|
1363
1396
|
this.onReauthRequired = config.onReauthRequired;
|
|
1364
1397
|
this.maxReauthRetries = config.maxReauthRetries ?? 1;
|
|
1365
|
-
this.oauthManager = new OAuthManager(oauthApiBase, config.oauthFlow, this.apiBaseUrl
|
|
1398
|
+
this.oauthManager = new OAuthManager(oauthApiBase, config.oauthFlow, this.apiBaseUrl, {
|
|
1399
|
+
getProviderToken: config.getProviderToken,
|
|
1400
|
+
setProviderToken: config.setProviderToken
|
|
1401
|
+
});
|
|
1366
1402
|
const providers = this.integrations.filter((p) => p.oauth).map((p) => p.oauth.provider);
|
|
1367
|
-
this.oauthManager.loadAllProviderTokens(providers)
|
|
1403
|
+
this.oauthManager.loadAllProviderTokens(providers).catch((error) => {
|
|
1404
|
+
console.error("Failed to load provider tokens:", error);
|
|
1405
|
+
});
|
|
1368
1406
|
for (const integration of this.integrations) {
|
|
1369
1407
|
for (const toolName of integration.tools) {
|
|
1370
1408
|
this.enabledToolNames.add(toolName);
|
|
1371
1409
|
}
|
|
1372
1410
|
if (integration.oauth) {
|
|
1373
|
-
const
|
|
1374
|
-
this.authState.set(
|
|
1411
|
+
const provider = integration.oauth.provider;
|
|
1412
|
+
this.authState.set(provider, { authenticated: false });
|
|
1413
|
+
this.oauthManager.getProviderToken(provider).then((tokenData) => {
|
|
1414
|
+
if (tokenData) {
|
|
1415
|
+
this.authState.set(provider, { authenticated: true });
|
|
1416
|
+
}
|
|
1417
|
+
}).catch((error) => {
|
|
1418
|
+
console.error(`Failed to check token for ${provider}:`, error);
|
|
1419
|
+
});
|
|
1375
1420
|
}
|
|
1376
1421
|
}
|
|
1377
1422
|
const integrationIds = this.integrations.map((i) => i.id);
|
|
@@ -1399,9 +1444,9 @@ class MCPClientBase {
|
|
|
1399
1444
|
}
|
|
1400
1445
|
return new Proxy({}, {
|
|
1401
1446
|
get: (_target, methodName) => {
|
|
1402
|
-
return async (args) => {
|
|
1447
|
+
return async (args, options) => {
|
|
1403
1448
|
const toolName = methodToToolName(methodName, integrationId);
|
|
1404
|
-
return await this.callToolWithRetry(toolName, args, 0);
|
|
1449
|
+
return await this.callToolWithRetry(toolName, args, 0, options);
|
|
1405
1450
|
};
|
|
1406
1451
|
}
|
|
1407
1452
|
});
|
|
@@ -1409,17 +1454,17 @@ class MCPClientBase {
|
|
|
1409
1454
|
createServerProxy() {
|
|
1410
1455
|
return new Proxy({}, {
|
|
1411
1456
|
get: (_target, methodName) => {
|
|
1412
|
-
return async (args) => {
|
|
1457
|
+
return async (args, options) => {
|
|
1413
1458
|
const toolName = methodToToolName(methodName, "");
|
|
1414
1459
|
const finalToolName = toolName.startsWith("_") ? toolName.substring(1) : toolName;
|
|
1415
|
-
return await this.callServerToolInternal(finalToolName, args);
|
|
1460
|
+
return await this.callServerToolInternal(finalToolName, args, options);
|
|
1416
1461
|
};
|
|
1417
1462
|
}
|
|
1418
1463
|
});
|
|
1419
1464
|
}
|
|
1420
|
-
async callServerToolInternal(name, args) {
|
|
1465
|
+
async callServerToolInternal(name, args, options) {
|
|
1421
1466
|
try {
|
|
1422
|
-
const response = await this.callToolThroughHandler(name, args);
|
|
1467
|
+
const response = await this.callToolThroughHandler(name, args, undefined, options);
|
|
1423
1468
|
return response;
|
|
1424
1469
|
} catch (error) {
|
|
1425
1470
|
const parsedError = parseServerError(error, { toolName: name });
|
|
@@ -1468,8 +1513,8 @@ class MCPClientBase {
|
|
|
1468
1513
|
const enabledTools = response.tools.filter((tool) => this.enabledToolNames.has(tool.name));
|
|
1469
1514
|
console.log(`Discovered ${response.tools.length} tools, ${enabledTools.length} enabled by integrations`);
|
|
1470
1515
|
}
|
|
1471
|
-
async _callToolByName(name, args) {
|
|
1472
|
-
return await this.callToolWithRetry(name, args, 0);
|
|
1516
|
+
async _callToolByName(name, args, options) {
|
|
1517
|
+
return await this.callToolWithRetry(name, args, 0, options);
|
|
1473
1518
|
}
|
|
1474
1519
|
async callServerTool(name, args) {
|
|
1475
1520
|
try {
|
|
@@ -1480,12 +1525,12 @@ class MCPClientBase {
|
|
|
1480
1525
|
throw parsedError;
|
|
1481
1526
|
}
|
|
1482
1527
|
}
|
|
1483
|
-
async callToolThroughHandler(name, args, provider) {
|
|
1528
|
+
async callToolThroughHandler(name, args, provider, options) {
|
|
1484
1529
|
const transportHeaders = this.transport.headers || {};
|
|
1485
1530
|
const hasApiKey = !!transportHeaders["X-API-KEY"];
|
|
1486
1531
|
if (hasApiKey) {
|
|
1487
1532
|
if (provider) {
|
|
1488
|
-
const tokenData = this.oauthManager.getProviderToken(provider);
|
|
1533
|
+
const tokenData = await this.oauthManager.getProviderToken(provider, options?.context);
|
|
1489
1534
|
if (tokenData && this.transport.setHeader) {
|
|
1490
1535
|
const previousAuthHeader = transportHeaders["Authorization"];
|
|
1491
1536
|
try {
|
|
@@ -1515,7 +1560,7 @@ class MCPClientBase {
|
|
|
1515
1560
|
"Content-Type": "application/json"
|
|
1516
1561
|
};
|
|
1517
1562
|
if (provider) {
|
|
1518
|
-
const tokenData = this.oauthManager.getProviderToken(provider);
|
|
1563
|
+
const tokenData = await this.oauthManager.getProviderToken(provider, options?.context);
|
|
1519
1564
|
if (tokenData) {
|
|
1520
1565
|
headers["Authorization"] = `Bearer ${tokenData.accessToken}`;
|
|
1521
1566
|
}
|
|
@@ -1553,13 +1598,13 @@ class MCPClientBase {
|
|
|
1553
1598
|
const result = await response.json();
|
|
1554
1599
|
return result;
|
|
1555
1600
|
}
|
|
1556
|
-
async callToolWithRetry(name, args, retryCount = 0) {
|
|
1601
|
+
async callToolWithRetry(name, args, retryCount = 0, options) {
|
|
1557
1602
|
if (!this.enabledToolNames.has(name)) {
|
|
1558
1603
|
throw new Error(`Tool "${name}" is not enabled. Enable it by adding the appropriate integration.`);
|
|
1559
1604
|
}
|
|
1560
1605
|
const provider = this.getProviderForTool(name);
|
|
1561
1606
|
try {
|
|
1562
|
-
const response = await this.callToolThroughHandler(name, args, provider);
|
|
1607
|
+
const response = await this.callToolThroughHandler(name, args, provider, options);
|
|
1563
1608
|
if (provider) {
|
|
1564
1609
|
this.authState.set(provider, { authenticated: true });
|
|
1565
1610
|
}
|
|
@@ -1710,7 +1755,7 @@ class MCPClientBase {
|
|
|
1710
1755
|
this.eventEmitter.emit("auth:started", { provider });
|
|
1711
1756
|
try {
|
|
1712
1757
|
await this.oauthManager.initiateFlow(provider, integration.oauth, options?.returnUrl);
|
|
1713
|
-
const tokenData = this.oauthManager.getProviderToken(provider);
|
|
1758
|
+
const tokenData = await this.oauthManager.getProviderToken(provider);
|
|
1714
1759
|
if (tokenData) {
|
|
1715
1760
|
this.eventEmitter.emit("auth:complete", {
|
|
1716
1761
|
provider,
|
|
@@ -1741,11 +1786,11 @@ class MCPClientBase {
|
|
|
1741
1786
|
throw error;
|
|
1742
1787
|
}
|
|
1743
1788
|
}
|
|
1744
|
-
getProviderToken(provider) {
|
|
1745
|
-
return this.oauthManager.getProviderToken(provider);
|
|
1789
|
+
async getProviderToken(provider, context) {
|
|
1790
|
+
return await this.oauthManager.getProviderToken(provider, context);
|
|
1746
1791
|
}
|
|
1747
|
-
setProviderToken(provider, tokenData) {
|
|
1748
|
-
this.oauthManager.setProviderToken(provider, tokenData);
|
|
1792
|
+
async setProviderToken(provider, tokenData, context) {
|
|
1793
|
+
await this.oauthManager.setProviderToken(provider, tokenData, context);
|
|
1749
1794
|
this.authState.set(provider, { authenticated: true });
|
|
1750
1795
|
}
|
|
1751
1796
|
getAllProviderTokens() {
|