Skip to content

feat(mcp): give a draft as a page ready for a CMS, as Send to CMS makes it - #1728

Merged
gkhngyk merged 18 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-content-package
Oct 29, 2026
Merged

gkhngyk merged 18 commits into
ansvisor:mainfrom
cihan-gyk:feature/mcp-content-package

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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 (bulkCmsItems for one draft).
    • Fields: title, slug, meta_title, meta_description, html (cleaned like Copy HTML: signature line, no H1), json_ld (parsed), author, featured_image and source_url.
    • A draft that isn't ready answers not_ready with the Library's problem code (placeholders, noTitle, empty…): JSON in the MCP result, 409 over REST.
    • Read-only (readOnlyHint, title).
  • The editor's DOM code runs on the server. The package is built by the editor's own pipeline (clean-html, ready-to-publish, page-schema, placeholders, readability), so it can't drift from Copy HTML or Send to CMS.
    • Those modules now take their window from lib/content/dom.ts (contentDom() / setContentDom() / parseHtml()): the browser's own in the app, a jsdom window on the server.
    • jsdom loads only on this tool's first call: a dynamic import inside the handler, so /api/mcp's other tools don't load it. If that load fails, the next call retries it.
    • Both routes run on runtime = 'nodejs'. jsdom is in Next's default serverExternalPackages, so it isn't bundled. .next/static has no JSDOM class after yarn build.
  • Dependency: jsdom moves from devDependencies to dependencies in web/package.json. It was already installed at 26.1.0, so yarn.lock doesn't change. linkedom was tried first, but DOMPurify didn't sanitise with it (<script> and onclick survived) and it lacks document.implementation.createHTMLDocument. The user approved jsdom. Local type stub src/types/jsdom.d.ts instead of a new @types/jsdom.
  • Security (checked before opening):
    • 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:).
    • DOMPurify on the jsdom window strips them; a test fails if it doesn't.
    • Each call builds its own purifier, so concurrent calls don't share hooks. 300 calls left the heap flat.
  • Moves, no copies: readContentAuthors → lib/content/authors-read.ts (org filter brands!inner(organization_id) for the service role, with a test); draftLanguage exported from rules.ts.
  • Guide: 56 tools on this base (feat(mcp): read the Content plan and where drafts went, and mark a draft published #1715 + this). If feat(mcp): review the tasks waiting for review: list, approve and dismiss #1713 merges first, recount.

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.ts and sources.ts still use the global DOMParser. They aren't on the package path.

Related issue

None (MCP next round, B5 c).

Type of change

  • feat — New feature

How to test

  1. With an API key, call get_content_draft_package on a ready draft. Compare html with the editor's Copy HTML for the same draft, and the title/slug/meta with what Send to CMS shows.
  2. A draft with a [placeholder] left: not_ready with problem: "placeholders" (REST: 409).
  3. Another organization's draft, or an archived one: not found (REST: 404).
  4. REST: GET /api/mcp/content-drafts/<id>/package.
  5. In the app, Copy HTML, Send to CMS and the editor's checks behave as before; the browser path is unchanged.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web, before the review fixes: yarn lint (0 errors), yarn typecheck, yarn test (558/558 files) and yarn build pass. 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.
  • Tests added: the package in a jsdom window and under 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
  • The embed guard from test(web): fail on an embed between twice-joined tables that doesn't name its foreign key #1714 passes on this branch
  • Docs updated
  • Pre-PR security review done; its findings are fixed in this branch
  • Live call with a real key, and Copy HTML vs package on a real draft: the maintainer's

🤖 Generated with Claude Code

cihan-gyk and others added 16 commits October 28, 2026 21:04
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]>
@mintlify

mintlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 6603f32.

Approve preview

… 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 gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. getContentDraftPackageFor builds the page with cmsSource + bulkCmsItems from lib/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_ready reports bulkCmsItems' own problem codes. The only new page logic is jsonLdOf, which parses the application/ld+json scripts already in the page's HTML. The DOM changes (contentDom() / parseHtml(), root.ownerDocument.createElement, ELEMENT_NODE/TEXT_NODE in place of Node.*) don't change anything in the browser, where contentDom() is still window. I found no other document., DOMParser, Node. or instanceof Element on the package path.
  • Scripts and javascript:. new JSDOM(html) with no options doesn't run scripts or load resources, and fragment() sets innerHTML on a document made by createHTMLDocument, which never runs scripts. I ran a page with <script>, onerror, onclick and javascript: links through it under node. In the page's own content all of them are stripped (DOMPurify on the jsdom window plus cleanHref). Each call builds its own purifier, so the hooks aren't shared.
  • Output size. bulkCmsItems refuses 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 of html. json_ld only repeats scripts that are already in html. 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. readContentAuthors moved without changes except one: under the service role, content_author_defaults is now filtered by brands!inner(organization_id). There's a test for it.
  • PostgREST embeds: content_drafts → brands!inner(…) (1 FK: content_drafts.brand_id) and content_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.ts passes.
  • jsdom. It loads through a dynamic import on the tool's first call, and a failed load is retried. Moving it to dependencies doesn't change yarn.lock. Both routes are runtime = 'nodejs'.
  • Tests on this tree: src/lib/mcp, src/app/api/mcp, src/lib/content, the content actions and subscription-guard-coverage: 106 files / 1,142 tests pass. (hand-off.test.ts needs server/ beside web/ and passes with it.) tsc --noEmit is 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

  1. 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 / tooLong above about 200 KB and point to Copy HTML in the app. Another option is to cache the package by the draft's updated_at. Either can be a small follow-up. It's worth timing the largest real draft on a preview first.
  2. HTML blocks are passed through unchanged. publishableHtml expands each HTML block as written, as Copy HTML and Send to CMS do, so html can carry a block's <script>, iframe or javascript: 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.
  3. rules.ts and sources.ts still use the global DOMParser. The PR body already notes this. It's fine while they're off this path.

@gkhngyk
gkhngyk merged commit 45492d1 into ansvisor:main Oct 29, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants