Files
webstudio/apps/builder/app/canvas/interceptor.ts
Oleg Isonen dde30ffb54 feat: Improve MCP authoring, compact output, and agent onboarding (#5883)
## Summary

This improves how agents discover, author, inspect, and verify Webstudio
projects while reducing unnecessary MCP context usage.

## What changed

- Adds an **Agents** section to Project Settings with ready-to-run setup
commands and client configuration for Claude Code, Codex, Cursor, and VS
Code.
- Makes large list and schema responses compact and paginated by
default, with **verbose** available when complete records or diagnostics
are needed.
- Applies consistent compact output to assets, instances, styles, design
tokens, CLI schemas, and project-session metadata.
- Returns actionable expression warnings for props, resources, and
Collection bindings, including unknown variables and risky render-time
mutation resources.
- Improves Collection expression guidance so agents can bind array items
with the correct scoped variables.
- Reports rendered audit outcomes explicitly as complete, partial,
confirmation-required, or failed.
- Expands runtime output schemas so MCP clients can validate responses
reliably.
- Strengthens the packaged CLI smoke workflow with a realistic
seven-page stdio MCP scenario.

## Architecture

- Consolidates `project-build` from 65 file-oriented entrypoints to 11
architecture-owned surfaces.
- Separates the core model, contracts, runtime authoring, transfer,
state, project sessions, MCP, visual tooling, persistence codecs, and
authenticated server services.
- Isolates Node-only JSX evaluation from browser-safe transfer utilities
and authenticated server modules.
- Keeps persistence codecs independent from authenticated database
services, preventing unrelated module initialization.
- Defines shared instance-path types once and reuses them across runtime
modules.
- Preserves Immer initialization through explicit package side-effect
metadata.
- Adds package tests that prevent a return to source-file entrypoints or
generic exposure of Node-only surfaces.

## Validation

- Full repository checks, tests, typechecks, generated-file checks, and
fixtures
- All 163 Builder test files and 2,141 tests
- All 71 project-build test files and 1,794 tests
- Lint and package-boundary validation
- Full packed CLI release smoke test, including install, preview, agent
setup, seven-page stdio MCP workflow, rendered audit, and structured
commands
2026-07-15 18:11:28 +02:00

194 lines
6.3 KiB
TypeScript

import { getAllPages, getPagePath, isAbsoluteUrl } from "@webstudio-is/sdk";
import {
compilePathnamePattern,
matchPathnamePattern,
tokenizePathnamePattern,
} from "@webstudio-is/project-build/runtime";
import { $selectedPage } from "~/shared/nano-states";
import { selectPage } from "~/shared/nano-states";
import { $isPreviewMode, $selectedPageHash } from "~/shared/nano-states";
import { $pages } from "~/shared/sync/data-stores";
import { $currentSystem, updateCurrentSystem } from "~/shared/system";
import { comparePatterns } from "./shared/routing-priority";
const getSelectedPagePathname = () => {
const pages = $pages.get();
const page = $selectedPage.get();
if (page && pages) {
const tokens = tokenizePathnamePattern(getPagePath(page.id, pages));
const system = $currentSystem.get();
return compilePathnamePattern(tokens, system.params);
}
};
const switchPageAndUpdateSystem = (href: string, formData?: FormData) => {
const pages = $pages.get();
if (pages === undefined) {
return;
}
// preserve pathname when not specified in href/action
if (href === "" || href.startsWith("?")) {
const pathname = getSelectedPagePathname();
if (pathname) {
href = `${pathname}${href}`;
}
}
// preserve also search params when navigate with hash
if (href.startsWith("#")) {
const pathname = getSelectedPagePathname();
if (pathname) {
const system = $currentSystem.get();
const searchParams = new URLSearchParams(
system.search as Record<string, string>
);
href = `${pathname}?${searchParams}${href}`;
}
}
const pageHref = new URL(href, "https://any-valid.url");
// sort pages before matching to not depend on order of page creation
const sortedPages = getAllPages(pages).toSorted((leftPage, rightPage) =>
comparePatterns(leftPage.path, rightPage.path)
);
for (const page of sortedPages) {
const pagePath = getPagePath(page.id, pages);
const params = matchPathnamePattern(pagePath, pageHref.pathname);
if (params) {
// populate search params with form data values if available
if (formData) {
for (const [key, value] of formData.entries()) {
pageHref.searchParams.set(key, value.toString());
}
}
const search = Object.fromEntries(pageHref.searchParams);
$selectedPageHash.set({ hash: pageHref.hash });
selectPage(page.id);
updateCurrentSystem({ params, search });
break;
}
}
};
export const subscribeInterceptedEvents = () => {
const handleClick = (event: MouseEvent) => {
if (!(event.target instanceof Element)) {
return;
}
const isPreviewMode = $isPreviewMode.get();
// Prevent forwarding the click event on an input element when the associated label has a "for" attribute
// and prevent checkbox or radio inputs changing when clicked
if (event.target.closest("label[for]") || event.target.closest("input")) {
if (isPreviewMode) {
return;
}
event.preventDefault();
}
const a = event.target.closest("a");
if (a) {
if (isPreviewMode) {
// use attribute instead of a.href to get raw unresolved value
const href = a.getAttribute("href") ?? "";
if (isAbsoluteUrl(href)) {
window.open(href, "_blank");
// relative paths can be safely downloaded
} else if (a.hasAttribute("download")) {
return;
} else {
switchPageAndUpdateSystem(href);
}
event.preventDefault();
return;
}
event.preventDefault();
}
// prevent invoking submit with buttons in canvas mode
// because form with prevented submit still invokes validation
if (event.target.closest("button")) {
if (isPreviewMode) {
return;
}
event.preventDefault();
}
};
const handlePointerDown = (event: PointerEvent) => {
if (!(event.target instanceof Element)) {
return;
}
const isPreviewMode = $isPreviewMode.get();
if (event.target.closest("select")) {
if (isPreviewMode) {
return;
}
event.preventDefault();
}
};
const handleSubmit = (event: SubmitEvent) => {
if ($isPreviewMode.get()) {
const form =
event.target instanceof HTMLFormElement ? event.target : undefined;
if (form === undefined) {
return;
}
// use attribute instead of form.action to get raw unresolved value
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-action
const action = form.getAttribute("action") ?? "";
// lower case just for safety
const method = form.method.toLowerCase();
if (method === "get" && isAbsoluteUrl(action) === false) {
switchPageAndUpdateSystem(action, new FormData(form));
}
}
// prevent submitting the form when clicking a button type submit
event.preventDefault();
};
const handleKeydown = (event: KeyboardEvent) => {
if (!(event.target instanceof Element)) {
return;
}
if ($isPreviewMode.get()) {
return;
}
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLTextAreaElement
) {
// prevent typing in inputs only in canvas mode
event.preventDefault();
}
};
// Note: Event handlers behave unexpectedly when used inside a dialog component.
// In Dialogs, React intercepts and processes events before they reach our handlers.
// To ensure consistent behavior across all components, we're using event capturing.
// This allows us to intercept events before React gets a chance to handle them.
document.documentElement.addEventListener("click", handleClick, {
capture: true,
});
document.documentElement.addEventListener("submit", handleSubmit, {
capture: true,
});
document.documentElement.addEventListener("keydown", handleKeydown);
document.documentElement.addEventListener("pointerdown", handlePointerDown);
return () => {
document.documentElement.removeEventListener(
"pointerdown",
handlePointerDown
);
document.documentElement.removeEventListener("click", handleClick, {
capture: true,
});
document.documentElement.removeEventListener("submit", handleSubmit, {
capture: true,
});
document.documentElement.removeEventListener("keydown", handleKeydown);
};
};