@mcp-fe/mcp-worker 0.0.16 → 0.0.17

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.
@@ -4375,13 +4375,13 @@ var require_core = __commonJS({
4375
4375
  }, warn() {
4376
4376
  }, error() {
4377
4377
  } };
4378
- function getLogger(logger) {
4379
- if (logger === false)
4378
+ function getLogger(logger2) {
4379
+ if (logger2 === false)
4380
4380
  return noLogs;
4381
- if (logger === void 0)
4381
+ if (logger2 === void 0)
4382
4382
  return console;
4383
- if (logger.log && logger.warn && logger.error)
4384
- return logger;
4383
+ if (logger2.log && logger2.warn && logger2.error)
4384
+ return logger2;
4385
4385
  throw new Error("logger must implement log, warn and error methods");
4386
4386
  }
4387
4387
  var KEYWORD_NAME = /^[a-z_$][a-z0-9_$:-]*$/i;
@@ -11017,7 +11017,7 @@ __export(core_exports2, {
11017
11017
  parse: () => parse,
11018
11018
  parseAsync: () => parseAsync,
11019
11019
  prettifyError: () => prettifyError,
11020
- process: () => process,
11020
+ process: () => process2,
11021
11021
  regexes: () => regexes_exports,
11022
11022
  registry: () => registry,
11023
11023
  safeDecode: () => safeDecode,
@@ -21417,7 +21417,7 @@ function initializeContext(params) {
21417
21417
  external: params?.external ?? void 0
21418
21418
  };
21419
21419
  }
21420
- function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
21420
+ function process2(schema, ctx, _params = { path: [], schemaPath: [] }) {
21421
21421
  var _a2;
21422
21422
  const def = schema._zod.def;
21423
21423
  const seen = ctx.seen.get(schema);
@@ -21454,7 +21454,7 @@ function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
21454
21454
  if (parent) {
21455
21455
  if (!result.ref)
21456
21456
  result.ref = parent;
21457
- process(parent, ctx, params);
21457
+ process2(parent, ctx, params);
21458
21458
  ctx.seen.get(parent).isParent = true;
21459
21459
  }
21460
21460
  }
@@ -21735,14 +21735,14 @@ function isTransforming(_schema, _ctx) {
21735
21735
  }
21736
21736
  var createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
21737
21737
  const ctx = initializeContext({ ...params, processors });
21738
- process(schema, ctx);
21738
+ process2(schema, ctx);
21739
21739
  extractDefs(ctx, schema);
21740
21740
  return finalize(ctx, schema);
21741
21741
  };
21742
21742
  var createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
21743
21743
  const { libraryOptions, target } = params ?? {};
21744
21744
  const ctx = initializeContext({ ...libraryOptions ?? {}, target, io, processors });
21745
- process(schema, ctx);
21745
+ process2(schema, ctx);
21746
21746
  extractDefs(ctx, schema);
21747
21747
  return finalize(ctx, schema);
21748
21748
  };
@@ -21999,7 +21999,7 @@ var arrayProcessor = (schema, ctx, _json, params) => {
21999
21999
  if (typeof maximum === "number")
22000
22000
  json2.maxItems = maximum;
22001
22001
  json2.type = "array";
22002
- json2.items = process(def.element, ctx, { ...params, path: [...params.path, "items"] });
22002
+ json2.items = process2(def.element, ctx, { ...params, path: [...params.path, "items"] });
22003
22003
  };
