Skip to content

fix(core): require the revision token on MCP content writes - #2912

Merged
ascorbic merged 17 commits into
emdash-cms:mainfrom
danielmlr:fix/mcp-require-rev
Sep 7, 2026
Merged

ascorbic merged 17 commits into
emdash-cms:mainfrom
danielmlr:fix/mcp-require-rev

Conversation

@danielmlr

@danielmlr danielmlr commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Requires _rev on the MCP content_update, content_publish,
content_unpublish and content_discard_draft tools, so an agent can no longer
write 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_update hand the token on to the publish
and 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_publish and content_unpublish refused 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_get rather than "expected string, received undefined", since a
client holding a cached schema never re-reads a description. content_update
also states that a change to a published item is staged as a draft when
status is not set.

The MCP reference is updated to match. It listed _rev as optional on
content_update and left it out of the other three tools altogether, so a
client 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_update alone.

The unreleased changeset atomic-expected-publish.md said calls that omit
_rev keep the existing behaviour. Both changesets ship in the same release, so
that sentence is removed here.

Closes: N/A

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change): packages/core is 6309 passing and pnpm test:e2e is 283 passing with 31 skipped; packages/registry-verification test:workerd fails identically on an unrelated branch, so it is not from this change
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main. (n/a: no admin UI strings)
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/... (n/a: not a feature)
  • I have included screenshots below if this PR changes the UI (n/a: no UI change)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5 (review pass: Claude Fable 5.1)

Screenshots / test output

Not applicable for screenshots.

Tests carry the token the same two ways a caller does. revOf takes it from a
response for a chained write, currentRev reads it fresh where the subject is
not concurrency. Each of the four tools has a test that goes red when the
requirement is removed: reverting all four declarations to .optional() fails
exactly 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 goes
through, 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
_rev still succeeds now asserts the rejection and the message, and the two
parallel-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 the
field-validation suite's pattern accepts, so a further three content_update
calls 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, called content_get, and wrote successfully on
the 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, because
the 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_update now says so.

`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-bot

changeset-bot Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b0a29b9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
emdash Minor
@emdash-cms/cloudflare Minor
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Minor
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
create-emdash Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
@emdash-cms/auth-atproto Patch
@emdash-cms/release-service Patch
@emdash-cms/plugin-embeds Patch

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

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Scope check

This 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.

@pkg-pr-new

pkg-pr-new Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@2912

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@2912

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@2912

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@2912

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@2912

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@2912

emdash

npm i https://pkg.pr.new/emdash@2912

create-emdash

npm i https://pkg.pr.new/create-emdash@2912

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@2912

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@2912

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@2912

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@2912

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@2912

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@2912

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@2912

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@2912

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@2912

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@2912

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@2912

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@2912

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@2912

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@2912

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@2912

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@2912

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@2912

commit: b0a29b9

@emdashbot emdashbot Bot 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.

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:

  1. Stale changeset text. .changeset/atomic-expected-publish.md still says "Calls that omit _rev keep 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.

  2. Missed tests in validation.test.ts. Three content_update calls 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 loose VALIDATION_ERROR regex — but they now test the _rev requirement instead of the field validation they claim to test. The file also needs revOf imported.

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:25

    This test file calls content_update without _rev in three places (lines 139, 247, 477). Those calls now fail with the _rev is required message before ever reaching field validation, so the tests need revOf available.

    import { connectMcpHarness, extractText, revOf, type McpHarness } from "../../utils/mcp-runtime.js";
    
  • [needs fixing] packages/core/tests/integration/mcp/validation.test.ts:139

    This content_update call is missing _rev. It appears to pass right now because the new error text (“_rev is required…”) matches the loose VALIDATION_ERROR regex, but it no longer tests the intended behavior (rejecting an empty required title). Pass the token from the preceding content_create.

    			arguments: { collection: "post", id, data: { title: "" }, _rev: revOf(created) },
    
  • [needs fixing] packages/core/tests/integration/mcp/validation.test.ts:247

    Same issue as line 139: the call omits _rev, so the test now exercises the new _rev requirement 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:477

    This content_update call 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:5

    This unreleased changeset says calls that omit _rev keep the existing behavior, but this PR makes _rev required on content_publish, content_unpublish, content_discard_draft, and content_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:33

    This comment is misleading: the MCP SDK validates inputSchema before invoking the tool handler, so schema/token validation runs before the requireScope/requireRole checks in the handler. Because the unit tests mock the handler call itself, any syntactically valid _rev string 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.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/needs-review No maintainer or bot review yet labels Sep 4, 2026
@danielmlr

Copy link
Copy Markdown
Member Author

The fixes for the review are pushed.

The validation test file was the one MCP suite the first push skipped. Its
three content_update calls now carry the token, two from the create response
and one read fresh. Under a pattern that rejects the missing-token message all
seventeen tests in the file still pass, so the three are rejected by the field
check again rather than by the missing token.

Both changesets ship in the same release, so the older publish changeset's
sentence about calls that omit _rev is gone, and the body no longer flags
it as open.

The comment above the stub token in the authorization unit test now says why
any string works: the schema takes any string, and the mocked handlers
never compare it with a revision. The SDK validates the input before the scope
and role checks run, so the old comment had the order backwards.

@danielmlr
danielmlr marked this pull request as draft September 4, 2026 15:44
@danielmlr
danielmlr marked this pull request as ready for review September 4, 2026 15:46

@emdashbot emdashbot Bot 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.

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.md no longer claims that calls omitting _rev keep the existing behavior.
  • packages/core/tests/integration/mcp/validation.test.ts now imports revOf (currentRev too) and passes _rev in the three calls that previously omitted it, so those tests exercise field validation again rather than accidentally testing the new _rev requirement.
  • packages/core/tests/unit/mcp/authorization.test.ts replaced the misleading comment with an accurate note on the stub revision, and supplies _rev / STUB_REV everywhere 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, or content_discard_draft now supplies a _rev token (either revOf(...), currentRev(...), or STUB_REV).
  • The status-transition logic in content_update correctly passes the original _rev for status-only calls and the fresh _rev from the intermediate update result when data/seo/bylines/etc. are also changing.
  • extractContentRev reads _rev from the handler response shape { item, _rev }, matching the other extract* helpers.
  • The new changeset mcp-require-rev-on-writes.md names 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 _rev as required on all four tools and describe the retry protocol.

No remaining blocking issues. LGTM.

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Sep 4, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@ascorbic
ascorbic merged commit 6da29d3 into emdash-cms:main Sep 7, 2026
51 checks passed
@danielmlr
danielmlr deleted the fix/mcp-require-rev branch September 7, 2026 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants