@ax-llm/ax 11.0.25 → 11.0.26

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/index.cjs CHANGED
@@ -8625,6 +8625,53 @@ var AxEmbeddingAdapter = class {
8625
8625
  }
8626
8626
  };
8627
8627
 
8628
+ // ../../node_modules/uuid/dist/esm-node/rng.js
8629
+ var import_crypto2 = __toESM(require("crypto"));
8630
+ var rnds8Pool = new Uint8Array(256);
8631
+ var poolPtr = rnds8Pool.length;
8632
+ function rng() {
8633
+ if (poolPtr > rnds8Pool.length - 16) {
8634
+ import_crypto2.default.randomFillSync(rnds8Pool);
8635
+ poolPtr = 0;
8636
+ }
8637
+ return rnds8Pool.slice(poolPtr, poolPtr += 16);
8638
+ }
8639
+
8640
+ // ../../node_modules/uuid/dist/esm-node/stringify.js
8641
+ var byteToHex = [];
8642
+ for (let i = 0; i < 256; ++i) {
8643
+ byteToHex.push((i + 256).toString(16).slice(1));
8644
+ }
8645
+ function unsafeStringify(arr, offset = 0) {
8646
+ return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
8647
+ }
8648
+
8649
+ // ../../node_modules/uuid/dist/esm-node/native.js
8650
+ var import_crypto3 = __toESM(require("crypto"));
8651
+ var native_default = {
8652
+ randomUUID: import_crypto3.default.randomUUID
8653
+ };
8654
+
8655
+ // ../../node_modules/uuid/dist/esm-node/v4.js
8656
+ function v4(options, buf, offset) {
8657
+ if (native_default.randomUUID && !buf && !options) {
8658
+ return native_default.randomUUID();
8659
+ }
8660
+ options = options || {};
8661
+ const rnds = options.random || (options.rng || rng)();
8662
+ rnds[6] = rnds[6] & 15 | 64;
8663
+ rnds[8] = rnds[8] & 63 | 128;
8664
+ if (buf) {
8665
+ offset = offset || 0;
8666
+ for (let i = 0; i < 16; ++i) {
8667
+ buf[offset + i] = rnds[i];
8668
+ }
8669
+ return buf;
8670
+ }
8671
+ return unsafeStringify(rnds);
8672
+ }
8673
+ var v4_default = v4;
8674
+
8628
8675
  // mcp/client.ts
8629
8676
  var colorLog7 = new ColorLog();
