Vitest MCP Server

Vitest MCP Server

AI-optimized Vitest interface that provides structured test output, visual debugging context, and intelligent coverage analysis for more effective AI assistance with testing.

Category
访问服务器

Tools

run_tests

Execute Vitest commands with configurable output formatting optimized for LLM consumption

list_tests

Find and list test files in the project or specified directory

analyze_coverage

Run comprehensive coverage analysis with detailed metrics about line, function, and branch coverage

README

Vitest MCP Server

AI-optimized Vitest interface with structured output, visual debugging, and intelligent coverage analysis.

Problem & Solution

The Problem

When AI assistants help with testing, they typically run raw Vitest commands that produce:

  • Verbose terminal output that's hard for AI to parse
  • Missing failure context - no code snippets or visual indicators
  • Accidental full test runs when no target is specified
  • Basic coverage metrics without actionable insights

The Solution

This MCP server provides AI-optimized testing tools that deliver:

  • Structured JSON output designed for AI consumption
  • Visual debugging context with code snippets and failure markers
  • Intelligent targeting prevents accidental full test suite runs
  • Coverage gap analysis with line-by-line insights and recommendations

Features

  • 🎯 Smart Test Execution - Run specific files/folders with structured output
  • 📊 Coverage Analysis - Line-by-line gap analysis with actionable insights
  • 📁 Test Discovery - Find and organize test files across your project
  • 🔗 Claude Code Hooks - Automatic interception of Vitest commands
  • 🛡️ Safety Guards - Prevents accidental full project test runs
  • 🏢 Multi-Repository Support - Work across multiple projects in a single session

Quick Start

1. Add to Claude Desktop

Add this to your Claude Desktop configuration:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%/Claude/claude_desktop_config.json
{
  "mcpServers": {
    "vitest": {
      "command": "npx",
      "args": ["-y", "@djankies/vitest-mcp"]
    }
  }
}

2. Restart Claude Desktop

3. Set Project Root (Required)

Before using any tools, you must first specify which repository to work with:

set_project_root({ path: "/absolute/path/to/your/project" })

4. Start Using

Ask Claude to:

  • "Run tests in the components folder"
  • "Check coverage for the auth module"
  • "List all test files"

Requirements

Minimum Versions

  • Node.js: 18+
  • Vitest: 0.34.0+ (3.0.0+ recommended)
  • Coverage Provider: @vitest/coverage-v8 (for coverage analysis)
npm install --save-dev vitest@latest @vitest/coverage-v8@latest

Project Setup

Ensure your vitest.config.ts includes:

export default defineConfig({
  test: {
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html']
    }
  }
})

Tools

set_project_root (Required First)

IMPORTANT: This must be called before using any other tools.

Set the project root directory for all subsequent operations.

set_project_root({ 
  path: "/Users/username/Projects/my-app" 
})

Parameters:

  • path (required): Absolute path to the project root directory

Features:

  • Validates project has package.json or vitest.config
  • Prevents running on the MCP package itself
  • Supports path restrictions via configuration

list_tests

List test files in your project.

list_tests({ directory: "./src" })

run_tests

Execute tests with AI-optimized output.

run_tests({ 
  target: "./src/components", 
  format: "detailed", // or "summary"
  project: "client" // optional: specify Vitest project (for monorepos)
})

Parameters:

  • target (required): File path or directory to test
  • format (optional): Output format - "summary" or "detailed"
  • project (optional): Name of the Vitest project (as defined in vitest.workspace or vitest.config)

analyze_coverage

Analyze test coverage with gap insights.

analyze_coverage({
  target: "./src/api",
  threshold: 80,
  format: "detailed",
  exclude: ["**/*.stories.*", "**/e2e/**"]  // Optional: exclude patterns
})

Parameters:

  • target (required): File path or directory to analyze
  • threshold (optional): Coverage threshold percentage (default: 80)
  • format (optional): "summary" or "detailed" output
  • exclude (optional): Array of patterns to exclude from coverage (e.g., Storybook files)