22004
22004
  var objectProcessor = (schema, ctx, _json, params) => {
22005
22005
  const json2 = _json;
@@ -22008,7 +22008,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
22008
22008
  json2.properties = {};
22009
22009
  const shape = def.shape;
22010
22010
  for (const key in shape) {
22011
- json2.properties[key] = process(shape[key], ctx, {
22011
+ json2.properties[key] = process2(shape[key], ctx, {
22012
22012
  ...params,
22013
22013
  path: [...params.path, "properties", key]
22014
22014
  });
@@ -22031,7 +22031,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
22031
22031
  if (ctx.io === "output")
22032
22032
  json2.additionalProperties = false;
22033
22033
  } else if (def.catchall) {
22034
- json2.additionalProperties = process(def.catchall, ctx, {
22034
+ json2.additionalProperties = process2(def.catchall, ctx, {
22035
22035
  ...params,
22036
22036
  path: [...params.path, "additionalProperties"]
22037
22037
  });
@@ -22040,7 +22040,7 @@ var objectProcessor = (schema, ctx, _json, params) => {
22040
22040
  var unionProcessor = (schema, ctx, json2, params) => {
22041
22041
  const def = schema._zod.def;
22042
22042
  const isExclusive = def.inclusive === false;
22043
- const options = def.options.map((x, i) => process(x, ctx, {
22043
+ const options = def.options.map((x, i) => process2(x, ctx, {
22044
22044
  ...params,
22045
22045
  path: [...params.path, isExclusive ? "oneOf" : "anyOf", i]
22046
22046
  }));
@@ -22052,11 +22052,11 @@ var unionProcessor = (schema, ctx, json2, params) => {
22052
22052
  };
22053
22053
  var intersectionProcessor = (schema, ctx, json2, params) => {
22054
22054
  const def = schema._zod.def;
22055
- const a = process(def.left, ctx, {
22055
+ const a = process2(def.left, ctx, {
22056
22056
  ...params,
22057
22057
  path: [...params.path, "allOf", 0]
22058
22058
  });
22059
- const b = process(def.right, ctx, {
22059
+ const b = process2(def.right, ctx, {
22060
22060
  ...params,
22061
22061
  path: [...params.path, "allOf", 1]
22062
22062
  });
@@ -22073,11 +22073,11 @@ var tupleProcessor = (schema, ctx, _json, params) => {
22073
22073
  json2.type = "array";
22074
22074
  const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
22075
22075
  const restPath = ctx.target === "draft-2020-12" ? "items" : ctx.target === "openapi-3.0" ? "items" : "additionalItems";
22076
- const prefixItems = def.items.map((x, i) => process(x, ctx, {
22076
+ const prefixItems = def.items.map((x, i) => process2(x, ctx, {
22077
22077
  ...params,
22078
22078
  path: [...params.path, prefixPath, i]
22079
22079
  }));
22080
- const rest = def.rest ? process(def.rest, ctx, {
22080
+ const rest = def.rest ? process2(def.rest, ctx, {
22081
22081
  ...params,
22082
22082
  path: [...params.path, restPath, ...ctx.target === "openapi-3.0" ? [def.items.length] : []]
22083
22083
  }) : null;
@@ -22117,7 +22117,7 @@ var recordProcessor = (schema, ctx, _json, params) => {
22117
22117
  const keyBag = keyType._zod.bag;
22118
22118
  const patterns = keyBag?.patterns;
22119
22119
  if (def.mode === "loose" && patterns && patterns.size > 0) {
22120
- const valueSchema = process(def.valueType, ctx, {
22120
+ const valueSchema = process2(def.valueType, ctx, {
22121
22121
  ...params,
22122
22122
  path: [...params.path, "patternProperties", "*"]
22123
22123
  });
@@ -22127,12 +22127,12 @@ var recordProcessor = (schema, ctx, _json, params) => {
22127
22127
  }
22128
22128
  } else {
22129
22129
  if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") {
22130
- json2.propertyNames = process(def.keyType, ctx, {
22130
+ json2.propertyNames = process2(def.keyType, ctx, {
22131
22131
  ...params,
22132
22132
  path: [...params.path, "propertyNames"]
22133
22133
  });
22134
22134
  }
22135
- json2.additionalProperties = process(def.valueType, ctx, {
22135
+ json2.additionalProperties = process2(def.valueType, ctx, {
22136
22136
  ...params,
22137
22137
  path: [...params.path, "additionalProperties"]
22138
22138
  });
@@ -22147,7 +22147,7 @@ var recordProcessor = (schema, ctx, _json, params) => {
22147
22147
  };
22148
22148
  var nullableProcessor = (schema, ctx, json2, params) => {
22149
22149
  const def = schema._zod.def;
22150
- const inner = process(def.innerType, ctx, params);
22150
+ const inner = process2(def.innerType, ctx, params);
22151
22151
  const seen = ctx.seen.get(schema);
22152
22152
  if (ctx.target === "openapi-3.0") {
22153
22153
  seen.ref = def.innerType;
@@ -22158,20 +22158,20 @@ var nullableProcessor = (schema, ctx, json2, params) => {
22158
22158
  };
22159
22159
  var nonoptionalProcessor = (schema, ctx, _json, params) => {
22160
22160
  const def = schema._zod.def;
22161
- process(def.innerType, ctx, params);
22161
+ process2(def.innerType, ctx, params);
22162
22162
  const seen = ctx.seen.get(schema);
22163
22163
  seen.ref = def.innerType;
22164
22164
  };
22165
22165
  var defaultProcessor = (schema, ctx, json2, params) => {
22166
22166
  const def = schema._zod.def;
22167
- process(def.innerType, ctx, params);
22167
+ process2(def.innerType, ctx, params);
22168
22168
  const seen = ctx.seen.get(schema);
22169
22169
  seen.ref = def.innerType;
22170
22170
  json2.default = JSON.parse(JSON.stringify(def.defaultValue));
22171
22171
  };
22172
22172
  var prefaultProcessor = (schema, ctx, json2, params) => {
22173
22173
  const def = schema._zod.def;
22174
- process(def.innerType, ctx, params);
22174
+ process2(def.innerType, ctx, params);
22175
22175
  const seen = ctx.seen.get(schema);
22176
22176
  seen.ref = def.innerType;
22177
22177
  if (ctx.io === "input")
@@ -22179,7 +22179,7 @@ var prefaultProcessor = (schema, ctx, json2, params) => {
22179
22179
  };
22180
22180
  var catchProcessor = (schema, ctx, json2, params) => {
22181
22181
  const def = schema._zod.def;
22182
- process(def.innerType, ctx, params);
22182
+ process2(def.innerType, ctx, params);
22183
22183
  const seen = ctx.seen.get(schema);
22184
22184
  seen.ref = def.innerType;
22185
22185
  let catchValue;
@@ -22193,32 +22193,32 @@ var catchProcessor = (schema, ctx, json2, params) => {
22193
22193
  var pipeProcessor = (schema, ctx, _json, params) => {
22194
22194
  const def = schema._zod.def;
22195
22195
  const innerType = ctx.io === "input" ? def.in._zod.def.type === "transform" ? def.out : def.in : def.out;
22196
- process(innerType, ctx, params);
22196
+ process2(innerType, ctx, params);
22197
22197
  const seen = ctx.seen.get(schema);
22198
22198
  seen.ref = innerType;
22199
22199
  };
22200
22200
  var readonlyProcessor = (schema, ctx, json2, params) => {
22201
22201
  const def = schema._zod.def;
22202
- process(def.innerType, ctx, params);
22202
+ process2(def.innerType, ctx, params);
22203
22203
  const seen = ctx.seen.get(schema);
22204
22204
  seen.ref = def.innerType;
22205
22205
  json2.readOnly = true;
22206
22206
  };
22207
22207
  var promiseProcessor = (schema, ctx, _json, params) => {
22208
22208
  const def = schema._zod.def;
22209
- process(def.innerType, ctx, params);
22209
+ process2(def.innerType, ctx, params);
22210
22210
  const seen = ctx.seen.get(schema);
22211
22211
  seen.ref = def.innerType;
22212
22212
  };
22213
22213
  var optionalProcessor = (schema, ctx, _json, params) => {
22214
22214
  const def = schema._zod.def;
22215
- process(def.innerType, ctx, params);
22215
+ process2(def.innerType, ctx, params);
22216
22216
  const seen = ctx.seen.get(schema);
22217
22217
  seen.ref = def.innerType;
22218
22218
  };
22219
22219
  var lazyProcessor = (schema, ctx, _json, params) => {
22220
22220
  const innerType = schema._zod.innerType;
22221
- process(innerType, ctx, params);
22221
+ process2(innerType, ctx, params);
22222
22222
  const seen = ctx.seen.get(schema);
22223
22223
  seen.ref = innerType;
22224
22224
  };
@@ -22270,7 +22270,7 @@ function toJSONSchema(input, params) {
22270
22270
  const defs = {};
22271
22271
  for (const entry of registry2._idmap.entries()) {
22272
22272
  const [_, schema] = entry;
22273
- process(schema, ctx2);
22273
+ process2(schema, ctx2);
22274
22274
  }
22275
22275
  const schemas = {};
22276
22276
  const external = {
@@ -22293,7 +22293,7 @@ function toJSONSchema(input, params) {
22293
22293
  return { schemas };
22294
22294
  }
22295
22295
  const ctx = initializeContext({ ...params, processors: allProcessors });
22296
- process(input, ctx);
22296
+ process2(input, ctx);
22297
22297
  extractDefs(ctx, input);
22298
22298
  return finalize(ctx, input);
22299
22299
  }
@@ -22351,7 +22351,7 @@ var JSONSchemaGenerator = class {
22351
22351
  * This must be called before emit().
22352
22352
  */
22353
22353
  process(schema, _params = { path: [], schemaPath: [] }) {
22354
- return process(schema, this.ctx, _params);
22354
+ return process2(schema, this.ctx, _params);
22355
22355
  }
22356
22356
  /**
22357
22357
  * Emit the final JSON Schema after processing.
@@ -27828,6 +27828,34 @@ var WebSocketTransport = class {
27828
27828
  }
27829
27829
  };
27830
27830
 
27831
+ // libs/mcp-worker/src/lib/logger.ts
27832
+ var isProduction = typeof process !== "undefined" && true;
27833
+ var mcpDebug = typeof process !== "undefined" && process.env?.["MCP_DEBUG"];
27834
+ var debugEnabled = mcpDebug === "true" || !isProduction && mcpDebug !== "false";
27835
+ var logger = {
27836
+ log: (...args) => {
27837
+ if (debugEnabled) {
27838
+ console.log(...args);
27839
+ }
27840
+ },
27841
+ debug: (...args) => {
27842
+ if (debugEnabled) {
27843
+ console.debug(...args);
27844
+ }
27845
+ },
27846
+ info: (...args) => {
27847
+ console.info(...args);
27848
+ },
27849
+ error: (...args) => {
27850
+ console.error(...args);
27851
+ },
27852
+ warn: (...args) => {
27853
+ if (debugEnabled) {
27854
+ console.warn(...args);
27855
+ }
27856
+ }
27857
+ };
27858
+
27831
27859
  // libs/mcp-worker/src/lib/mcp-controller.ts
27832
27860
  var MAX_RECONNECT_DELAY = 3e4;
27833
27861
  var INITIAL_RECONNECT_DELAY = 1e3;
@@ -27843,6 +27871,7 @@ var MCPController = class _MCPController {
27843
27871
  authToken = null;
27844
27872
  keepAliveInterval = null;
27845
27873
  requireAuth;
27874
+ isReconnectingForToken = false;
27846
27875
  startKeepAlive() {
27847
27876
  if (this.keepAliveInterval) {
27848
27877
  clearInterval(this.keepAliveInterval);
@@ -27864,7 +27893,9 @@ var MCPController = class _MCPController {
27864
27893
  }
27865
27894
  async connectWebSocket() {
27866
27895
  if (this.requireAuth && !this.authToken) {
27867
- console.log("[MCPController] Skipping WebSocket connect: auth token not set and requireAuth=true");
27896
+ logger.log(
27897
+ "[MCPController] Skipping WebSocket connect: auth token not set and requireAuth=true"
27898
+ );
27868
27899
  return;
27869
27900
  }
27870
27901
  if (this.socket?.readyState === WebSocket.OPEN || this.socket?.readyState === WebSocket.CONNECTING) {
@@ -27874,7 +27905,7 @@ var MCPController = class _MCPController {
27874
27905
  try {
27875
27906
  await mcpServer.close();
27876
27907
  } catch (error48) {
27877
- console.error("[MCPController] Error closing MCP server:", error48);
27908
+ logger.error("[MCPController] Error closing MCP server:", error48);
27878
27909
  }
27879
27910
  this.transport = null;
27880
27911
  }
@@ -27891,8 +27922,9 @@ var MCPController = class _MCPController {
27891
27922
  const url2 = this.authToken ? `${this.backendUrl}?token=${this.authToken}` : this.backendUrl;
27892
27923
  this.socket = new WebSocket(url2);
27893
27924
  this.socket.onopen = async () => {
27894
- console.log("[MCPController] Connected to backend MCP server");
27925
+ logger.log("[MCPController] Connected to backend MCP server");
27895
27926
  this.reconnectAttempts = 0;
27927
+ this.isReconnectingForToken = false;
27896
27928
  try {
27897
27929
  if (this.socket) {
27898
27930
  this.transport = new WebSocketTransport(this.socket);
@@ -27903,13 +27935,16 @@ var MCPController = class _MCPController {
27903
27935
  }
27904
27936
  }
27905
27937
  await mcpServer.connect(this.transport);
27906
- console.log("[MCPController] MCP Server connected to WebSocket transport");
27938
+ logger.log(
27939
+ "[MCPController] MCP Server connected to WebSocket transport"
27940
+ );
27907
27941
  this.startKeepAlive();
27908
27942
  this.broadcastFn({ type: "CONNECTION_STATUS", connected: true });
27909
27943
  resolve();
27910
27944
  }
27911
27945
  } catch (error48) {
27912
- console.error("[MCPController] Error setting up MCP server:", error48);
27946
+ logger.error("[MCPController] Error setting up MCP server:", error48);
27947
+ this.isReconnectingForToken = false;
27913
27948
  this.broadcastFn({ type: "CONNECTION_STATUS", connected: false });
27914
27949
  if (this.socket) {
27915
27950
  this.socket.close();
@@ -27918,31 +27953,37 @@ var MCPController = class _MCPController {
27918
27953
  }
27919
27954
  };
27920
27955
  this.socket.onclose = async (event) => {
27921
- console.log("[MCPController] Disconnected from backend MCP server", event?.code, event?.reason);
27922
- this.broadcastFn({ type: "CONNECTION_STATUS", connected: false });
27956
+ logger.log(
27957
+ "[MCPController] Disconnected from backend MCP server",
27958
+ event?.code,
27959
+ event?.reason
27960
+ );
27961
+ if (!this.isReconnectingForToken) {
27962
+ this.broadcastFn({ type: "CONNECTION_STATUS", connected: false });
27963
+ }
27923
27964
  if (this.transport) {
27924
27965
  try {
27925
27966
  await mcpServer.close();
27926
27967
  } catch (error48) {
27927
- console.error("[MCPController] Error closing MCP server:", error48);
27968
+ logger.error("[MCPController] Error closing MCP server:", error48);
27928
27969
  }
27929
27970
  this.transport = null;
27930
27971
  }
27931
27972
  this.socket = null;
27932
27973
  this.stopKeepAlive();
27933
- if (event?.code !== 1e3) {
27974
+ if (!this.isReconnectingForToken && event?.code !== 1e3) {
27934
27975
  const delay = Math.min(
27935
27976
  INITIAL_RECONNECT_DELAY * Math.pow(2, this.reconnectAttempts),
27936
27977
  MAX_RECONNECT_DELAY
27937
27978
  );
27938
27979
  this.reconnectAttempts++;
27939
- console.log(`[MCPController] Retrying in ${delay}ms...`);
27980
+ logger.log(`[MCPController] Retrying in ${delay}ms...`);
27940
27981
  setTimeout(() => this.connectWebSocket(), delay);
27941
27982
  }
27942
27983
  resolve();
27943
27984
  };
27944
27985
  this.socket.onerror = (event) => {
27945
- console.error("[MCPController] WebSocket error:", event);
27986
+ logger.error("[MCPController] WebSocket error:", event);
27946
27987
  this.broadcastFn({ type: "CONNECTION_STATUS", connected: false });
27947
27988
  };
27948
27989
  });
@@ -27951,12 +27992,21 @@ var MCPController = class _MCPController {
27951
27992
  const tokenChanged = this.authToken !== token;
27952
27993
  this.authToken = token;
27953
27994
  if (tokenChanged) {
27954
- console.log("[MCPController] Auth token changed, reconnecting WebSocket...");
27995
+ logger.log(
27996
+ "[MCPController] Auth token changed, reconnecting WebSocket..."
27997
+ );
27998
+ this.isReconnectingForToken = true;
27955
27999
  if (this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING)) {
27956
28000
  this.socket.close(1e3, "Reconnecting with new auth token");
27957
28001
  }
27958
28002
  setTimeout(() => {
27959
- this.connectWebSocket().catch((error48) => console.error("[MCPController] Failed to reconnect with new token:", error48));
28003
+ this.connectWebSocket().catch((error48) => {
28004
+ logger.error(
28005
+ "[MCPController] Failed to reconnect with new token:",
28006
+ error48
28007
+ );
28008
+ this.isReconnectingForToken = false;
28009
+ });
27960
28010
  }, 100);
27961
28011
  }
27962
28012
  }
@@ -28006,11 +28056,11 @@ var setBackendUrl = (url2) => {
28006
28056
  try {
28007
28057
  client.postMessage(message);
28008
28058
  } catch (e) {
28009
- console.error("[ServiceWorker] Failed to post message to client:", e);
28059
+ logger.error("[ServiceWorker] Failed to post message to client:", e);
28010
28060
  }
28011
28061
  });
28012
28062
  }).catch((err) => {
28013
- console.error("[ServiceWorker] Failed to match clients for broadcast:", err);
28063
+ logger.error("[ServiceWorker] Failed to match clients for broadcast:", err);
28014
28064
  });
28015
28065
  });
28016
28066
  return controller;
@@ -28022,7 +28072,7 @@ self.addEventListener("message", async (event) => {
28022
28072
  if (msg["type"] === "INIT") {
28023
28073
  const url2 = msg["backendUrl"];
28024
28074
  if (!url2) {
28025
- console.error("[ServiceWorker] INIT missing backendUrl");
28075
+ logger.error("[ServiceWorker] INIT missing backendUrl");
28026
28076
  return;
28027
28077
  }
28028
28078
  try {
@@ -28035,7 +28085,7 @@ self.addEventListener("message", async (event) => {
28035
28085
  pendingToken = null;
28036
28086
  }
28037
28087
  } catch (e) {
28038
- console.error("[ServiceWorker] Failed to apply INIT:", e);
28088
+ logger.error("[ServiceWorker] Failed to apply INIT:", e);
28039
28089
  }
28040
28090
  return;
28041
28091
  }
@@ -28050,7 +28100,7 @@ self.addEventListener("message", async (event) => {
28050
28100
  pendingToken = token;
28051
28101
  }
28052
28102
  } catch (e) {
28053
- console.error("[ServiceWorker] Failed to set auth token:", e);
28103
+ logger.error("[ServiceWorker] Failed to set auth token:", e);
28054
28104
  }
28055
28105
  return;
28056
28106
  }