Skip to content

Fix/mcp menu locale awareness - #939

Merged
ascorbic merged 3 commits into
emdash-cms:mainfrom
schiste:fix/mcp-menu-locale-awareness
May 9, 2026
Merged

ascorbic merged 3 commits into
emdash-cms:mainfrom
schiste:fix/mcp-menu-locale-awareness

Conversation

@schiste

@schiste schiste commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Makes the MCP menu write tools locale-aware on multi-locale installs.

Upstream already models menus as per-locale records: (name, locale) is the
unique key, and the same menu name can exist in multiple locales within one
translation group. The handler layer already partly reflects that:
handleMenuCreate() accepts locale and translationOf,
handleMenuUpdate() accepts locale, and handleMenuDelete() accepts
locale.

The gap is the MCP write surface:

  • menu_create, menu_update, menu_delete, and menu_set_items did not
    expose locale
  • menu_create did not expose translationOf
  • handleMenuSetItems() still selected menus by name only

On a multi-locale install, that meant an MCP caller trying to update main in
fr-fr could silently modify a different locale’s menu instead.

This PR fixes that by:

  • extending handleMenuSetItems() to accept and honor locale
  • exposing locale on all MCP menu write tools
  • exposing translationOf on menu_create
  • requiring locale when translationOf is provided on menu_create
  • writing inserted menu_set_items rows with locale: menu.locale
  • improving locale-scoped NOT_FOUND messages on menu writes
  • updating the MCP reference docs for the menu tool surface

Type of change

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

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: GPT-5 Codex

Screenshots / test output

Targeted validation run locally:

pnpm --silent lint:quick
pnpm --dir packages/core exec vitest run tests/unit/menus/menus.test.ts tests/integration/mcp/menu.test.ts
pnpm --dir packages/core typecheck

Added regression coverage for:

- locale-targeted handleMenuSetItems()
- locale-specific NOT_FOUND on scoped menu writes
- menu_create forwarding locale and translationOf
- rejecting translationOf without locale
- locale-scoped menu_update
- locale-scoped menu_set_items
- locale-scoped menu_delete

Note: I did not mark full pnpm lint or full pnpm test as passing here.
lint:quick and the targeted menu suites passed cleanly. In this local
environment, a full upstream test run also hits an unrelated network-dependent
WordPress migration fixture fetch.

@changeset-bot

changeset-bot Bot commented May 7, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c28b865

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

This PR includes changesets to release 13 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto 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 May 7, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@schiste
schiste marked this pull request as draft May 7, 2026 14:33
@schiste

schiste commented May 7, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@schiste

schiste commented May 7, 2026

Copy link
Copy Markdown
Contributor Author

recheck

github-actions Bot added a commit that referenced this pull request May 7, 2026
@pkg-pr-new

pkg-pr-new Bot commented May 7, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

emdash

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

create-emdash

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

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

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: c28b865

@schiste
schiste force-pushed the fix/mcp-menu-locale-awareness branch from afd1d40 to 3881454 Compare May 7, 2026 15:44
@schiste
schiste marked this pull request as ready for review May 7, 2026 16:49
@ascorbic

ascorbic commented May 8, 2026

Copy link
Copy Markdown
Member

/review

@ask-bonk ask-bonk 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.

Fix is correct and well-scoped. The locale-scoped existence check stays inside the transaction (preserves the concurrency-safety property of the original code), inserted items now get tagged with the menu's locale instead of falling back to the column DEFAULT (defaultLocale), and the new translationOf -> locale refine on the MCP boundary is a nice safety net. Tests cover the targeted behavior — per-locale menu_set_items, item locale tagging, locale-scoped NOT_FOUND, locale-scoped update/delete, and the translationOf requirement. Two small follow-up notes inline; neither is a blocker.

Comment thread packages/core/src/api/handlers/menus.ts Resolved
Comment thread packages/core/src/api/handlers/menus.ts Resolved
Comment thread packages/core/src/mcp/server.ts Resolved
@ask-bonk

ask-bonk Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

