@blaxel/core 0.2.2-preview1 → 0.2.2-preview3
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/common/node.js +7 -2
- package/dist/common/settings.js +1 -1
- package/dist/jobs/batches.d.ts +32 -0
- package/dist/jobs/batches.js +40 -0
- package/dist/jobs/job.d.ts +33 -0
- package/dist/jobs/job.js +51 -0
- package/dist/jobs/jobs.d.ts +1 -0
- package/dist/jobs/jobs.js +22 -9
- package/package.json +1 -3
- package/dist/mcp/server/http.d.ts +0 -8
- package/dist/mcp/server/http.js +0 -16
- package/dist/mcp/server/index.d.ts +0 -4
- package/dist/mcp/server/index.js +0 -21
- package/dist/mcp/server/websocket.d.ts +0 -24
- package/dist/mcp/server/websocket.js +0 -213
package/dist/common/node.js
CHANGED
|
@@ -10,6 +10,11 @@ exports.fs = fs;
|
|
|
10
10
|
let os = null;
|
|
11
11
|
exports.os = os;
|
|
12
12
|
if (isNode) {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
try {
|
|
14
|
+
exports.fs = fs = eval("require")("fs");
|
|
15
|
+
exports.os = os = eval("require")("os");
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.warn("fs and os are not available in this environment");
|
|
19
|
+
}
|
|
15
20
|
}
|
package/dist/common/settings.js
CHANGED
|
@@ -75,7 +75,7 @@ class Settings {
|
|
|
75
75
|
if (!this.generation) {
|
|
76
76
|
return "";
|
|
77
77
|
}
|
|
78
|
-
return env_js_1.env.
|
|
78
|
+
return env_js_1.env.BL_RUN_INTERNAL_HOST || "";
|
|
79
79
|
}
|
|
80
80
|
get runInternalProtocol() {
|
|
81
81
|
return env_js_1.env.BL_RUN_INTERNAL_PROTOCOL || "https";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
declare class BlBatch {
|
|
2
|
+
/**
|
|
3
|
+
* Lists all remote batch jobs for a given name
|
|
4
|
+
* @param name - The name of the job to list
|
|
5
|
+
*/
|
|
6
|
+
list(name: string): void;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new remote batch job configuration.
|
|
9
|
+
* This is used for remote execution management, not for local execution.
|
|
10
|
+
* @param name - The name of the job
|
|
11
|
+
* @param args - Array of argument arrays for each job in the batch
|
|
12
|
+
* @returns The batch configuration object
|
|
13
|
+
*/
|
|
14
|
+
create(name: string, args: any[][]): {
|
|
15
|
+
name: string;
|
|
16
|
+
args: any[][];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Kills a remote batch job
|
|
20
|
+
* @param name - The name of the job
|
|
21
|
+
* @param batchId - The ID of the batch to kill
|
|
22
|
+
*/
|
|
23
|
+
kill(name: string, batchId: string): void;
|
|
24
|
+
/**
|
|
25
|
+
* Retrieves information about a remote batch job
|
|
26
|
+
* @param name - The name of the job
|
|
27
|
+
* @param batchId - The ID of the batch to get information for
|
|
28
|
+
*/
|
|
29
|
+
get(name: string, batchId: string): void;
|
|
30
|
+
}
|
|
31
|
+
export declare const blBatch: BlBatch;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.blBatch = void 0;
|
|
4
|
+
class BlBatch {
|
|
5
|
+
/**
|
|
6
|
+
* Lists all remote batch jobs for a given name
|
|
7
|
+
* @param name - The name of the job to list
|
|
8
|
+
*/
|
|
9
|
+
list(name) {
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new remote batch job configuration.
|
|
13
|
+
* This is used for remote execution management, not for local execution.
|
|
14
|
+
* @param name - The name of the job
|
|
15
|
+
* @param args - Array of argument arrays for each job in the batch
|
|
16
|
+
* @returns The batch configuration object
|
|
17
|
+
*/
|
|
18
|
+
create(name, args) {
|
|
19
|
+
const batch = {
|
|
20
|
+
name,
|
|
21
|
+
args,
|
|
22
|
+
};
|
|
23
|
+
return batch;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Kills a remote batch job
|
|
27
|
+
* @param name - The name of the job
|
|
28
|
+
* @param batchId - The ID of the batch to kill
|
|
29
|
+
*/
|
|
30
|
+
kill(name, batchId) {
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Retrieves information about a remote batch job
|
|
34
|
+
* @param name - The name of the job
|
|
35
|
+
* @param batchId - The ID of the batch to get information for
|
|
36
|
+
*/
|
|
37
|
+
get(name, batchId) {
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.blBatch = new BlBatch();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
declare class BlJob {
|
|
2
|
+
start(func: (...args: any[]) => Promise<void>): void;
|
|
3
|
+
/**
|
|
4
|
+
* Lists all remote batch jobs for a given name
|
|
5
|
+
* @param name - The name of the job to list
|
|
6
|
+
*/
|
|
7
|
+
list(name: string): void;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new remote batch job configuration.
|
|
10
|
+
* This is used for remote execution management, not for local execution.
|
|
11
|
+
* @param name - The name of the job
|
|
12
|
+
* @param args - Array of argument arrays for each job in the batch
|
|
13
|
+
* @returns The batch configuration object
|
|
14
|
+
*/
|
|
15
|
+
create(name: string, args: any[][]): {
|
|
16
|
+
name: string;
|
|
17
|
+
args: any[][];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Kills a remote batch job
|
|
21
|
+
* @param name - The name of the job
|
|
22
|
+
* @param batchId - The ID of the batch to kill
|
|
23
|
+
*/
|
|
24
|
+
kill(name: string, batchId: string): void;
|
|
25
|
+
/**
|
|
26
|
+
* Retrieves information about a remote batch job
|
|
27
|
+
* @param name - The name of the job
|
|
28
|
+
* @param batchId - The ID of the batch to get information for
|
|
29
|
+
*/
|
|
30
|
+
get(name: string, batchId: string): void;
|
|
31
|
+
}
|
|
32
|
+
export declare const blJob: BlJob;
|
|
33
|
+
export {};
|
package/dist/jobs/job.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.blJob = void 0;
|
|
4
|
+
function retrieveArguments() {
|
|
5
|
+
const args = process.argv.slice(2);
|
|
6
|
+
return args;
|
|
7
|
+
}
|
|
8
|
+
class BlJob {
|
|
9
|
+
/*
|
|
10
|
+
Run a job defined in a function, it's run in the current process
|
|
11
|
+
*/
|
|
12
|
+
start(func) {
|
|
13
|
+
const args = retrieveArguments();
|
|
14
|
+
func(...args);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Lists all remote batch jobs for a given name
|
|
18
|
+
* @param name - The name of the job to list
|
|
19
|
+
*/
|
|
20
|
+
list(name) {
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new remote batch job configuration.
|
|
24
|
+
* This is used for remote execution management, not for local execution.
|
|
25
|
+
* @param name - The name of the job
|
|
26
|
+
* @param args - Array of argument arrays for each job in the batch
|
|
27
|
+
* @returns The batch configuration object
|
|
28
|
+
*/
|
|
29
|
+
create(name, args) {
|
|
30
|
+
const batch = {
|
|
31
|
+
name,
|
|
32
|
+
args,
|
|
33
|
+
};
|
|
34
|
+
return batch;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Kills a remote batch job
|
|
38
|
+
* @param name - The name of the job
|
|
39
|
+
* @param batchId - The ID of the batch to kill
|
|
40
|
+
*/
|
|
41
|
+
kill(name, batchId) {
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves information about a remote batch job
|
|
45
|
+
* @param name - The name of the job
|
|
46
|
+
* @param batchId - The ID of the batch to get information for
|
|
47
|
+
*/
|
|
48
|
+
get(name, batchId) {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.blJob = new BlJob();
|
package/dist/jobs/jobs.d.ts
CHANGED
package/dist/jobs/jobs.js
CHANGED
|
@@ -1,25 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.blJob = void 0;
|
|
7
|
-
const yargs_1 = __importDefault(require("yargs"));
|
|
8
|
-
const helpers_1 = require("yargs/helpers");
|
|
9
4
|
const env_js_1 = require("../common/env.js");
|
|
10
5
|
class BlJob {
|
|
11
6
|
async getArguments() {
|
|
12
7
|
if (!env_js_1.env.BL_EXECUTION_DATA_URL) {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
return argv;
|
|
8
|
+
const args = this.parseCommandLineArgs();
|
|
9
|
+
return args;
|
|
16
10
|
}
|
|
17
11
|
const response = await fetch(env_js_1.env.BL_EXECUTION_DATA_URL);
|
|
18
12
|
const data = await response.json();
|
|
19
13
|
return data.tasks[this.index] ?? {};
|
|
20
14
|
}
|
|
15
|
+
parseCommandLineArgs() {
|
|
16
|
+
const args = process.argv.slice(2);
|
|
17
|
+
const result = {};
|
|
18
|
+
for (let i = 0; i < args.length; i++) {
|
|
19
|
+
const arg = args[i];
|
|
20
|
+
if (arg.startsWith('--')) {
|
|
21
|
+
const key = arg.slice(2);
|
|
22
|
+
const value = args[i + 1];
|
|
23
|
+
if (value && !value.startsWith('--')) {
|
|
24
|
+
result[key] = value;
|
|
25
|
+
i++;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
result[key] = 'true';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
21
34
|
get indexKey() {
|
|
22
|
-
return env_js_1.env.
|
|
35
|
+
return env_js_1.env.BL_TASK_KEY ?? "TASK_INDEX";
|
|
23
36
|
}
|
|
24
37
|
get index() {
|
|
25
38
|
return env_js_1.env[this.indexKey] ? Number(env_js_1.env[this.indexKey]) ?? 0 : 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blaxel/core",
|
|
3
|
-
"version": "0.2.2-
|
|
3
|
+
"version": "0.2.2-preview3",
|
|
4
4
|
"description": "Blaxel Core SDK for TypeScript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Blaxel, INC (https://blaxel.ai)",
|
|
@@ -62,13 +62,11 @@
|
|
|
62
62
|
"uuid": "^11.1.0",
|
|
63
63
|
"ws": "^8.18.2",
|
|
64
64
|
"yaml": "^2.7.1",
|
|
65
|
-
"yargs": "^17.7.2",
|
|
66
65
|
"zod": "^3.24.3"
|
|
67
66
|
},
|
|
68
67
|
"devDependencies": {
|
|
69
68
|
"@eslint/js": "^9.26.0",
|
|
70
69
|
"@types/ws": "^8.18.1",
|
|
71
|
-
"@types/yargs": "^17.0.33",
|
|
72
70
|
"typescript": "^5.0.0",
|
|
73
71
|
"typescript-eslint": "^8.31.1"
|
|
74
72
|
},
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
2
|
-
import { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
-
export declare class BlaxelHttpMcpServerTransport implements Transport {
|
|
4
|
-
private server;
|
|
5
|
-
constructor(port: number);
|
|
6
|
-
start(): Promise<void>;
|
|
7
|
-
send(msg: JSONRPCMessage): Promise<void>;
|
|
8
|
-
}
|
package/dist/mcp/server/http.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BlaxelHttpMcpServerTransport = void 0;
|
|
4
|
-
const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
|
|
5
|
-
class BlaxelHttpMcpServerTransport {
|
|
6
|
-
server;
|
|
7
|
-
constructor(port) {
|
|
8
|
-
this.server = new streamableHttp_js_1.StreamableHTTPServerTransport({ port });
|
|
9
|
-
}
|
|
10
|
-
async start() {
|
|
11
|
-
await this.server.start();
|
|
12
|
-
}
|
|
13
|
-
async send(msg) {
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.BlaxelHttpMcpServerTransport = BlaxelHttpMcpServerTransport;
|
package/dist/mcp/server/index.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
exports.BlaxelMcpServerTransport = void 0;
|
|
18
|
-
__exportStar(require("./http.js"), exports);
|
|
19
|
-
__exportStar(require("./websocket.js"), exports);
|
|
20
|
-
const websocket_js_1 = require("./websocket.js");
|
|
21
|
-
exports.BlaxelMcpServerTransport = websocket_js_1.BlaxelWebsocketMcpServerTransport;
|
|
@@ -1,24 +0,0 @@
|
|
|
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 BlaxelWebsocketMcpServerTransport 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 {};
|
|
@@ -1,213 +0,0 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.BlaxelWebsocketMcpServerTransport = void 0;
|
|
37
|
-
const uuid_1 = require("uuid");
|
|
38
|
-
const ws_1 = __importStar(require("ws"));
|
|
39
|
-
const env_js_1 = require("../../common/env.js");
|
|
40
|
-
const logger_js_1 = require("../../common/logger.js");
|
|
41
|
-
const telemetry_js_1 = require("../../telemetry/telemetry.js");
|
|
42
|
-
const spans = new Map();
|
|
43
|
-
class BlaxelWebsocketMcpServerTransport {
|
|
44
|
-
port;
|
|
45
|
-
wss;
|
|
46
|
-
clients = new Map();
|
|
47
|
-
onclose;
|
|
48
|
-
onerror;
|
|
49
|
-
messageHandler;
|
|
50
|
-
onconnection;
|
|
51
|
-
ondisconnection;
|
|
52
|
-
set onmessage(handler) {
|
|
53
|
-
this.messageHandler = handler
|
|
54
|
-
? (msg, clientId) => {
|
|
55
|
-
if (!("id" in msg)) {
|
|
56
|
-
return handler(msg);
|
|
57
|
-
}
|
|
58
|
-
return handler({
|
|
59
|
-
...msg,
|
|
60
|
-
id: clientId + ":" + msg.id,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
: undefined;
|
|
64
|
-
}
|
|
65
|
-
constructor(port) {
|
|
66
|
-
this.port = port ?? parseInt(env_js_1.env.BL_SERVER_PORT ?? "8080", 10);
|
|
67
|
-
this.wss = new ws_1.WebSocketServer({ port: this.port });
|
|
68
|
-
}
|
|
69
|
-
async start() {
|
|
70
|
-
logger_js_1.logger.info("Starting WebSocket Server on port " + this.port);
|
|
71
|
-
this.wss.on("connection", (ws) => {
|
|
72
|
-
const clientId = (0, uuid_1.v4)();
|
|
73
|
-
this.clients.set(clientId, {
|
|
74
|
-
ws,
|
|
75
|
-
});
|
|
76
|
-
this.onconnection?.(clientId);
|
|
77
|
-
ws.on("message", (data) => {
|
|
78
|
-
const span = (0, telemetry_js_1.startSpan)("message", {
|
|
79
|
-
attributes: {
|
|
80
|
-
"mcp.client.id": clientId,
|
|
81
|
-
"span.type": "mcp.message",
|
|
82
|
-
},
|
|
83
|
-
isRoot: false,
|
|
84
|
-
});
|
|
85
|
-
try {
|
|
86
|
-
const msg = JSON.parse(data.toString());
|
|
87
|
-
this.messageHandler?.(msg, clientId);
|
|
88
|
-
if ("method" in msg && "id" in msg && "params" in msg) {
|
|
89
|
-
span.setAttributes({
|
|
90
|
-
"mcp.message.parsed": true,
|
|
91
|
-
"mcp.method": msg.method,
|
|
92
|
-
"mcp.messageId": msg.id,
|
|
93
|
-
"mcp.toolName": msg.params?.name,
|
|
94
|
-
});
|
|
95
|
-
spans.set(clientId + ":" + msg.id, span);
|
|
96
|
-
}
|
|
97
|
-
// Handle msg.id safely
|
|
98
|
-
const msgId = msg.id ? String(msg.id) : "";
|
|
99
|
-
const [cId, parsedMsgId] = msgId.split(":");
|
|
100
|
-
msg.id = parsedMsgId ? parseInt(parsedMsgId) : undefined;
|
|
101
|
-
// Use optional chaining for safe access
|
|
102
|
-
const client = this.clients.get(cId ?? "");
|
|
103
|
-
if (client?.ws?.readyState === ws_1.default.OPEN) {
|
|
104
|
-
const msgSpan = spans.get(cId + ":" + (msg.id ?? ""));
|
|
105
|
-
try {
|
|
106
|
-
client.ws.send(JSON.stringify(msg));
|
|
107
|
-
if (msgSpan) {
|
|
108
|
-
msgSpan.setAttributes({
|
|
109
|
-
"mcp.message.response_sent": true,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
catch (err) {
|
|
114
|
-
if (msgSpan) {
|
|
115
|
-
msgSpan.setStatus("error"); // Error status
|
|
116
|
-
msgSpan.recordException(err);
|
|
117
|
-
}
|
|
118
|
-
throw err;
|
|
119
|
-
}
|
|
120
|
-
finally {
|
|
121
|
-
if (msgSpan) {
|
|
122
|
-
msgSpan.end();
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
this.clients.delete(cId);
|
|
128
|
-
this.ondisconnection?.(cId);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
catch (err) {
|
|
132
|
-
if (err instanceof Error) {
|
|
133
|
-
span.setStatus("error"); // Error status
|
|
134
|
-
span.recordException(err);
|
|
135
|
-
this.onerror?.(err);
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
this.onerror?.(new Error(`Failed to parse message: ${String(err)}`));
|
|
139
|
-
}
|
|
140
|
-
span.end();
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
ws.on("close", () => {
|
|
144
|
-
this.clients.delete(clientId);
|
|
145
|
-
this.ondisconnection?.(clientId);
|
|
146
|
-
});
|
|
147
|
-
ws.on("error", (err) => {
|
|
148
|
-
this.onerror?.(err);
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
return Promise.resolve();
|
|
152
|
-
}
|
|
153
|
-
async send(msg) {
|
|
154
|
-
const [cId, msgId] = msg.id ? String(msg.id).split(":") : [];
|
|
155
|
-
msg.id = parseInt(msgId);
|
|
156
|
-
const data = JSON.stringify(msg);
|
|
157
|
-
const deadClients = [];
|
|
158
|
-
if (cId) {
|
|
159
|
-
// Send to specific client
|
|
160
|
-
const client = this.clients.get(cId);
|
|
161
|
-
if (client?.ws?.readyState === ws_1.default.OPEN) {
|
|
162
|
-
const msgSpan = spans.get(cId + ":" + msg.id);
|
|
163
|
-
try {
|
|
164
|
-
client.ws.send(data);
|
|
165
|
-
if (msgSpan) {
|
|
166
|
-
msgSpan.setAttributes({
|
|
167
|
-
"mcp.message.response_sent": true,
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
catch (err) {
|
|
172
|
-
if (msgSpan) {
|
|
173
|
-
msgSpan.setStatus("error"); // Error status
|
|
174
|
-
msgSpan.recordException(err);
|
|
175
|
-
}
|
|
176
|
-
throw err;
|
|
177
|
-
}
|
|
178
|
-
finally {
|
|
179
|
-
if (msgSpan) {
|
|
180
|
-
msgSpan.end();
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
this.clients.delete(cId);
|
|
186
|
-
this.ondisconnection?.(cId);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
for (const [id, client] of this.clients.entries()) {
|
|
190
|
-
if (client.ws.readyState !== ws_1.default.OPEN) {
|
|
191
|
-
deadClients.push(id);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
// Cleanup dead clients
|
|
195
|
-
deadClients.forEach((id) => {
|
|
196
|
-
this.clients.delete(id);
|
|
197
|
-
this.ondisconnection?.(id);
|
|
198
|
-
});
|
|
199
|
-
return Promise.resolve();
|
|
200
|
-
}
|
|
201
|
-
async broadcast(msg) {
|
|
202
|
-
return this.send(msg);
|
|
203
|
-
}
|
|
204
|
-
async close() {
|
|
205
|
-
return new Promise((resolve) => {
|
|
206
|
-
this.wss.close(() => {
|
|
207
|
-
this.clients.clear();
|
|
208
|
-
resolve();
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
exports.BlaxelWebsocketMcpServerTransport = BlaxelWebsocketMcpServerTransport;
|