vite-react-mcp

MCP.Pizza Chef: jazelly

vite-react-mcp is a Vite plugin client that creates an MCP server to enable large language models (LLMs) to interact with and understand the context of React applications. It provides features like highlighting React components by name, retrieving component props, states, and contexts in JSON format, and extracting the React component tree in ASCII format. This client facilitates real-time, structured communication between LLMs and React app environments, enhancing AI-driven workflows and debugging.

Use This MCP client To

Highlight React components by name for debugging or demonstration Retrieve React component props, states, and contexts as JSON Extract and visualize React component tree structure in ASCII format Enable LLMs to query and interact with React app state dynamically Support AI-assisted UI testing by exposing component states Facilitate real-time React app introspection for development tools

README

vite-react-mcp

npm version npm downloads

A Vite plugin that creates an MCP server to help LLMs understand your React App context

Features

  • highlight-component

    • description: Highlight React component based on the component name.
    • params:
      • componentName: string

    highlight-component

  • get-component-states

    • description: Get the React component props, states, and contexts in JSON structure format.
    • params:
      • componentName: string

    get-component-states

  • get-component-tree

    • description: Get the React component tree of the current page in ASCII format.
    • params:
      • allComponent: boolean, if truthy, return a tree for all components instead of your self-defined components only.

    get-component-tree

  • get-unnecessary-rerenders

    • description: Get the wasted re-rendered components of the current page.
    • params:
      • timeframe: number, if present, only get unnecessary renders within the last timeframe seconds. If not, get all unnecessary renders happened on the current page.
      • allComponent: boolean, if truthy, get unnecessary renders for all components instead of self-defined components only.

    get-unnecessary-rerenders

Getting Started

Installation

pnpm install vite-react-mcp -D

You also need @babel/preset-react installed, as this plugins traverses AST to collect your React components names.

pnpm install @babel/preset-react

Usage

// vite.config.ts
import ReactMCP from 'vite-react-mcp'

export default defineConfig({
  plugins: [ReactMCP()],
})

At this point, you already can access window.__VITE_REACT_MCP_TOOLS__ to use the tools in Developer panel on your browser.

To use it as an MCP server, setup MCP configuration in your MCP client.

  • For Cursor, create a ./cursor/mcp.json at the root level of your react project.

    {
      "mcpServers": {
        "vite-react-mcp": {
          "url": "http://localhost:3000/sse"
        }
      }
    }

    Make sure the port is the same as your react app

  • For Claude Desktop, it requires a bit of workaround. If you are interested, you can take a look at this thread.

    The reason is Claude MCP Client does execution based on command, while what we have here is HTTP based API. You need to write a script acting as a bridge to make it look like execution based.

How it works

MCP exposes a tool-call api through natural language. The tool itself is injected on your browser runtime. It works without requiring React Devtools extension, as we use bippy, which injects a __REACT_GLOBAL_DEVTOOLS_HOOK__ to window. The tool then is triggered from vite's websocket call to do corresponding actions by receiving mcp tool call command from the mcp client you interact.

Test

pnpm run playground

The playground contains a simple user profile application to test React component interactions.

Acknowledgement

This project is inspired by vite-plugin-vue-mcp. Thanks for the awesome idea bridging mcp and devtools.

License

MIT

vite-react-mcp FAQ

How do I install vite-react-mcp in my React project?
You can install vite-react-mcp via npm or yarn and add it as a plugin in your Vite config file to enable MCP features in your React app.
Can vite-react-mcp highlight any React component dynamically?
Yes, by specifying the component name, vite-react-mcp can highlight that component in the app UI for easy identification.
How does vite-react-mcp expose component states and props?
It provides a method to retrieve component props, states, and contexts in a structured JSON format accessible to LLMs.
Is vite-react-mcp compatible with all React versions?
vite-react-mcp is designed to work with modern React versions supported by Vite, but compatibility should be verified for very old React versions.
Can vite-react-mcp provide a full component tree of the current page?
Yes, it can generate an ASCII format tree of the React component hierarchy, optionally including all components or only user-defined ones.
Does vite-react-mcp support real-time updates of component states?
It supports querying current component states on demand, enabling real-time introspection during development.
What LLM providers can integrate with vite-react-mcp?
vite-react-mcp can integrate with any LLM provider compatible with MCP, including OpenAI, Anthropic Claude, and Google Gemini.
How secure is the data exposed by vite-react-mcp?
Data exposure is scoped and controlled by the plugin configuration, ensuring only intended component data is accessible to LLMs.