Review posted. Summary:

  • Approve-shaped review with three inline notes, none blocking.
  • The fix is correct: closes the silent-modification bug on multi-locale installs, keeps the existence check inside the transaction, and tags inserted items with the menu's locale instead of the column DEFAULT.
  • Inline notes:
    1. menu_set_items doesn't set translation_group on inserted items (pre-existing — handleMenuItemAdd does). Pointed out as a cheap follow-up since the diff is already touching the insert.
    2. Non-deterministic locale resolution when locale is omitted on multi-locale installs — handleMenuGet adds orderBy("locale", "asc") for determinism; this and the other write handlers don't. Pre-existing pattern; calling out for consistency.
    3. The translationOf → locale refine is only enforced at the MCP boundary, not in the handler itself. REST/direct callers can still bypass it. Flagged in case the intent was to make this a hard handler-level rule.

github run

Copilot AI 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.

Pull request overview

This PR makes MCP menu tooling locale-aware on multi-locale installs so menu write operations can target the intended (name, locale) menu row (and improve NOT_FOUND messaging accordingly), closing a gap where MCP writes could affect the wrong locale variant.

Changes:

  • Extend MCP menu write tools (menu_create, menu_update, menu_delete, menu_set_items) to accept locale; extend menu_create to accept translationOf and require locale when translationOf is set.
  • Update handleMenuSetItems() to optionally scope menu lookup by locale and to write new menu items with the menu’s locale.
  • Add/expand unit + integration tests and update MCP reference docs to reflect the locale-aware menu surface.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/core/src/api/handlers/menus.ts Add locale-scoped lookup and locale-tagging for handleMenuSetItems(), plus improved locale-scoped NOT_FOUND messages.
packages/core/src/mcp/server.ts Expose locale on menu write tools and translationOf on menu_create, including validation when translating.
packages/core/tests/unit/menus/menus.test.ts Add regression coverage for locale-scoped handleMenuSetItems() behavior and NOT_FOUND messaging.
packages/core/tests/integration/mcp/menu.test.ts Add integration coverage ensuring MCP menu writes target the correct locale variant.
docs/src/content/docs/reference/mcp-server.mdx Document the locale-aware menu tool parameters for MCP.
.changeset/mcp-menu-locale-awareness.md Changeset describing the locale-awareness patch for MCP menu tools.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/core/src/api/handlers/menus.ts Resolved
Comment thread packages/core/src/api/handlers/menus.ts Resolved
Addresses Copilot review feedback on emdash-cms#939.

handleMenuUpdate, handleMenuDelete, and handleMenuSetItems now return
the new AMBIGUOUS_LOCALE error code (HTTP 400) when a name resolves to
multiple locale variants and the caller did not pass `locale`. The error
message lists the available locales so callers can recover. Previously
the lookup silently picked an arbitrary translation, which could rewrite
or delete the wrong locale's menu on multi-locale installs. Single-locale
installs and callers that already pass `locale` are unaffected.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@github-actions github-actions Bot added size/XL and removed size/L labels May 9, 2026
Two extensions to the AMBIGUOUS_LOCALE work in 061577f:

1. Apply the same fail-loud rule to the four item-level handlers
   (handleMenuItemCreate / Update / Delete / Reorder) so REST callers
   to /api/menus/[name]/items get the same protection as MCP callers.
   All seven name-based menu lookups now refuse ambiguous resolution
   and list the available locales in the error message.

2. Move the `translationOf` -> `locale` requirement from the MCP Zod
   .refine() into handleMenuCreate itself, returning VALIDATION_ERROR.
   The MCP boundary previously was the only enforcement point, leaving
   REST/SDK callers free to clone into the default locale by accident.

Addresses ask-bonk review note emdash-cms#3 on PR emdash-cms#939 and closes the gap noted
in the prior review of the AMBIGUOUS_LOCALE follow-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@schiste

schiste commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

ready for re-review

@ascorbic

ascorbic commented May 9, 2026

Copy link
Copy Markdown
Member

/review

@ask-bonk ask-bonk 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.

Solid follow-up to the earlier review notes. The fix correctly closes the silent-cross-locale-write bug, the AMBIGUOUS_LOCALE expansion to all seven name-keyed lookups is the right call (fail-loud beats arbitrary tie-break), and lifting the translationOf -> locale guard from the MCP boundary into handleMenuCreate is exactly what was asked for. Tests cover the targeted behavior plus transaction rollback, which is the bit most likely to regress quietly.

