Repository navigation
fix(core): require the revision token on MCP content writes - #2912
Conversation
`content_update`, `content_publish`, `content_unpublish` and `content_discard_draft` accepted a write with no `_rev`, so an agent could write back over changes it had never read. The CLI has always required the token on `content update`; the MCP surface is now consistent with it. The four parameter descriptions stated only what the token was. Agents see the schema and nothing else, so they now carry the protocol: read the item first, pass the token back, and on a conflict read again and retry.
A client written before the token was required failed with "expected string, received undefined", which names the field and nothing else. The tool description carries the protocol, but a caller that already has the schema cached never re-reads it, so the validation error is the only thing it sees.
Content writes now reject a call without `_rev`, so every test that creates an entry and then edits it has to hold the token. Two helpers cover the two cases: `revOf` reads it from a response for a chained write, and `currentRev` reads it fresh where the test's subject is not concurrency. Three tests changed meaning rather than shape. The one asserting that an update without `_rev` still succeeds now asserts the rejection and the message that tells a caller where to get a token. The two parallel-write tests guard state integrity and permission isolation, so a lost race is an acceptable outcome there and only corruption or a wrong refusal is not.
An update to a published item writes the draft layer. The response carries the new values and a read-back agrees, while the public page keeps the old ones until content_publish, so a caller that trusts the response reports a change it did not make.
House style keeps a test docstring to a line or two.
… tools Only content_update asserted that a call without _rev is refused. Reverting the requirement on content_publish, content_unpublish and content_discard_draft left the whole suite green, so three quarters of the change was unguarded.
…lpers revOf and currentRev were inserted between extractJson's docstring and extractJson itself, which left the docstring describing revOf and extractJson with none.
The reference listed _rev as optional on content_update and omitted it entirely from content_publish, content_unpublish and content_discard_draft, so a client written from this page fails on all four.
The entry described the new contract but never named the break or the previous behaviour, which the changeset guide requires for a breaking change.
The content_get description and the reference page said every tool that changes the item requires the token. Six tools that change an item accept no _rev at all (delete, restore, permanent delete, schedule, unschedule, duplicate), so the sentence promised a contract the server does not have. Both places now name content_update, content_publish, content_unpublish and content_discard_draft, and the reference Aside no longer repeats the list.
Both docstrings named the alternative they had rejected and one narrated when the parameter became required. The comment convention allows the non-obvious reason and nothing else, so each now states only what a later reader would otherwise get wrong.
…update A content_update that only set status handed nothing to the publish or unpublish handler, so a stale _rev published or unpublished the item while content_publish and content_unpublish refused the same token. Both branches now pass the caller's token on, or the token the preceding field update returned, so the conflict check holds on every path of the tool.
The description and the reference said a change to a published item is staged as a draft. With status set to published the same call publishes at once, so the sentence now names the condition.
🦋 Changeset detectedLatest commit: b0a29b9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Scope checkThis PR changes 564 lines across 15 files. Large PRs are harder to review and more likely to be closed without review. If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs. See CONTRIBUTING.md for contribution guidelines. |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
The approach is sound and the right fix for the stated problem: requiring _rev on the four MCP write tools closes the stale-read gap, and forwarding the token through content_update status transitions fixes the inconsistency where a status-only update bypassed the check. The PR updates the MCP reference docs, the tool parameter descriptions, and most MCP integration tests.
I found two concrete gaps that need to be addressed before this should ship:
-
Stale changeset text.
.changeset/atomic-expected-publish.mdstill says "Calls that omit_revkeep the existing behavior." That stops being true the moment this PR lands, and both changesets would be released together. The old entry needs that sentence removed. -
Missed tests in
validation.test.ts. Threecontent_updatecalls were not updated to include_rev. They do not fail the suite outright because the new error message contains "required", which happens to match the looseVALIDATION_ERRORregex — but they now test the_revrequirement instead of the field validation they claim to test. The file also needsrevOfimported.
Both are needs_fixing. There is also a suggestion to remove a misleading unit-test comment about authorization order.
Otherwise the implementation, docs, and the bulk of the tests look correct.
Findings
-
[needs fixing]
packages/core/tests/integration/mcp/validation.test.ts:25This test file calls
content_updatewithout_revin three places (lines 139, 247, 477). Those calls now fail with the_rev is requiredmessage before ever reaching field validation, so the tests needrevOfavailable.import { connectMcpHarness, extractText, revOf, type McpHarness } from "../../utils/mcp-runtime.js"; -
[needs fixing]
packages/core/tests/integration/mcp/validation.test.ts:139This
content_updatecall is missing_rev. It appears to pass right now because the new error text (“_rev is required…”) matches the looseVALIDATION_ERRORregex, but it no longer tests the intended behavior (rejecting an empty required title). Pass the token from the precedingcontent_create.arguments: { collection: "post", id, data: { title: "" }, _rev: revOf(created) }, -
[needs fixing]
packages/core/tests/integration/mcp/validation.test.ts:247Same issue as line 139: the call omits
_rev, so the test now exercises the new_revrequirement rather than the invalid select-value validation it is named for.arguments: { collection: "post", id, data: { priority: "URGENT" }, _rev: revOf(created) }, -
[needs fixing]
packages/core/tests/integration/mcp/validation.test.ts:477This
content_updatecall is also missing_rev. With the token missing, the assertion that no draft revision is written is now trivial (no request reaches the repository), so the test loses its claim that field validation runs before any draft is created.arguments: { collection: "post", id: postId, data: { title: "" }, _rev: revOf(create) }, -
[needs fixing]
.changeset/atomic-expected-publish.md:5This unreleased changeset says calls that omit
_revkeep the existing behavior, but this PR makes_revrequired oncontent_publish,content_unpublish,content_discard_draft, andcontent_update. Both changesets would land in the same release, so the sentence is now false and must be removed.Fixes publication workflows so callers can pass the approved `_rev` to publish, unpublish, or discard a draft and receive a `CONFLICT` response when the entry changed. -
[suggestion]
packages/core/tests/unit/mcp/authorization.test.ts:33This comment is misleading: the MCP SDK validates
inputSchemabefore invoking the tool handler, so schema/token validation runs before therequireScope/requireRolechecks in the handler. Because the unit tests mock the handler call itself, any syntactically valid_revstring suffices, but the comment implies the opposite order. Remove or reword it.const STUB_REV = "c3R1Yi1yZXY=";
Three content_update calls in validation.test.ts had no _rev and kept passing after the token became required: the missing-token message contains the word "required", which the file's VALIDATION_ERROR pattern accepts. They asserted the token check instead of the field validation they are named for, and the one guarding that no draft revision is written before validation was trivially true. Each now carries a current token, so the rejection comes from the field check again.
atomic-expected-publish.md said calls that omit _rev keep the existing behaviour. Both changesets ship in the same release, in which the four MCP write tools require the token, so the sentence would have contradicted the entry next to it.
The docstring on STUB_REV said authorization runs before the token is validated. The token sits in the tool input schema, which the SDK validates before the handler's scope and role checks run, so the order was backwards. Any string works because the handlers are mocked and never compare it with a revision, and the comment now says that.
|
The fixes for the review are pushed. The validation test file was the one MCP suite the first push skipped. Its Both changesets ship in the same release, so the older publish changeset's The comment above the stub token in the authorization unit test now says why |
There was a problem hiding this comment.
Re-review of PR #2912. The approach remains sound: requiring _rev on the four MCP write tools closes the stale-read gap and aligns MCP with the CLI, and forwarding the token through content_update status transitions fixes the inconsistency where a status-only update bypassed the check.
All six findings from the previous review have been addressed:
.changeset/atomic-expected-publish.mdno longer claims that calls omitting_revkeep the existing behavior.packages/core/tests/integration/mcp/validation.test.tsnow importsrevOf(currentRevtoo) and passes_revin the three calls that previously omitted it, so those tests exercise field validation again rather than accidentally testing the new_revrequirement.packages/core/tests/unit/mcp/authorization.test.tsreplaced the misleading comment with an accurate note on the stub revision, and supplies_rev/STUB_REVeverywhere the write-tool schemas now require it.
I also verified that:
- Every MCP integration and unit test that invokes
content_update,content_publish,content_unpublish, orcontent_discard_draftnow supplies a_revtoken (eitherrevOf(...),currentRev(...), orSTUB_REV). - The status-transition logic in
content_updatecorrectly passes the original_revfor status-only calls and the fresh_revfrom the intermediate update result when data/seo/bylines/etc. are also changing. extractContentRevreads_revfrom the handler response shape{ item, _rev }, matching the otherextract*helpers.- The new changeset
mcp-require-rev-on-writes.mdnames the affected audience (MCP clients), explains the previous and current behavior, gives a concrete migration example, and notes that the previous behavior cannot be restored. - The MCP reference docs list
_revas required on all four tools and describe the retry protocol.
No remaining blocking issues. LGTM.
Overlapping PRsThis PR modifies files that are also changed by other open PRs:
This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
What does this PR do?
Requires
_revon the MCPcontent_update,content_publish,content_unpublishandcontent_discard_drafttools, so an agent can no longerwrite over changes it never read. The token was optional on those four while the
CLI has always required it on
content update.The status transitions inside
content_updatehand the token on to the publishand unpublish handlers as well. A status-only call never checked the token it
carried, so a stale read could publish or unpublish the item while
content_publishandcontent_unpublishrefused the same token.The parameter descriptions named the token without saying what to do with it,
and now carry the protocol. A call that omits it fails with a message pointing
at
content_getrather than "expected string, received undefined", since aclient holding a cached schema never re-reads a description.
content_updatealso states that a change to a published item is staged as a draft when
statusis not set.The MCP reference is updated to match. It listed
_revas optional oncontent_updateand left it out of the other three tools altogether, so aclient written from that page would have failed on all four.
Breaking for existing MCP clients, approved on Discord on 4 September along with
the decision to cover all four tools rather than
content_updatealone.The unreleased changeset
atomic-expected-publish.mdsaid calls that omit_revkeep the existing behaviour. Both changesets ship in the same release, sothat sentence is removed here.
Closes: N/A
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change):packages/coreis 6309 passing andpnpm test:e2eis 283 passing with 31 skipped;packages/registry-verificationtest:workerdfails identically on an unrelated branch, so it is not from this changepnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain. (n/a: no admin UI strings)AI-generated code disclosure
Screenshots / test output
Not applicable for screenshots.
Tests carry the token the same two ways a caller does.
revOftakes it from aresponse for a chained write,
currentRevreads it fresh where the subject isnot concurrency. Each of the four tools has a test that goes red when the
requirement is removed: reverting all four declarations to
.optional()failsexactly those four and leaves the rest of the suite green. Two more cover a
status-only
content_update: a stale token is refused and the current one goesthrough, and dropping the token from the publish and unpublish calls fails
exactly those two. Three existing tests changed meaning rather than shape: the one asserting that an update without
_revstill succeeds now asserts the rejection and the message, and the twoparallel-write tests guard state integrity and permission isolation, so a lost
race became an acceptable outcome there while corruption and a wrong refusal
did not. The missing-token message contains the word
required, which thefield-validation suite's pattern accepts, so a further three
content_updatecalls kept passing without a token; they now carry it and are rejected by the
field check again.
Whether a required token confuses an agent was checked rather than assumed.
Five agents with no prior context were pointed at a running MCP server over
HTTP and given ordinary editorial tasks, one per tool. All five read the
requirement out of
tools/list, calledcontent_get, and wrote successfully onthe first attempt, including on publish, unpublish and discard, where the read
serves no purpose except obtaining the token.
Two limits of that exercise are worth stating. A sixth run under a writer
rewriting the same entry every few seconds never tripped a
CONFLICT, becausethe round trip won the race every time, so the retry half of the protocol is
covered by tests and not by observation. And two of the five agents
independently mistook a staged draft for a live change, which is why
content_updatenow says so.