ai 4.1.61 → 4.1.62

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.mjs CHANGED
@@ -1,11 +1,4 @@
1
1
  var __defProp = Object.defineProperty;
2
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
3
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
4
- }) : x)(function(x) {
5
- if (typeof require !== "undefined")
6
- return require.apply(this, arguments);
7
- throw Error('Dynamic require of "' + x + '" is not supported');
8
- });
9
2
  var __export = (target, all) => {
10
3
  for (var name17 in all)
11
4
  __defProp(target, name17, { get: all[name17], enumerable: true });
@@ -6631,79 +6624,11 @@ function tool(tool2) {
6631
6624
  return tool2;
6632
6625
  }
6633
6626
 
6634
- // core/tool/mcp/create-child-process.ts
6635
- async function createChildProcess(config, signal) {
6636
- var _a17, _b, _c;
6637
- const runtime = detectRuntime();
6638
- if (runtime !== "node") {
6639
- throw new MCPClientError({
6640
- message: "Attempted to use child_process module outside of Node.js environment"
6641
- });
6642
- }
6643
- let childProcess;
6644
- const nodePrefix = "node:";
6645
- try {
6646
- childProcess = await import(`${nodePrefix}child_process`);
6647
- } catch (error) {
6648
- try {
6649
- childProcess = __require(`${nodePrefix}child_process`);
6650
- } catch (innerError) {
6651
- throw new MCPClientError({
6652
- message: "Failed to load child_process module dynamically",
6653
- cause: innerError
6654
- });
6655
- }
6656
- }
6657
- const { spawn } = childProcess;
6658
- return spawn(config.command, (_a17 = config.args) != null ? _a17 : [], {
6659
- env: (_b = config.env) != null ? _b : getDefaultEnvironment(),
6660
- stdio: ["pipe", "pipe", (_c = config.stderr) != null ? _c : "inherit"],
6661
- shell: false,
6662
- signal,
6663
- windowsHide: globalThis.process.platform === "win32" && isElectron(),
6664
- cwd: config.cwd
6665
- });
6666
- }
6667
- function detectRuntime() {
6668
- var _a17, _b;
6669
- if (typeof window !== "undefined") {
6670
- return "browser";
6671
- }
6672
- if (((_b = (_a17 = globalThis.process) == null ? void 0 : _a17.release) == null ? void 0 : _b.name) === "node") {
6673
- return "node";
6674
- }
6675
- return null;
6676
- }
6677
- function getDefaultEnvironment() {
6678
- const DEFAULT_INHERITED_ENV_VARS = globalThis.process.platform === "win32" ? [
6679
- "APPDATA",
6680
- "HOMEDRIVE",
6681
- "HOMEPATH",
6682
- "LOCALAPPDATA",
6683
- "PATH",
6684
- "PROCESSOR_ARCHITECTURE",
6685
- "SYSTEMDRIVE",
6686
- "SYSTEMROOT",
6687
- "TEMP",
6688
- "USERNAME",
6689
- "USERPROFILE"
6690
- ] : ["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"];
6691
- const env = {};
6692
- for (const key of DEFAULT_INHERITED_ENV_VARS) {
6693
- const value = globalThis.process.env[key];
6694
- if (value === void 0) {
6695
- continue;
6696
- }
6697
- if (value.startsWith("()")) {
6698
- continue;
6699
- }
6700
- env[key] = value;
6701
- }
6702
- return env;
6703
- }
6704
- function isElectron() {
6705
- return "type" in globalThis.process;
6706
- }
6627
+ // core/tool/mcp/mcp-sse-transport.ts
6628
+ import { EventSourceParserStream } from "eventsource-parser/stream";
6629
+
6630
+ // core/tool/mcp/json-rpc-message.ts
6631
+ import { z as z9 } from "zod";
6707
6632
 
6708
6633
  // core/tool/mcp/types.ts
6709
6634
  import { z as z8 } from "zod";
@@ -6712,7 +6637,6 @@ var SUPPORTED_PROTOCOL_VERSIONS = [
6712
6637
  LATEST_PROTOCOL_VERSION,
6713
6638
  "2024-10-07"
6714
6639
  ];
6715
- var JSONRPC_VERSION = "2.0";
6716
6640
  var ClientOrServerImplementationSchema = z8.object({
6717
6641
  name: z8.string(),
6718
6642
  version: z8.string()
@@ -6720,43 +6644,11 @@ var ClientOrServerImplementationSchema = z8.object({
6720
6644
  var BaseParamsSchema = z8.object({
6721
6645
  _meta: z8.optional(z8.object({}).passthrough())
6722
6646
  }).passthrough();
6723
- var RequestSchema = z8.object({
6724
- method: z8.string(),
6725
- params: z8.optional(BaseParamsSchema)
6726
- });
6727
6647
  var ResultSchema = BaseParamsSchema;
6728
- var NotificationSchema = z8.object({
6648
+ var RequestSchema = z8.object({
6729
6649
  method: z8.string(),
6730
6650
  params: z8.optional(BaseParamsSchema)
6731
6651
  });
6732
- var RequestIdSchema = z8.union([z8.string(), z8.number().int()]);
6733
- var JSONRPCRequestSchema = z8.object({
6734
- jsonrpc: z8.literal(JSONRPC_VERSION),
6735
- id: RequestIdSchema
6736
- }).merge(RequestSchema).strict();
6737
- var JSONRPCResponseSchema = z8.object({
6738
- jsonrpc: z8.literal(JSONRPC_VERSION),
6739
- id: RequestIdSchema,
6740
- result: ResultSchema
6741
- }).strict();
6742
- var JSONRPCErrorSchema = z8.object({
6743
- jsonrpc: z8.literal(JSONRPC_VERSION),
6744
- id: RequestIdSchema,
6745
- error: z8.object({
6746
- code: z8.number().int(),
6747
- message: z8.string(),
6748
- data: z8.optional(z8.unknown())
6749
- })
6750
- }).strict();
6751
- var JSONRPCNotificationSchema = z8.object({
6752
- jsonrpc: z8.literal(JSONRPC_VERSION)
6753
- }).merge(NotificationSchema).strict();
6754
- var JSONRPCMessageSchema = z8.union([
6755
- JSONRPCRequestSchema,
6756
- JSONRPCNotificationSchema,
6757
- JSONRPCResponseSchema,
6758
- JSONRPCErrorSchema
6759
- ]);
6760
6652
  var ServerCapabilitiesSchema = z8.object({
6761
6653
  experimental: z8.optional(z8.object({}).passthrough()),
6762
6654
  logging: z8.optional(z8.object({}).passthrough()),
@@ -6837,127 +6729,43 @@ var CallToolResultSchema = ResultSchema.extend({
6837
6729
  })
6838
6730
  );
6839
6731
 
6840
- // core/tool/mcp/mcp-stdio-transport.ts
6841
- var StdioClientTransport = class {
6842
- constructor(server) {
6843
- this.abortController = new AbortController();
6844
- this.readBuffer = new ReadBuffer();
6845
- this.serverParams = server;
6846
- }
6847
- async start() {
6848
- if (this.process) {
6849
- throw new MCPClientError({
6850
- message: "StdioClientTransport already started."
6851
- });
6852
- }
6853
- return new Promise(async (resolve, reject) => {
6854
- var _a17, _b, _c, _d;
6855
- try {
6856
- const process2 = await createChildProcess(
6857
- this.serverParams,
6858
- this.abortController.signal
6859
- );
6860
- this.process = process2;
6861
- this.process.on("error", (error) => {
6862
- var _a18, _b2;
6863
- if (error.name === "AbortError") {
6864
- (_a18 = this.onClose) == null ? void 0 : _a18.call(this);
6865
- return;
6866
- }
6867
- reject(error);
6868
- (_b2 = this.onError) == null ? void 0 : _b2.call(this, error);
6869
- });
6870
- this.process.on("spawn", () => {
6871
- resolve();
6872
- });
6873
- this.process.on("close", (_code) => {
6874
- var _a18;
6875
- this.process = void 0;
6876
- (_a18 = this.onClose) == null ? void 0 : _a18.call(this);
6877
- });
6878
- (_a17 = this.process.stdin) == null ? void 0 : _a17.on("error", (error) => {
6879
- var _a18;
6880
- (_a18 = this.onError) == null ? void 0 : _a18.call(this, error);
6881
- });
6882
- (_b = this.process.stdout) == null ? void 0 : _b.on("data", (chunk) => {
6883
- this.readBuffer.append(chunk);
6884
- this.processReadBuffer();
6885
- });
6886
- (_c = this.process.stdout) == null ? void 0 : _c.on("error", (error) => {
6887
- var _a18;
6888
- (_a18 = this.onError) == null ? void 0 : _a18.call(this, error);
6889
- });
6890
- } catch (error) {
6891
- reject(error);
6892
- (_d = this.onError) == null ? void 0 : _d.call(this, error);
6893
- }
6894
- });
6895
- }
6896
- processReadBuffer() {
6897
- var _a17, _b;
6898
- while (true) {
6899
- try {
6900
- const message = this.readBuffer.readMessage();
6901
- if (message === null) {
6902
- break;
6903
- }
6904
- (_a17 = this.onMessage) == null ? void 0 : _a17.call(this, message);
6905
- } catch (error) {
6906
- (_b = this.onError) == null ? void 0 : _b.call(this, error);
6907
- }
6908
- }
6909
- }
6910
- async close() {
6911
- this.abortController.abort();
6912
- this.process = void 0;
6913
- this.readBuffer.clear();
6914
- }
6915
- send(message) {
6916
- return new Promise((resolve) => {
6917
- var _a17;
6918
- if (!((_a17 = this.process) == null ? void 0 : _a17.stdin)) {
6919
- throw new MCPClientError({
6920
- message: "StdioClientTransport not connected"
6921
- });
6922
- }
6923
- const json = serializeMessage(message);
6924
- if (this.process.stdin.write(json)) {
6925
- resolve();
6926
- } else {
6927
- this.process.stdin.once("drain", resolve);
6928
- }
6929
- });
6930
- }
6931
- };
6932
- var ReadBuffer = class {
6933
- append(chunk) {
6934
- this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk]) : chunk;
6935
- }
6936
- readMessage() {
6937
- if (!this.buffer)
6938
- return null;
6939
- const index = this.buffer.indexOf("\n");
6940
- if (index === -1) {
6941
- return null;
6942
- }
6943
- const line = this.buffer.toString("utf8", 0, index);
6944
- this.buffer = this.buffer.subarray(index + 1);
6945
- return deserializeMessage(line);
6946
- }
6947
- clear() {
6948
- this.buffer = void 0;
6949
- }
6950
- };
6951
- function serializeMessage(message) {
6952
- return JSON.stringify(message) + "\n";
6953
- }
6954
- function deserializeMessage(line) {
6955
- return JSONRPCMessageSchema.parse(JSON.parse(line));
6956
- }
6732
+ // core/tool/mcp/json-rpc-message.ts
6733
+ var JSONRPC_VERSION = "2.0";
6734
+ var JSONRPCRequestSchema = z9.object({
6735
+ jsonrpc: z9.literal(JSONRPC_VERSION),
6736
+ id: z9.union([z9.string(), z9.number().int()])
6737
+ }).merge(RequestSchema).strict();
6738
+ var JSONRPCResponseSchema = z9.object({
6739
+ jsonrpc: z9.literal(JSONRPC_VERSION),
6740
+ id: z9.union([z9.string(), z9.number().int()]),
6741
+ result: ResultSchema
6742
+ }).strict();
6743
+ var JSONRPCErrorSchema = z9.object({
6744
+ jsonrpc: z9.literal(JSONRPC_VERSION),
6745
+ id: z9.union([z9.string(), z9.number().int()]),
6746
+ error: z9.object({
6747
+ code: z9.number().int(),
6748
+ message: z9.string(),
6749
+ data: z9.optional(z9.unknown())
6750
+ })
6751
+ }).strict();
6752
+ var JSONRPCNotificationSchema = z9.object({
6753
+ jsonrpc: z9.literal(JSONRPC_VERSION)
6754
+ }).merge(
6755
+ z9.object({
6756
+ method: z9.string(),
6757
+ params: z9.optional(BaseParamsSchema)
6758
+ })
6759
+ ).strict();
6760
+ var JSONRPCMessageSchema = z9.union([
6761
+ JSONRPCRequestSchema,
6762
+ JSONRPCNotificationSchema,
6763
+ JSONRPCResponseSchema,
6764
+ JSONRPCErrorSchema
6765
+ ]);
6957
6766
 
6958
6767
  // core/tool/mcp/mcp-sse-transport.ts
6959
- import { EventSourceParserStream } from "eventsource-parser/stream";
6960
- var SSEClientTransport = class {
6768
+ var SseMCPTransport = class {
6961
6769
  constructor({ url }) {
6962
6770
  this.connected = false;
6963
6771
  this.url = new URL(url);
@@ -6981,7 +6789,7 @@ var SSEClientTransport = class {
6981
6789
  const error = new MCPClientError({
6982
6790
  message: `MCP SSE Transport Error: ${response.status} ${response.statusText}`
6983
6791
  });
6984
- (_b = this.onError) == null ? void 0 : _b.call(this, error);
6792
+ (_b = this.onerror) == null ? void 0 : _b.call(this, error);
6985
6793
  return reject(error);
6986
6794
  }
6987
6795
  const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream());
@@ -7015,13 +6823,13 @@ var SSEClientTransport = class {
7015
6823
  const message = JSONRPCMessageSchema.parse(
7016
6824
  JSON.parse(data)
7017
6825
  );
7018
- (_a18 = this.onMessage) == null ? void 0 : _a18.call(this, message);
6826
+ (_a18 = this.onmessage) == null ? void 0 : _a18.call(this, message);
7019
6827
  } catch (error) {
7020
6828
  const e = new MCPClientError({
7021
6829
  message: "MCP SSE Transport Error: Failed to parse message",
7022
6830
  cause: error
7023
6831
  });
7024
- (_b2 = this.onError) == null ? void 0 : _b2.call(this, e);
6832
+ (_b2 = this.onerror) == null ? void 0 : _b2.call(this, e);
7025
6833
  }
7026
6834
  }
7027
6835
  }
@@ -7029,7 +6837,7 @@ var SSEClientTransport = class {
7029
6837
  if (error instanceof Error && error.name === "AbortError") {
7030
6838
  return;
7031
6839
  }
7032
- (_c2 = this.onError) == null ? void 0 : _c2.call(this, error);
6840
+ (_c2 = this.onerror) == null ? void 0 : _c2.call(this, error);
7033
6841
  reject(error);
7034
6842
  }
7035
6843
  };
@@ -7041,7 +6849,7 @@ var SSEClientTransport = class {
7041
6849
  if (error instanceof Error && error.name === "AbortError") {
7042
6850
  return;
7043
6851
  }
7044
- (_c = this.onError) == null ? void 0 : _c.call(this, error);
6852
+ (_c = this.onerror) == null ? void 0 : _c.call(this, error);
7045
6853
  reject(error);
7046
6854
  }
7047
6855
  };
@@ -7053,7 +6861,7 @@ var SSEClientTransport = class {
7053
6861
  this.connected = false;
7054
6862
  (_a17 = this.sseConnection) == null ? void 0 : _a17.close();
7055
6863
  (_b = this.abortController) == null ? void 0 : _b.abort();
7056
- (_c = this.onClose) == null ? void 0 : _c.call(this);
6864
+ (_c = this.onclose) == null ? void 0 : _c.call(this);
7057
6865
  }
7058
6866
  async send(message) {
7059
6867
  var _a17, _b, _c;
@@ -7077,11 +6885,11 @@ var SSEClientTransport = class {
7077
6885
  const error = new MCPClientError({
7078
6886
  message: `MCP SSE Transport Error: POSTing to endpoint (HTTP ${response.status}): ${text2}`
7079
6887
  });
7080
- (_b = this.onError) == null ? void 0 : _b.call(this, error);
6888
+ (_b = this.onerror) == null ? void 0 : _b.call(this, error);
7081
6889
  return;
7082
6890
  }
7083
6891
  } catch (error) {
7084
- (_c = this.onError) == null ? void 0 : _c.call(this, error);
6892
+ (_c = this.onerror) == null ? void 0 : _c.call(this, error);
7085
6893
  return;
7086
6894
  }
7087
6895
  }
@@ -7089,7 +6897,15 @@ var SSEClientTransport = class {
7089
6897
 
7090
6898
  // core/tool/mcp/mcp-transport.ts
7091
6899
  function createMcpTransport(config) {
7092
- return config.type === "stdio" ? new StdioClientTransport(config) : new SSEClientTransport(config);
6900
+ if (config.type !== "sse") {
6901
+ throw new MCPClientError({
6902
+ message: "Unsupported or invalid transport configuration. If you are using a custom transport, make sure it implements the MCPTransport interface."
6903
+ });
6904
+ }
6905
+ return new SseMCPTransport(config);
6906
+ }
6907
+ function isCustomMcpTransport(transport) {
6908
+ return "start" in transport && typeof transport.start === "function" && "send" in transport && typeof transport.send === "function" && "close" in transport && typeof transport.close === "function";
7093
6909
  }
7094
6910
 
7095
6911
  // core/tool/mcp/mcp-client.ts
@@ -7110,10 +6926,14 @@ var MCPClient = class {
7110
6926
  this.serverCapabilities = {};
7111
6927
  this.isClosed = true;
7112
6928
  this.onUncaughtError = onUncaughtError;
7113
- this.transport = createMcpTransport(transportConfig);
7114
- this.transport.onClose = () => this.onClose();
7115
- this.transport.onError = (error) => this.onError(error);
7116
- this.transport.onMessage = (message) => {
6929
+ if (isCustomMcpTransport(transportConfig)) {
6930
+ this.transport = transportConfig;
6931
+ } else {
6932
+ this.transport = createMcpTransport(transportConfig);
6933
+ }
6934
+ this.transport.onclose = () => this.onClose();
6935
+ this.transport.onerror = (error) => this.onError(error);
6936
+ this.transport.onmessage = (message) => {
7117
6937
  if ("method" in message) {
7118
6938
  this.onError(
7119
6939
  new MCPClientError({