@kontext-dev/js-sdk 0.3.0 → 1.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/dist/adapters/ai/index.cjs +12 -2
- package/dist/adapters/ai/index.cjs.map +1 -1
- package/dist/adapters/ai/index.js +12 -2
- package/dist/adapters/ai/index.js.map +1 -1
- package/dist/adapters/cloudflare/index.cjs +13 -0
- package/dist/adapters/cloudflare/index.cjs.map +1 -1
- package/dist/adapters/cloudflare/index.js +13 -0
- package/dist/adapters/cloudflare/index.js.map +1 -1
- package/dist/adapters/cloudflare/react.cjs +12 -2
- package/dist/adapters/cloudflare/react.cjs.map +1 -1
- package/dist/adapters/cloudflare/react.js +12 -2
- package/dist/adapters/cloudflare/react.js.map +1 -1
- package/dist/adapters/react/index.cjs +12 -2
- package/dist/adapters/react/index.cjs.map +1 -1
- package/dist/adapters/react/index.js +12 -2
- package/dist/adapters/react/index.js.map +1 -1
- package/dist/client/index.cjs +108 -69
- package/dist/client/index.cjs.map +1 -1
- package/dist/client/index.d.cts +2 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +109 -71
- package/dist/client/index.js.map +1 -1
- package/dist/errors.cjs +78 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +7 -1
- package/dist/errors.d.ts +7 -1
- package/dist/errors.js +78 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +151 -87
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +152 -88
- package/dist/index.js.map +1 -1
- package/dist/{kontext-CgIBANFo.d.cts → kontext-CBPuE-hq.d.cts} +3 -0
- package/dist/{kontext-CgIBANFo.d.ts → kontext-CBPuE-hq.d.ts} +3 -0
- package/dist/management/index.cjs +15 -0
- package/dist/management/index.cjs.map +1 -1
- package/dist/management/index.d.cts +2 -2
- package/dist/management/index.d.ts +2 -2
- package/dist/management/index.js +15 -1
- package/dist/management/index.js.map +1 -1
- package/dist/mcp/index.cjs +32 -3
- package/dist/mcp/index.cjs.map +1 -1
- package/dist/mcp/index.d.cts +7 -1
- package/dist/mcp/index.d.ts +7 -1
- package/dist/mcp/index.js +33 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/oauth/index.cjs +12 -2
- package/dist/oauth/index.cjs.map +1 -1
- package/dist/oauth/index.d.cts +1 -1
- package/dist/oauth/index.d.ts +1 -1
- package/dist/oauth/index.js +12 -2
- package/dist/oauth/index.js.map +1 -1
- package/dist/server/index.cjs +55 -20
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +2 -2
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +56 -21
- package/dist/server/index.js.map +1 -1
- package/dist/{types-CzhnlJHW.d.cts → types-DicGI7ix.d.cts} +23 -1
- package/dist/{types-CzhnlJHW.d.ts → types-DicGI7ix.d.ts} +23 -1
- package/package.json +1 -1
package/dist/client/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { KontextMcp } from '../mcp/index.cjs';
|
|
2
2
|
import { K as KontextStorage } from '../types-RIzHnRpk.cjs';
|
|
3
3
|
import { KontextError } from '../errors.cjs';
|
|
4
|
+
export { translateError } from '../errors.cjs';
|
|
4
5
|
import '@modelcontextprotocol/sdk/client/index.js';
|
|
5
6
|
import '@modelcontextprotocol/sdk/types.js';
|
|
6
7
|
|
|
@@ -108,6 +109,7 @@ interface KontextClient {
|
|
|
108
109
|
*/
|
|
109
110
|
readonly mcp: KontextMcp;
|
|
110
111
|
}
|
|
112
|
+
|
|
111
113
|
declare function createSingleEndpointKontextClient(config: KontextClientConfig): KontextClient;
|
|
112
114
|
/**
|
|
113
115
|
* Primary SDK client entrypoint.
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { KontextMcp } from '../mcp/index.js';
|
|
2
2
|
import { K as KontextStorage } from '../types-RIzHnRpk.js';
|
|
3
3
|
import { KontextError } from '../errors.js';
|
|
4
|
+
export { translateError } from '../errors.js';
|
|
4
5
|
import '@modelcontextprotocol/sdk/client/index.js';
|
|
5
6
|
import '@modelcontextprotocol/sdk/types.js';
|
|
6
7
|
|
|
@@ -108,6 +109,7 @@ interface KontextClient {
|
|
|
108
109
|
*/
|
|
109
110
|
readonly mcp: KontextMcp;
|
|
110
111
|
}
|
|
112
|
+
|
|
111
113
|
declare function createSingleEndpointKontextClient(config: KontextClientConfig): KontextClient;
|
|
112
114
|
/**
|
|
113
115
|
* Primary SDK client entrypoint.
|
package/dist/client/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
2
2
|
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
3
|
-
import { UrlElicitationRequiredError, ElicitRequestSchema, ElicitationCompleteNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
3
|
+
import { ErrorCode, UrlElicitationRequiredError, ElicitRequestSchema, ElicitationCompleteNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
4
4
|
import { randomBytes } from 'crypto';
|
|
5
5
|
|
|
6
6
|
// src/mcp/client.ts
|
|
@@ -67,8 +67,6 @@ var StorageKeys = {
|
|
|
67
67
|
function resourceTokenKey(resource) {
|
|
68
68
|
return `${StorageKeys.RESOURCE_TOKENS}:${resource}`;
|
|
69
69
|
}
|
|
70
|
-
|
|
71
|
-
// src/errors.ts
|
|
72
70
|
var KontextError = class extends Error {
|
|
73
71
|
/** Brand field for type narrowing without instanceof */
|
|
74
72
|
kontextError = true;
|
|
@@ -188,15 +186,90 @@ function isNetworkError(err) {
|
|
|
188
186
|
if (typeof causeCode === "string" && NETWORK_ERROR_CODES.has(causeCode))
|
|
189
187
|
return true;
|
|
190
188
|
}
|
|
189
|
+
if (err.name === "TypeError") {
|
|
190
|
+
const msg = err.message.toLowerCase();
|
|
191
|
+
if (msg === "failed to fetch" || msg === "load failed" || msg.includes("networkerror")) {
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
191
195
|
return false;
|
|
192
196
|
}
|
|
193
197
|
function isUnauthorizedError(err) {
|
|
194
198
|
const props = errorProps(err);
|
|
195
199
|
if (props.statusCode === 401 || props.status === 401) return true;
|
|
200
|
+
if (props.code === 401) return true;
|
|
196
201
|
if (err.name === "UnauthorizedError") return true;
|
|
202
|
+
if (err.constructor?.name === "UnauthorizedError") return true;
|
|
197
203
|
if (err.message === "Unauthorized") return true;
|
|
198
204
|
return false;
|
|
199
205
|
}
|
|
206
|
+
var MCP_CODE_MAP = {
|
|
207
|
+
[ErrorCode.ParseError]: { code: "kontext_mcp_parse_error" },
|
|
208
|
+
[ErrorCode.InvalidRequest]: { code: "kontext_mcp_invalid_request" },
|
|
209
|
+
[ErrorCode.MethodNotFound]: { code: "kontext_mcp_method_not_found" },
|
|
210
|
+
[ErrorCode.InvalidParams]: { code: "kontext_mcp_invalid_params" },
|
|
211
|
+
[ErrorCode.InternalError]: {
|
|
212
|
+
code: "kontext_mcp_internal_error",
|
|
213
|
+
statusCode: 500
|
|
214
|
+
},
|
|
215
|
+
[ErrorCode.RequestTimeout]: {
|
|
216
|
+
code: "kontext_mcp_session_expired",
|
|
217
|
+
statusCode: 401
|
|
218
|
+
},
|
|
219
|
+
[ErrorCode.ConnectionClosed]: { code: "kontext_mcp_session_error" }
|
|
220
|
+
};
|
|
221
|
+
function translateError(err) {
|
|
222
|
+
if (isKontextError(err)) return err;
|
|
223
|
+
if (!(err instanceof Error)) {
|
|
224
|
+
return new KontextError(String(err), "kontext_unknown_error");
|
|
225
|
+
}
|
|
226
|
+
const props = err;
|
|
227
|
+
if (props.code === ErrorCode.UrlElicitationRequired) {
|
|
228
|
+
const elicitations = props.elicitations ?? props.data?.elicitations;
|
|
229
|
+
const elicitation = elicitations?.[0];
|
|
230
|
+
return new IntegrationConnectionRequiredError(
|
|
231
|
+
elicitation?.integrationId ?? "unknown",
|
|
232
|
+
{
|
|
233
|
+
integrationName: elicitation?.integrationName,
|
|
234
|
+
connectUrl: elicitation?.url,
|
|
235
|
+
message: elicitation?.message,
|
|
236
|
+
cause: err
|
|
237
|
+
}
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
if (typeof props.code === "number" && props.code < 0) {
|
|
241
|
+
const entry = MCP_CODE_MAP[props.code];
|
|
242
|
+
if (entry) {
|
|
243
|
+
return new KontextError(err.message, entry.code, {
|
|
244
|
+
statusCode: entry.statusCode,
|
|
245
|
+
cause: err
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return new KontextError(err.message, "kontext_mcp_error", {
|
|
249
|
+
cause: err,
|
|
250
|
+
meta: { mcpCode: props.code }
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
const statusCode = props.statusCode ?? props.status ?? (typeof props.code === "number" && props.code >= 400 && props.code < 600 ? props.code : void 0);
|
|
254
|
+
if (typeof statusCode === "number" && statusCode >= 400) {
|
|
255
|
+
if (statusCode === 401) {
|
|
256
|
+
return new AuthorizationRequiredError(err.message, { cause: err });
|
|
257
|
+
}
|
|
258
|
+
return new KontextError(err.message, "kontext_server_error", {
|
|
259
|
+
statusCode,
|
|
260
|
+
cause: err
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
if (isUnauthorizedError(err)) {
|
|
264
|
+
return new AuthorizationRequiredError(err.message, { cause: err });
|
|
265
|
+
}
|
|
266
|
+
if (isNetworkError(err)) {
|
|
267
|
+
return new NetworkError(err.message, { cause: err });
|
|
268
|
+
}
|
|
269
|
+
return new KontextError(err.message, "kontext_unknown_error", {
|
|
270
|
+
cause: err
|
|
271
|
+
});
|
|
272
|
+
}
|
|
200
273
|
|
|
201
274
|
// src/oauth/provider.ts
|
|
202
275
|
var KontextOAuthProvider = class {
|
|
@@ -634,7 +707,22 @@ var KontextMcp = class {
|
|
|
634
707
|
const url = typeof item.url === "string" ? item.url : "";
|
|
635
708
|
if (!id || !url) return null;
|
|
636
709
|
const category = item.category === "internal_mcp_credentials" ? "internal_mcp_credentials" : "gateway_remote_mcp";
|
|
637
|
-
const
|
|
710
|
+
const rawConnectType = item.connectType;
|
|
711
|
+
if (typeof rawConnectType !== "string") {
|
|
712
|
+
throw new KontextError(
|
|
713
|
+
"Runtime integration connectType is required in API response.",
|
|
714
|
+
"kontext_runtime_integrations_invalid_response",
|
|
715
|
+
{ meta: { integrationId: id, connectType: rawConnectType } }
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
const connectType = rawConnectType === "credentials" || rawConnectType === "oauth" || rawConnectType === "user_token" || rawConnectType === "none" ? rawConnectType : null;
|
|
719
|
+
if (!connectType) {
|
|
720
|
+
throw new KontextError(
|
|
721
|
+
`Unknown runtime integration connectType "${rawConnectType}".`,
|
|
722
|
+
"kontext_runtime_integrations_invalid_response",
|
|
723
|
+
{ meta: { integrationId: id, connectType: rawConnectType } }
|
|
724
|
+
);
|
|
725
|
+
}
|
|
638
726
|
const rawConnection = item.connection && typeof item.connection === "object" ? item.connection : void 0;
|
|
639
727
|
const connected = rawConnection && typeof rawConnection.connected === "boolean" ? rawConnection.connected : false;
|
|
640
728
|
const status = rawConnection?.status === "connected" ? "connected" : "disconnected";
|
|
@@ -645,6 +733,9 @@ var KontextMcp = class {
|
|
|
645
733
|
category,
|
|
646
734
|
connectType,
|
|
647
735
|
authMode: item.authMode === "oauth" || item.authMode === "user_token" || item.authMode === "server_token" || item.authMode === "none" ? item.authMode : void 0,
|
|
736
|
+
tokenLabel: typeof item.tokenLabel === "string" ? item.tokenLabel : void 0,
|
|
737
|
+
tokenHelpUrl: typeof item.tokenHelpUrl === "string" ? item.tokenHelpUrl : void 0,
|
|
738
|
+
tokenPlaceholder: typeof item.tokenPlaceholder === "string" ? item.tokenPlaceholder : void 0,
|
|
648
739
|
credentialSchema: item.credentialSchema,
|
|
649
740
|
requiresOauth: typeof item.requiresOauth === "boolean" ? item.requiresOauth : void 0,
|
|
650
741
|
connection: rawConnection ? {
|
|
@@ -1346,35 +1437,21 @@ async function withTransientRetry(operation, maxRetries = 1) {
|
|
|
1346
1437
|
function toKontextError(err, context) {
|
|
1347
1438
|
const contextMeta = context ? { ...context } : void 0;
|
|
1348
1439
|
const mergeMeta = (base) => contextMeta ? { ...base ?? {}, ...contextMeta } : base ?? {};
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
}
|
|
1353
|
-
const cloned = Object.create(Object.getPrototypeOf(err));
|
|
1354
|
-
Object.defineProperties(cloned, Object.getOwnPropertyDescriptors(err));
|
|
1355
|
-
Object.defineProperty(cloned, "meta", {
|
|
1356
|
-
value: mergeMeta(err.meta),
|
|
1357
|
-
enumerable: true,
|
|
1358
|
-
writable: true,
|
|
1359
|
-
configurable: true
|
|
1360
|
-
});
|
|
1361
|
-
return cloned;
|
|
1440
|
+
const translated = translateError(err);
|
|
1441
|
+
if (!contextMeta) {
|
|
1442
|
+
return translated;
|
|
1362
1443
|
}
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
cause: err
|
|
1373
|
-
});
|
|
1374
|
-
}
|
|
1375
|
-
return new KontextError(String(err), "kontext_unknown_error", {
|
|
1376
|
-
meta: mergeMeta()
|
|
1444
|
+
const cloned = Object.create(
|
|
1445
|
+
Object.getPrototypeOf(translated)
|
|
1446
|
+
);
|
|
1447
|
+
Object.defineProperties(cloned, Object.getOwnPropertyDescriptors(translated));
|
|
1448
|
+
Object.defineProperty(cloned, "meta", {
|
|
1449
|
+
value: mergeMeta(translated.meta),
|
|
1450
|
+
enumerable: true,
|
|
1451
|
+
writable: true,
|
|
1452
|
+
configurable: true
|
|
1377
1453
|
});
|
|
1454
|
+
return cloned;
|
|
1378
1455
|
}
|
|
1379
1456
|
function isAuthorizationRequired(err) {
|
|
1380
1457
|
if (err instanceof AuthorizationRequiredError) return true;
|
|
@@ -2088,45 +2165,6 @@ function extractTextContent(result) {
|
|
|
2088
2165
|
}
|
|
2089
2166
|
return JSON.stringify(result);
|
|
2090
2167
|
}
|
|
2091
|
-
function translateError(err) {
|
|
2092
|
-
if (isKontextError(err)) return err;
|
|
2093
|
-
if (!(err instanceof Error)) {
|
|
2094
|
-
return new KontextError(String(err), "kontext_unknown_error");
|
|
2095
|
-
}
|
|
2096
|
-
const props = err;
|
|
2097
|
-
if (props.code === -32042) {
|
|
2098
|
-
const elicitations = props.elicitations ?? props.data?.elicitations;
|
|
2099
|
-
const elicitation = elicitations?.[0];
|
|
2100
|
-
return new IntegrationConnectionRequiredError(
|
|
2101
|
-
elicitation?.integrationId ?? "unknown",
|
|
2102
|
-
{
|
|
2103
|
-
integrationName: elicitation?.integrationName,
|
|
2104
|
-
connectUrl: elicitation?.url,
|
|
2105
|
-
message: elicitation?.message,
|
|
2106
|
-
cause: err
|
|
2107
|
-
}
|
|
2108
|
-
);
|
|
2109
|
-
}
|
|
2110
|
-
const statusCode = props.statusCode ?? props.status;
|
|
2111
|
-
if (typeof statusCode === "number" && statusCode >= 400) {
|
|
2112
|
-
if (statusCode === 401) {
|
|
2113
|
-
return new AuthorizationRequiredError(err.message, { cause: err });
|
|
2114
|
-
}
|
|
2115
|
-
return new KontextError(err.message, "kontext_server_error", {
|
|
2116
|
-
statusCode,
|
|
2117
|
-
cause: err
|
|
2118
|
-
});
|
|
2119
|
-
}
|
|
2120
|
-
if (isUnauthorizedError(err)) {
|
|
2121
|
-
return new AuthorizationRequiredError(err.message, { cause: err });
|
|
2122
|
-
}
|
|
2123
|
-
if (isNetworkError(err)) {
|
|
2124
|
-
return new NetworkError(err.message, { cause: err });
|
|
2125
|
-
}
|
|
2126
|
-
return new KontextError(err.message, "kontext_unknown_error", {
|
|
2127
|
-
cause: err
|
|
2128
|
-
});
|
|
2129
|
-
}
|
|
2130
2168
|
function createSingleEndpointKontextClient(config) {
|
|
2131
2169
|
if (!config.clientId) {
|
|
2132
2170
|
throw new ConfigError(
|
|
@@ -2413,6 +2451,6 @@ function createKontextClient(config) {
|
|
|
2413
2451
|
return createKontextOrchestrator(config);
|
|
2414
2452
|
}
|
|
2415
2453
|
|
|
2416
|
-
export { createKontextClient, createSingleEndpointKontextClient };
|
|
2454
|
+
export { createKontextClient, createSingleEndpointKontextClient, translateError };
|
|
2417
2455
|
//# sourceMappingURL=index.js.map
|
|
2418
2456
|
//# sourceMappingURL=index.js.map
|