Skip to content

design-system-mcp: Serve pattern guidance through get_patterns and get_pattern_details - #82548

Closed
juanmaguitar wants to merge 5 commits into
trunkfrom
add/design-system-mcp-pattern-guidance
Closed

juanmaguitar wants to merge 5 commits into
trunkfrom
add/design-system-mcp-pattern-guidance

Conversation

@juanmaguitar

@juanmaguitar juanmaguitar commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

What?

Adds get_patterns and get_pattern_details to the design system MCP server, serving the pattern guidance documents under storybook/stories/design-system/patterns/.

Closes #82547

Why?

The server answers which component to use, but not when to use it. Asked to build a delete-account flow with the MCP server as its only source, an agent gets ConfirmDialog back and nothing about when to use it versus a Modal with a destructive button, so it guesses. The decision tree that answers exactly that already exists in destructive-actions.mdx, and no tool serves it. Same shape as the Tooltip case in #80649 (comment).

No documentation is duplicated: the same .mdx files serve humans in the Storybook and agents through the MCP.

How?

  • storybook/scripts/generate-patterns-manifest.mjs runs as part of storybook:build, alongside the post-processors already applied to the components manifest, and writes build/manifests/patterns.json from every document in the patterns directory. Each entry's title and description come from the document's own heading and introductory paragraph, so the manifest cannot describe a pattern differently from how the Storybook presents it, and the content is the document with its Storybook framing (module imports, <Meta />, <Canvas />) stripped.
  • The server consumes that manifest the same way it consumes manifests/components.json, with PATTERNS_MANIFEST_URL to override the location.
  • Discovery is therefore automatic: a new .mdx in the directory is published and served with no registration step anywhere. A document without a heading or an introduction fails the build rather than shipping unnamed.
  • The two tools mirror get_components and get_component_details, including the array form for fetching several documents in one call.
  • The server instructions now point clients at the patterns before implementing a flow. A tool that is never called does not close the gap.

