@eko-ai/eko-nodejs 2.2.0 → 3.0.0-alpha.1
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/index.cjs.js +103 -0
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +104 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/src/browser.d.ts +48 -0
- package/dist/src/browser.d.ts.map +1 -0
- package/dist/src/file.d.ts +23 -0
- package/dist/src/file.d.ts.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/mcp/stdio.d.ts +17 -0
- package/dist/src/mcp/stdio.d.ts.map +1 -0
- package/dist/src/utils.d.ts +2 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/test/mcp.test.d.ts +2 -0
- package/dist/test/mcp.test.d.ts.map +1 -0
- package/package.json +7 -3
package/dist/index.cjs.js
CHANGED
|
@@ -12,6 +12,7 @@ var promises = require('node:fs/promises');
|
|
|
12
12
|
var node_events = require('node:events');
|
|
13
13
|
var Stream = require('node:stream');
|
|
14
14
|
var node_string_decoder = require('node:string_decoder');
|
|
15
|
+
var child_process = require('child_process');
|
|
15
16
|
|
|
16
17
|
function _interopNamespaceDefault(e) {
|
|
17
18
|
var n = Object.create(null);
|
|
@@ -8250,7 +8251,109 @@ class FileAgent extends eko.BaseFileAgent {
|
|
|
8250
8251
|
}
|
|
8251
8252
|
}
|
|
8252
8253
|
|
|
8254
|
+
class SimpleStdioMcpClient {
|
|
8255
|
+
constructor(command, args, options) {
|
|
8256
|
+
this.process = null;
|
|
8257
|
+
this.command = command;
|
|
8258
|
+
this.args = args || [];
|
|
8259
|
+
this.options = options || {
|
|
8260
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
8261
|
+
};
|
|
8262
|
+
this.requestMap = new Map();
|
|
8263
|
+
}
|
|
8264
|
+
async connect(signal) {
|
|
8265
|
+
if (this.process) {
|
|
8266
|
+
try {
|
|
8267
|
+
this.process.kill();
|
|
8268
|
+
}
|
|
8269
|
+
catch (e) { }
|
|
8270
|
+
}
|
|
8271
|
+
this.process = child_process.spawn(this.command, this.args, this.options);
|
|
8272
|
+
this.process.stdout.on("data", (data) => {
|
|
8273
|
+
const response = data.toString().trim();
|
|
8274
|
+
eko.Log.debug("MCP Client, onmessage", this.command, this.args, response);
|
|
8275
|
+
if (!response.startsWith("{")) {
|
|
8276
|
+
return;
|
|
8277
|
+
}
|
|
8278
|
+
const message = JSON.parse(response);
|
|
8279
|
+
if (message.id) {
|
|
8280
|
+
const callback = this.requestMap.get(message.id);
|
|
8281
|
+
if (callback) {
|
|
8282
|
+
callback(message);
|
|
8283
|
+
}
|
|
8284
|
+
}
|
|
8285
|
+
});
|
|
8286
|
+
this.process.on("error", (error) => {
|
|
8287
|
+
eko.Log.error("MCP process error:", this.command, this.args, error);
|
|
8288
|
+
});
|
|
8289
|
+
eko.Log.info("MCP Client, connection successful:", this.command, this.args);
|
|
8290
|
+
}
|
|
8291
|
+
async sendMessage(method, params = {}, signal) {
|
|
8292
|
+
if (!this.process) {
|
|
8293
|
+
await this.connect();
|
|
8294
|
+
if (!this.process) {
|
|
8295
|
+
throw new Error("Failed to connect to MCP server");
|
|
8296
|
+
}
|
|
8297
|
+
}
|
|
8298
|
+
const id = eko.uuidv4();
|
|
8299
|
+
try {
|
|
8300
|
+
const callback = new Promise((resolve, reject) => {
|
|
8301
|
+
if (signal) {
|
|
8302
|
+
signal.addEventListener("abort", () => {
|
|
8303
|
+
reject(new Error("AbortError"));
|
|
8304
|
+
});
|
|
8305
|
+
}
|
|
8306
|
+
this.requestMap.set(id, resolve);
|
|
8307
|
+
});
|
|
8308
|
+
const message = JSON.stringify({
|
|
8309
|
+
jsonrpc: "2.0",
|
|
8310
|
+
id: id,
|
|
8311
|
+
method: method,
|
|
8312
|
+
params: {
|
|
8313
|
+
...params,
|
|
8314
|
+
},
|
|
8315
|
+
});
|
|
8316
|
+
eko.Log.debug(`MCP Client, ${method}`, id, params);
|
|
8317
|
+
const suc = this.process.stdin.write(message + "\n", "utf-8");
|
|
8318
|
+
if (!suc) {
|
|
8319
|
+
throw new Error("SseClient Response Exception: " + message);
|
|
8320
|
+
}
|
|
8321
|
+
return await callback;
|
|
8322
|
+
}
|
|
8323
|
+
finally {
|
|
8324
|
+
this.requestMap.delete(id);
|
|
8325
|
+
}
|
|
8326
|
+
}
|
|
8327
|
+
async listTools(param, signal) {
|
|
8328
|
+
const message = await this.sendMessage("tools/list", {
|
|
8329
|
+
...param,
|
|
8330
|
+
}, signal);
|
|
8331
|
+
if (message.error) {
|
|
8332
|
+
eko.Log.error("McpClient listTools error: ", param, message);
|
|
8333
|
+
throw new Error("listTools Exception");
|
|
8334
|
+
}
|
|
8335
|
+
return message.result.tools || [];
|
|
8336
|
+
}
|
|
8337
|
+
async callTool(param, signal) {
|
|
8338
|
+
const message = await this.sendMessage("tools/call", {
|
|
8339
|
+
...param,
|
|
8340
|
+
}, signal);
|
|
8341
|
+
if (message.error) {
|
|
8342
|
+
eko.Log.error("McpClient callTool error: ", param, message);
|
|
8343
|
+
throw new Error("callTool Exception");
|
|
8344
|
+
}
|
|
8345
|
+
return message.result;
|
|
8346
|
+
}
|
|
8347
|
+
isConnected() {
|
|
8348
|
+
return (this.process != null && !this.process.killed && !this.process.exitCode);
|
|
8349
|
+
}
|
|
8350
|
+
async close() {
|
|
8351
|
+
this.process && this.process.kill();
|
|
8352
|
+
}
|
|
8353
|
+
}
|
|
8354
|
+
|
|
8253
8355
|
exports.BrowserAgent = BrowserAgent;
|
|
8254
8356
|
exports.FileAgent = FileAgent;
|
|
8357
|
+
exports.SimpleStdioMcpClient = SimpleStdioMcpClient;
|
|
8255
8358
|
exports.getCdpWsEndpoint = getCdpWsEndpoint;
|
|
8256
8359
|
//# sourceMappingURL=index.cjs.js.map
|