@blaxel/core 0.2.0-dev1
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/LICENSE +21 -0
- package/dist/agents/index.d.ts +15 -0
- package/dist/agents/index.js +114 -0
- package/dist/authentication/apikey.d.ts +10 -0
- package/dist/authentication/apikey.js +24 -0
- package/dist/authentication/clientcredentials.d.ts +15 -0
- package/dist/authentication/clientcredentials.js +70 -0
- package/dist/authentication/credentials.d.ts +6 -0
- package/dist/authentication/credentials.js +17 -0
- package/dist/authentication/deviceMode.d.ts +15 -0
- package/dist/authentication/deviceMode.js +70 -0
- package/dist/authentication/index.d.ts +3 -0
- package/dist/authentication/index.js +63 -0
- package/dist/authentication/types.d.ts +9 -0
- package/dist/authentication/types.js +2 -0
- package/dist/cache/index.d.ts +1 -0
- package/dist/cache/index.js +26 -0
- package/dist/client/authentication.d.ts +25 -0
- package/dist/client/authentication.js +15 -0
- package/dist/client/client.d.ts +2 -0
- package/dist/client/client.gen.d.ts +12 -0
- package/dist/client/client.gen.js +8 -0
- package/dist/client/client.js +17 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +19 -0
- package/dist/client/interceptors.d.ts +3 -0
- package/dist/client/interceptors.js +17 -0
- package/dist/client/sdk.gen.d.ts +410 -0
- package/dist/client/sdk.gen.js +1436 -0
- package/dist/client/types.gen.d.ts +3534 -0
- package/dist/client/types.gen.js +4 -0
- package/dist/common/autoload.d.ts +3 -0
- package/dist/common/autoload.js +27 -0
- package/dist/common/env.d.ts +5 -0
- package/dist/common/env.js +43 -0
- package/dist/common/errors.d.ts +1 -0
- package/dist/common/errors.js +17 -0
- package/dist/common/internal.d.ts +2 -0
- package/dist/common/internal.js +19 -0
- package/dist/common/logger.d.ts +1 -0
- package/dist/common/logger.js +4 -0
- package/dist/common/node.d.ts +3 -0
- package/dist/common/node.js +14 -0
- package/dist/common/settings.d.ts +26 -0
- package/dist/common/settings.js +84 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +30 -0
- package/dist/mcp/client.d.ts +19 -0
- package/dist/mcp/client.js +151 -0
- package/dist/mcp/index.d.ts +2 -0
- package/dist/mcp/index.js +18 -0
- package/dist/mcp/server.d.ts +24 -0
- package/dist/mcp/server.js +213 -0
- package/dist/models/index.d.ts +8 -0
- package/dist/models/index.js +31 -0
- package/dist/sandbox/action.d.ts +18 -0
- package/dist/sandbox/action.js +74 -0
- package/dist/sandbox/client/client.gen.d.ts +12 -0
- package/dist/sandbox/client/client.gen.js +6 -0
- package/dist/sandbox/client/index.d.ts +2 -0
- package/dist/sandbox/client/index.js +19 -0
- package/dist/sandbox/client/sdk.gen.d.ts +83 -0
- package/dist/sandbox/client/sdk.gen.js +153 -0
- package/dist/sandbox/client/types.gen.d.ts +410 -0
- package/dist/sandbox/client/types.gen.js +4 -0
- package/dist/sandbox/filesystem.d.ts +18 -0
- package/dist/sandbox/filesystem.js +121 -0
- package/dist/sandbox/index.d.ts +1 -0
- package/dist/sandbox/index.js +17 -0
- package/dist/sandbox/network.d.ts +5 -0
- package/dist/sandbox/network.js +10 -0
- package/dist/sandbox/process.d.ts +12 -0
- package/dist/sandbox/process.js +61 -0
- package/dist/sandbox/sandbox.d.ts +23 -0
- package/dist/sandbox/sandbox.js +92 -0
- package/dist/telemetry/telemetry.d.ts +58 -0
- package/dist/telemetry/telemetry.js +59 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +52 -0
- package/dist/tools/mcpTool.d.ts +21 -0
- package/dist/tools/mcpTool.js +193 -0
- package/dist/tools/types.d.ts +8 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/zodSchema.d.ts +9 -0
- package/dist/tools/zodSchema.js +46 -0
- package/package.json +76 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initialize = initialize;
|
|
4
|
+
exports.authenticate = authenticate;
|
|
5
|
+
const client_gen_js_1 = require("../client/client.gen.js");
|
|
6
|
+
const interceptors_js_1 = require("../client/interceptors.js");
|
|
7
|
+
const client_gen_js_2 = require("../sandbox/client/client.gen.js");
|
|
8
|
+
const settings_js_1 = require("./settings.js");
|
|
9
|
+
client_gen_js_1.client.setConfig({
|
|
10
|
+
baseUrl: settings_js_1.settings.baseUrl,
|
|
11
|
+
});
|
|
12
|
+
for (const interceptor of interceptors_js_1.interceptors) {
|
|
13
|
+
// @ts-expect-error - Interceptor is not typed
|
|
14
|
+
client_gen_js_1.client.interceptors.request.use(interceptor);
|
|
15
|
+
// @ts-expect-error - Interceptor is not typed
|
|
16
|
+
client_gen_js_2.client.interceptors.request.use(interceptor);
|
|
17
|
+
}
|
|
18
|
+
// Allow to set custom configuration for browser environment
|
|
19
|
+
function initialize(config) {
|
|
20
|
+
settings_js_1.settings.setConfig(config);
|
|
21
|
+
client_gen_js_1.client.setConfig({
|
|
22
|
+
baseUrl: settings_js_1.settings.baseUrl,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function authenticate() {
|
|
26
|
+
return settings_js_1.settings.authenticate();
|
|
27
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.env = void 0;
|
|
7
|
+
const toml_1 = __importDefault(require("toml"));
|
|
8
|
+
const node_js_1 = require("./node.js");
|
|
9
|
+
const secretEnv = {};
|
|
10
|
+
const configEnv = {};
|
|
11
|
+
if (node_js_1.fs !== null) {
|
|
12
|
+
try {
|
|
13
|
+
const configFile = node_js_1.fs.readFileSync("blaxel.toml", "utf8");
|
|
14
|
+
const configInfos = toml_1.default.parse(configFile);
|
|
15
|
+
for (const key in configInfos.env) {
|
|
16
|
+
configEnv[key] = configInfos.env[key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
catch (error) { }
|
|
20
|
+
try {
|
|
21
|
+
const secretFile = node_js_1.fs.readFileSync(".env", "utf8");
|
|
22
|
+
secretFile.split("\n").forEach((line) => {
|
|
23
|
+
if (line.startsWith("#")) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const [key, value] = line.split("=");
|
|
27
|
+
secretEnv[key] = value;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch (error) { }
|
|
31
|
+
}
|
|
32
|
+
const env = new Proxy({}, {
|
|
33
|
+
get: (target, prop) => {
|
|
34
|
+
if (secretEnv[prop]) {
|
|
35
|
+
return secretEnv[prop];
|
|
36
|
+
}
|
|
37
|
+
if (configEnv[prop]) {
|
|
38
|
+
return configEnv[prop];
|
|
39
|
+
}
|
|
40
|
+
return process.env[prop];
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
exports.env = env;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function handleDynamicImportError(err: any): void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleDynamicImportError = handleDynamicImportError;
|
|
4
|
+
function handleDynamicImportError(err) {
|
|
5
|
+
if (err instanceof Error) {
|
|
6
|
+
// We check if it's module import error and retrieve package name from the error message with a regex
|
|
7
|
+
if (err.message.includes("Cannot find module")) {
|
|
8
|
+
const packageName = err.message.match(/Cannot find module '([^']+)'/)?.[1];
|
|
9
|
+
if (packageName) {
|
|
10
|
+
err.message = `Dependency not found. Please run one of the following commands to install it:
|
|
11
|
+
- npm: 'npm install ${packageName}'
|
|
12
|
+
- yarn: 'yarn add ${packageName}'
|
|
13
|
+
- pnpm: 'pnpm add ${packageName}'`;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getAlphanumericLimitedHash = getAlphanumericLimitedHash;
|
|
7
|
+
exports.getGlobalUniqueHash = getGlobalUniqueHash;
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
+
function getAlphanumericLimitedHash(input, maxSize) {
|
|
10
|
+
const hash = crypto_1.default.createHash("sha256").update(input).digest("base64");
|
|
11
|
+
const alphanumeric = hash.replace(/[^a-z0-9]+/g, "");
|
|
12
|
+
return alphanumeric.length > maxSize
|
|
13
|
+
? alphanumeric.substring(0, maxSize)
|
|
14
|
+
: alphanumeric;
|
|
15
|
+
}
|
|
16
|
+
function getGlobalUniqueHash(workspace, type, name) {
|
|
17
|
+
const globalUniqueName = `${workspace}-${type}-${name}`;
|
|
18
|
+
return getAlphanumericLimitedHash(globalUniqueName, 48);
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const logger: Console;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.os = exports.fs = void 0;
|
|
4
|
+
const isNode = typeof process !== "undefined" &&
|
|
5
|
+
process.versions != null &&
|
|
6
|
+
process.versions.node != null;
|
|
7
|
+
let fs = null;
|
|
8
|
+
exports.fs = fs;
|
|
9
|
+
let os = null;
|
|
10
|
+
exports.os = os;
|
|
11
|
+
if (isNode) {
|
|
12
|
+
exports.fs = fs = require("fs");
|
|
13
|
+
exports.os = os = require("os");
|
|
14
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Credentials } from "../authentication/credentials.js";
|
|
2
|
+
export type Config = {
|
|
3
|
+
proxy?: string;
|
|
4
|
+
apikey?: string;
|
|
5
|
+
workspace?: string;
|
|
6
|
+
};
|
|
7
|
+
declare class Settings {
|
|
8
|
+
credentials: Credentials;
|
|
9
|
+
config: Config;
|
|
10
|
+
constructor();
|
|
11
|
+
setConfig(config: Config): void;
|
|
12
|
+
get env(): string;
|
|
13
|
+
get baseUrl(): string;
|
|
14
|
+
get runUrl(): string;
|
|
15
|
+
get workspace(): string;
|
|
16
|
+
get authorization(): string;
|
|
17
|
+
get token(): string;
|
|
18
|
+
get headers(): Record<string, string>;
|
|
19
|
+
get name(): string;
|
|
20
|
+
get type(): string;
|
|
21
|
+
get runInternalHostname(): string;
|
|
22
|
+
get runInternalProtocol(): string;
|
|
23
|
+
authenticate(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export declare const settings: Settings;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.settings = void 0;
|
|
4
|
+
const index_js_1 = require("../authentication/index.js");
|
|
5
|
+
const env_js_1 = require("../common/env.js");
|
|
6
|
+
class Settings {
|
|
7
|
+
credentials;
|
|
8
|
+
config;
|
|
9
|
+
constructor() {
|
|
10
|
+
this.credentials = (0, index_js_1.authentication)();
|
|
11
|
+
this.config = {
|
|
12
|
+
proxy: "",
|
|
13
|
+
apikey: "",
|
|
14
|
+
workspace: "",
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
setConfig(config) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
}
|
|
20
|
+
get env() {
|
|
21
|
+
return env_js_1.env.BL_ENV || "prod";
|
|
22
|
+
}
|
|
23
|
+
get baseUrl() {
|
|
24
|
+
if (this.config.proxy) {
|
|
25
|
+
return this.config.proxy + "/api";
|
|
26
|
+
}
|
|
27
|
+
if (env_js_1.env.BL_API_URL) {
|
|
28
|
+
return env_js_1.env.BL_API_URL;
|
|
29
|
+
}
|
|
30
|
+
if (this.env === "prod") {
|
|
31
|
+
return "https://api.blaxel.ai/v0";
|
|
32
|
+
}
|
|
33
|
+
return "https://api.blaxel.dev/v0";
|
|
34
|
+
}
|
|
35
|
+
get runUrl() {
|
|
36
|
+
if (this.config.proxy) {
|
|
37
|
+
return this.config.proxy + "/run";
|
|
38
|
+
}
|
|
39
|
+
if (env_js_1.env.BL_RUN_URL) {
|
|
40
|
+
return env_js_1.env.BL_RUN_URL;
|
|
41
|
+
}
|
|
42
|
+
if (this.env === "prod") {
|
|
43
|
+
return "https://run.blaxel.ai";
|
|
44
|
+
}
|
|
45
|
+
return "https://run.blaxel.dev";
|
|
46
|
+
}
|
|
47
|
+
get workspace() {
|
|
48
|
+
return this.config.workspace || this.credentials.workspace || "";
|
|
49
|
+
}
|
|
50
|
+
get authorization() {
|
|
51
|
+
if (this.config.apikey) {
|
|
52
|
+
return 'Bearer ' + this.token;
|
|
53
|
+
}
|
|
54
|
+
return this.credentials.authorization;
|
|
55
|
+
}
|
|
56
|
+
get token() {
|
|
57
|
+
if (this.config.apikey) {
|
|
58
|
+
return this.config.apikey;
|
|
59
|
+
}
|
|
60
|
+
return this.credentials.token;
|
|
61
|
+
}
|
|
62
|
+
get headers() {
|
|
63
|
+
return {
|
|
64
|
+
"x-blaxel-authorization": this.authorization,
|
|
65
|
+
"x-blaxel-workspace": this.workspace || "",
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
get name() {
|
|
69
|
+
return env_js_1.env.BL_NAME || "";
|
|
70
|
+
}
|
|
71
|
+
get type() {
|
|
72
|
+
return env_js_1.env.BL_TYPE || "agents";
|
|
73
|
+
}
|
|
74
|
+
get runInternalHostname() {
|
|
75
|
+
return env_js_1.env.BL_RUN_INTERNAL_HOSTNAME || "";
|
|
76
|
+
}
|
|
77
|
+
get runInternalProtocol() {
|
|
78
|
+
return env_js_1.env.BL_RUN_INTERNAL_PROTOCOL || "https";
|
|
79
|
+
}
|
|
80
|
+
async authenticate() {
|
|
81
|
+
await this.credentials.authenticate();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.settings = new Settings();
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import './common/autoload';
|
|
2
|
+
export * from "./agents/index.js";
|
|
3
|
+
export * from "./client/client.js";
|
|
4
|
+
export * from "./common/autoload.js";
|
|
5
|
+
export * from "./common/env.js";
|
|
6
|
+
export * from "./common/errors.js";
|
|
7
|
+
export * from "./common/internal.js";
|
|
8
|
+
export * from "./common/logger.js";
|
|
9
|
+
export * from "./common/settings.js";
|
|
10
|
+
export * from "./mcp/index.js";
|
|
11
|
+
export * from "./models/index.js";
|
|
12
|
+
export * from "./sandbox/index.js";
|
|
13
|
+
export * from "./tools/index.js";
|
|
14
|
+
export * from "./tools/types.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
require("./common/autoload");
|
|
18
|
+
__exportStar(require("./agents/index.js"), exports);
|
|
19
|
+
__exportStar(require("./client/client.js"), exports);
|
|
20
|
+
__exportStar(require("./common/autoload.js"), exports);
|
|
21
|
+
__exportStar(require("./common/env.js"), exports);
|
|
22
|
+
__exportStar(require("./common/errors.js"), exports);
|
|
23
|
+
__exportStar(require("./common/internal.js"), exports);
|
|
24
|
+
__exportStar(require("./common/logger.js"), exports);
|
|
25
|
+
__exportStar(require("./common/settings.js"), exports);
|
|
26
|
+
__exportStar(require("./mcp/index.js"), exports);
|
|
27
|
+
__exportStar(require("./models/index.js"), exports);
|
|
28
|
+
__exportStar(require("./sandbox/index.js"), exports);
|
|
29
|
+
__exportStar(require("./tools/index.js"), exports);
|
|
30
|
+
__exportStar(require("./tools/types.js"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
2
|
+
import { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Client transport for WebSocket: this will connect to a server over the WebSocket protocol.
|
|
5
|
+
*/
|
|
6
|
+
export declare class BlaxelMcpClientTransport implements Transport {
|
|
7
|
+
private _socket?;
|
|
8
|
+
private _url;
|
|
9
|
+
private _headers;
|
|
10
|
+
onclose?: () => void;
|
|
11
|
+
onerror?: (error: Error) => void;
|
|
12
|
+
onmessage?: (message: JSONRPCMessage) => void;
|
|
13
|
+
constructor(url: string, headers?: Record<string, string>);
|
|
14
|
+
start(): Promise<void>;
|
|
15
|
+
private _connect;
|
|
16
|
+
get isConnected(): boolean;
|
|
17
|
+
close(): Promise<void>;
|
|
18
|
+
send(message: JSONRPCMessage): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.BlaxelMcpClientTransport = void 0;
|
|
7
|
+
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
|
|
8
|
+
const ws_1 = __importDefault(require("ws"));
|
|
9
|
+
const logger_js_1 = require("../common/logger.js");
|
|
10
|
+
//const SUBPROTOCOL = "mcp";
|
|
11
|
+
const MAX_RETRIES = 3;
|
|
12
|
+
const RETRY_DELAY_MS = 1000;
|
|
13
|
+
// Helper function to wait
|
|
14
|
+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
15
|
+
/**
|
|
16
|
+
* Client transport for WebSocket: this will connect to a server over the WebSocket protocol.
|
|
17
|
+
*/
|
|
18
|
+
class BlaxelMcpClientTransport {
|
|
19
|
+
_socket;
|
|
20
|
+
_url;
|
|
21
|
+
_headers;
|
|
22
|
+
onclose;
|
|
23
|
+
onerror;
|
|
24
|
+
onmessage;
|
|
25
|
+
constructor(url, headers) {
|
|
26
|
+
this._url = new URL(url.replace("http", "ws"));
|
|
27
|
+
this._headers = headers ?? {};
|
|
28
|
+
}
|
|
29
|
+
async start() {
|
|
30
|
+
if (this._socket) {
|
|
31
|
+
throw new Error("Blaxel already started! If using Client class, note that connect() calls start() automatically.");
|
|
32
|
+
}
|
|
33
|
+
let attempts = 0;
|
|
34
|
+
while (attempts < MAX_RETRIES) {
|
|
35
|
+
try {
|
|
36
|
+
await this._connect();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof Error) {
|
|
41
|
+
logger_js_1.logger.warn(error.stack);
|
|
42
|
+
}
|
|
43
|
+
attempts++;
|
|
44
|
+
if (attempts === MAX_RETRIES) {
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
logger_js_1.logger.debug(`WebSocket connection attempt ${attempts} failed, retrying in ${RETRY_DELAY_MS}ms...`);
|
|
48
|
+
await delay(RETRY_DELAY_MS);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
_connect() {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
this._socket = new ws_1.default(this._url, {
|
|
55
|
+
//protocols: SUBPROTOCOL,
|
|
56
|
+
headers: this._headers,
|
|
57
|
+
});
|
|
58
|
+
this._socket.onerror = (event) => {
|
|
59
|
+
const error = "error" in event
|
|
60
|
+
? event.error
|
|
61
|
+
: new Error(`WebSocket error: ${JSON.stringify(event)}`);
|
|
62
|
+
reject(error);
|
|
63
|
+
this.onerror?.(error);
|
|
64
|
+
};
|
|
65
|
+
this._socket.onopen = () => {
|
|
66
|
+
logger_js_1.logger.debug("WebSocket opened");
|
|
67
|
+
resolve();
|
|
68
|
+
};
|
|
69
|
+
this._socket.onclose = () => {
|
|
70
|
+
logger_js_1.logger.debug("WebSocket closed");
|
|
71
|
+
this.onclose?.();
|
|
72
|
+
this._socket = undefined;
|
|
73
|
+
};
|
|
74
|
+
this._socket.onmessage = (event) => {
|
|
75
|
+
logger_js_1.logger.debug("WebSocket message received");
|
|
76
|
+
let message;
|
|
77
|
+
try {
|
|
78
|
+
let dataString;
|
|
79
|
+
if (typeof event.data === "string") {
|
|
80
|
+
dataString = event.data;
|
|
81
|
+
}
|
|
82
|
+
else if (event.data instanceof Buffer) {
|
|
83
|
+
dataString = event.data.toString("utf-8");
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
throw new Error("Unsupported data type for event.data");
|
|
87
|
+
}
|
|
88
|
+
message = types_js_1.JSONRPCMessageSchema.parse(JSON.parse(dataString));
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
logger_js_1.logger.error(`Error parsing message: ${typeof event.data === "object"
|
|
92
|
+
? JSON.stringify(event.data)
|
|
93
|
+
: event.data}`);
|
|
94
|
+
this.onerror?.(error);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.onmessage?.(message);
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
get isConnected() {
|
|
102
|
+
return this._socket?.readyState === ws_1.default.OPEN;
|
|
103
|
+
}
|
|
104
|
+
async close() {
|
|
105
|
+
this._socket?.close();
|
|
106
|
+
this._socket = undefined;
|
|
107
|
+
this.onclose?.();
|
|
108
|
+
return Promise.resolve();
|
|
109
|
+
}
|
|
110
|
+
async send(message) {
|
|
111
|
+
let attempts = 0;
|
|
112
|
+
while (attempts < MAX_RETRIES) {
|
|
113
|
+
try {
|
|
114
|
+
if (!this._socket || this._socket.readyState !== ws_1.default.OPEN) {
|
|
115
|
+
if (!this._socket) {
|
|
116
|
+
// Only try to start if socket doesn't exist
|
|
117
|
+
await this.start();
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
throw new Error("WebSocket is not in OPEN state");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
await new Promise((resolve, reject) => {
|
|
124
|
+
try {
|
|
125
|
+
this._socket?.send(JSON.stringify(message), (error) => {
|
|
126
|
+
if (error) {
|
|
127
|
+
reject(error);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
resolve();
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
reject(error);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
attempts++;
|
|
142
|
+
if (attempts === MAX_RETRIES) {
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
logger_js_1.logger.warn(`WebSocket send attempt ${attempts} failed, retrying in ${RETRY_DELAY_MS}ms...`);
|
|
146
|
+
await delay(RETRY_DELAY_MS);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.BlaxelMcpClientTransport = BlaxelMcpClientTransport;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./client.js"), exports);
|
|
18
|
+
__exportStar(require("./server.js"), exports);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
2
|
+
interface JSONRPCMessage {
|
|
3
|
+
jsonrpc: "2.0";
|
|
4
|
+
id?: string | number;
|
|
5
|
+
method?: string;
|
|
6
|
+
params?: Record<string, unknown>;
|
|
7
|
+
}
|
|
8
|
+
export declare class BlaxelMcpServerTransport implements Transport {
|
|
9
|
+
private port;
|
|
10
|
+
private wss;
|
|
11
|
+
private clients;
|
|
12
|
+
onclose?: () => void;
|
|
13
|
+
onerror?: (err: Error) => void;
|
|
14
|
+
private messageHandler?;
|
|
15
|
+
onconnection?: (clientId: string) => void;
|
|
16
|
+
ondisconnection?: (clientId: string) => void;
|
|
17
|
+
set onmessage(handler: ((message: JSONRPCMessage) => void) | undefined);
|
|
18
|
+
constructor(port?: number);
|
|
19
|
+
start(): Promise<void>;
|
|
20
|
+
send(msg: JSONRPCMessage): Promise<void>;
|
|
21
|
+
broadcast(msg: JSONRPCMessage): Promise<void>;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export {};
|