@qwik.dev/mcp 2.0.0-rc.2 → 2.0.0-rc.4

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.js CHANGED
@@ -118,6 +118,19 @@ var ALL_HOOK_NAMES = /* @__PURE__ */ new Set([...USE_HOOK_LIST]);
118
118
  function isAstNodeLike(value) {
119
119
  return Boolean(value) && typeof value === "object" && "type" in value;
120
120
  }
121
+ function getNodeStart(node) {
122
+ if (node && typeof node === "object") {
123
+ const maybeRange = node.range;
124
+ if (Array.isArray(maybeRange)) {
125
+ return maybeRange[0] ?? 0;
126
+ }
127
+ const maybeStart = node.start;
128
+ if (typeof maybeStart === "number") {
129
+ return maybeStart;
130
+ }
131
+ }
132
+ return 0;
133
+ }
121
134
  function getVariableIdentifierName(id) {
122
135
  if (!isAstNodeLike(id)) {
123
136
  return null;
@@ -137,41 +150,11 @@ function isCustomHook(hookName) {
137
150
  const isBuiltIn = USE_HOOK_LIST.some((item) => item.startsWith(hookName));
138
151
  return !isBuiltIn && /^use[A-Z_]/.test(hookName);
139
152
  }
140
- function findLineStart(code, index) {
141
- for (let i = index - 1; i >= 0; i--) {
142
- const ch = code[i];
143
- if (ch === "\n" || ch === "\r") {
144
- return i + 1;
145
- }
146
- }
147
- return 0;
148
- }
149
- function readIndent(code, indexFrom) {
150
- let indent = "";
151
- let i = indexFrom;
152
- while (i < code.length) {
153
- const ch = code[i];
154
- if (ch === " " || ch === " ") {
155
- indent += ch;
156
- i++;
157
- } else {
158
- break;
159
- }
160
- }
161
- return indent;
162
- }
163
153
  function trimStatementSemicolon(segment) {
164
154
  return segment.trim().replace(/;?\s*$/, "");
165
155
  }
166
- function buildCollecthookPayload(indent, variableName, hookType, category, hookExpression) {
167
- const dataValue = hookExpression === "undefined" ? "undefined" : hookExpression;
168
- return `${indent}collecthook({
169
- ${indent} variableName: '${variableName}',
170
- ${indent} hookType: '${hookType}',
171
- ${indent} category: '${category}',
172
- ${indent} data: ${dataValue}
173
- ${indent}});
174
- `;
156
+ function buildCollecthookPayload(variableName, hookType, category, hookExpression) {
157
+ return `collecthook({ variableName: '${variableName}', hookType: '${hookType}', category: '${category}', data: ${hookExpression} });`;
175
158
  }
176
159
  function hasCollecthookAfterByVariableId(code, fromIndex, variableId, maxLookahead = 600) {
177
160
  const lookahead = code.slice(fromIndex, fromIndex + maxLookahead);
@@ -274,6 +257,9 @@ function findAllComponentBodyRangesFromProgram(program) {
274
257
  });
275
258
  return deduplicateRanges(ranges);
276
259
  }
260
+ function isInsideComponentBody(bodies, position) {
261
+ return bodies.some((body) => body.bodyStart < position && position < body.bodyEnd);
262
+ }
277
263
  function getComponentBodyRange(node, parent) {
278
264
  if (!isComponentCall(node)) {
279
265
  return null;
@@ -363,26 +349,14 @@ function createInitTask(code, body, options) {
363
349
  if (hasExistingCollecthook(code, insertPos)) {
364
350
  return null;
365
351
  }
366
- const { insertIndex, prefixNewline } = calculateInsertPosition(code, insertPos);
367
- const indent = readIndent(code, insertIndex);
368
352
  const componentArg = buildComponentArg(options?.path, exportName);
369
- const initLine = `${prefixNewline}${indent}const collecthook = ${INNER_USE_HOOK}(${componentArg})
370
- `;
371
- return { kind: "insert", pos: insertIndex, text: initLine };
353
+ const initStatement = ` const collecthook = ${INNER_USE_HOOK}(${componentArg});`;
354
+ return { kind: "insert", pos: insertPos, text: initStatement };
372
355
  }
373
356
  function hasExistingCollecthook(code, insertPos) {
374
357
  const lookahead = code.slice(insertPos, insertPos + 200);
375
358
  return /const\s+collecthook\s*=\s*useCollectHooks\s*\(/.test(lookahead);
376
359
  }
377
- function calculateInsertPosition(code, insertPos) {
378
- if (code[insertPos] === "\r" && code[insertPos + 1] === "\n") {
379
- return { insertIndex: insertPos + 2, prefixNewline: "" };
380
- }
381
- if (code[insertPos] === "\n") {
382
- return { insertIndex: insertPos + 1, prefixNewline: "" };
383
- }
384
- return { insertIndex: insertPos, prefixNewline: "\n" };
385
- }
386
360
  function buildComponentArg(path, exportName) {
387
361
  const rawArg = String(path ?? "");
388
362
  const baseArg = rawArg.split("?")[0].split("#")[0];
@@ -409,12 +383,13 @@ function buildComponentSuffix(baseArg, exportName) {
409
383
  // ../devtools/plugin/src/parse/hookTracker.ts
410
384
  function injectHookTrackers(code) {
411
385
  const program = parseProgram(code);
386
+ const componentBodies = findAllComponentBodyRangesFromProgram(program);
412
387
  const tasks = [];
413
388
  let customHookIndex = 0;
414
389
  traverseProgram(program, {
415
390
  enter: (path) => {
416
391
  const node = path.node;
417
- if (!node) {
392
+ if (!node || !isInsideComponentBody(componentBodies, getNodeStart(node))) {
418
393
  return;
419
394
  }
420
395
  if (node.type === "VariableDeclarator") {
@@ -447,19 +422,18 @@ function processVariableDeclarator(code, node, path) {
447
422
  if (!range) {
448
423
  return null;
449
424
  }
450
- const { declEnd, indent } = getPositionInfo(code, range);
425
+ const [, declEnd] = range;
451
426
  if (isCustomHook(normalizedName)) {
452
427
  if (hasCollecthookAfterByVariableId(code, declEnd, variableId)) {
453
428
  return null;
454
429
  }
455
430
  const payload2 = buildCollecthookPayload(
456
- indent,
457
431
  variableId,
458
432
  "customhook",
459
433
  "VariableDeclarator",
460
434
  variableId
461
435
  );
462
- return { kind: "insert", pos: declEnd, text: "\n" + payload2 };
436
+ return insertAfterStatement(code, declEnd, payload2);
463
437
  }
464
438
  if (!isKnownHook(normalizedName)) {
465
439
  return null;
@@ -468,13 +442,12 @@ function processVariableDeclarator(code, node, path) {
468
442
  return null;
469
443
  }
470
444
  const payload = buildCollecthookPayload(
471
- indent,
472
445
  variableId,
473
446
  normalizedName,
474
447
  "VariableDeclarator",
475
448
  variableId
476
449
  );
477
- return { kind: "insert", pos: declEnd, text: "\n" + payload };
450
+ return insertAfterStatement(code, declEnd, payload);
478
451
  }
479
452
  function processExpressionStatement(code, node, currentIndex) {
480
453
  const hookInfo = extractExpressionHookInfo(node);
@@ -490,36 +463,30 @@ function processExpressionStatement(code, node, currentIndex) {
490
463
  return null;
491
464
  }
492
465
  const [stmtStart, stmtEnd] = stmtRange;
493
- const lineStart = findLineStart(code, stmtStart);
494
- const indent = readIndent(code, lineStart);
495
466
  if (isKnownHook(normalizedName)) {
496
467
  if (hasCollecthookAfterByVariableName(code, stmtEnd, normalizedName)) {
497
468
  return null;
498
469
  }
499
470
  const payload = buildCollecthookPayload(
500
- indent,
501
471
  normalizedName,
502
472
  normalizedName,
503
473
  "expressionStatement",
504
474
  "undefined"
505
475
  );
506
476
  return {
507
- task: { kind: "insert", pos: stmtEnd, text: "\n" + payload },
477
+ task: insertAfterStatement(code, stmtEnd, payload),
508
478
  newIndex: currentIndex
509
479
  };
510
480
  }
511
481
  if (isCustomHook(normalizedName)) {
512
- return convertToVariableDeclaration(code, stmtStart, stmtEnd, indent, currentIndex);
482
+ return convertToVariableDeclaration(code, stmtStart, stmtEnd, currentIndex);
513
483
  }
514
484
  return null;
515
485
  }
516
- function convertToVariableDeclaration(code, stmtStart, stmtEnd, indent, currentIndex) {
486
+ function convertToVariableDeclaration(code, stmtStart, stmtEnd, currentIndex) {
517
487
  const callSource = code.slice(stmtStart, stmtEnd);
518
488
  const variableName = `_customhook_${currentIndex}`;
519
- const declLine = `${indent}let ${variableName} = ${trimStatementSemicolon(callSource)};
520
- `;
521
489
  const payload = buildCollecthookPayload(
522
- indent,
523
490
  variableName,
524
491
  "customhook",
525
492
  "VariableDeclarator",
@@ -530,7 +497,7 @@ function convertToVariableDeclaration(code, stmtStart, stmtEnd, indent, currentI
530
497
  kind: "replace",
531
498
  start: stmtStart,
532
499
  end: stmtEnd,
533
- text: declLine + payload
500
+ text: `let ${variableName} = ${trimStatementSemicolon(callSource)}; ${payload}`
534
501
  },
535
502
  newIndex: currentIndex + 1
536
503
  };
@@ -563,6 +530,10 @@ function extractHookCall(node) {
563
530
  normalizedName: normalizeQrlHookName(hookName)
564
531
  };
565
532
  }
533
+ function insertAfterStatement(code, statementEnd, statement) {
534
+ const separator = code[statementEnd - 1] === ";" ? " " : "; ";
535
+ return { kind: "insert", pos: statementEnd, text: separator + statement };
536
+ }
566
537
  function getParentRange(parent) {
567
538
  const range = parent?.range;
568
539
  if (!range) {
@@ -570,12 +541,6 @@ function getParentRange(parent) {
570
541
  }
571
542
  return [range[0], range[1]];
572
543
  }
573
- function getPositionInfo(code, range) {
574
- const [declStart, declEnd] = range;
575
- const lineStart = findLineStart(code, declStart);
576
- const indent = readIndent(code, lineStart);
577
- return { declStart, declEnd, indent };
578
- }
579
544
 
580
545
  // ../devtools/plugin/src/parse/parse.ts
581
546
  function parseQwikCode(code, options) {
@@ -590,12 +555,14 @@ function parseQwikCode(code, options) {
590
555
  }
591
556
 
592
557
  // ../devtools/plugin/src/transforms/source-utils.ts
558
+ function prependImportIfMissing(code, importStatement) {
559
+ return code.includes(importStatement) ? code : `${importStatement}${code}`;
560
+ }
593
561
  function injectNamedImportIfMissing(code, key, name) {
594
562
  if (code.includes(key)) {
595
563
  return code;
596
564
  }
597
- return `import { ${name} } from '${key}';
598
- ${code}`;
565
+ return prependImportIfMissing(code, `import { ${name} } from '${key}';`);
599
566
  }
600
567
 
601
568
  // ../devtools/plugin/src/transforms/component-transform.ts
@@ -608,12 +575,85 @@ function transformComponentFile(code, id) {
608
575
  return parseQwikCode(codeWithCollectorImport, { path: id });
609
576
  }
610
577
 
578
+ // ../devtools/plugin/src/runtime/create-runtime-module.ts
579
+ function createRuntimeModule(parts) {
580
+ return parts.filter((part) => typeof part === "string" && part.length > 0).map((part) => part.trim()).join("\n\n") + "\n";
581
+ }
582
+ function createRuntimeInstallerSource(installer) {
583
+ return installer.toString();
584
+ }
585
+ function createRuntimeCall(name, args) {
586
+ return `${name}(${args.map(serializeRuntimeValue).join(", ")});`;
587
+ }
588
+ function runtimeExpression(source) {
589
+ return { __runtimeExpression: source };
590
+ }
591
+ function createRuntimeVar(name, value) {
592
+ return `var ${name} = ${JSON.stringify(value)};`;
593
+ }
594
+ function serializeRuntimeValue(value) {
595
+ if (isRuntimeExpression(value)) {
596
+ return value.__runtimeExpression;
597
+ }
598
+ if (Array.isArray(value)) {
599
+ return `[${value.map(serializeRuntimeValue).join(", ")}]`;
600
+ }
601
+ if (value && typeof value === "object") {
602
+ return `{ ${Object.entries(value).map(([key, item]) => `${JSON.stringify(key)}: ${serializeRuntimeValue(item)}`).join(", ")} }`;
603
+ }
604
+ return JSON.stringify(value);
605
+ }
606
+ function isRuntimeExpression(value) {
607
+ return Boolean(value) && typeof value === "object" && typeof value.__runtimeExpression === "string";
608
+ }
609
+
611
610
  // ../devtools/plugin/src/virtualmodules/useCollectHooks.ts
612
611
  var globalKey = JSON.stringify(QWIK_DEVTOOLS_GLOBAL.key);
613
612
  var globalVersion = JSON.stringify(QWIK_DEVTOOLS_GLOBAL.version);
614
613
  var componentStateKey = JSON.stringify(QWIK_DEVTOOLS_GLOBAL.props.componentState);
615
- var useCollectHooks = `import { $, useSignal, useVisibleTask$ } from "@qwik.dev/core"
616
- import { _getContextContainer, _getContextHostElement } from "@qwik.dev/core/internal"
614
+ function __qwik_is_serializable_hook_value__(value, isReactive, seen = /* @__PURE__ */ new Set()) {
615
+ if (typeof value === "function") {
616
+ return typeof value.getSymbol === "function";
617
+ }
618
+ if (typeof value !== "object" || value === null) {
619
+ return typeof value !== "symbol";
620
+ }
621
+ if (isReactive(value) || seen.has(value)) {
622
+ return true;
623
+ }
624
+ seen.add(value);
625
+ if (Array.isArray(value)) {
626
+ for (let i = 0; i < value.length; i++) {
627
+ if (!(i in value) || !__qwik_is_serializable_hook_value__(value[i], isReactive, seen)) {
628
+ return false;
629
+ }
630
+ }
631
+ return true;
632
+ }
633
+ if (Object.getPrototypeOf(value) !== Object.prototype) {
634
+ return false;
635
+ }
636
+ for (const item of Object.values(value)) {
637
+ if (!__qwik_is_serializable_hook_value__(item, isReactive, seen)) {
638
+ return false;
639
+ }
640
+ }
641
+ return true;
642
+ }
643
+ var useCollectHooks = `import { $, isServer, isSignal, useSignal, useVisibleTask$ } from "@qwik.dev/core"
644
+ import { _getContextContainer, _getContextHostElement, _isStore } from "@qwik.dev/core/internal"
645
+
646
+ ${createRuntimeInstallerSource(__qwik_is_serializable_hook_value__)}
647
+
648
+ const isReactive = (value) => isSignal(value) || _isStore(value)
649
+
650
+ // SSR serializes the collected hooks, but custom hook values may not be serializable.
651
+ function stripUnserializableData(args) {
652
+ if (!isServer || args.hookType !== 'customhook') {
653
+ return args
654
+ }
655
+ return __qwik_is_serializable_hook_value__(args.data, isReactive) ? args : { ...args, data: undefined }
656
+ }
617
657
 
618
658
  function initComponentState() {
619
659
  return {
@@ -651,10 +691,7 @@ export const useCollectHooks = (src) => {
651
691
  }, { strategy: 'document-ready' })
652
692
 
653
693
  return $((args) => {
654
- if (hooksList.value.has(args)) {
655
- return
656
- }
657
- hooksList.value.add(args)
694
+ hooksList.value.add(stripUnserializableData(args))
658
695
  if (typeof window !== 'undefined') {
659
696
  getOrCreateState(src).hooks = [...hooksList.value]
660
697
  window[${globalKey}]?.hook?.refreshVNodeTree?.()
@@ -664,38 +701,6 @@ export const useCollectHooks = (src) => {
664
701
  `;
665
702
  var useCollectHooks_default = useCollectHooks;
666
703
 
667
- // ../devtools/plugin/src/runtime/create-runtime-module.ts
668
- function createRuntimeModule(parts) {
669
- return parts.filter((part) => typeof part === "string" && part.length > 0).map((part) => part.trim()).join("\n\n") + "\n";
670
- }
671
- function createRuntimeInstallerSource(installer) {
672
- return installer.toString();
673
- }
674
- function createRuntimeCall(name, args) {
675
- return `${name}(${args.map(serializeRuntimeValue).join(", ")});`;
676
- }
677
- function runtimeExpression(source) {
678
- return { __runtimeExpression: source };
679
- }
680
- function createRuntimeVar(name, value) {
681
- return `var ${name} = ${JSON.stringify(value)};`;
682
- }
683
- function serializeRuntimeValue(value) {
684
- if (isRuntimeExpression(value)) {
685
- return value.__runtimeExpression;
686
- }
687
- if (Array.isArray(value)) {
688
- return `[${value.map(serializeRuntimeValue).join(", ")}]`;
689
- }
690
- if (value && typeof value === "object") {
691
- return `{ ${Object.entries(value).map(([key, item]) => `${JSON.stringify(key)}: ${serializeRuntimeValue(item)}`).join(", ")} }`;
692
- }
693
- return JSON.stringify(value);
694
- }
695
- function isRuntimeExpression(value) {
696
- return Boolean(value) && typeof value === "object" && typeof value.__runtimeExpression === "string";
697
- }
698
-
699
704
  // ../devtools/plugin/src/runtime/installers.ts
700
705
  function __qwik_derive_component_name__(path) {
701
706
  const lastSeg = path.split("/").pop() || path;
@@ -1445,6 +1450,7 @@ var inspectInput = pageInput.extend({
1445
1450
  includeSerializedVNodeTree: z.boolean().default(false),
1446
1451
  offset: z.number().int().nonnegative().safe().default(0)
1447
1452
  });
1453
+ var locateInput = pageInput.extend({ selector: z.string().min(1) });
1448
1454
 
1449
1455
  // src/access.ts
1450
1456
  function isLoopback(address) {
@@ -1473,6 +1479,11 @@ function qwikMcp() {
1473
1479
  }
1474
1480
  };
1475
1481
  const router = () => server.config.plugins.find((plugin) => plugin.name === "vite-plugin-qwik-router")?.api;
1482
+ const pageToolInputs = {
1483
+ get_dev_errors: inspectInput,
1484
+ inspect_page: inspectInput,
1485
+ locate_element: locateInput
1486
+ };
1476
1487
  const call = async (name, args) => {
1477
1488
  if (name === "get_project_info") {
1478
1489
  return {
@@ -1493,10 +1504,10 @@ function qwikMcp() {
1493
1504
  })) ?? []
1494
1505
  };
1495
1506
  }
1496
- if (name !== "inspect_page" && name !== "get_dev_errors") {
1507
+ if (!Object.hasOwn(pageToolInputs, name)) {
1497
1508
  throw new Error("Unknown MCP tool.");
1498
1509
  }
1499
- args = inspectInput.parse(args);
1510
+ args = pageToolInputs[name].parse(args);
1500
1511
  const matches = [...pages.values()].filter((page) => !args.url || page.url === args.url);
1501
1512
  if (!matches.length) {
1502
1513
  throw new Error("No connected page. Open the requested Qwik URL in a local browser.");
@@ -1545,6 +1556,7 @@ function qwikMcp() {
1545
1556
  return createHookRuntime() + createVNodeRuntime() + await readFile(new URL("../dist/browser.js", import.meta.url), "utf8");
1546
1557
  }
1547
1558
  },
1559
+ // Runs before the optimizer, which has no input sourcemap: never add or remove source lines.
1548
1560
  transform(code, id) {
1549
1561
  if (/\.[jt]sx(?:\?|$)/.test(id) && !id.includes("/node_modules/") && code.includes("component$")) {
1550
1562
  return { code: transformComponentFile(code, id), map: null };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qwik.dev/mcp",
3
- "version": "2.0.0-rc.2",
3
+ "version": "2.0.0-rc.4",
4
4
  "description": "Local MCP tools for Qwik development",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -18,24 +18,24 @@
18
18
  "README.md"
19
19
  ],
20
20
  "dependencies": {
21
- "@modelcontextprotocol/server": "^2.1.0",
21
+ "@modelcontextprotocol/server": "^2.3.0",
22
22
  "oxc-parser": "^0.144.0",
23
23
  "zod": "^4.2.0"
24
24
  },
25
25
  "devDependencies": {
26
- "@modelcontextprotocol/client": "2.1.0",
26
+ "@modelcontextprotocol/client": "2.3.0",
27
27
  "@playwright/test": "1.61.1",
28
28
  "@types/node": "25.6.0",
29
29
  "esbuild": "0.28.2",
30
30
  "typescript": "5.9.3",
31
- "vite": "8.2.1",
31
+ "vite": "8.2.4",
32
32
  "vitest": "5.0.0",
33
- "@qwik.dev/core": "2.0.0-rc.2",
34
- "@qwik.dev/router": "2.0.0-rc.2"
33
+ "@qwik.dev/core": "2.0.0-rc.4",
34
+ "@qwik.dev/router": "2.0.0-rc.4"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "vite": ">=8 <9",
38
- "@qwik.dev/core": "2.0.0-rc.2"
38
+ "@qwik.dev/core": "2.0.0-rc.4"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=22.18.0"