Repository navigation
feat(mcp): give a draft as a page ready for a CMS, as Send to CMS makes it - #1728
Conversation
get_content_plan (REST GET /api/mcp/content-plan): what to write or fix next at a brand, as Content › Plan ranks it by the Priority Score, with each row's action (create, optimize, protect), why it's there, its score's parts, the draft already being written for it and whether a person marked it a priority; per card counts, over 7, 30 or 90 days, narrowed to a card when asked (MCP plan B5). The page's reads move to lib/content/plan-read.ts (readContentPlan), which takes the client: the member's session for the page, the service role for the MCP once the brand is the key's. The ranking is the page's own buildPlan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…l sell list_content_drafts and get_content_draft give each draft its last Send to CMS (last_send: status, target, when, the webhook's answer, the error and where the CMS says it went live), as the Content Library shows it, and its shopping links' check after the catalog sync (shopping_links: ok or attention, what it links, and the linked products that no longer sell). get_content_draft also gives its shopping path (shopping_path: the category pages and products it should link, and those left out and why), as its Brief tab shows it. MCP plan B5: Ansvisor's MCP never sends a page itself. The Library's reading of a send (lastSend) moves to cms-status.ts, so the browser's Library and editor and the MCP read a send the same way; the shopping check is the Library's own draftShopping. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ut it mark_content_draft_published (REST POST /api/mcp/content-drafts/:id/published): for a page the user's own AI tool put on their CMS with its own connector (Ansvisor's MCP never sends one), the draft is marked Published at its live address, as the editor's Mark published does: its Live page tab measures that address and a linked action's Publish task completes once its pages are live. MCP plan B5. The API's publishDraft runs it for the key's user through a new CRON_SECRET internal endpoint, /api/internal/content-drafts/:id/publish (publishDraftForKeyUser checks the draft is the key's organization's and not archived). The tool is gated on a read-only workspace like every write; the demo brand's gate follows when feature/demo-brand-mcp merges (TODO in content-delivery.ts). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d the drafts' send and shopping state Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-plan # Conflicts: # docs/guides/mcp-server.mdx # web/src/lib/content/cms-send-client.ts # web/src/lib/mcp/server.ts # web/src/lib/mcp/subscription-gate.test.ts Co-Authored-By: Claude Opus 5.5 <[email protected]>
… published, and validate the plan's brand_id - last_send names a send to WordPress as wordpress (the rule moved to cms-status.ts with the merge), in the tool description too. - mark_content_draft_published checks the workspace's plan before it asks the API, as the other internal-endpoint calls do; the REST mirror answers 402 for a read-only workspace. Only the API's invalid_url reads as a bad address. - GET /api/mcp/content-plan refuses a brand_id that isn't a UUID with 400. - list_content_drafts reads open suggestions and last sends together. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… when an API key marks it again publishDraftForKeyUser (mark_content_draft_published) answers a page already published at the same address with its stored live-since time and writes nothing, so a call made again changes nothing, as the tool's idempotent hint says. Another address still moves the page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…en a Publish task completes Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-plan # Conflicts: # web/src/lib/content/cms-send-client.ts
…es it get_content_draft_package (REST GET /api/mcp/content-drafts/:id/package, MCP plan B5): a Content Editor draft as the page Ansvisor's own Send to CMS would send, so the user's own AI tool can put it on their CMS with its own connector (Ansvisor's MCP never sends it): title, slug, meta title and description, the HTML cleaned as Copy HTML cleans it with the byline line and no H1, its JSON-LD parsed, the author and the featured image. A page that can't go yet says why, as the Library's Send does. The editor's HTML helpers read pages with the browser's DOM, which the server has none of. They now read through lib/content/dom.ts: the browser's window, or one the server gives. The MCP gives jsdom's, imported in content-package.ts alone and made on the first call, so no other tool waits for it and the browser's bundle never carries it. - jsdom moves from devDependencies to dependencies (already in yarn.lock, nothing new downloaded; ~13 MB with its dependencies in the MCP's server function, and outside Next's bundle: it is in Next's serverExternalPackages). linkedom was tried first: DOMPurify cleaned nothing with it, and it has no createHTMLDocument. - The MCP routes say runtime = 'nodejs'. - The authors read (readContentAuthors) and a draft's language (draftLanguage) move to shared modules; the service role's authors read keeps to the organization's brands' defaults. - Tests: the same fixture makes the same page in the browser (content-package.editor.test.ts) and on the server with no window (content-package.test.ts). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eature/mcp-content-package # Conflicts: # docs/guides/mcp-server.mdx # web/src/lib/mcp/server.ts
… page can't go as REST does - The MCP server imports content-package.ts when get_content_draft_package is first called, so no other tool loads the editor's page code. - jsdom's window is made again on the next call when a load fails, instead of every later call failing with the same error. - A page that can't go yet answers as the REST route's 409 does: JSON with the message, code not_ready and its problem. - The tool says "no placeholders left": Ansvisor doesn't fill them, a page with any answers not_ready. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…anization readContentAuthors reads with the service role for the MCP's get_content_draft_package: the authors and brands by organization_id, and the brands' default authors only through the organization's brands (brands!inner, brands.organization_id). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…, an H4 and non-ASCII text The page both tests make (the Library's Send to CMS in the browser, the MCP's on the server with jsdom) now runs through more of the editor's helpers: an H4 becomes an H3, a table loses its column widths, a link keeps its address only, text past ASCII stays as written, and an HTML block's code goes out as written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… drop casts jsdom's window doesn't need - ./dom is imported among each helper's local imports, as the files order them, and draftLanguage in its alphabetical place. - jsdom's window is a ContentWindow as typed: no cast. - Say why the package's authors read names no role. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
… HTML and Send to CMS do The tool's description said the page's HTML was cleaned. The page's own content is, but an HTML block's code is passed as written, script and iframe included, as Copy HTML and Send to CMS pass it. Say so in the tool, the type and the guide, and pin both the behaviour and the wording in a test.
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good with ad94613: the tool description, the html field and the guide now say HTML blocks are passed as written (scripts, iframes and links included), as Copy HTML and Send to CMS pass them, instead of "cleaned"; a test pins it.
Reviewed this PR's own change (ed169b8..790715c, on top of #1715). No migration.
What I checked
- It is Send to CMS's code, not a copy.
getContentDraftPackageForbuilds the page withcmsSource+bulkCmsItemsfromlib/content/cms-send.ts, the same two calls the Library's bulk Send makes, with the slug and meta the draft kept from its last send.not_readyreportsbulkCmsItems' own problem codes. The only new page logic isjsonLdOf, which parses theapplication/ld+jsonscripts already in the page's HTML. The DOM changes (contentDom()/parseHtml(),root.ownerDocument.createElement,ELEMENT_NODE/TEXT_NODEin place ofNode.*) don't change anything in the browser, wherecontentDom()is stillwindow. I found no otherdocument.,DOMParser,Node.orinstanceof Elementon the package path. - Scripts and
javascript:.new JSDOM(html)with no options doesn't run scripts or load resources, andfragment()setsinnerHTMLon a document made bycreateHTMLDocument, which never runs scripts. I ran a page with<script>,onerror,onclickandjavascript:links through it undernode. In the page's own content all of them are stripped (DOMPurify on the jsdom window pluscleanHref). Each call builds its own purifier, so the hooks aren't shared. - Output size.
bulkCmsItemsrefuses a page over 1,000,000 characters (tooLong). A stored body is capped at 500 KB, and an HTML block's base64 is longer than its code, so a real page comes out under that. A 495 KB body gave 278 KB ofhtml.json_ldonly repeats scripts that are already inhtml. A response stays well under the 4.5 MB serverless limit. - Org boundary. The draft is read with
brands!inner(organization_id)filtered to the key's organization. Archived drafts are not found.readContentAuthorsmoved without changes except one: under the service role,content_author_defaultsis now filtered bybrands!inner(organization_id). There's a test for it. - PostgREST embeds:
content_drafts → brands!inner(…)(1 FK:content_drafts.brand_id) andcontent_author_defaults → brands!inner(organization_id)(1 FK:content_author_defaults.brand_id). Neither is ambiguous. - Read-only workspace: one tool (
get_,readOnlyHint: true) and one GET route, so there's nothing new to gate.subscription-guard-coverage.test.tspasses. - jsdom. It loads through a dynamic import on the tool's first call, and a failed load is retried. Moving it to
dependenciesdoesn't changeyarn.lock. Both routes areruntime = 'nodejs'. - Tests on this tree:
src/lib/mcp,src/app/api/mcp,src/lib/content, the content actions andsubscription-guard-coverage: 106 files / 1,142 tests pass. (hand-off.test.tsneedsserver/besideweb/and passes with it.)tsc --noEmitis clean, and eslint and prettier pass on the 22 touched web files.
Merging
Merges clean on current main (ce9a3dc) together with #1715. It conflicts with #1729 and #1733 in docs/guides/mcp-server.mdx and web/src/lib/mcp/server.ts, and with #1733 in subscription-gate.test.ts too. These are adjacent tool additions and the tool count. Whichever merges later recounts.
Follow-ups
- Speed at the body cap. On my machine the package took 0.37 s for a 36 KB body, 2.0 s for 200 KB and 7.8 s for 495 KB. All of it is synchronous, so the instance does nothing else meanwhile, and the MCP has no rate limit. Suggest a cap for this tool, e.g. answer
not_ready/tooLongabove about 200 KB and point to Copy HTML in the app. Another option is to cache the package by the draft'supdated_at. Either can be a small follow-up. It's worth timing the largest real draft on a preview first. - HTML blocks are passed through unchanged.
publishableHtmlexpands each HTML block as written, as Copy HTML and Send to CMS do, sohtmlcan carry a block's<script>, iframe orjavascript:link. I confirmed this with a block holding both. That's by design, but the tool's description says the HTML is "cleaned". Suggest one clause, such as "HTML blocks are given as their own code", and a test that pins it, so nobody "fixes" it later and a client knows to expect it. rules.tsandsources.tsstill use the globalDOMParser. The PR body already notes this. It's fine while they're off this path.
Summary
MCP next round, B5 (c): an AI assistant can get a draft as the exact page Send to CMS would send, then put it on the site with its own connector and record it with
mark_content_draft_published(#1715).Stacked on #1715. Review the commits after its head (ed169b8) and merge after it. The two PRs share the guide's "the MCP never sends a page" paragraph, now one paragraph in this branch.
get_content_draft_package+GET /api/mcp/content-drafts/:id/package: the page Library › Send to CMS would send (bulkCmsItemsfor one draft).title,slug,meta_title,meta_description,html(cleaned like Copy HTML: signature line, no H1),json_ld(parsed),author,featured_imageandsource_url.not_readywith the Library's problem code (placeholders,noTitle,empty…): JSON in the MCP result, 409 over REST.readOnlyHint,title).clean-html,ready-to-publish,page-schema,placeholders,readability), so it can't drift from Copy HTML or Send to CMS.lib/content/dom.ts(contentDom()/setContentDom()/parseHtml()): the browser's own in the app, a jsdom window on the server./api/mcp's other tools don't load it. If that load fails, the next call retries it.runtime = 'nodejs'. jsdom is in Next's defaultserverExternalPackages, so it isn't bundled..next/statichas no JSDOM class afteryarn build.jsdommoves fromdevDependenciestodependenciesinweb/package.json. It was already installed at 26.1.0, soyarn.lockdoesn't change. linkedom was tried first, but DOMPurify didn't sanitise with it (<script>andonclicksurvived) and it lacksdocument.implementation.createHTMLDocument. The user approved jsdom. Local type stubsrc/types/jsdom.d.tsinstead of a new@types/jsdom.new JSDOM(html)runs with no options: scripts off, no resource loading. A local test server received 0 requests from img, iframe, link, script src,@import, object, embed, media, svg image and meta refresh. No script ran (onerror, onload,javascript:).readContentAuthors→lib/content/authors-read.ts(org filterbrands!inner(organization_id)for the service role, with a test);draftLanguageexported fromrules.ts.Speed (for the maintainer): jsdom is synchronous. A ~36 KB page took 0.5–1.4 s on a loaded laptop, and a 500 KB page (the body cap) ~5.5 s, which blocks that instance meanwhile. Worth measuring with the largest real draft on a preview; a size cap or a worker can follow if needed.
Not covered:
rules.tsandsources.tsstill use the globalDOMParser. They aren't on the package path.Related issue
None (MCP next round, B5 c).
Type of change
feat— New featureHow to test
get_content_draft_packageon a ready draft. Comparehtmlwith the editor's Copy HTML for the same draft, and the title/slug/meta with what Send to CMS shows.[placeholder]left:not_readywithproblem: "placeholders"(REST: 409).GET /api/mcp/content-drafts/<id>/package.Checklist
yarn lint(0 errors),yarn typecheck,yarn test(558/558 files) andyarn buildpass. After the fixes:tsc --noEmit, eslint and prettier on the 35 changed files,src/lib/mcp+src/app/api/mcp(180 tests),src/lib/content(787) and the content actions (76) pass.node(same EXPECTED_HTML incl. an HTML block, table, links, h4 and non-ASCII), XSS stripping, not_ready / not_found, the jsdom retry, authors' org filter, REST route🤖 Generated with Claude Code