8630
8677
  var AxMCPClient = class {
@@ -8633,13 +8680,13 @@ var AxMCPClient = class {
8633
8680
  this.options = options;
8634
8681
  }
8635
8682
  functions = [];
8636
- requestId = 0;
8683
+ activeRequests = /* @__PURE__ */ new Map();
8637
8684
  capabilities = {};
8638
8685
  async init() {
8639
8686
  if ("connect" in this.transport) {
8640
8687
  await this.transport.connect?.();
8641
8688
  }
8642
- const res = await this.sendRequest("initialize", {
8689
+ const { result: res } = await this.sendRequest("initialize", {
8643
8690
  protocolVersion: "2024-11-05",
8644
8691
  capabilities: {
8645
8692
  roots: { listChanged: true },
@@ -8650,6 +8697,12 @@ var AxMCPClient = class {
8650
8697
  version: "1.0.0"
8651
8698
  }
8652
8699
  });
8700
+ const expectedProtocolVersion = "2024-11-05";
8701
+ if (res.protocolVersion !== expectedProtocolVersion) {
8702
+ throw new Error(
8703
+ `Protocol version mismatch. Expected ${expectedProtocolVersion} but got ${res.protocolVersion}`
8704
+ );
8705
+ }
8653
8706
  if (res.capabilities.tools) {
8654
8707
  this.capabilities.tools = true;
8655
8708
  }
@@ -8659,67 +8712,121 @@ var AxMCPClient = class {
8659
8712
  if (res.capabilities.prompts) {
8660
8713
  this.capabilities.prompts = true;
8661
8714
  }
8662
- await this.sendNotification("initialized");
8715
+ await this.sendNotification("notifications/initialized");
8663
8716
  await this.discoverFunctions();
8664
8717
  }
8665
8718
  async discoverFunctions() {
8666
8719
  if (!this.capabilities.tools) {
8667
8720
  throw new Error("Tools are not supported");
8668
8721
  }
8669
- const res = await this.sendRequest(
8670
- "tools/list"
8671
- );
8672
- this.functions = res.tools.map(
8673
- (fn) => ({
8674
- name: fn.name,
8675
- description: fn.description,
8676
- parameters: fn.inputSchema,
8722
+ const { result: res } = await this.sendRequest("tools/list");
8723
+ this.functions = res.tools.map((fn) => {
8724
+ const override = this.options.functionOverrides?.find(
8725
+ (o) => o.name === fn.name
8726
+ );
8727
+ const parameters = fn.inputSchema.properties ? {
8728
+ properties: fn.inputSchema.properties,
8729
+ required: fn.inputSchema.required ?? [],
8730
+ type: fn.inputSchema.type
8731
+ } : void 0;
8732
+ return {
8733
+ name: override?.updates.name ?? fn.name,
8734
+ description: override?.updates.description ?? fn.description,
8735
+ parameters,
8677
8736
  func: async (args) => {
8678
- const result = await this.sendRequest("tools/call", { name: fn.name, arguments: args });
8737
+ const { result } = await this.sendRequest("tools/call", { name: fn.name, arguments: args });
8679
8738
  return result;
8680
8739
  }
8681
- })
8682
- );
8740
+ };
8741
+ });
8742
+ if (this.options.debug) {
8743
+ console.log(
8744
+ colorLog7.yellow(`> Discovered ${this.functions.length} functions:`)
8745
+ );
8746
+ for (const fn of this.functions) {
8747
+ console.log(colorLog7.yellow(` - ${fn.name}: ${fn.description}`));
8748
+ }
8749
+ }
8683
8750
  }
8684
- async ping() {
8685
- await this.sendRequest("ping");
8751
+ async ping(timeout = 3e3) {
8752
+ const pingPromise = this.sendRequest("ping");
8753
+ const timeoutPromise = new Promise(
8754
+ (_, reject) => setTimeout(
8755
+ () => reject(new Error("Ping response timeout exceeded")),
8756
+ timeout
8757
+ )
8758
+ );
8759
+ const response = await Promise.race([pingPromise, timeoutPromise]);
8760
+ const { result } = response;
8761
+ if (typeof result !== "object" || result === null || Object.keys(result).length !== 0) {
8762
+ throw new Error(`Unexpected ping response: ${JSON.stringify(result)}`);
8763
+ }
8686
8764
  }
8687
8765
  toFunction() {
8688
8766
  return this.functions;
8689
8767
  }
8690
- async sendRequest(method, params) {
8768
+ cancelRequest(id) {
8769
+ if (this.activeRequests.has(id)) {
8770
+ this.sendNotification("notifications/cancelled", {
8771
+ requestId: id,
8772
+ reason: "Client cancelled request"
8773
+ });
8774
+ const entry = this.activeRequests.get(id);
8775
+ if (entry) {
8776
+ entry.reject(new Error(`Request ${id} cancelled`));
8777
+ }
8778
+ this.activeRequests.delete(id);
8779
+ }
8780
+ }
8781
+ async sendRequest(method, params = {}) {
8782
+ const requestId = v4_default();
8691
8783
  const request = {
8692
8784
  jsonrpc: "2.0",
8693
- id: ++this.requestId,
8785
+ id: requestId,
8694
8786
  method,
8695
8787
  params
8696
8788
  };
8697
8789
  if (this.options.debug) {
8698
8790
  console.log(
8699
8791
  colorLog7.blueBright(
8700
- `> Sending request:
8792
+ `> Sending request ${requestId}:
8701
8793
  ${JSON.stringify(request, null, 2)}`
8702
8794
  )
8703
8795
  );
8704
8796
  }
8705
- const res = await this.transport.send(request);
8706
- if (this.options.debug) {
8707
- console.log(
8708
- colorLog7.greenBright(
8709
- `> Received response:
8797
+ const responsePromise = new Promise((resolve, reject) => {
8798
+ this.activeRequests.set(requestId, { reject });
8799
+ this.transport.send(request).then((res) => {
8800
+ this.activeRequests.delete(requestId);
8801
+ if (this.options.debug) {
8802
+ console.log(
8803
+ colorLog7.greenBright(
8804
+ `> Received response for request ${requestId}:
8710
8805
  ${JSON.stringify(res, null, 2)}`
8711
- )
8712
- );
8713
- }
8714
- if ("error" in res) {
8715
- throw new Error(`RPC Error ${res.error.code}: ${res.error.message}`);
8716
- }
8717
- if ("result" in res) {
8718
- return res.result;
8719
- }
8720
- throw new Error("Invalid response no result or error");
8806
+ )
8807
+ );
8808
+ }
8809
+ if (res !== null && typeof res === "object" && "error" in res) {
8810
+ const errorObj = res;
8811
+ reject(
8812
+ new Error(
8813
+ `RPC Error ${errorObj.error.code}: ${errorObj.error.message}`
8814
+ )
8815
+ );
8816
+ } else if (res !== null && typeof res === "object" && "result" in res) {
8817
+ resolve({ result: res.result });
8818
+ } else {
8819
+ reject(new Error("Invalid response no result or error"));
8820
+ }
8821
+ }).catch((err) => {
8822
+ this.activeRequests.delete(requestId);
8823
+ reject(err);
8824
+ });
8825
+ });
8826
+ const { result } = await responsePromise;
8827
+ return { id: requestId, result };
8721
8828
  }
8722
- async sendNotification(method, params) {
8829
+ async sendNotification(method, params = {}) {
8723
8830
  const notification = {
8724
8831
  jsonrpc: "2.0",
8725
8832
  method,