@mcpc-tech/cli 0.1.50 → 0.1.51

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.
Files changed (11) hide show
  1. package/app.cjs +463 -267
  2. package/app.mjs +463 -267
  3. package/bin/mcpc.cjs +443 -265
  4. package/bin/mcpc.mjs +443 -265
  5. package/bin.cjs +443 -265
  6. package/bin.mjs +443 -265
  7. package/index.cjs +463 -267
  8. package/index.mjs +463 -267
  9. package/package.json +1 -1
  10. package/server.cjs +463 -267
  11. package/server.mjs +463 -267
package/index.cjs CHANGED
@@ -3037,6 +3037,7 @@ data:
3037
3037
  async handleGetRequest(req) {
3038
3038
  const acceptHeader = req.headers.get("accept");
3039
3039
  if (!acceptHeader?.includes("text/event-stream")) {
3040
+ this.onerror?.(new Error("Not Acceptable: Client must accept text/event-stream"));
3040
3041
  return this.createJsonErrorResponse(406, -32e3, "Not Acceptable: Client must accept text/event-stream");
3041
3042
  }
3042
3043
  const sessionError = this.validateSession(req);
@@ -3054,6 +3055,7 @@ data:
3054
3055
  }
3055
3056
  }
3056
3057
  if (this._streamMapping.get(this._standaloneSseStreamId) !== void 0) {
3058
+ this.onerror?.(new Error("Conflict: Only one SSE stream is allowed per session"));
3057
3059
  return this.createJsonErrorResponse(409, -32e3, "Conflict: Only one SSE stream is allowed per session");
3058
3060
  }
3059
3061
  const encoder2 = new TextEncoder();
@@ -3093,6 +3095,7 @@ data:
3093
3095
  */
