Add raw HTML draft editing to LaySite MCP

This commit is contained in:
2026-11-28 17:42:32 +08:00
parent 214098c2e1
commit 1c927d360c
7 changed files with 125 additions and 11 deletions
+23 -3
View File
@@ -95,6 +95,15 @@ final class McpController
$optional = [
'name' => $string, 'title' => $string, 'heading' => $string, 'description' => $string, 'lead' => $string,
'accent' => $string, 'image' => $string, 'alt' => $string,
'raw_html' => ['oneOf' => [
$string + ['description' => 'Replace the custom hero HTML.'],
$object([
'hero' => ['type' => ['string', 'null'], 'description' => 'Custom hero fragment. Null restores the template hero.'],
'head' => $string + ['description' => 'Page Head, including style tags. Only existing scripts may be retained.'],
'sections' => ['type' => 'object', 'maxProperties' => 50, 'additionalProperties' => $string,
'description' => 'Section keys mapped to replacement body HTML. Preserves other section data.'],
]),
]],
'header_id' => ['type' => ['integer', 'null']], 'footer_id' => ['type' => ['integer', 'null']], 'palette_id' => ['type' => ['integer', 'null']],
'visibility' => $object(['hero' => ['type' => 'boolean'], 'header' => ['type' => 'boolean'], 'footer' => ['type' => 'boolean']]),
'actions' => ['type' => 'array', 'maxItems' => 8, 'items' => ['type' => 'array', 'description' => '[URL, label, style, boolean opensNewTab]. Style: solid, outline, underline, plain.']],
@@ -105,7 +114,7 @@ final class McpController
];
$definitions = [
'list_pages' => ['List pages and their current revisions.', $object([])],
'get_page' => ['Read a page and its editable sections.', $object(['slug' => $string], ['slug'])],
'get_page' => ['Read a page, editable sections and raw_html.', $object(['slug' => $string], ['slug'])],
'list_templates' => ['List whole-page templates and reusable components.', $object([])],
'list_designs' => ['List saved header, footer and palette IDs. Authentication settings are private.', $object([])],
'create_draft' => ['Create a draft from content or a template. Returns the editor URL.', $object(['slug' => $string, 'template' => $string] + $optional, ['slug', 'name'])],
@@ -131,7 +140,7 @@ final class McpController
try {
$data = match ($name) {
'list_pages' => $repo->pages(),
'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [], includeSpecial: true)),
'get_page' => $this->readSource($repo, PageDraft::slug($args['slug'] ?? null, [], includeSpecial: true)),
'list_templates' => $this->templates(),
'list_designs' => DesignLibrary::all(),
'create_draft' => $this->create($repo, $args),
@@ -171,6 +180,13 @@ final class McpController
return $this->summary($repo, $slug);
}
private function readSource(Repository $repo, string $slug): array
{
$page = $this->read($repo, $slug);
return $page + ['raw_html' => PageDraft::rawHtml($page)];
}
private function save(Repository $repo, array $args): array
{
$slug = PageDraft::slug($args['slug'] ?? null);
@@ -181,7 +197,11 @@ final class McpController
}
$page = PageDraft::content($args, $page);
$replace = array_key_exists('sections', $args);
$repo->savePage($slug, $page, $version, $replace ? $page['sections'] : [], replaceDraftSections: $replace);
$raw = $args['raw_html'] ?? null;
$patches = is_array($raw) ? ($raw['sections'] ?? []) : [];
$sections = $replace ? $page['sections'] : array_filter($page['sections'],
static fn (array $section): bool => array_key_exists($section['key'], $patches));
$repo->savePage($slug, $page, $version, $sections, replaceDraftSections: $replace);
return $this->summary($repo, $slug);
}