Per-component guidance (packages/ui/src/*/stories/usage-guidelines.mdx, best-practices.mdx) has the same problem but is scoped to a component rather than cross-cutting, so it fits better as an addition to get_component_details. Left as a follow-up, noted in the issue.

Testing Instructions

1. Unit tests

npm run test:unit:vitest -- storybook/test/generate-patterns-manifest packages/design-system-mcp

The generator test reads the real patterns directory and asserts the manifest has an entry for every .mdx in it, so a pattern cannot be added without being served.

2. Run the server, no coding agent needed

Node 24 runs the TypeScript sources directly, so this needs no build, and no Storybook deploy: the dev server generates the manifest itself and hands it to the server as a data: URL. Create packages/design-system-mcp/dev-server.mjs:

import { StdioServerTransport } from '@modelcontextprotocol/server';
import { generatePatternsManifest } from '../../storybook/scripts/generate-patterns-manifest.mjs';

const manifest = JSON.stringify( await generatePatternsManifest() );
process.env.PATTERNS_MANIFEST_URL = `data:application/json;base64,${ Buffer.from(
	manifest
).toString( 'base64' ) }`;

const { createServer } = await import( './src/index.ts' );
await createServer().connect( new StdioServerTransport() );

Then open it in MCP Inspector, which lists the tools and calls them from a form:

npx @modelcontextprotocol/inspector node packages/design-system-mcp/dev-server.mjs

Expected: five tools listed. get_patterns returns the three patterns, each described by its own opening paragraph. get_pattern_details with destructive-actions returns the decision tree as plain markdown, starting at # Destructive Actions, with no import line and no <Meta />. An unknown slug returns isError and points at get_patterns.

Drop an .mdx with a heading and a paragraph into storybook/stories/design-system/patterns/, restart, and it appears in get_patterns with no code change. Delete it afterwards, along with dev-server.mjs, which is deliberately not part of this PR.

To exercise the published artifact instead, run npm run storybook:build for a real build/manifests/patterns.json and npm run build for the package, then point the Inspector at packages/design-system-mcp/bin/design-system-mcp.mjs with PATTERNS_MANIFEST_URL set to that file, served over HTTP.

3. End to end, the part the unit tests cannot cover

The tests prove the tools return the right documents. They cannot prove an agent decides to call them, which is the actual gap this addresses. Register that dev server with an agent in an empty directory outside Gutenberg, and ask for something the component list alone does not answer:

Using the design system MCP as your only source, implement a delete-account action in React. Before writing code, tell me which component you use for the confirmation and why.

Expected: the agent calls get_patterns, then get_pattern_details for destructive-actions, and justifies the choice from the decision tree. Deleting an account cannot be undone, so it should reach for a Modal with a destructive button rather than ConfirmDialog. On trunk the same prompt returns ConfirmDialog with no reasoning available to back it.

Use of AI Tools

Claude Code was used to implement this change and to write the tests. All of it was reviewed before submitting.

🤖 Generated with Claude Code

juanmaguitar and others added 2 commits September 7, 2026 16:09
Pattern guidance lives as MDX under storybook/stories/design-system/patterns
and is not reachable through any of the server's tools. Add the index, the
fetching, and the MDX-to-markdown formatting, so the tools can serve it.

The index is maintained by hand because a document's title can be declared
indirectly (<Meta of={ ... } />), leaving no readable title in the source. A
test reads the stories directory and fails when the two fall out of sync.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The server answers which component to use, but not when to use it. Asked to
build a delete-account flow with the server as its only source, an agent gets
ConfirmDialog back and nothing about when to use it versus a Modal with a
destructive button, so it guesses.

The two tools mirror get_components and get_component_details, including the
array form for fetching several documents in one call. The server instructions
now point clients at the patterns before implementing a flow, since a tool that
is never called does not close the gap.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: juanmaguitar <juanmaguitar@git.wordpress.org>
Co-authored-by: aduth <aduth@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: 0 B

Total Size: 8 MB

baf2367 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric d60149e trunk % Change
timeToFirstByte 65.2 ms +17.18% -12.12% 66.05 ms +3.79% -10.07% -1.29%
largestContentfulPaint 104 ms +5.77% -11.54% 100 ms +6% -4% 4%
lcpMinusTtfb 34.05 ms +18.21% -6.17% 32.9 ms +29.03% -4.41% 3.5%
wpBeforeTemplate 35.2 ms +21.79% -16.96% 35.13 ms +4.55% -14.83% 0.2%
wpTemplate 25.37 ms +7.45% -4.69% 25.53 ms +4.74% -2.27% -0.63%
wpTotal 60.11 ms +18.28% -11.4% 60.78 ms +4.95% -8.67% -1.1%
wpMemoryUsage 7.52 MB +0% -0% 7.48 MB +0% -0% 0.46%
wpDbQueries 17 +0% -0% 17 +0% -0% 0%

front-end-classic-theme

Metric d60149e trunk % Change
timeToFirstByte 50.5 ms +3.76% -9.31% 52.9 ms +5.58% -9.64% -4.54%
largestContentfulPaint 98 ms +6.12% -2.04% 100 ms +6% -4% -2%
lcpMinusTtfb 48.55 ms +3.4% -2.99% 46.9 ms +9.38% -2.67% 3.52%
wpBeforeTemplate 30.42 ms +9.83% -10.91% 30.95 ms +14.12% -11.47% -1.71%
wpTemplate 15.23 ms +3.41% -1.12% 16.21 ms +5% -6.17% -6.05%
wpTotal 47.19 ms +2.86% -9.73% 49.04 ms +5.12% -11.22% -3.77%
wpMemoryUsage 5.51 MB +0% -0% 5.47 MB +0% -0% 0.65%
wpDbQueries 14 +0% -0% 14 +0% -0% 0%

media-processing

Metric d60149e trunk % Change
mediaProcessingJpeg 409.46 ms +1.13% -0.34% 411.88 ms +1.77% -1.58% -0.59%
mediaProcessingAvif 6230.08 ms +0.33% -0.32% 6241.91 ms +0.19% -0.25% -0.19%
mediaProcessingJpegToAvif 4419.82 ms +0.4% -0.45% 4397.88 ms +0.59% -0.05% 0.5%

media-upload

Metric d60149e trunk % Change
jpegUploadProcessing 1732.73 ms +14.16% -15.02% 1454.32 ms +0.76% -2.43% 19.14%
pngUploadProcessing 255.97 ms +5.25% -9.2% 239.65 ms +4.17% -2.77% 6.81%
largeJpegUploadProcessing 1450.33 ms +0.43% -1.37% 1429.1 ms +0.9% -0.42% 1.49%
multipleImageUploadProcessing 2142.84 ms +0.9% -7.71% 1605.7 ms +0.75% -0.57% 33.45%

post-editor

Metric d60149e trunk % Change
serverResponse 380.63 ms +5.81% -5.75% 378.78 ms +7.03% -7.89% 0.49%
firstPaint 229.44 ms +35.99% -28.13% 200.54 ms +5.56% -26.79% 14.41%
domContentLoaded 1047.13 ms +1.42% -2.33% 1037.18 ms +5.95% -1.89% 0.96%
loaded 1048.2 ms +1.41% -2.35% 1038.23 ms +5.95% -1.89% 0.96%
firstContentfulPaint 351.31 ms +5.43% -3.4% 351.67 ms +2.75% -5.37% -0.1%
firstBlock 2763.96 ms +1.08% -3.21% 2843.72 ms +0.42% -2.92% -2.8%
type 12.96 ms +3.09% -6.79% 13.53 ms +5.47% -4.07% -4.21%
typeWithoutInspector 12.38 ms +5.25% -3.96% 12.69 ms +7.8% -7.17% -2.44%
typeWithTopToolbar 16.31 ms +5.82% -7.11% 16.19 ms +14.08% -3.64% 0.74%
typeContainer 8.54 ms +2.93% -6.79% 8.52 ms +7.63% -7.28% 0.23%
focus 65.38 ms +7.77% -2.28% 62.97 ms +12.91% -4.8% 3.83%
firstFocus 141.75 ms +0% -0% 145.45 ms +0% -0% -2.54%
selectAll 383.53 ms +5.47% -0.63% 403.49 ms +6.14% -1.59% -4.95%
listViewOpen 57.41 ms +14.84% -9.14% 57.22 ms +16.43% -13.4% 0.33%
inserterOpen 19.75 ms +15.19% -5.06% 19.61 ms +8.62% -8.41% 0.71%
inserterHover 2.95 ms +20.68% -21.36% 3.5 ms +14% -26% -15.71%
inserterSearch 8.04 ms +11.07% -4.85% 7.34 ms +12.94% -10.08% 9.54%
loadPatterns 543.59 ms +9.12% -4.85% 540.27 ms +11.87% -3.26% 0.61%
wpTotal 372.17 ms +5.6% -5.81% 368.97 ms +7.07% -7.83% 0.87%
wpMemoryUsage 13.06 MB +0% -0% 13.02 MB +0% -0% 0.28%
wpDbQueries 54 +0% -0% 54 +0% -1.85% 0%

site-editor

Metric d60149e trunk % Change
serverResponse 415.2 ms +4.24% -5.14% 414.6 ms +2.34% -7.95% 0.14%
firstPaint 243.79 ms +19.23% -15.85% 239.98 ms +8.07% -14.46% 1.59%
domContentLoaded 1369.4 ms +1.59% -0.92% 1379.64 ms +1.93% -0.62% -0.74%
loaded 1370.49 ms +1.6% -0.9% 1380.96 ms +1.94% -0.63% -0.76%
firstContentfulPaint 439.84 ms +5% -1.72% 435.37 ms +4.31% -1.98% 1.03%
firstBlock 4280.71 ms +0.83% -0.9% 4324.68 ms +1.27% -0.83% -1.02%
type 14.54 ms +3.99% -1.99% 15.24 ms +6.23% -8.73% -4.59%
navigate 109.22 ms +8.24% -6.12% 99.9 ms +2.15% -8.32% 9.33%
loadPatterns 1498.88 ms +4.45% -10.51% 1454.14 ms +8.73% -11.73% 3.08%
loadPages 1097.04 ms +5.44% -0.51% 1095.25 ms +3.31% -1.46% 0.16%
wpTotal 404.27 ms +4.22% -5.3% 403.64 ms +2.4% -8.11% 0.16%
wpMemoryUsage 12.07 MB +0% -0% 12.03 MB +0% -0% 0.27%
wpDbQueries 43 +2.33% -0% 43 +2.33% -0% 0%

baf2367 Run

@github-actions github-actions Bot added the [Package] Design System MCP /packages/design-system-mcp label Sep 7, 2026
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@juanmaguitar
juanmaguitar marked this pull request as ready for review September 7, 2026 14:19
@juanmaguitar
juanmaguitar requested a review from a team as a code owner September 7, 2026 14:19
Registering each pattern by hand in the server made a new document silently
invisible to agents until someone remembered the extra step, and a test that
fails on the omission still leaves the step there.

The Storybook build now generates build/manifests/patterns.json from every
document in the patterns directory, alongside the components manifest it
already publishes, and the server only consumes it. Titles and descriptions
come from each document's own heading and introduction, so the manifest cannot
describe a pattern differently from how the Storybook presents it, and a new
pattern needs no registration anywhere.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@juanmaguitar

juanmaguitar commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Ran the testing instructions locally against the MCP Inspector. Recording the results so a reviewer does not have to repeat them.

Tools. Five listed: get_components, get_component_details, get_design_tokens, get_patterns, get_pattern_details.

get_patterns. Returns the three patterns, each described by its own opening paragraph rather than by prose written into the server.

see snapshot CleanShot 2026-09-07 at 16 39 31

get_pattern_details with destructive-actions. Returns the whole document starting at # Destructive Actions, with no import line and no <Meta />. It carries the part the component list cannot answer: the decision tree, and under "Use Modal with Destructive Button" the criterion (permanent, cannot be undone) plus the implementation detail (isDismissible={ false }, shouldCloseOnEsc={ false }, isDestructive={ true }, cancel as tertiary) and the accessibility notes. Deleting an account lands there, not on ConfirmDialog, which is exactly what the agent in #82547 guessed wrong.

see snapshot CleanShot 2026-09-07 at 16 41 22

Automatic discovery. Dropped an undo.mdx into storybook/stories/design-system/patterns/ with a heading, an introduction, and the usual <Meta /> framing. After a restart it appears as a fourth pattern, correctly described and stripped, with no code change anywhere. Removed it afterwards.

see command to create storybook/stories/design-system/patterns/undo.mdx
cat > storybook/stories/design-system/patterns/undo.mdx <<'EOF'
import { Meta } from '@storybook/addon-docs/blocks';

<Meta title="Design System/Patterns/Undo" />

# Undo

A throwaway document to check that a new pattern is discovered with no code change. Delete it after testing.

## When to Use

- Never. This is a test fixture.
EOF
see snapshot CleanShot 2026-09-07 at 16 43 02

@aduth

aduth commented Sep 8, 2026

Copy link
Copy Markdown
Member

Exciting to see this starting to get integrated, thanks for working on it @juanmaguitar ! I'll plan to give it a closer look tomorrow, but one initial thought I want to highlight based on first impression: Can we explore the Storybook-native equivalent of the components manifest for written documentation? I recall that they had a concept of a docs.json which exists alongside components.json and which I expected was designed for the exact sort of thing we're trying to do with exposing patterns guidance (per the documentation, "standalone documentation pages (e.g. a "Getting Started" guide, accessibility guidelines, design tokens, etc.)").

The manifests feature is still experimental and I know it's been changing pretty dramatically lately (which I encountered in #80697), so I'm not familiar with what the "state of the art" is here at the moment.

I think the most important thing to unblock us now would be to make sure that we don't bind ourselves to something that will be difficult to update later. A URL like https://wordpress.github.io/gutenberg/manifests/patterns.json could become invalid in the future, and old MCP clients will fail when trying to find it. We've had to deal with various migrations in #80697 and #77382 (inject-legacy-docgen.mjs), so I'd be interested to try to standardize sooner than later.

@Mamaduka

Copy link
Copy Markdown
Member

Should we close this? The linked issue is marked as resolved.

@aduth

aduth commented Nov 17, 2026

Copy link
Copy Markdown
Member

Yes, this was superseded by #84864, which built on this and credited @juanmaguitar 's work. The description there includes details about how it differs from this pull request.

Thanks @juanmaguitar for pushing this along, and @Mamaduka for the nudge to close it!

@aduth aduth closed this Nov 17, 2026
@Mamaduka
Mamaduka deleted the add/design-system-mcp-pattern-guidance branch November 17, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Design System MCP /packages/design-system-mcp [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Design System: Serve pattern guidance documents through the MCP server

4 participants