Default excludes automatically applied:

  • **/*.stories.* - Storybook story files
  • **/*.story.* - Alternative Storybook naming
  • **/.storybook/** - Storybook configuration directories
  • **/storybook-static/** - Built Storybook files
  • **/e2e/** - End-to-end test directories
  • **/*.e2e.* - E2E test files
  • **/test-utils/** - Test utility directories
  • **/mocks/** - Mock directories
  • **/__mocks__/** - Jest-style mock directories
  • **/setup-tests.* - Test setup files
  • **/test-setup.* - Alternative test setup naming

Multi-Repository Workflow

Work across multiple projects in a single Claude session:

// Switch to project A
set_project_root({ path: "/Users/username/Projects/frontend" })
run_tests({ target: "./src/components" })

// Switch to project B
set_project_root({ path: "/Users/username/Projects/backend" })
run_tests({ target: "./src/api" })

// Switch back to project A
set_project_root({ path: "/Users/username/Projects/frontend" })
analyze_coverage({ target: "./src/utils" })

Claude Code Hook Integration (Recommended)

Automatically intercept Vitest commands and suggest MCP tools.

Copy hook script

curl -o .claude/vitest-hook.sh https://raw.githubusercontent.com/djankies/vitest-mcp/main/hooks/vitest-hook.sh
chmod +x .claude/vitest-hook.sh

Update .claude/settings.local.json

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": ".claude/vitest-hook.sh"
          }
        ]
      }
    ]
  }
}

Monorepo Support

The run_tests tool supports Vitest projects in monorepo setups through the project parameter:

// Run tests for a specific project in a monorepo
run_tests({
  target: "./packages/client/src",
  project: "client"  // Matches project name in vitest.workspace.ts
})

// Run tests for another project
run_tests({
  target: "./packages/api/src", 
  project: "api"
})

This works with:

  • Vitest workspace configurations (vitest.workspace.ts)
  • Projects defined in vitest.config.ts with the projects option
  • Yarn/npm/pnpm workspace monorepos

Configuration Options

Project Configuration (.vitest-mcp.json)

Create a .vitest-mcp.json file in your home directory or project root:

{
  "safety": {
    "allowedPaths": ["/Users/username/Projects"]
  },
  "testDefaults": {
    "format": "detailed",
    "timeout": 60000
  },
  "coverageDefaults": {
    "threshold": 80,
    "format": "detailed",
    "exclude": [
      "**/*.stories.*",
      "**/e2e/**"
    ]
  }
}

Security Options:

  • allowedPaths: Restrict set_project_root to specific directories (string or array)

MCP Server Options

{
  "mcpServers": {
    "vitest": {
      "command": "npx",
      "args": [
        "-y", "@djankies/vitest-mcp",
        "--format", "detailed",
        "--timeout", "60000"
      ]
    }
  }
}

Available CLI Options

  • --format <summary|detailed> - Default output format
  • --timeout <ms> - Test timeout (default: 30000)
  • --verbose - Debug information

Troubleshooting

Storybook Configuration Errors

If you encounter errors like TypeError: Cannot create property 'exclude' on boolean 'true' when running coverage analysis on projects with Storybook, this is typically caused by configuration conflicts. See STORYBOOK_ISSUES.md for detailed solutions.

Quick Fix: Create a separate minimal Vitest config for coverage that doesn't load Storybook configuration.

Version Issues

# Check compatibility
npx -y @djankies/vitest-mcp --version-check

Common Issues

"Project root has not been set"

Always call set_project_root first:

set_project_root({ path: "/path/to/project" })

"Cannot set project root to the Vitest MCP package"

The tool prevents running on itself. Use it on other projects.

"Path is outside allowed directories"

Check your .vitest-mcp.json configuration for allowedPaths restrictions.

"Vitest not found"

npm install --save-dev vitest@latest

"Coverage provider not found"

npm install --save-dev @vitest/coverage-v8@latest

Hook not working

# Test hook directly
./.claude/vitest-hook.sh vitest run src/
# Bypass hook
npx vitest run src/ --bypass-hook

For AI/LLM Users

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选