nuxt-mcp-dev

repository·main·Indexed 21 days ago

https://github.com/antfu/nuxt-mcp-dev

An experimental collection of tools providing Model Context Protocol (MCP) server support for Nuxt and Vite development environments. It includes the `nuxt-mcp-dev` Nuxt module and `vite-plugin-mcp` Vite plugin, enabling AI models to interact with local application structures. Features include built-in tools for Nuxt inspection (config, auto-imports, components, pages), a `nuxt-scaffold` tool for file generation, and automatic configuration updates for AI tools like Cursor, VSCode, Windsurf, and Claude Code.

Tokens
4.3K
Snippets
12
Records
16
Agent score
74%

What's inside nuxt-mcp-dev

  1. Overview of nuxt-mcp-dev / vite-plugin-mcp

    main

    The nuxt-mcp-dev monorepo provides Model Context Protocol (MCP) server capabilities to help AI models better understand your local development environment. It is divided into two primary packages depending on your framework:

    • nuxt-mcp-dev: A Nuxt module that adds MCP support directly to your Nuxt development server.
    • vite-plugin-mcp: A Vite plugin that adds MCP support to any Vite-based application.
    IMPORTANT

    This project is experimental. Use it with caution in your development workflows.

  2. How ViteMcp automatically updates AI tool configurations

    main

    When updateConfig is enabled (set to 'auto' or a specific list), the plugin attempts to write the MCP SSE URL into the configuration files of supported AI tools. This allows tools to immediately recognize and connect to your local development MCP server.

    Supported tools and their configuration locations:

    • Cursor: Writes to .cursor/mcp.json. Uses the mcpServers key.
    • VSCode: Writes to .vscode/mcp.json. Uses the servers key with type: 'sse'.
    • Windsurf: Writes to ~/.codeium/windsurf/mcp_config.json. Uses the mcpServers key with serverUrl.
    • Claude Code: Writes to .mcp.json. Uses the mcpServers key with type: 'sse'.

    If updateConfigAdditionalServers is provided, those servers are also injected into these files alongside the primary Vite MCP server.

  3. Configure vite-plugin-mcp options

    main

    The ViteMcpOptions interface defines the configuration for the vite-plugin-mcp plugin. You can use these options to control the MCP server's network settings, route, and how it integrates with IDE configuration files.

    Network and Routing

    • host: The host to listen on (defaults to localhost).
    • port: The port to listen on (defaults to the Vite dev server port).
    • mcpRouteRoot: The root route for the MCP server (defaults to /__mcp).
    • printUrl: Whether to print the MCP server URL in the console (defaults to true).

    MCP Server Customization

    • mcpServer: A function that receives the ViteDevServer and returns an McpServer. Providing this will cause the built-in MCP tools to be ignored.
    • mcpServerInfo: Provides MCP server metadata. This is ignored if mcpServer is provided.
    • mcpServerSetup: A lifecycle hook called when the MCP server is created. It receives the McpServer and ViteDevServer. You can use this to modify the existing server or return a new one to replace it.

    IDE Configuration Updates

    • updateConfig: Controls which IDE configuration files are automatically updated with the MCP server details.
      • 'auto': Automatically update existing config files.
      • false: Disable automatic updates.
      • SupportedUpdateConfigType[]: Specify specific targets like 'cursor', 'vscode', 'windsurf', or 'claude-code'.
    • updateConfigServerName: The name used for the server in the generated config files (defaults to 'vite').
    • updateConfigAdditionalServers: An array of UpdateConfigAdditionalServer objects to include in the configuration updates.

    Deprecated Options

    • mcpPath: Use mcpRouteRoot instead.
    // Example configuration
    import mcp from 'vite-plugin-mcp'
    
    export default {
      plugins: [
        mcp({
          mcpRouteRoot: '/my-mcp',
          updateConfig: ['cursor', 'vscode'],
          updateConfigServerName: 'my-custom-server',
          mcpServerSetup: async (server, viteServer) => {
            // Custom setup logic here
          }
        })
      ]
    }
  4. Configure ViteMcp options

    main

    The ViteMcp plugin accepts a ViteMcpOptions object to customize its behavior. Key options include:

    • printUrl: (boolean, default: true) Whether to log the MCP SSE URL to the console.
    • mcpRouteRoot: (string) The base path for the MCP routes. Defaults to options.mcpPath or '/__mcp'.
    • mcpPath: (string) Alternative base path for MCP routes.
    • mcpServer: (function, default: createMcpServerDefault) A function that receives the ViteDevServer and returns an MCP server instance. This allows for custom server implementations.
    • mcpServerSetup: (function) A hook to perform additional setup on the created MCP server instance.
    • updateConfig: (boolean | string | string[]) Controls automatic configuration updates for AI tools.
      • false: Disables automatic updates.
      • 'auto': Automatically detects and updates supported tools (Cursor, VSCode, Windsurf, Claude Code) based on existing config files in the workspace.
      • string[]: A specific list of tool identifiers to update.
    • updateConfigServerName: (string, default: 'vite') The name used to identify the server in the tool's configuration file.
    • updateConfigAdditionalServers: (Array<{ name: string, url: string }>) A list of additional MCP servers to include in the generated configuration files.
    • host: (string) The host to use for the SSE URL (defaults to 'localhost').
    • port: (number) The port to use for the SSE URL (defaults to the Vite server port).
    import { ViteMcp } from 'vite-plugin-mcp'
    
    export default {
      plugins: [
        ViteMcp({
          mcpRouteRoot: '/my-mcp',
          updateConfig: ['cursor', 'vscode'],
          updateConfigServerName: 'my-custom-server',
          updateConfigAdditionalServers: [
            { name: 'other-server', url: 'http://localhost:3000/sse' }
          ]
        })
      ]
    }
  5. Configure nuxt-mcp-dev module options

    main

    The nuxt-mcp-dev module is configured using the mcp key in your Nuxt configuration. It extends the options provided by vite-plugin-mcp and adds a specific option for Nuxt documentation integration.

    Options

    OptionTypeDefaultDescription
    includeNuxtDocsMcpbooleantrueIf true, includes the online Nuxt MCP server (https://nuxt.com/mcp) which provides information about the Nuxt ecosystem, documentation, and available modules.
    portnumbernuxt.options.devServer.portThe port used by the MCP server.
    updateConfigAdditionalServersArray<{ name: string, url: string }>[]Additional MCP servers to include in the configuration.
    mcpServerSetup(mcp: any, vite: any) => Promise<void>undefinedA callback function to perform custom setup on the MCP server and Vite instance.
    export default defineNuxtConfig({
      modules: ['nuxt-mcp-dev'],
      mcp: {
        includeNuxtDocsMcp: true,
        port: 3000,
        // Additional custom setup
        mcpServerSetup: async (mcp, vite) => {
          // custom logic here
        }
      }
    })
  6. Use the `nuxt-basic` MCP prompt

    main

    The nuxt-basic prompt is a built-in Model Context Protocol (MCP) prompt designed to provide an AI assistant with essential context about your Nuxt application. When invoked, it provides the AI with details regarding:

    • Project Structure: Root, App, Source, and Server directory paths.
    • Rendering Mode: Whether the project uses SSR (Server-Side Rendering) or CSR (Client-Side Rendering), including guidance on using import.meta.client and import.meta.server for isomorphic code.
    • Auto-imports Configuration: Whether Nuxt's auto-import system is enabled or disabled, and how to handle imports (e.g., using import { xxx } from '#imports' if disabled).

    This prompt helps the AI understand the environment constraints and best practices specific to your current Nuxt configuration.

  7. Scaffold Nuxt files with the `nuxt-scaffold` tool

    main

    The nuxt-scaffold tool allows you to automatically generate new files (such as components, pages, layouts, or middleware) within your current Nuxt project using the Nuxt CLI's nuxi add command. This tool is exposed via the MCP server.

    Parameters

    ParameterTypeDescription
    templateenumThe type of file to scaffold. See Supported Templates for the full list.
    namestringThe name of the file/component to create.

    Supported Templates

    You can scaffold the following types:

    • api
    • app
    • app-config
    • component
    • composable
    • error
    • layer
    • layout
    • middleware
    • module
    • page
    • plugin
    • server-middleware
    • server-plugin
    • server-route
    • server-util
    // Example tool call via MCP
    // template: 'component', name: 'MyButton'
    // This effectively runs: nuxi add component MyButton
  8. Register MCP runtime tools with useToolsRuntime

    main

    The useToolsRuntime function provides a way to register a set of built-in MCP (Model Context Protocol) tools that allow an AI agent to inspect the Nuxt application's runtime state. This is typically used within the MCP server setup to expose Nuxt-specific metadata to the LLM.

    To use it, call useToolsRuntime() and pass the resulting registerTools function to your MCP server initialization logic, providing an McpToolContext which contains the mcp instance and the unimport promise.

    import { useToolsRuntime } from 'nuxt-mcp-dev'
    
    // Inside your MCP server setup
    const { registerTools } = useToolsRuntime()
    
    registerTools({
      mcp: mcpInstance, // The MCP server instance
      unimport: unimportPromise // The unimport instance/promise
    })
  9. Use the mcp:setup hook to extend MCP tools

    main

    The nuxt-mcp-dev module provides a mcp:setup hook that allows you to register custom tools or logic within the MCP context. This hook is called during the mcpServerSetup phase.

    The hook receives an McpToolContext object containing:

    • unimport: The unimport instance for auto-importing utilities.
    • nitro: The nitro instance for server-side logic.
    • nuxt: The Nuxt instance.
    • vite: The Vite instance.
    • mcp: The MCP server instance.

    To use the hook, extend the NuxtHooks interface in your project.

    // In a Nuxt plugin or module
    export default defineNuxtModule({
      setup(options, nuxt) {
        nuxt.hook('mcp:setup', async (context) => {
          // context contains unimport, nitro, nuxt, vite, and mcp
          // Register your custom MCP tools here
        })
      }
    })
  10. Use the ViteMcp plugin

    main

    The ViteMcp function is a Vite plugin that sets up a Model Context Protocol (MCP) server within your Vite development environment. It automatically exposes an SSE (Server-Sent Events) endpoint and can automatically update configuration files for various AI-powered IDEs and tools like Cursor, VSCode, Windsurf, and Claude Code so they can connect to your local MCP server.

    import { ViteMcp } from 'vite-plugin-mcp'
    
    export default {
      plugins: [
        ViteMcp({
          // options here
        })
      ]
    }
  11. Create a default MCP server with createMcpServerDefault

    main

    The createMcpServerDefault function is a factory that initializes a Model Context Protocol (MCP) server instance pre-configured with Vite-specific tools. It integrates with an existing ViteDevServer to expose Vite's internal state and module graph to MCP clients.

    By default, the server includes two tools:

    1. get-vite-config: Retrieves the Vite configuration digest (root, resolve, plugin names, and environment names).
    2. get-vite-module-info: Retrieves detailed graph information for a specific module (importers, imported modules, transform results, etc.) across all Vite environments.

    To use this, you must provide a ViteMcpOptions object and an active ViteDevServer instance.

    import { createMcpServerDefault } from 'vite-plugin-mcp'
    import type { ViteDevServer } from 'vite'
    import type { ViteMcpOptions } from './types'
    
    // Assuming 'vite' is an instance of ViteDevServer
    const vite: ViteDevServer = /* ... */;
    
    const options: ViteMcpOptions = {
      mcpServerInfo: {
        name: 'my-custom-vite-server',
        version: '1.0.0'
      }
    };
    
    const server = createMcpServerDefault(options, vite);