3094
3096
  async replayEvents(lastEventId) {
3095
3097
  if (!this._eventStore) {
3098
+ this.onerror?.(new Error("Event store not configured"));
3096
3099
  return this.createJsonErrorResponse(400, -32e3, "Event store not configured");
3097
3100
  }
3098
3101
  try {
@@ -3100,9 +3103,11 @@ data:
3100
3103
  if (this._eventStore.getStreamIdForEventId) {
3101
3104
  streamId = await this._eventStore.getStreamIdForEventId(lastEventId);
3102
3105
  if (!streamId) {
3106
+ this.onerror?.(new Error("Invalid event ID format"));
3103
3107
  return this.createJsonErrorResponse(400, -32e3, "Invalid event ID format");
3104
3108
  }
3105
3109
  if (this._streamMapping.get(streamId) !== void 0) {
3110
+ this.onerror?.(new Error("Conflict: Stream already has an active connection"));
3106
3111
  return this.createJsonErrorResponse(409, -32e3, "Conflict: Stream already has an active connection");
3107
3112
  }
3108
3113
  }
@@ -3168,7 +3173,8 @@ data:
3168
3173
  `;
3169
3174
  controller.enqueue(encoder2.encode(eventData));
3170
3175
  return true;
3171
- } catch {
3176
+ } catch (error) {
3177
+ this.onerror?.(error);
3172
3178
  return false;
3173
3179
  }
3174
3180
  }
@@ -3176,6 +3182,7 @@ data:
3176
3182
  * Handles unsupported requests (PUT, PATCH, etc.)
3177
3183
  */
3178
3184
  handleUnsupportedRequest() {
3185
+ this.onerror?.(new Error("Method not allowed."));
3179
3186
  return new Response(JSON.stringify({
3180
3187
  jsonrpc: "2.0",
3181
3188
  error: {
@@ -3198,14 +3205,17 @@ data:
3198
3205
  try {
3199
3206
  const acceptHeader = req.headers.get("accept");
3200
3207
  if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
3208
+ this.onerror?.(new Error("Not Acceptable: Client must accept both application/json and text/event-stream"));
3201
3209
  return this.createJsonErrorResponse(406, -32e3, "Not Acceptable: Client must accept both application/json and text/event-stream");
3202
3210
  }
3203
3211
  const ct = req.headers.get("content-type");
3204
3212
  if (!ct || !ct.includes("application/json")) {
3213
+ this.onerror?.(new Error("Unsupported Media Type: Content-Type must be application/json"));
3205
3214
  return this.createJsonErrorResponse(415, -32e3, "Unsupported Media Type: Content-Type must be application/json");
3206
3215
  }
3207
3216
  const requestInfo = {
3208
- headers: Object.fromEntries(req.headers.entries())
3217
+ headers: Object.fromEntries(req.headers.entries()),
3218
+ url: new URL(req.url)
3209
3219
  };
3210
3220
  let rawMessage;
3211
3221
  if (options?.parsedBody !== void 0) {
@@ -3214,6 +3224,7 @@ data:
3214
3224
  try {
3215
3225
  rawMessage = await req.json();
3216
3226
  } catch {
3227
+ this.onerror?.(new Error("Parse error: Invalid JSON"));
3217
3228
  return this.createJsonErrorResponse(400, -32700, "Parse error: Invalid JSON");
3218
3229
  }
3219
3230
  }
@@ -3225,14 +3236,17 @@ data:
3225
3236
  messages = [JSONRPCMessageSchema.parse(rawMessage)];
3226
3237
  }
3227
3238
  } catch {
3239
+ this.onerror?.(new Error("Parse error: Invalid JSON-RPC message"));
3228
3240
  return this.createJsonErrorResponse(400, -32700, "Parse error: Invalid JSON-RPC message");
3229
3241
  }
3230
3242
  const isInitializationRequest = messages.some(isInitializeRequest);
3231
3243
  if (isInitializationRequest) {
3232
3244
  if (this._initialized && this.sessionId !== void 0) {
3245
+ this.onerror?.(new Error("Invalid Request: Server already initialized"));
3233
3246
  return this.createJsonErrorResponse(400, -32600, "Invalid Request: Server already initialized");
3234
3247
  }
3235
3248
  if (messages.length > 1) {
3249
+ this.onerror?.(new Error("Invalid Request: Only one initialization request is allowed"));
3236
3250
  return this.createJsonErrorResponse(400, -32600, "Invalid Request: Only one initialization request is allowed");
3237
3251
  }
3238
3252
  this.sessionId = this.sessionIdGenerator?.();
@@ -3358,13 +3372,16 @@ data:
3358
3372
  return void 0;
3359
3373
  }
3360
3374
  if (!this._initialized) {
3375
+ this.onerror?.(new Error("Bad Request: Server not initialized"));
3361
3376
  return this.createJsonErrorResponse(400, -32e3, "Bad Request: Server not initialized");
3362
3377
  }
3363
3378
  const sessionId = req.headers.get("mcp-session-id");
3364
3379
  if (!sessionId) {
3380
+ this.onerror?.(new Error("Bad Request: Mcp-Session-Id header is required"));
3365
3381
  return this.createJsonErrorResponse(400, -32e3, "Bad Request: Mcp-Session-Id header is required");
3366
3382
  }
3367
3383
  if (sessionId !== this.sessionId) {
3384
+ this.onerror?.(new Error("Session not found"));
3368
3385
  return this.createJsonErrorResponse(404, -32001, "Session not found");
3369
3386
  }
3370
3387
  return void 0;
@@ -3385,6 +3402,7 @@ data:
3385
3402
  validateProtocolVersion(req) {
3386
3403
  const protocolVersion = req.headers.get("mcp-protocol-version");
3387
3404
  if (protocolVersion !== null && !SUPPORTED_PROTOCOL_VERSIONS.includes(protocolVersion)) {
3405
+ this.onerror?.(new Error(`Bad Request: Unsupported protocol version: ${protocolVersion} (supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")})`));
3388
3406
  return this.createJsonErrorResponse(400, -32e3, `Bad Request: Unsupported protocol version: ${protocolVersion} (supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")})`);
3389
3407
  }
3390
3408
  return void 0;
@@ -3591,7 +3609,7 @@ var StreamableHTTPServerTransport = class {
3591
3609
  };
3592
3610
 
3593
3611
  // __mcpc__cli_latest/node_modules/@jsr/std__cli/parse_args.js
3594
- var FLAG_REGEXP = /^(?:-(?:(?<doubleDash>-)(?<negated>no-)?)?)(?<key>.+?)(?:=(?<value>.+?))?$/s;
3612
+ var FLAG_REGEXP = /^(?:-(?:(?<doubleDash>-)(?<negated>no-)?)?)(?<key>.+?)(?:=(?<value>.*))?$/s;
3595
3613
  var LETTER_REGEXP = /[A-Za-z]/;
3596
3614
  var NUMBER_REGEXP = /-?\d+(\.\d*)?(e-?\d+)?$/;
3597
3615
  var HYPHEN_REGEXP = /^(-|--)[^-]/;
@@ -3602,12 +3620,19 @@ var NON_WHITESPACE_REGEXP = /\S/;
3602
3620
  function isNumber(string3) {
3603
3621
  return NON_WHITESPACE_REGEXP.test(string3) && Number.isFinite(Number(string3));
3604
3622
  }
3623
+ function isConstructorOrProto(obj, key) {
3624
+ return key === "constructor" && typeof obj[key] === "function" || key === "__proto__";
3625
+ }
3605
3626
  function setNested(object5, keys, value, collect = false) {
3606
3627
  keys = [
3607
3628
  ...keys
3608
3629
  ];
3609
3630
  const key = keys.pop();
3610
- keys.forEach((key2) => object5 = object5[key2] ??= {});
3631
+ for (const k of keys) {
3632
+ if (isConstructorOrProto(object5, k)) return;
3633
+ object5 = object5[k] ??= {};
3634
+ }
3635
+ if (isConstructorOrProto(object5, key)) return;
3611
3636
  if (collect) {
3612
3637
  const v = object5[key];
3613
3638
  if (Array.isArray(v)) {
@@ -3738,7 +3763,7 @@ function parseArgs(args, options) {
3738
3763
  let key = groups.key;
3739
3764
  let value = groups.value;
3740
3765
  if (doubleDash2) {
3741
- if (value) {
3766
+ if (value != null) {
3742
3767
  if (booleanSet.has(key)) value = parseBooleanString(value);
3743
3768
  setArgument(key, value, arg, true);
3744
3769
  continue;
@@ -3776,6 +3801,10 @@ function parseArgs(args, options) {
3776
3801
  setArgument(letter, next, arg, true);
3777
3802
  continue;
3778
3803
  }
3804
+ if (next === "=") {
3805
+ setArgument(letter, "", arg, true);
3806
+ continue argsLoop;
3807
+ }
3779
3808
  if (LETTER_REGEXP.test(letter)) {
3780
3809
  const groups2 = VALUE_REGEXP.exec(next)?.groups;
3781
3810
  if (groups2) {
@@ -4270,7 +4299,7 @@ Usage:
4270
4299
  - ${toolName}({ skill: "skill-name" }) - Load main SKILL.md content
4271
4300
  - ${toolName}({ skill: "skill-name", ref: "path/to/file" }) - Load reference file
4272
4301
 
4273
- Note: For scripts/ and assets/, use appropriate tools directly.`;
4302
+ Note: For scripts/, use the bash tool with the script path to execute.`;
4274
4303
  }
4275
4304
  function createSkillsPlugin(options) {
4276
4305
  const { paths } = options;
@@ -4378,11 +4407,15 @@ Available skills: ${available.join(", ")}` : "\n\nNo skills available.";
4378
4407
  try {
4379
4408
  const content = await (0, import_promises2.readFile)((0, import_node_path4.join)(meta.basePath, "SKILL.md"), "utf-8");
4380
4409
  const body = extractBody(content);
4410
+ const skillPathInfo = `
4411
+ ---
4412
+ Skill path: ${meta.basePath}
4413
+ `;
4381
4414
  return {
4382
4415
  content: [
4383
4416
  {
4384
4417
  type: "text",
4385
- text: body
4418
+ text: body + skillPathInfo
4386
4419
  }
4387
4420
  ]
4388
4421
  };
@@ -5253,12 +5286,29 @@ function writeFoldedLines(count) {
5253
5286
  if (count > 1) return "\n".repeat(count - 1);
5254
5287
  return "";
5255
5288
  }
5289
+ var Scanner = class {
5290
+ source;
5291
+ #length;
5292
+ position = 0;
5293
+ constructor(source) {
5294
+ source += "\0";
5295
+ this.source = source;
5296
+ this.#length = source.length;
5297
+ }
5298
+ peek(offset = 0) {
5299
+ return this.source.charCodeAt(this.position + offset);
5300
+ }
5301
+ next() {
5302
+ this.position += 1;
5303
+ }
5304
+ eof() {
5305
+ return this.position >= this.#length - 1;
5306
+ }
5307
+ };
5256
5308
  var LoaderState = class {
5257
- input;
5258
- length;
5309
+ #scanner;
5259
5310
  lineIndent = 0;
5260
5311
  lineStart = 0;
5261
- position = 0;
5262
5312
  line = 0;
5263
5313
  onWarning;
5264
5314
  allowDuplicateKeys;
@@ -5268,44 +5318,40 @@ var LoaderState = class {
5268
5318
  tagMap = /* @__PURE__ */ new Map();
5269
5319
  anchorMap = /* @__PURE__ */ new Map();
5270
5320
  constructor(input, { schema = DEFAULT_SCHEMA, onWarning, allowDuplicateKeys = false }) {
5271
- this.input = input;
5321
+ this.#scanner = new Scanner(input);
5272
5322
  this.onWarning = onWarning;
5273
5323
  this.allowDuplicateKeys = allowDuplicateKeys;
5274
5324
  this.implicitTypes = schema.implicitTypes;
5275
5325
  this.typeMap = schema.typeMap;
5276
- this.length = input.length;
5277
5326
  this.readIndent();
5278
5327
  }
5279
5328
  skipWhitespaces() {
5280
- let ch = this.peek();
5329
+ let ch = this.#scanner.peek();
5281
5330
  while (isWhiteSpace(ch)) {
5282
- ch = this.next();
5331
+ this.#scanner.next();
5332
+ ch = this.#scanner.peek();
5283
5333
  }
5284
5334
  }
5285
5335
  skipComment() {
5286
- let ch = this.peek();
5336
+ let ch = this.#scanner.peek();
5287
5337
  if (ch !== SHARP) return;
5288
- ch = this.next();
5338
+ this.#scanner.next();
5339
+ ch = this.#scanner.peek();
5289
5340
  while (ch !== 0 && !isEOL(ch)) {
5290
- ch = this.next();
5341
+ this.#scanner.next();
5342
+ ch = this.#scanner.peek();
5291
5343
  }
5292
5344
  }
5293
5345
  readIndent() {
5294
- let char = this.peek();
5295
- while (char === SPACE) {
5346
+ let ch = this.#scanner.peek();
5347
+ while (ch === SPACE) {
5296
5348
  this.lineIndent += 1;
5297
- char = this.next();
5349
+ this.#scanner.next();
5350
+ ch = this.#scanner.peek();
5298
5351
  }
5299
5352
  }
5300
- peek(offset = 0) {
5301
- return this.input.charCodeAt(this.position + offset);
5302
- }
5303
- next() {
5304
- this.position += 1;
5305
- return this.peek();
5306
- }
5307
5353
  #createError(message) {
5308
- const mark = markToString(this.input, this.position, this.line, this.position - this.lineStart);
5354
+ const mark = markToString(this.#scanner.source, this.#scanner.position, this.line, this.#scanner.position - this.lineStart);
5309
5355
  return new SyntaxError(`${message} ${mark}`);
5310
5356
  }
5311
5357
  dispatchWarning(message) {
@@ -5350,7 +5396,7 @@ var LoaderState = class {
5350
5396
  }
5351
5397
  captureSegment(start, end, checkJson) {
5352
5398
  if (start < end) {
5353
- const result = this.input.slice(start, end);
5399
+ const result = this.#scanner.source.slice(start, end);
5354
5400
  if (checkJson) {
5355
5401
  for (let position = 0; position < result.length; position++) {
5356
5402
  const character = result.charCodeAt(position);
@@ -5368,21 +5414,21 @@ var LoaderState = class {
5368
5414
  let detected = false;
5369
5415
  const result = [];
5370
5416
  if (anchor !== null) this.anchorMap.set(anchor, result);
5371
- let ch = this.peek();
5417
+ let ch = this.#scanner.peek();
5372
5418
  while (ch !== 0) {
5373
5419
  if (ch !== MINUS) {
5374
5420
  break;
5375
5421
  }
5376
- const following = this.peek(1);
5422
+ const following = this.#scanner.peek(1);
5377
5423
  if (!isWhiteSpaceOrEOL(following)) {
5378
5424
  break;
5379
5425
  }
5380
5426
  detected = true;
5381
- this.position++;
5427
+ this.#scanner.next();
5382
5428
  if (this.skipSeparationSpace(true, -1)) {
5383
5429
  if (this.lineIndent <= nodeIndent) {
5384
5430
  result.push(null);
5385
- ch = this.peek();
5431
+ ch = this.#scanner.peek();
5386
5432
  continue;
5387
5433
  }
5388
5434
  }
@@ -5395,7 +5441,7 @@ var LoaderState = class {
5395
5441
  });
5396
5442
  if (newState) result.push(newState.result);
5397
5443
  this.skipSeparationSpace(true, -1);
5398
- ch = this.peek();
5444
+ ch = this.#scanner.peek();
5399
5445
  if ((this.line === line || this.lineIndent > nodeIndent) && ch !== 0) {
5400
5446
  throw this.#createError("Cannot read block sequence: bad indentation of a sequence entry");
5401
5447
  } else if (this.lineIndent < nodeIndent) {
@@ -5451,7 +5497,7 @@ var LoaderState = class {
5451
5497
  } else {
5452
5498
  if (!this.allowDuplicateKeys && !overridableKeys.has(keyNode) && Object.hasOwn(result, keyNode)) {
5453
5499
  this.line = startLine || this.line;
5454
- this.position = startPos || this.position;
5500
+ this.#scanner.position = startPos || this.#scanner.position;
5455
5501
  throw this.#createError("Cannot store mapping pair: duplicated key");
5456
5502
  }
5457
5503
  Object.defineProperty(result, keyNode, {
@@ -5465,37 +5511,37 @@ var LoaderState = class {
5465
5511
  return result;
5466
5512
  }
5467
5513
  readLineBreak() {
5468
- const ch = this.peek();
5514
+ const ch = this.#scanner.peek();
5469
5515
  if (ch === LINE_FEED) {
5470
- this.position++;
5516
+ this.#scanner.next();
5471
5517
  } else if (ch === CARRIAGE_RETURN) {
5472
- this.position++;
5473
- if (this.peek() === LINE_FEED) {
5474
- this.position++;
5518
+ this.#scanner.next();
5519
+ if (this.#scanner.peek() === LINE_FEED) {
5520
+ this.#scanner.next();
5475
5521
  }
5476
5522
  } else {
5477
5523
  throw this.#createError("Cannot read line: line break not found");
5478
5524
  }
5479
5525
  this.line += 1;
5480
- this.lineStart = this.position;
5526
+ this.lineStart = this.#scanner.position;
5481
5527
  }
5482
5528
  skipSeparationSpace(allowComments, checkIndent) {
5483
5529
  let lineBreaks = 0;
5484
- let ch = this.peek();
5530
+ let ch = this.#scanner.peek();
5485
5531
  while (ch !== 0) {
5486
5532
  this.skipWhitespaces();
5487
- ch = this.peek();
5533
+ ch = this.#scanner.peek();
5488
5534
  if (allowComments) {
5489
5535
  this.skipComment();
5490
- ch = this.peek();
5536
+ ch = this.#scanner.peek();
5491
5537
  }
5492
5538
  if (isEOL(ch)) {
5493
5539
  this.readLineBreak();
5494
- ch = this.peek();
5540
+ ch = this.#scanner.peek();
5495
5541
  lineBreaks++;
5496
5542
  this.lineIndent = 0;
5497
5543
  this.readIndent();
5498
- ch = this.peek();
5544
+ ch = this.#scanner.peek();
5499
5545
  } else {
5500
5546
  break;
5501
5547
  }
@@ -5506,9 +5552,9 @@ var LoaderState = class {
5506
5552
  return lineBreaks;
5507
5553
  }
5508
5554
  testDocumentSeparator() {
5509
- let ch = this.peek();
5510
- if ((ch === MINUS || ch === DOT) && ch === this.peek(1) && ch === this.peek(2)) {
5511
- ch = this.peek(3);
5555
+ let ch = this.#scanner.peek();
5556
+ if ((ch === MINUS || ch === DOT) && ch === this.#scanner.peek(1) && ch === this.#scanner.peek(2)) {
5557
+ ch = this.#scanner.peek(3);
5512
5558
  if (ch === 0 || isWhiteSpaceOrEOL(ch)) {
5513
5559
  return true;
5514
5560
  }
@@ -5516,34 +5562,34 @@ var LoaderState = class {
5516
5562
  return false;
5517
5563
  }
5518
5564
  readPlainScalar(tag, anchor, nodeIndent, withinFlowCollection) {
5519
- let ch = this.peek();
5565
+ let ch = this.#scanner.peek();
5520
5566
  if (isWhiteSpaceOrEOL(ch) || isFlowIndicator(ch) || ch === SHARP || ch === AMPERSAND || ch === ASTERISK || ch === EXCLAMATION || ch === VERTICAL_LINE || ch === GREATER_THAN || ch === SINGLE_QUOTE || ch === DOUBLE_QUOTE || ch === PERCENT || ch === COMMERCIAL_AT || ch === GRAVE_ACCENT) {
5521
5567
  return;
5522
5568
  }
5523
5569
  let following;
5524
5570
  if (ch === QUESTION || ch === MINUS) {
5525
- following = this.peek(1);
5571
+ following = this.#scanner.peek(1);
5526
5572
  if (isWhiteSpaceOrEOL(following) || withinFlowCollection && isFlowIndicator(following)) {
5527
5573
  return;
5528
5574
  }
5529
5575
  }
5530
5576
  let result = "";
5531
- let captureEnd = this.position;
5532
- let captureStart = this.position;
5577
+ let captureEnd = this.#scanner.position;
5578
+ let captureStart = this.#scanner.position;
5533
5579
  let hasPendingContent = false;
5534
5580
  let line = 0;
5535
5581
  while (ch !== 0) {
5536
5582
  if (ch === COLON) {
5537
- following = this.peek(1);
5583
+ following = this.#scanner.peek(1);
5538
5584
  if (isWhiteSpaceOrEOL(following) || withinFlowCollection && isFlowIndicator(following)) {
5539
5585
  break;
5540
5586
  }
5541
5587
  } else if (ch === SHARP) {
5542
- const preceding = this.peek(-1);
5588
+ const preceding = this.#scanner.peek(-1);
5543
5589
  if (isWhiteSpaceOrEOL(preceding)) {
5544
5590
  break;
5545
5591
  }
5546
- } else if (this.position === this.lineStart && this.testDocumentSeparator() || withinFlowCollection && isFlowIndicator(ch)) {
5592
+ } else if (this.#scanner.position === this.lineStart && this.testDocumentSeparator() || withinFlowCollection && isFlowIndicator(ch)) {
5547
5593
  break;
5548
5594
  } else if (isEOL(ch)) {
5549
5595
  line = this.line;
@@ -5552,10 +5598,10 @@ var LoaderState = class {
5552
5598
  this.skipSeparationSpace(false, -1);
5553
5599
  if (this.lineIndent >= nodeIndent) {
5554
5600
  hasPendingContent = true;
5555
- ch = this.peek();
5601
+ ch = this.#scanner.peek();
5556
5602
  continue;
5557
5603
  } else {
5558
- this.position = captureEnd;
5604
+ this.#scanner.position = captureEnd;
5559
5605
  this.line = line;
5560
5606
  this.lineStart = lineStart;
5561
5607
  this.lineIndent = lineIndent;
@@ -5566,13 +5612,14 @@ var LoaderState = class {
5566
5612
  const segment2 = this.captureSegment(captureStart, captureEnd, false);
5567
5613
  if (segment2) result += segment2;
5568
5614
  result += writeFoldedLines(this.line - line);
5569
- captureStart = captureEnd = this.position;
5615
+ captureStart = captureEnd = this.#scanner.position;
5570
5616
  hasPendingContent = false;
5571
5617
  }
5572
5618
  if (!isWhiteSpace(ch)) {
5573
- captureEnd = this.position + 1;
5619
+ captureEnd = this.#scanner.position + 1;
5574
5620
  }
5575
- ch = this.next();
5621
+ this.#scanner.next();
5622
+ ch = this.#scanner.peek();
5576
5623
  }
5577
5624
  const segment = this.captureSegment(captureStart, captureEnd, false);
5578
5625
  if (segment) result += segment;
@@ -5585,22 +5632,23 @@ var LoaderState = class {
5585
5632
  };
5586
5633
  }
5587
5634
  readSingleQuotedScalar(tag, anchor, nodeIndent) {
5588
- let ch = this.peek();
5635
+ let ch = this.#scanner.peek();
5589
5636
  if (ch !== SINGLE_QUOTE) return;
5590
5637
  let result = "";
5591
- this.position++;
5592
- let captureStart = this.position;
5593
- let captureEnd = this.position;
5594
- ch = this.peek();
5638
+ this.#scanner.next();
5639
+ let captureStart = this.#scanner.position;
5640
+ let captureEnd = this.#scanner.position;
5641
+ ch = this.#scanner.peek();
5595
5642
  while (ch !== 0) {
5596
5643
  if (ch === SINGLE_QUOTE) {
5597
- const segment = this.captureSegment(captureStart, this.position, true);
5644
+ const segment = this.captureSegment(captureStart, this.#scanner.position, true);
5598
5645
  if (segment) result += segment;
5599
- ch = this.next();
5646
+ this.#scanner.next();
5647
+ ch = this.#scanner.peek();
5600
5648
  if (ch === SINGLE_QUOTE) {
5601
- captureStart = this.position;
5602
- this.position++;
5603
- captureEnd = this.position;
5649
+ captureStart = this.#scanner.position;
5650
+ this.#scanner.next();
5651
+ captureEnd = this.#scanner.position;
5604
5652
  } else {
5605
5653
  if (anchor !== null) this.anchorMap.set(anchor, result);
5606
5654
  return {
@@ -5614,31 +5662,31 @@ var LoaderState = class {
5614
5662
  const segment = this.captureSegment(captureStart, captureEnd, true);
5615
5663
  if (segment) result += segment;
5616
5664
  result += writeFoldedLines(this.skipSeparationSpace(false, nodeIndent));
5617
- captureStart = captureEnd = this.position;
5618
- } else if (this.position === this.lineStart && this.testDocumentSeparator()) {
5665
+ captureStart = captureEnd = this.#scanner.position;
5666
+ } else if (this.#scanner.position === this.lineStart && this.testDocumentSeparator()) {
5619
5667
  throw this.#createError("Unexpected end of the document within a single quoted scalar");
5620
5668
  } else {
5621
- this.position++;
5622
- captureEnd = this.position;
5669
+ this.#scanner.next();
5670
+ captureEnd = this.#scanner.position;
5623
5671
  }
5624
- ch = this.peek();
5672
+ ch = this.#scanner.peek();
5625
5673
  }
5626
5674
  throw this.#createError("Unexpected end of the stream within a single quoted scalar");
5627
5675
  }
5628
5676
  readDoubleQuotedScalar(tag, anchor, nodeIndent) {
5629
- let ch = this.peek();
5677
+ let ch = this.#scanner.peek();
5630
5678
  if (ch !== DOUBLE_QUOTE) return;
5631
5679
  let result = "";
5632
- this.position++;
5633
- let captureEnd = this.position;
5634
- let captureStart = this.position;
5680
+ this.#scanner.next();
5681
+ let captureEnd = this.#scanner.position;
5682
+ let captureStart = this.#scanner.position;
5635
5683
  let tmp;
5636
- ch = this.peek();
5684
+ ch = this.#scanner.peek();
5637
5685
  while (ch !== 0) {
5638
5686
  if (ch === DOUBLE_QUOTE) {
5639
- const segment = this.captureSegment(captureStart, this.position, true);
5687
+ const segment = this.captureSegment(captureStart, this.#scanner.position, true);
5640
5688
  if (segment) result += segment;
5641
- this.position++;
5689
+ this.#scanner.next();
5642
5690
  if (anchor !== null) this.anchorMap.set(anchor, result);
5643
5691
  return {
5644
5692
  tag,
@@ -5648,19 +5696,21 @@ var LoaderState = class {
5648
5696
  };
5649
5697
  }
5650
5698
  if (ch === BACKSLASH) {
5651
- const segment = this.captureSegment(captureStart, this.position, true);
5699
+ const segment = this.captureSegment(captureStart, this.#scanner.position, true);
5652
5700
  if (segment) result += segment;
5653
- ch = this.next();
5701
+ this.#scanner.next();
5702
+ ch = this.#scanner.peek();
5654
5703
  if (isEOL(ch)) {
5655
5704
  this.skipSeparationSpace(false, nodeIndent);
5656
5705
  } else if (ch < 256 && SIMPLE_ESCAPE_SEQUENCES.has(ch)) {
5657
5706
  result += SIMPLE_ESCAPE_SEQUENCES.get(ch);
5658
- this.position++;
5707
+ this.#scanner.next();
5659
5708
  } else if ((tmp = ESCAPED_HEX_LENGTHS.get(ch) ?? 0) > 0) {
5660
5709
  let hexLength = tmp;
5661
5710
  let hexResult = 0;
5662
5711
  for (; hexLength > 0; hexLength--) {
5663
- ch = this.next();
5712
+ this.#scanner.next();
5713
+ ch = this.#scanner.peek();
5664
5714
  if ((tmp = hexCharCodeToNumber(ch)) >= 0) {
5665
5715
  hexResult = (hexResult << 4) + tmp;
5666
5716
  } else {
@@ -5668,28 +5718,28 @@ var LoaderState = class {
5668
5718
  }
5669
5719
  }
5670
5720
  result += codepointToChar(hexResult);
5671
- this.position++;
5721
+ this.#scanner.next();
5672
5722
  } else {
5673
5723
  throw this.#createError("Cannot read double quoted scalar: unknown escape sequence");
5674
5724
  }
5675
- captureStart = captureEnd = this.position;
5725
+ captureStart = captureEnd = this.#scanner.position;
5676
5726
  } else if (isEOL(ch)) {
5677
5727
  const segment = this.captureSegment(captureStart, captureEnd, true);
5678
5728
  if (segment) result += segment;
5679
5729
  result += writeFoldedLines(this.skipSeparationSpace(false, nodeIndent));
5680
- captureStart = captureEnd = this.position;
5681
- } else if (this.position === this.lineStart && this.testDocumentSeparator()) {
5730
+ captureStart = captureEnd = this.#scanner.position;
5731
+ } else if (this.#scanner.position === this.lineStart && this.testDocumentSeparator()) {
5682
5732
  throw this.#createError("Unexpected end of the document within a double quoted scalar");
5683
5733
  } else {
5684
- this.position++;
5685
- captureEnd = this.position;
5734
+ this.#scanner.next();
5735
+ captureEnd = this.#scanner.position;
5686
5736
  }
5687
- ch = this.peek();
5737
+ ch = this.#scanner.peek();
5688
5738
  }
5689
5739
  throw this.#createError("Unexpected end of the stream within a double quoted scalar");
5690
5740
  }
5691
5741
  readFlowCollection(tag, anchor, nodeIndent) {
5692
- let ch = this.peek();
5742
+ let ch = this.#scanner.peek();
5693
5743
  let terminator;
5694
5744
  let isMapping = true;
5695
5745
  let result = {};
@@ -5703,7 +5753,8 @@ var LoaderState = class {
5703
5753
  return;
5704
5754
  }
5705
5755
  if (anchor !== null) this.anchorMap.set(anchor, result);
5706
- ch = this.next();
5756
+ this.#scanner.next();
5757
+ ch = this.#scanner.peek();
5707
5758
  let readNext = true;
5708
5759
  let valueNode = null;
5709
5760
  let keyNode = null;
@@ -5715,9 +5766,9 @@ var LoaderState = class {
5715
5766
  const overridableKeys = /* @__PURE__ */ new Set();
5716
5767
  while (ch !== 0) {
5717
5768
  this.skipSeparationSpace(true, nodeIndent);
5718
- ch = this.peek();
5769
+ ch = this.#scanner.peek();
5719
5770
  if (ch === terminator) {
5720
- this.position++;
5771
+ this.#scanner.next();
5721
5772
  const kind = isMapping ? "mapping" : "sequence";
5722
5773
  return {
5723
5774
  tag,
@@ -5732,10 +5783,10 @@ var LoaderState = class {
5732
5783
  keyTag = keyNode = valueNode = null;
5733
5784
  isPair = isExplicitPair = false;
5734
5785
  if (ch === QUESTION) {
5735
- following = this.peek(1);
5786
+ following = this.#scanner.peek(1);
5736
5787
  if (isWhiteSpaceOrEOL(following)) {
5737
5788
  isPair = isExplicitPair = true;
5738
- this.position++;
5789
+ this.#scanner.next();
5739
5790
  this.skipSeparationSpace(true, nodeIndent);
5740
5791
  }
5741
5792
  }
@@ -5751,10 +5802,11 @@ var LoaderState = class {
5751
5802
  keyNode = newState.result;
5752
5803
  }
5753
5804
  this.skipSeparationSpace(true, nodeIndent);
5754
- ch = this.peek();
5805
+ ch = this.#scanner.peek();
5755
5806
  if ((isExplicitPair || this.line === line) && ch === COLON) {
5756
5807
  isPair = true;
5757
- ch = this.next();
5808
+ this.#scanner.next();
5809
+ ch = this.#scanner.peek();
5758
5810
  this.skipSeparationSpace(true, nodeIndent);
5759
5811
  const newState2 = this.composeNode({
5760
5812
  parentIndent: nodeIndent,
@@ -5772,10 +5824,11 @@ var LoaderState = class {
5772
5824
  result.push(keyNode);
5773
5825
  }
5774
5826
  this.skipSeparationSpace(true, nodeIndent);
5775
- ch = this.peek();
5827
+ ch = this.#scanner.peek();
5776
5828
  if (ch === COMMA) {
5777
5829
  readNext = true;
5778
- ch = this.next();
5830
+ this.#scanner.next();
5831
+ ch = this.#scanner.peek();
5779
5832
  } else {
5780
5833
  readNext = false;
5781
5834
  }
@@ -5791,7 +5844,7 @@ var LoaderState = class {
5791
5844
  let textIndent = nodeIndent;
5792
5845
  let emptyLines = 0;
5793
5846
  let atMoreIndented = false;
5794
- let ch = this.peek();
5847
+ let ch = this.#scanner.peek();
5795
5848
  let folding = false;
5796
5849
  if (ch === VERTICAL_LINE) {
5797
5850
  folding = false;
@@ -5803,7 +5856,8 @@ var LoaderState = class {
5803
5856
  let result = "";
5804
5857
  let tmp = 0;
5805
5858
  while (ch !== 0) {
5806
- ch = this.next();
5859
+ this.#scanner.next();
5860
+ ch = this.#scanner.peek();
5807
5861
  if (ch === PLUS || ch === MINUS) {
5808
5862
  if (CHOMPING_CLIP === chomping) {
5809
5863
  chomping = ch === PLUS ? CHOMPING_KEEP : CHOMPING_STRIP;
@@ -5826,15 +5880,16 @@ var LoaderState = class {
5826
5880
  if (isWhiteSpace(ch)) {
5827
5881
  this.skipWhitespaces();
5828
5882
  this.skipComment();
5829
- ch = this.peek();
5883
+ ch = this.#scanner.peek();
5830
5884
  }
5831
5885
  while (ch !== 0) {
5832
5886
  this.readLineBreak();
5833
5887
  this.lineIndent = 0;
5834
- ch = this.peek();
5888
+ ch = this.#scanner.peek();
5835
5889
  while ((!detectedIndent || this.lineIndent < textIndent) && ch === SPACE) {
5836
5890
  this.lineIndent++;
5837
- ch = this.next();
5891
+ this.#scanner.next();
5892
+ ch = this.#scanner.peek();
5838
5893
  }
5839
5894
  if (!detectedIndent && this.lineIndent > textIndent) {
5840
5895
  textIndent = this.lineIndent;
@@ -5873,11 +5928,12 @@ var LoaderState = class {
5873
5928
  didReadContent = true;
5874
5929
  detectedIndent = true;
5875
5930
  emptyLines = 0;
5876
- const captureStart = this.position;
5931
+ const captureStart = this.#scanner.position;
5877
5932
  while (!isEOL(ch) && ch !== 0) {
5878
- ch = this.next();
5933
+ this.#scanner.next();
5934
+ ch = this.#scanner.peek();
5879
5935
  }
5880
- const segment = this.captureSegment(captureStart, this.position, false);
5936
+ const segment = this.captureSegment(captureStart, this.#scanner.position, false);
5881
5937
  if (segment) result += segment;
5882
5938
  }
5883
5939
  if (anchor !== null) this.anchorMap.set(anchor, result);
@@ -5900,11 +5956,11 @@ var LoaderState = class {
5900
5956
  let atExplicitKey = false;
5901
5957
  let detected = false;
5902
5958
  if (anchor !== null) this.anchorMap.set(anchor, result);
5903
- let ch = this.peek();
5959
+ let ch = this.#scanner.peek();
5904
5960
  while (ch !== 0) {
5905
- const following = this.peek(1);
5961
+ const following = this.#scanner.peek(1);
5906
5962
  line = this.line;
5907
- pos = this.position;
5963
+ pos = this.#scanner.position;
5908
5964
  if ((ch === QUESTION || ch === COLON) && isWhiteSpaceOrEOL(following)) {
5909
5965
  if (ch === QUESTION) {
5910
5966
  if (atExplicitKey) {
@@ -5922,7 +5978,7 @@ var LoaderState = class {
5922
5978
  } else {
5923
5979
  throw this.#createError("Cannot read block as explicit mapping pair is incomplete: a key node is missed or followed by a non-tabulated empty line");
5924
5980
  }
5925
- this.position += 1;
5981
+ this.#scanner.next();
5926
5982
  ch = following;
5927
5983
  } else {
5928
5984
  const newState = this.composeNode({
@@ -5933,11 +5989,12 @@ var LoaderState = class {
5933
5989
  });
5934
5990
  if (!newState) break;
5935
5991
  if (this.line === line) {
5936
- ch = this.peek();
5992
+ ch = this.#scanner.peek();
5937
5993
  this.skipWhitespaces();
5938
- ch = this.peek();
5994
+ ch = this.#scanner.peek();
5939
5995
  if (ch === COLON) {
5940
- ch = this.next();
5996
+ this.#scanner.next();
5997
+ ch = this.#scanner.peek();
5941
5998
  if (!isWhiteSpaceOrEOL(ch)) {
5942
5999
  throw this.#createError("Cannot read block: a whitespace character is expected after the key-value separator within a block mapping");
5943
6000
  }
@@ -5994,7 +6051,7 @@ var LoaderState = class {
5994
6051
  keyTag = keyNode = valueNode = null;
5995
6052
  }
5996
6053
  this.skipSeparationSpace(true, -1);
5997
- ch = this.peek();
6054
+ ch = this.#scanner.peek();
5998
6055
  }
5999
6056
  if (this.lineIndent > nodeIndent && ch !== 0) {
6000
6057
  throw this.#createError("Cannot read block: bad indentation of a mapping entry");
@@ -6017,30 +6074,35 @@ var LoaderState = class {
6017
6074
  let isNamed = false;
6018
6075
  let tagHandle = "";
6019
6076
  let tagName;
6020
- let ch = this.peek();
6077
+ let ch = this.#scanner.peek();
6021
6078
  if (ch !== EXCLAMATION) return;
6022
6079
  if (tag !== null) {
6023
6080
  throw this.#createError("Cannot read tag property: duplication of a tag property");
6024
6081
  }
6025
- ch = this.next();
6082
+ this.#scanner.next();
6083
+ ch = this.#scanner.peek();
6026
6084
  if (ch === SMALLER_THAN) {
6027
6085
  isVerbatim = true;
6028
- ch = this.next();
6086
+ this.#scanner.next();
6087
+ ch = this.#scanner.peek();
6029
6088
  } else if (ch === EXCLAMATION) {
6030
6089
  isNamed = true;
6031
6090
  tagHandle = "!!";
6032
- ch = this.next();
6091
+ this.#scanner.next();
6092
+ ch = this.#scanner.peek();
6033
6093
  } else {
6034
6094
  tagHandle = "!";
6035
6095
  }
6036
- let position = this.position;
6096
+ let position = this.#scanner.position;
6037
6097
  if (isVerbatim) {
6038
6098
  do {
6039
- ch = this.next();
6099
+ this.#scanner.next();
6100
+ ch = this.#scanner.peek();
6040
6101
  } while (ch !== 0 && ch !== GREATER_THAN);
6041
- if (this.position < this.length) {
6042
- tagName = this.input.slice(position, this.position);
6043
- ch = this.next();
6102
+ if (!this.#scanner.eof()) {
6103
+ tagName = this.#scanner.source.slice(position, this.#scanner.position);
6104
+ this.#scanner.next();
6105
+ ch = this.#scanner.peek();
6044
6106
  } else {
6045
6107
  throw this.#createError("Cannot read tag property: unexpected end of stream");
6046
6108
  }
@@ -6048,19 +6110,20 @@ var LoaderState = class {
6048
6110
  while (ch !== 0 && !isWhiteSpaceOrEOL(ch)) {
6049
6111
  if (ch === EXCLAMATION) {
6050
6112
  if (!isNamed) {
6051
- tagHandle = this.input.slice(position - 1, this.position + 1);
6113
+ tagHandle = this.#scanner.source.slice(position - 1, this.#scanner.position + 1);
6052
6114
  if (!PATTERN_TAG_HANDLE.test(tagHandle)) {
6053
6115
  throw this.#createError("Cannot read tag property: named tag handle contains invalid characters");
6054
6116
  }
6055
6117
  isNamed = true;
6056
- position = this.position + 1;
6118
+ position = this.#scanner.position + 1;
6057
6119
  } else {
6058
6120
  throw this.#createError("Cannot read tag property: tag suffix cannot contain an exclamation mark");
6059
6121
  }
6060
6122
  }
6061
- ch = this.next();
6123
+ this.#scanner.next();
6124
+ ch = this.#scanner.peek();
6062
6125
  }
6063
- tagName = this.input.slice(position, this.position);
6126
+ tagName = this.#scanner.source.slice(position, this.#scanner.position);
6064
6127
  if (PATTERN_FLOW_INDICATORS.test(tagName)) {
6065
6128
  throw this.#createError("Cannot read tag property: tag suffix cannot contain flow indicator characters");
6066
6129
  }
@@ -6080,32 +6143,36 @@ var LoaderState = class {
6080
6143
  throw this.#createError(`Cannot read tag property: undeclared tag handle "${tagHandle}"`);
6081
6144
  }
6082
6145
  readAnchorProperty(anchor) {
6083
- let ch = this.peek();
6146
+ let ch = this.#scanner.peek();
6084
6147
  if (ch !== AMPERSAND) return;
6085
6148
  if (anchor !== null) {
6086
6149
  throw this.#createError("Cannot read anchor property: duplicate anchor property");
6087
6150
  }
6088
- ch = this.next();
6089
- const position = this.position;
6151
+ this.#scanner.next();
6152
+ ch = this.#scanner.peek();
6153
+ const position = this.#scanner.position;
6090
6154
  while (ch !== 0 && !isWhiteSpaceOrEOL(ch) && !isFlowIndicator(ch)) {
6091
- ch = this.next();
6155
+ this.#scanner.next();
6156
+ ch = this.#scanner.peek();
6092
6157
  }
6093
- if (this.position === position) {
6158
+ if (this.#scanner.position === position) {
6094
6159
  throw this.#createError("Cannot read anchor property: name of an anchor node must contain at least one character");
6095
6160
  }
6096
- return this.input.slice(position, this.position);
6161
+ return this.#scanner.source.slice(position, this.#scanner.position);
6097
6162
  }
6098
6163
  readAlias() {
6099
- if (this.peek() !== ASTERISK) return;
6100
- let ch = this.next();
6101
- const position = this.position;
6164
+ if (this.#scanner.peek() !== ASTERISK) return;
6165
+ this.#scanner.next();
6166
+ let ch = this.#scanner.peek();
6167
+ const position = this.#scanner.position;
6102
6168
  while (ch !== 0 && !isWhiteSpaceOrEOL(ch) && !isFlowIndicator(ch)) {
6103
- ch = this.next();
6169
+ this.#scanner.next();
6170
+ ch = this.#scanner.peek();
6104
6171
  }
6105
- if (this.position === position) {
6172
+ if (this.#scanner.position === position) {
6106
6173
  throw this.#createError("Cannot read alias: alias name must contain at least one character");
6107
6174
  }
6108
- const alias = this.input.slice(position, this.position);
6175
+ const alias = this.#scanner.source.slice(position, this.#scanner.position);
6109
6176
  if (!this.anchorMap.has(alias)) {
6110
6177
  throw this.#createError(`Cannot read alias: unidentified alias "${alias}"`);
6111
6178
  }
@@ -6188,7 +6255,7 @@ var LoaderState = class {
6188
6255
  const cond = CONTEXT_FLOW_IN === nodeContext || CONTEXT_FLOW_OUT === nodeContext;
6189
6256
  const flowIndent = cond ? parentIndent : parentIndent + 1;
6190
6257
  if (allowBlockCollections) {
6191
- const blockIndent = this.position - this.lineStart;
6258
+ const blockIndent = this.#scanner.position - this.lineStart;
6192
6259
  const blockSequenceState = this.readBlockSequence(tag, anchor, blockIndent);
6193
6260
  if (blockSequenceState) return this.resolveTag(blockSequenceState);
6194
6261
  const blockMappingState = this.readBlockMapping(tag, anchor, blockIndent, flowIndent);
@@ -6222,7 +6289,7 @@ var LoaderState = class {
6222
6289
  return this.resolveTag(plainScalarState);
6223
6290
  }
6224
6291
  } else if (indentStatus === 0 && CONTEXT_BLOCK_OUT === nodeContext && allowBlockCollections) {
6225
- const blockIndent = this.position - this.lineStart;
6292
+ const blockIndent = this.#scanner.position - this.lineStart;
6226
6293
  const newState2 = this.readBlockSequence(tag, anchor, blockIndent);
6227
6294
  if (newState2) return this.resolveTag(newState2);
6228
6295
  }
@@ -6237,20 +6304,22 @@ var LoaderState = class {
6237
6304
  readDirectives() {
6238
6305
  let hasDirectives = false;
6239
6306
  let version = null;
6240
- let ch = this.peek();
6307
+ let ch = this.#scanner.peek();
6241
6308
  while (ch !== 0) {
6242
6309
  this.skipSeparationSpace(true, -1);
6243
- ch = this.peek();
6310
+ ch = this.#scanner.peek();
6244
6311
  if (this.lineIndent > 0 || ch !== PERCENT) {
6245
6312
  break;
6246
6313
  }
6247
6314
  hasDirectives = true;
6248
- ch = this.next();
6249
- let position = this.position;
6315
+ this.#scanner.next();
6316
+ ch = this.#scanner.peek();
6317
+ let position = this.#scanner.position;
6250
6318
  while (ch !== 0 && !isWhiteSpaceOrEOL(ch)) {
6251
- ch = this.next();
6319
+ this.#scanner.next();
6320
+ ch = this.#scanner.peek();
6252
6321
  }
6253
- const directiveName = this.input.slice(position, this.position);
6322
+ const directiveName = this.#scanner.source.slice(position, this.#scanner.position);
6254
6323
  const directiveArgs = [];
6255
6324
  if (directiveName.length < 1) {
6256
6325
  throw this.#createError("Cannot read document: directive name length must be greater than zero");
@@ -6258,13 +6327,14 @@ var LoaderState = class {
6258
6327
  while (ch !== 0) {
6259
6328
  this.skipWhitespaces();
6260
6329
  this.skipComment();
6261
- ch = this.peek();
6330
+ ch = this.#scanner.peek();
6262
6331
  if (isEOL(ch)) break;
6263
- position = this.position;
6332
+ position = this.#scanner.position;
6264
6333
  while (ch !== 0 && !isWhiteSpaceOrEOL(ch)) {
6265
- ch = this.next();
6334
+ this.#scanner.next();
6335
+ ch = this.#scanner.peek();
6266
6336
  }
6267
- directiveArgs.push(this.input.slice(position, this.position));
6337
+ directiveArgs.push(this.#scanner.source.slice(position, this.#scanner.position));
6268
6338
  }
6269
6339
  if (ch !== 0) this.readLineBreak();
6270
6340
  switch (directiveName) {
@@ -6281,20 +6351,20 @@ var LoaderState = class {
6281
6351
  this.dispatchWarning(`unknown document directive "${directiveName}"`);
6282
6352
  break;
6283
6353
  }
6284
- ch = this.peek();
6354
+ ch = this.#scanner.peek();
6285
6355
  }
6286
6356
  return hasDirectives;
6287
6357
  }
6288
6358
  readDocument() {
6289
- const documentStart = this.position;
6359
+ const documentStart = this.#scanner.position;
6290
6360
  this.checkLineBreaks = false;
6291
6361
  this.tagMap = /* @__PURE__ */ new Map();
6292
6362
  this.anchorMap = /* @__PURE__ */ new Map();
6293
6363
  const hasDirectives = this.readDirectives();
6294
6364
  this.skipSeparationSpace(true, -1);
6295
6365
  let result = null;
6296
- if (this.lineIndent === 0 && this.peek() === MINUS && this.peek(1) === MINUS && this.peek(2) === MINUS) {
6297
- this.position += 3;
6366
+ if (this.lineIndent === 0 && this.#scanner.peek() === MINUS && this.#scanner.peek(1) === MINUS && this.#scanner.peek(2) === MINUS) {
6367
+ this.#scanner.position += 3;
6298
6368
  this.skipSeparationSpace(true, -1);
6299
6369
  } else if (hasDirectives) {
6300
6370
  throw this.#createError("Cannot read document: directives end mark is expected");
@@ -6307,21 +6377,21 @@ var LoaderState = class {
6307
6377
  });
6308
6378
  if (newState) result = newState.result;
6309
6379
  this.skipSeparationSpace(true, -1);
6310
- if (this.checkLineBreaks && PATTERN_NON_ASCII_LINE_BREAKS.test(this.input.slice(documentStart, this.position))) {
6380
+ if (this.checkLineBreaks && PATTERN_NON_ASCII_LINE_BREAKS.test(this.#scanner.source.slice(documentStart, this.#scanner.position))) {
6311
6381
  this.dispatchWarning("non-ASCII line breaks are interpreted as content");
6312
6382
  }
6313
- if (this.position === this.lineStart && this.testDocumentSeparator()) {
6314
- if (this.peek() === DOT) {
6315
- this.position += 3;
6383
+ if (this.#scanner.position === this.lineStart && this.testDocumentSeparator()) {
6384
+ if (this.#scanner.peek() === DOT) {
6385
+ this.#scanner.position += 3;
6316
6386
  this.skipSeparationSpace(true, -1);
6317
6387
  }
6318
- } else if (this.position < this.length - 1) {
6388
+ } else if (!this.#scanner.eof()) {
6319
6389
  throw this.#createError("Cannot read document: end of the stream or a document separator is expected");
6320
6390
  }
6321
6391
  return result;
6322
6392
  }
6323
6393
  *readDocuments() {
6324
- while (this.position < this.length - 1) {
6394
+ while (!this.#scanner.eof()) {
6325
6395
  yield this.readDocument();
6326
6396
  }
6327
6397
  }
@@ -6334,7 +6404,6 @@ function sanitizeInput(input) {
6334
6404
  if (!isEOL(input.charCodeAt(input.length - 1))) input += "\n";
6335
6405
  if (input.charCodeAt(0) === 65279) input = input.slice(1);
6336
6406
  }
6337
- input += "\0";
6338
6407
  return input;
6339
6408
  }
6340
6409
  function parse(content, options = {}) {
@@ -6494,7 +6563,7 @@ function getDefaultAgents() {
6494
6563
  }
6495
6564
 
6496
6565
  // __mcpc__cli_latest/node_modules/@mcpc/cli/src/config/loader.js
6497
- var CLI_VERSION = "0.1.44";
6566
+ var CLI_VERSION = "0.1.51";
6498
6567
  function extractServerName(command, commandArgs) {
6499
6568
  for (const arg of commandArgs) {
6500
6569
  if (!arg.startsWith("-")) {
@@ -8175,6 +8244,147 @@ var ExperimentalServerTasks = class {
8175
8244
  requestStream(request, resultSchema, options) {
8176
8245
  return this._server.requestStream(request, resultSchema, options);
8177
8246
  }
8247
+ /**
8248
+ * Sends a sampling request and returns an AsyncGenerator that yields response messages.
8249
+ * The generator is guaranteed to end with either a 'result' or 'error' message.
8250
+ *
8251
+ * For task-augmented requests, yields 'taskCreated' and 'taskStatus' messages
8252
+ * before the final result.
8253
+ *
8254
+ * @example
8255
+ * ```typescript
8256
+ * const stream = server.experimental.tasks.createMessageStream({
8257
+ * messages: [{ role: 'user', content: { type: 'text', text: 'Hello' } }],
8258
+ * maxTokens: 100
8259
+ * }, {
8260
+ * onprogress: (progress) => {
8261
+ * // Handle streaming tokens via progress notifications
8262
+ * console.log('Progress:', progress.message);
8263
+ * }
8264
+ * });
8265
+ *
8266
+ * for await (const message of stream) {
8267
+ * switch (message.type) {
8268
+ * case 'taskCreated':
8269
+ * console.log('Task created:', message.task.taskId);
8270
+ * break;
8271
+ * case 'taskStatus':
8272
+ * console.log('Task status:', message.task.status);
8273
+ * break;
8274
+ * case 'result':
8275
+ * console.log('Final result:', message.result);
8276
+ * break;
8277
+ * case 'error':
8278
+ * console.error('Error:', message.error);
8279
+ * break;
8280
+ * }
8281
+ * }
8282
+ * ```
8283
+ *
8284
+ * @param params - The sampling request parameters
8285
+ * @param options - Optional request options (timeout, signal, task creation params, onprogress, etc.)
8286
+ * @returns AsyncGenerator that yields ResponseMessage objects
8287
+ *
8288
+ * @experimental
8289
+ */
8290
+ createMessageStream(params, options) {
8291
+ const clientCapabilities = this._server.getClientCapabilities();
8292
+ if ((params.tools || params.toolChoice) && !clientCapabilities?.sampling?.tools) {
8293
+ throw new Error("Client does not support sampling tools capability.");
8294
+ }
8295
+ if (params.messages.length > 0) {
8296
+ const lastMessage = params.messages[params.messages.length - 1];
8297
+ const lastContent = Array.isArray(lastMessage.content) ? lastMessage.content : [lastMessage.content];
8298
+ const hasToolResults = lastContent.some((c) => c.type === "tool_result");
8299
+ const previousMessage = params.messages.length > 1 ? params.messages[params.messages.length - 2] : void 0;
8300
+ const previousContent = previousMessage ? Array.isArray(previousMessage.content) ? previousMessage.content : [previousMessage.content] : [];
8301
+ const hasPreviousToolUse = previousContent.some((c) => c.type === "tool_use");
8302
+ if (hasToolResults) {
8303
+ if (lastContent.some((c) => c.type !== "tool_result")) {
8304
+ throw new Error("The last message must contain only tool_result content if any is present");
8305
+ }
8306
+ if (!hasPreviousToolUse) {
8307
+ throw new Error("tool_result blocks are not matching any tool_use from the previous message");
8308
+ }
8309
+ }
8310
+ if (hasPreviousToolUse) {
8311
+ const toolUseIds = new Set(previousContent.filter((c) => c.type === "tool_use").map((c) => c.id));
8312
+ const toolResultIds = new Set(lastContent.filter((c) => c.type === "tool_result").map((c) => c.toolUseId));
8313
+ if (toolUseIds.size !== toolResultIds.size || ![...toolUseIds].every((id) => toolResultIds.has(id))) {
8314
+ throw new Error("ids of tool_result blocks and tool_use blocks from previous message do not match");
8315
+ }
8316
+ }
8317
+ }
8318
+ return this.requestStream({
8319
+ method: "sampling/createMessage",
8320
+ params
8321
+ }, CreateMessageResultSchema, options);
8322
+ }
8323
+ /**
8324
+ * Sends an elicitation request and returns an AsyncGenerator that yields response messages.
8325
+ * The generator is guaranteed to end with either a 'result' or 'error' message.
8326
+ *
8327
+ * For task-augmented requests (especially URL-based elicitation), yields 'taskCreated'
8328
+ * and 'taskStatus' messages before the final result.
8329
+ *
8330
+ * @example
8331
+ * ```typescript
8332
+ * const stream = server.experimental.tasks.elicitInputStream({
8333
+ * mode: 'url',
8334
+ * message: 'Please authenticate',
8335
+ * elicitationId: 'auth-123',
8336
+ * url: 'https://example.com/auth'
8337
+ * }, {
8338
+ * task: { ttl: 300000 } // Task-augmented for long-running auth flow
8339
+ * });
8340
+ *
8341
+ * for await (const message of stream) {
8342
+ * switch (message.type) {
8343
+ * case 'taskCreated':
8344
+ * console.log('Task created:', message.task.taskId);
8345
+ * break;
8346
+ * case 'taskStatus':
8347
+ * console.log('Task status:', message.task.status);
8348
+ * break;
8349
+ * case 'result':
8350
+ * console.log('User action:', message.result.action);
8351
+ * break;
8352
+ * case 'error':
8353
+ * console.error('Error:', message.error);
8354
+ * break;
8355
+ * }
8356
+ * }
8357
+ * ```
8358
+ *
8359
+ * @param params - The elicitation request parameters
8360
+ * @param options - Optional request options (timeout, signal, task creation params, etc.)
8361
+ * @returns AsyncGenerator that yields ResponseMessage objects
8362
+ *
8363
+ * @experimental
8364
+ */
8365
+ elicitInputStream(params, options) {
8366
+ const clientCapabilities = this._server.getClientCapabilities();
8367
+ const mode = params.mode ?? "form";
8368
+ switch (mode) {
8369
+ case "url": {
8370
+ if (!clientCapabilities?.elicitation?.url) {
8371
+ throw new Error("Client does not support url elicitation.");
8372
+ }
8373
+ break;
8374
+ }
8375
+ case "form": {
8376
+ if (!clientCapabilities?.elicitation?.form) {
8377
+ throw new Error("Client does not support form elicitation.");
8378
+ }
8379
+ break;
8380
+ }
8381
+ }
8382
+ const normalizedParams = mode === "form" && params.mode === void 0 ? { ...params, mode: "form" } : params;
8383
+ return this.requestStream({
8384
+ method: "elicitation/create",
8385
+ params: normalizedParams
8386
+ }, ElicitResultSchema, options);
8387
+ }
8178
8388
  /**
8179
8389
  * Gets the current status of a task.
8180
8390
  *
@@ -10393,22 +10603,45 @@ async function auth(provider, options) {
10393
10603
  }
10394
10604
  }
10395
10605
  async function authInternal(provider, { serverUrl, authorizationCode, scope, resourceMetadataUrl, fetchFn }) {
10606
+ const cachedState = await provider.discoveryState?.();
10396
10607
  let resourceMetadata;
10397
10608
  let authorizationServerUrl;
10398
- try {
10399
- resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl }, fetchFn);
10400
- if (resourceMetadata.authorization_servers && resourceMetadata.authorization_servers.length > 0) {
10401
- authorizationServerUrl = resourceMetadata.authorization_servers[0];
10609
+ let metadata;
10610
+ let effectiveResourceMetadataUrl = resourceMetadataUrl;
10611
+ if (!effectiveResourceMetadataUrl && cachedState?.resourceMetadataUrl) {
10612
+ effectiveResourceMetadataUrl = new URL(cachedState.resourceMetadataUrl);
10613
+ }
10614
+ if (cachedState?.authorizationServerUrl) {
10615
+ authorizationServerUrl = cachedState.authorizationServerUrl;
10616
+ resourceMetadata = cachedState.resourceMetadata;
10617
+ metadata = cachedState.authorizationServerMetadata ?? await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn });
10618
+ if (!resourceMetadata) {
10619
+ try {
10620
+ resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl: effectiveResourceMetadataUrl }, fetchFn);
10621
+ } catch {
10622
+ }
10402
10623
  }
10403
- } catch {
10404
- }
10405
- if (!authorizationServerUrl) {
10406
- authorizationServerUrl = new URL("/", serverUrl);
10624
+ if (metadata !== cachedState.authorizationServerMetadata || resourceMetadata !== cachedState.resourceMetadata) {
10625
+ await provider.saveDiscoveryState?.({
10626
+ authorizationServerUrl: String(authorizationServerUrl),
10627
+ resourceMetadataUrl: effectiveResourceMetadataUrl?.toString(),
10628
+ resourceMetadata,
10629
+ authorizationServerMetadata: metadata
10630
+ });
10631
+ }
10632
+ } else {
10633
+ const serverInfo = await discoverOAuthServerInfo(serverUrl, { resourceMetadataUrl: effectiveResourceMetadataUrl, fetchFn });
10634
+ authorizationServerUrl = serverInfo.authorizationServerUrl;
10635
+ metadata = serverInfo.authorizationServerMetadata;
10636
+ resourceMetadata = serverInfo.resourceMetadata;
10637
+ await provider.saveDiscoveryState?.({
10638
+ authorizationServerUrl: String(authorizationServerUrl),
10639
+ resourceMetadataUrl: effectiveResourceMetadataUrl?.toString(),
10640
+ resourceMetadata,
10641
+ authorizationServerMetadata: metadata
10642
+ });
10407
10643
  }
10408
10644
  const resource = await selectResourceURL(serverUrl, provider, resourceMetadata);
10409
- const metadata = await discoverAuthorizationServerMetadata(authorizationServerUrl, {
10410
- fetchFn
10411
- });
10412
10645
  let clientInformation = await Promise.resolve(provider.clientInformation());
10413
10646
  if (!clientInformation) {
10414
10647
  if (authorizationCode !== void 0) {
@@ -10663,6 +10896,26 @@ async function discoverAuthorizationServerMetadata(authorizationServerUrl, { fet
10663
10896
  }
10664
10897
  return void 0;
10665
10898
  }
10899
+ async function discoverOAuthServerInfo(serverUrl, opts) {
10900
+ let resourceMetadata;
10901
+ let authorizationServerUrl;
10902
+ try {
10903
+ resourceMetadata = await discoverOAuthProtectedResourceMetadata(serverUrl, { resourceMetadataUrl: opts?.resourceMetadataUrl }, opts?.fetchFn);
10904
+ if (resourceMetadata.authorization_servers && resourceMetadata.authorization_servers.length > 0) {
10905
+ authorizationServerUrl = resourceMetadata.authorization_servers[0];
10906
+ }
10907
+ } catch {
10908
+ }
10909
+ if (!authorizationServerUrl) {
10910
+ authorizationServerUrl = String(new URL("/", serverUrl));
10911
+ }
10912
+ const authorizationServerMetadata = await discoverAuthorizationServerMetadata(authorizationServerUrl, { fetchFn: opts?.fetchFn });
10913
+ return {
10914
+ authorizationServerUrl,
10915
+ authorizationServerMetadata,
10916
+ resourceMetadata
10917
+ };
10918
+ }
10666
10919
  async function startAuthorization(authorizationServerUrl, { metadata, clientInformation, redirectUrl, scope, state, resource }) {
10667
10920
  let authorizationUrl;
10668
10921
  if (metadata) {
@@ -11527,18 +11780,6 @@ var cleanToolSchema = (schema) => {
11527
11780
  var import_node_process6 = require("node:process");
11528
11781
  var import_node_process7 = __toESM(require("node:process"), 1);
11529
11782
  var import_node_crypto = require("node:crypto");
11530
- var mcpClientPool = /* @__PURE__ */ new Map();
11531
- var mcpClientConnecting = /* @__PURE__ */ new Map();
11532
- var shortHash = (s) => (0, import_node_crypto.createHash)("sha256").update(s).digest("hex").slice(0, 8);
11533
- function defSignature(def) {
11534
- const defCopy = {
11535
- ...def
11536
- };
11537
- if (defCopy.transportType === "memory" || defCopy.transport) {
11538
- return `memory:${Date.now()}:${Math.random()}`;
11539
- }
11540
- return JSON.stringify(defCopy);
11541
- }
11542
11783
  function createTransport(def) {
11543
11784
  const defAny = def;
11544
11785
  const explicitType = defAny.transportType || defAny.type;
@@ -11586,90 +11827,43 @@ function createTransport(def) {
11586
11827
  }
11587
11828
  throw new Error(`Unsupported transport configuration: ${JSON.stringify(def)}`);
11588
11829
  }
11589
- async function getOrCreateMcpClient(defKey, def) {
11590
- const pooled = mcpClientPool.get(defKey);
11591
- if (pooled) {
11592
- pooled.refCount += 1;
11593
- return pooled.client;
11594
- }
11595
- const existingConnecting = mcpClientConnecting.get(defKey);
11596
- if (existingConnecting) {
11597
- const client = await existingConnecting;
11598
- const entry = mcpClientPool.get(defKey);
11599
- if (entry) entry.refCount += 1;
11600
- return client;
11601
- }
11602
- const transport = createTransport(def);
11603
- const connecting = (async () => {
11604
- const client = new Client({
11605
- name: `mcp_${shortHash(defSignature(def))}`,
11606
- version: "1.0.0"
11607
- });
11608
- await client.connect(transport, {
11609
- timeout: 6e4 * 10
11610
- });
11611
- return client;
11612
- })();
11613
- mcpClientConnecting.set(defKey, connecting);
11614
- try {
11615
- const client = await connecting;
11616
- mcpClientPool.set(defKey, {
11617
- client,
11618
- refCount: 1
11619
- });
11620
- return client;
11621
- } finally {
11622
- mcpClientConnecting.delete(defKey);
11830
+ function defSignature(def) {
11831
+ const defCopy = {
11832
+ ...def
11833
+ };
11834
+ if (defCopy.transportType === "memory" || defCopy.transport) {
11835
+ return `memory:${Date.now()}:${Math.random()}`;
11623
11836
  }
11837
+ return JSON.stringify(defCopy);
11624
11838
  }
11625
- async function releaseMcpClient(defKey) {
11626
- const entry = mcpClientPool.get(defKey);
11627
- if (!entry) return;
11628
- entry.refCount -= 1;
11629
- if (entry.refCount <= 0) {
11630
- mcpClientPool.delete(defKey);
11631
- try {
11632
- await entry.client.close();
11633
- } catch (err) {
11634
- console.error("Error closing MCP client:", err);
11635
- }
11636
- }
11839
+ var shortHash = (s) => (0, import_node_crypto.createHash)("sha256").update(s).digest("hex").slice(0, 8);
11840
+ async function createMcpClient(def) {
11841
+ const transport = createTransport(def);
11842
+ const client = new Client({
11843
+ name: `mcp_${shortHash(defSignature(def))}`,
11844
+ version: "1.0.0"
11845
+ });
11846
+ await client.connect(transport, {
11847
+ timeout: 6e4 * 10
11848
+ });
11849
+ return client;
11637
11850
  }
11638
- var cleanupAllPooledClients = async () => {
11639
- const entries = Array.from(mcpClientPool.entries());
11640
- mcpClientPool.clear();
11641
- await Promise.all(entries.map(async ([, { client }]) => {
11642
- try {
11643
- await client.close();
11644
- } catch (err) {
11645
- console.error("Error closing MCP client:", err);
11646
- }
11647
- }));
11648
- };
11649
- import_node_process7.default.once?.("exit", () => {
11650
- cleanupAllPooledClients();
11651
- });
11652
- import_node_process7.default.once?.("SIGINT", () => {
11653
- cleanupAllPooledClients().finally(() => import_node_process7.default.exit(0));
11654
- });
11655
11851
  async function composeMcpDepTools(mcpConfig, filterIn) {
11656
11852
  const allTools = {};
11657
11853
  const allClients = {};
11658
- const acquiredKeys = [];
11854
+ const clientsToClose = [];
11659
11855
  for (const [name, definition] of Object.entries(mcpConfig.mcpServers)) {
11660
11856
  const def = definition;
11661
11857
  if (def.disabled) continue;
11662
- const defKey = shortHash(defSignature(def));
11663
- const serverId = name;
11664
11858
  try {
11665
- const client = await getOrCreateMcpClient(defKey, def);
11666
- acquiredKeys.push(defKey);
11667
- allClients[serverId] = client;
11859
+ const client = await createMcpClient(def);
11860
+ clientsToClose.push(client);
11861
+ allClients[name] = client;
11668
11862
  const { tools } = await client.listTools();
11669
11863
  tools.forEach((tool2) => {
11670
11864
  const toolNameWithScope = `${name}.${tool2.name}`;
11671
11865
  const internalToolName = tool2.name;
11672
- const rawToolId = `${serverId}_${internalToolName}`;
11866
+ const rawToolId = `${name}_${internalToolName}`;
11673
11867
  const toolId = sanitizePropertyKey(rawToolId);
11674
11868
  if (filterIn && !filterIn({
11675
11869
  action: internalToolName,
@@ -11681,7 +11875,7 @@ async function composeMcpDepTools(mcpConfig, filterIn) {
11681
11875
  })) {
11682
11876
  return;
11683
11877
  }
11684
- const execute = (args) => allClients[serverId].callTool({
11878
+ const execute = (args) => allClients[name].callTool({
11685
11879
  name: internalToolName,
11686
11880
  arguments: args
11687
11881
  }, void 0, {
@@ -11698,10 +11892,12 @@ async function composeMcpDepTools(mcpConfig, filterIn) {
11698
11892
  }
11699
11893
  }
11700
11894
  const cleanupClients = async () => {
11701
- await Promise.all(acquiredKeys.map((k) => releaseMcpClient(k)));
11702
- acquiredKeys.length = 0;
11703
- Object.keys(allTools).forEach((key) => delete allTools[key]);
11704
- Object.keys(allClients).forEach((key) => delete allClients[key]);
11895
+ await Promise.all(clientsToClose.map((client) => {
11896
+ try {
11897
+ return client.close();
11898
+ } catch {
11899
+ }
11900
+ }));
11705
11901
  };
11706
11902
  return {
11707
11903
  tools: allTools,