Replies: 1 comment 1 reply
-
Thanks for building and sharing! I can see how this MCP might be useful if you're already using Storybook in order to help you build your app using existing stories and maintain your stories etc. But I'm curious if you think it'll help someone like me who is new to Storybook. Do you think this MCP would or could help in the following contexts that don't currently use Storybook?
Thanks! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
🤖 Problem
When you're building UI using agentic programming, you lack visibility into the UI that's being created, and it's hard to verify that things work properly. LLMs can generate UI components and code, but developers need a reliable way to see, test, and validate the visual output before integrating it into their applications.
Storybook is a tool that provides those capabilities for humans, but it doesn’t integrate very well with LLMs today.
Note
We’ve identified two separate uses of Storybook in relation to LLMs.
@storybook/addon-mcp
and this RFC focuses on improving the development experience when using Storybook in a project - this is focused on the users that are building components with Storybook.The other use case focuses on consumers of Storybook. That is not necessarily users that are building with Storybook (although they could be), but they are building UI and using a Storybook as documentation. We think that is a separate workflow to investigate, and we’re discussing it in the Storybook Design Systems with Agents RFC
🧩 Proposed Solution
The experimental MCP addon in this repository is an MCP server that users can add to their agents to improve the agent’s understanding of Storybook. It is packaged as a regular Storybook addon, it supports Storybook 7 and up (for now) but only for Vite-based projects.
See the documentation on how to setting up and get started with the addon.
Along with the MCP server that the addon spawns, we also have a prompt that we recommend users add to their system prompts:
When users start up their Storybook dev server, the MCP addon starts up an MCP server on
http://localhost:PORT/mcp
that agents can connect to.The MCP server exposes two tools for the agent to use:
UI Building Instructions Tool
This tool currently responds with instructions about:
These instructions tend to nudge the LLM to write stories when relevant, teaches the LLM how to use up-to-date syntax and APIs - because it’s baseline knowledge from training data is generally okay, just outdated - and also reliably provide relevant story links for the user to inspect the result of the work.
The instructions are “hidden” behind a tool call, so it doesn’t pollute the context when working on non-UI related tasks.
Story URL Tool
This tool provides the LLM with a programmatic way to turn any story path + name into a valid Storybook URL. This enables the LLM to provide correct links to stories in the running Storybook based on its own selection of which stories are relevant for a user to inspect.
🔮 Future Work
We’ve just scratched the surface of what’s possible here, and we’ve already identified key areas to focus on.
We need to have a clearer definition of what a “good” story is. If we are to write clear instructions for the LLM to improve its story writing capabilities, we first need to align on a clear set of best practices to follow. Eg. it’s not clear cut how much should be asserted on in play-functions, how exhaustive stories should cover prop combinations, etc.
Prompt Engineering on the UI Building Instructions. We could spend a lot more time on fine tuning the instructions and the wording to make the LLM understand them better. Potentially migration notes don’t produce the best outcome, but instead a distilled, LLM-friendly version of the documentation (like how Svelte-LLM does it) produces better results. There are also more obvious improvements, like not including Storybook 9 migration docs if we detect that the current project is running Storybook 8.
Build automated evaluation setup. Our research methodology was rigorous and included clear experiments with expected outputs and structured findings. However it was a manual process trying out each scenario with each prompt, and that doesn’t scale well. Changing the wording on a prompt required re-running a matrix of 15 experiments and logging the results. We need to improve this process so that we can evaluate our iterations in a more automated setup, like what something like evalite provides.
We also have an abundance of ideas for how the MCP server can improve the workflow in other areas. (and maybe you do too? 🙏) For now we’ve focused on the first-shot attempt at writing stories and UI, and improving the LLMs general knowledge. Other ideas include:
💬 Request For Comments
We’re asking for input and feedback here. We hope you’ll try this out, see what works for you and what doesn’t. Please share thoughts and ideas on what could be improved, or suggest changes to the prompts, etc. 🙏
🧪 Research and Findings
If you’re curious about how we got here, we’ve written about our methodology and research findings too, but it’s not required reading for this RFC.
❓Questions
To research how we could improve an agent-based development workflow with Storybook, we defined a set of goals:
To reach these goals we defined a set of questions we needed to answer:
How do we make the LLM consistently write and update stories when working with UI?Can we change an LLMs behavior so the user doesn’t need to explicitly ask the LLM to write stories, but instead the LLM just knows that it’s a core part of any tasks that requires UI work?
How can we make an LLM write “good” stories?
How can we make an LLM send valid story links to the user when relevant?
Investigating these questions and trying out different solutions is what has ultimately led to the experimental release of
@storybook/addon-mcp
:📖 UI Building Instructions Tool
We found that “hiding” these instructions behind an MCP tool was an effective way of managing the LLMs’ context as much as possible. In general, minimising the usage of LLMs’ context windows is critical to get good results. Not only are there hard limits and costs associated with having too many tokens in the context. LLMs perform worse and worse the more context they have - similar to how humans struggle to focus on a task if they get bombarded with irrelevant information. Our findings show that generally the LLM will only request the UI instructions when it’s working on UI, ensuring that it doesn’t consider it when it’s working on other tasks eg. backend only.
The combination of the system prompt, the tool’s description, and the paragraph about when to write stories have proven to be an effective way to nudge the LLM to write and update stories when working on UI components, without requiring a specific user prompt for it. However AI’s are unreliable, models, agents and clients all behave different, so it’s not a surefire way to achieve this. We believe that the reliability can be improved in the future by iterating on the prompts (feedback and ideas welcome! 🙏).
We found that generally speaking, LLMs have an okay understanding of CSF and how to write stories already embedded in their training data from all the publicly available content there already exists about Storybook today. However they struggle to stay up to date, and so providing the migration guide and instructions on new features was a way to ensure it wrote valid and modern stories. It had a noticeable impact on the validity of the output, however there’s still much to be desired when it comes to writing “good” stories. More Prompt Engineering™ is needed to make it understand best practices better, eg. relevant usage of assertions in play-functions and useful args combinations.
Finally, including clear instructions on when and how to provide story links to the users proved to be very effective. Our findings indicate that with the MCP server the LLM will consistently end tasks with a set of links that the user can visit to inspect the UI work the LLM has performed. Not only when creating new stories, but also when working on UI only without any story modifications.
🔗 Story URL Tool
An exact URL to a story requires a few things:
http://localhost:6006
./src/Button.stories.ts
WithLongLabel
Long Label
With this information it is possible for the tool to programmatically look up the story index and find the stories’ IDs. From there it’s just about combining the IDs with the origin correctly.
To get an understanding of the LLMs knowledge and capabilities we first explored if it could generate these URLs on its own, without assistance from a tool. We found that it worked in the most basic scenarios, where stories-files had explicit meta titles, no custom story name, and where running on localhost:6006. That was too limiting for practical, but interesting nonetheless that it understood the Storybook URL structure.
We then gave the LLM information about how Storybook internally constructs the story IDs, by giving it the raw code that does it, or alternatively an LLM generated description of the code. This was very reliable, and the LLM correctly gathered all the necessary information and produced valid links in all advanced scenarios. But it struggled to get the origin right when it was not
localhost:6006
, which led to the creation of the tool that does it programmatically and reliably.In future iterations we need to make sure the LLM always passes stories’ custom names into the tool. Currently it’s a two-shot process where the tool will return an error reminding the LLM to pass in a story’s custom name, and then the LLM will get it right the second time.
🤔 Why an addon
There are many ways to distribute an MCP server. Here’s a quick overview of what we explored and the pros and cons:
npx
command or similar. This is the regular way to distribute MCP servers, and is capable of accessing anything locally. The downside of this is that if it needs access to anything from the Storybook runtime - stories, the index, statuses, etc. - it would have to request that from a running Storybook dev server, and we would have to expose that in API endpoints in Storybook core. This would be cumbersome to maintain, it would be slower to iterate on and release, and the MCP server would only be compatible with specific Storybook versions.afterEach
-hooks, etc. The MCP server will be tied to a specific instance, and in the future we’d need to figure out how that would play out in monorepo situations where there might be multiple Storybooks running at the same time.🔍 Related Work
Beta Was this translation helpful? Give feedback.
All reactions