Three small notes inline, none blocking. The PR is good to merge as-is.

deleted and the new list is inserted in the order provided. Use this rather
than per-item add/remove operations so the resulting order and parent links are
unambiguous. On multi-locale installs, pass `locale` so only the intended
translation is rewritten.

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.

Not blocking, doc gap: the menu write tools (menu_update, menu_delete, menu_set_items, plus the per-item ones) now return a structured AMBIGUOUS_LOCALE (HTTP 400) when called without locale on a multi-locale install. That's an observable behavior change for MCP callers — worth one sentence here so users don't have to discover it the hard way. Something like:

On multi-locale installs, omitting locale when the same menu name exists in more than one locale returns AMBIGUOUS_LOCALE (the message lists the available locales). Pass locale to disambiguate.

if (matches.length === 0) {
return {
success: false,
error: { code: "NOT_FOUND", message: "Menu not found" },

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.

Nit, pre-existing on main but the PR is rewriting the surrounding lines: the item-level handlers (handleMenuItemCreate/Update/Delete/Reorder) still say "Menu not found" here without interpolating menuName or the options.locale, while handleMenuUpdate/Delete/SetItems now produce the more debuggable "Menu '<name>' not found in locale '<locale>'". The AMBIGUOUS_LOCALE messages on the item handlers do include the name (via the shared helper), so the inconsistency is only on the NOT_FOUND branch. Cheap cleanup if you want to do it while you're here:

Suggested change
error: { code: "NOT_FOUND", message: "Menu not found" },
if (matches.length === 0) {
return {
success: false,
error: {
code: "NOT_FOUND",
message: `Menu '${menuName}' not found${
options.locale ? ` in locale '${options.locale}'` : ""
}`,
},
};
}

Or leave for a follow-up — flagging because the diff is already touching every one of these blocks.

if (error === ambiguousSentinel && ambiguousLocales) {
return ambiguousMenuLocaleError(menuName, ambiguousLocales);
}
throw error;

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.

Defensive nit, not blocking: if (error === ambiguousSentinel && ambiguousLocales) is technically correct because the sentinel is only thrown after ambiguousLocales is assigned, but if anyone ever moves the assignment or the throw, the truthy check will silently swallow the ambiguous case and re-throw as the outer 500. Two cheap options: (a) split into two ifs with an else throw so a future refactor can't reorder them into a no-op, or (b) just check error === ambiguousSentinel and assert(ambiguousLocales !== null) after the throw is caught — the invariant is real and should fail loud if it's ever broken.

Not a real bug today; calling out as a future-proofing thing.

@ask-bonk

ask-bonk Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

Review posted successfully. Summary of what I posted:

  • Approve-shaped review, three inline non-blocking notes.
  • Verdict: Fix correctly closes the silent cross-locale write bug; the AMBIGUOUS_LOCALE expansion to all seven name-keyed lookups is the right call; lifting the translationOf→locale guard into handleMenuCreate addresses the prior reviewer's REST-bypass concern. Tests cover targeted behavior plus transaction rollback.
  • Inline notes:
    1. Doc gap (mcp-server.mdx) — AMBIGUOUS_LOCALE is now an observable behavior change for MCP callers but isn't documented in the menu tool reference. Suggested a one-sentence note.
    2. Pre-existing inconsistency (menus.ts:532) — Item handlers still say "Menu not found" without name/locale interpolation while menu-level handlers now produce "Menu '<name>' not found in locale '<locale>'". PR is touching all these blocks; cheap to align. Posted as a suggestion block.
    3. Defensive nit (menus.ts:873) — if (error === ambiguousSentinel && ambiguousLocales) is correct today but a future refactor that reordered assignment/throw would silently swallow the ambiguous case. Suggested splitting the branches or asserting the invariant.

github run

@ascorbic
ascorbic merged commit f1d4c0b into emdash-cms:main May 9, 2026
31 checks passed
@ascorbic

ascorbic commented May 9, 2026

Copy link
Copy Markdown
Member

Thanks!

@emdashbot emdashbot Bot mentioned this pull request May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants