@eko-ai/eko-nodejs 2.2.1 → 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.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Log, BaseBrowserLabelsAgent, BaseFileAgent } from '@eko-ai/eko';
1
+ import { Log, BaseBrowserLabelsAgent, BaseFileAgent, uuidv4 } from '@eko-ai/eko';
2
2
  import { chromium } from 'playwright';
3
3
  import * as fs from 'fs/promises';
4
4
  import * as path$1 from 'path';
@@ -10,6 +10,7 @@ import { realpath, readlink, readdir, lstat } from 'node:fs/promises';
10
10
  import { EventEmitter } from 'node:events';
11
11
  import Stream from 'node:stream';
12
12
  import { StringDecoder } from 'node:string_decoder';
13
+ import { spawn } from 'child_process';
13
14
 
14
15
  async function getCdpWsEndpoint(port) {
15
16
  // Example => ws://localhost:9222/devtools/browser/{session-id}
@@ -8227,5 +8228,106 @@ class FileAgent extends BaseFileAgent {
8227
8228
  }
8228
8229
  }
8229
8230
 
8230
- export { BrowserAgent, FileAgent, getCdpWsEndpoint };
8231
+ class SimpleStdioMcpClient {
8232
+ constructor(command, args, options) {
8233
+ this.process = null;
8234
+ this.command = command;
8235
+ this.args = args || [];
8236
+ this.options = options || {
8237
+ stdio: ["pipe", "pipe", "pipe"],
8238
+ };
8239
+ this.requestMap = new Map();
8240
+ }
8241
+ async connect(signal) {
8242
+ if (this.process) {
8243
+ try {
8244
+ this.process.kill();
8245
+ }
8246
+ catch (e) { }
8247
+ }
8248
+ this.process = spawn(this.command, this.args, this.options);
8249
+ this.process.stdout.on("data", (data) => {
8250
+ const response = data.toString().trim();
8251
+ Log.debug("MCP Client, onmessage", this.command, this.args, response);
8252
+ if (!response.startsWith("{")) {
8253
+ return;
8254
+ }
8255
+ const message = JSON.parse(response);
8256
+ if (message.id) {
8257
+ const callback = this.requestMap.get(message.id);
8258
+ if (callback) {
8259
+ callback(message);
8260
+ }
8261
+ }
8262
+ });
8263
+ this.process.on("error", (error) => {
8264
+ Log.error("MCP process error:", this.command, this.args, error);
8265
+ });
8266
+ Log.info("MCP Client, connection successful:", this.command, this.args);
8267
+ }
8268
+ async sendMessage(method, params = {}, signal) {
8269
+ if (!this.process) {
8270
+ await this.connect();
8271
+ if (!this.process) {
8272
+ throw new Error("Failed to connect to MCP server");
8273
+ }
8274
+ }
8275
+ const id = uuidv4();
8276
+ try {
8277
+ const callback = new Promise((resolve, reject) => {
8278
+ if (signal) {
8279
+ signal.addEventListener("abort", () => {
8280
+ reject(new Error("AbortError"));
8281
+ });
8282
+ }
8283
+ this.requestMap.set(id, resolve);
8284
+ });
8285
+ const message = JSON.stringify({
8286
+ jsonrpc: "2.0",
8287
+ id: id,
8288
+ method: method,
8289
+ params: {
8290
+ ...params,
8291
+ },
8292
+ });
8293
+ Log.debug(`MCP Client, ${method}`, id, params);
8294
+ const suc = this.process.stdin.write(message + "\n", "utf-8");
8295
+ if (!suc) {
8296
+ throw new Error("SseClient Response Exception: " + message);
8297
+ }
8298
+ return await callback;
8299
+ }
8300
+ finally {
8301
+ this.requestMap.delete(id);
8302
+ }
8303
+ }
8304
+ async listTools(param, signal) {
8305
+ const message = await this.sendMessage("tools/list", {
8306
+ ...param,
8307
+ }, signal);
8308
+ if (message.error) {
8309
+ Log.error("McpClient listTools error: ", param, message);
8310
+ throw new Error("listTools Exception");
8311
+ }
8312
+ return message.result.tools || [];
8313
+ }
8314
+ async callTool(param, signal) {
8315
+ const message = await this.sendMessage("tools/call", {
8316
+ ...param,
8317
+ }, signal);
8318
+ if (message.error) {
8319
+ Log.error("McpClient callTool error: ", param, message);
8320
+ throw new Error("callTool Exception");
8321
+ }
8322
+ return message.result;
8323
+ }
8324
+ isConnected() {
8325
+ return (this.process != null && !this.process.killed && !this.process.exitCode);
8326
+ }
8327
+ async close() {
8328
+ this.process && this.process.kill();
8329
+ }
8330
+ }
8331
+
8332
+ export { BrowserAgent, FileAgent, SimpleStdioMcpClient, getCdpWsEndpoint };
8231
8333
  //# sourceMappingURL=index.esm.js.map