@djankies/vitest-mcp

中文文档
0.6.0发布于 2026/10/17MITTypeScriptES Module构建来源已验证近 30 天下载 3839

A Model Context Protocol server for Vitest test execution and management

原始使用文档
文档目录44 节
  1. Table of Contents
  2. 😢 The Problem with LLMs and Testing 😢
  3. ✨ Key Features ✨
  4. 🚀 Quick Start 🚀
  5. 🛠️ Client-Specific Setup
  6. Configuration Methods
  7. Configuration Scopes
  8. Setup Steps
  9. Usage
  10. Configuration Location
  11. Example Configuration
  12. Setup Steps
  13. Configuration Methods
  14. Example Configuration
  15. Setup Steps
  16. Configuration Location
  17. Example Configuration
  18. Setup Steps
  19. Configuration Location
  20. Example Configuration
  21. Setup Steps
  22. Configuration via Cline UI
  23. Example Configuration
  24. Setup Steps
  25. 3. Use It 🎮
  26. 📋 Requirements
  27. 🧰 Tools
  28. set_project_root
  29. list_tests
  30. run_tests
  31. analyze_coverage
  32. 🔄 Multi-Repository Support
  33. 🪝 Claude Code Hook
  34. 🤖 LLM instructions
  35. ⚙️ Configuration
  36. Vitest Configuration Priority
  37. Coverage Thresholds
  38. Vitest-MCP Configuration File
  39. Priority Order
  40. 🧑🏻‍💻 Development Mode & Debugging
  41. Enable Debug Mode
  42. Development Mode
  43. 🔧 Troubleshooting
  44. 📜 License

🧪 Vitest MCP Server

CI

Vitest Server MCP server

MCP Vitest runner with LLM-optimized test output, log capturing, and line-by-line coverage analysis.

Table of Contents

😢 The Problem with LLMs and Testing 😢

  • Noisy test output - Raw Vitest output can be extremely verbose eating up tokens/context with useless information.
  • Full test suite runs - LLMs sometimes forget to limit the scope of the test run, causing full test suite runs to be executed.
  • Buried console logs - Console logs can be hidden in test output, making it inefficient for LLMs to debug test failures.
  • Raw coverage files - Raw coverage files are too large for AI to parse.
  • Watch mode - LLMs get stuck when vitest runs in watch mode.

✨ Key Features ✨

  • Smart Test Results with adaptive, structured output to limit noise.
  • Console Log Capture to prevent logs from getting lost in test output.
  • Coverage Analysis with line-by-line gap insights.
  • Multi-Repository Support in a single session with context switching.
  • Safety Guards prevent full project runs, watch mode, invalid vitest commands, and context hogging.

🚀 Quick Start 🚀

The Vitest MCP server can be used with any MCP-compatible IDE or tool and works with all Javascript or Typescript Vitests. The basic configuration is:

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

⚠️ Note: The above example may not be valid for all MCP clients. Verify your client's specific setup instructions.

🛠️ Client-Specific Setup

Select your preferred IDE or tool from the setup guides below:

Claude Code

Configuration Methods

Claude Code supports multiple configuration approaches:

Method 1: CLI Wizard (Interactive)

# Add server with interactive prompts
claude mcp add vitest -- npx -y @djankies/vitest-mcp

# Add to project scope (shareable with team)
claude mcp add --scope project vitest -- npx -y @djankies/vitest-mcp

Method 2: Direct Configuration File

Create or edit .mcp.json in your project root:

{
  "mcpServers": {
    "vitest": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "@djankies/vitest-mcp"],
      "env": {}
    }
  }
}

Configuration Scopes

  • Local (default): Personal, project-specific - stored in user settings
  • Project: Team-shared - stored in .mcp.json in project root
  • User: Available across all projects - global configuration

Setup Steps

  1. Navigate to your project directory
  2. Choose either CLI or file-based configuration
  3. For CLI: Run claude mcp add vitest npx -y @djankies/vitest-mcp
  4. For file: Create .mcp.json with the configuration above
  5. Verify with /mcp command in Claude Code to see connection status

Usage

Once configured, use the MCP server naturally in your conversations:

  • "Set the project root to this directory"
  • "Run the auth component tests"
  • "Show me the test coverage gaps"
Claude Desktop

Configuration Location

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json

Example Configuration

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

Setup Steps

  1. Open Claude Desktop
  2. Navigate to File → Settings → Developer
  3. Click Edit Config
  4. Add the configuration above
  5. Save and restart Claude Desktop
  6. Look for the MCP indicator (🔌) in the conversation input
VS Code

Configuration Methods

  • Workspace: .vscode/mcp.json in your project
  • Global: Run command MCP: Open User Configuration

Example Configuration

{
  "mcpServers": {
    "vitest": {
      "command": "npx",
      "args": ["-y", "@djankies/vitest-mcp"],
      "type": "stdio"
    }
  }
}

Setup Steps

  1. Install VS Code 1.86+ with GitHub Copilot
  2. Open Command Palette (Cmd/Ctrl + Shift + P)
  3. Run MCP: Add Server
  4. Select Workspace Settings or Global
  5. Add the configuration and save
  6. Access via Copilot Chat: Add Context → MCP Resources
Cursor

Configuration Location

  • Project: .cursor/mcp.json in your project directory
  • Global: ~/.cursor/mcp.json for all workspaces

Example Configuration

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

Setup Steps

  1. Create the configuration file in your preferred location
  2. Add the server configuration
  3. Restart Cursor
  4. The MCP server will be available in Cursor's AI chat
  5. Use natural language to run tests: "Run the auth component tests"
Windsurf

Configuration Location

Similar to Cursor, but use serverUrl instead of url for remote servers

Example Configuration

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

Setup Steps

  1. Open Windsurf Settings (Cmd/Ctrl + ,)
  2. Navigate to Cascade settings
  3. Add MCP server configuration
  4. Reload MCP servers to activate
Cline (VS Code Extension)

Configuration via Cline UI

Example Configuration

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

Setup Steps

  1. Open VS Code with Cline extension installed
  2. Click the Cline icon in the sidebar
  3. Click MCP Servers → Configure MCP Servers
  4. Add the vitest-mcp configuration
  5. Save and reload VS Code
  6. Access through Cline's chat interface

3. Use It 🎮

Once configured, you can use natural language to interact with your tests:

  • "Run the tests for this component"
  • "Debug this test file"
  • "Analyze the coverage for this file"

Or prepend your message with vitest-mcp: to ensure the tools are used:

  • "vitest-mcp: run tests for this component"
  • "vitest-mcp: debug this test file"
  • "vitest-mcp: analyze coverage for this file"

📋 Requirements

  • Node.js: 22.12+ 🟢
  • Vitest: 3.x, 4.x or 5.x installed in your project 🧪 (each major is tested in CI against real projects)
  • Coverage: @vitest/coverage-v8 or @vitest/coverage-istanbul, same major version as Vitest (for coverage analysis) 📊
npm install --save-dev vitest @vitest/coverage-v8

The server always runs your project's own Vitest (resolved from node_modules, including hoisted monorepo installs). It never downloads Vitest or runs commands through a shell.

🧰 Tools

set_project_root

🚨 Required first - Set the project root for all operations.

ParameterTypeRequiredDescription
pathstringYesProject root path

list_tests

List test files in your project. Helps keep LLMs from slipping back to using command line tools while working with tests.

ParameterTypeRequiredDescription
pathstringNoDirectory to search for test files (defaults to the project root)

run_tests

Execute tests with structured output.

Parameters:

ParameterTypeRequiredDescription
targetstringYesFile or directory to test
formatstringNoOutput format: "summary" or "detailed" (auto-detects based on results)
showLogsbooleanNoInclude console output with [stdout] or [stderr] prefixes
projectstringNoVitest project name for monorepos

analyze_coverage

Analyze test coverage with gap insights.

ParameterTypeRequiredDescription
targetstringYesSource file or directory to analyze coverage for
formatstringNoOutput format: "summary" (overview only) or "detailed" (includes line-by-line coverage)
projectstringNoOnly use tests from this Vitest project (e.g. "unit")
excludestring[]NoPatterns to exclude from coverage (e.g., ["**/.stories."]). Replaces your config's coverage.exclude for this run

Note: Coverage thresholds should be configured in your vitest.config.ts file, not via MCP parameters.

The tool runs every test that imports the target (vitest related), wherever your tests live (./test, __tests__, next to the source...), and reports coverage for the target only, including files that no test touches. Reports are written to a temporary directory, so your own coverage/ output is left alone. Test utilities, mocks, stories, and e2e files are excluded automatically.

🔄 Multi-Repository Support

// Project A
set_project_root({ path: "/path/to/frontend" })
run_tests({ target: "./src" })

// Project B
set_project_root({ path: "/path/to/backend" })
run_tests({ target: "./src" })

🪝 Claude Code Hook

Automatically redirect Vitest commands to MCP tools:

# Run from project root
curl -o .claude/vitest-hook.sh https://raw.githubusercontent.com/djankies/vitest-mcp/refs/heads/main/.claude/vitest-hook.sh
chmod +x .claude/vitest-hook.sh

Then add to your project's .claude/settings.local.json:

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

The hook can be bypassed with the BYPASS_VITEST_HOOK environment variable:

BYPASS_VITEST_HOOK=1 npm test

Or export for entire session:

export BYPASS_VITEST_HOOK=1

🤖 LLM instructions

Encourage claude or your ide to use the tools correctly: CLAUDE.example.md

⚙️ Configuration

Vitest Configuration Priority

If your project has a vitest.mcp.config.* file (.ts, .mts, .cts, .js, .mjs or .cjs), the MCP server passes it to Vitest with --config. Otherwise Vitest finds your normal vitest.config.* / vite.config.* as usual.

This allows you to have a dedicated MCP-specific configuration that won't interfere with your regular development workflow:

// vitest.mcp.config.ts
import { defineConfig, mergeConfig } from 'vitest/config';
import baseConfig from './vitest.config';

export default mergeConfig(baseConfig, defineConfig({
  test: {
    // MCP-specific settings, e.g. a longer timeout for AI-driven runs
    testTimeout: 30000,
  },
}));

Reporters, coverage output and console capture are set by the server on the command line, so you don't need to configure them.

Coverage Thresholds

To enable coverage threshold reporting in the analyze_coverage tool, just configure in your Vitest configuration file:

// vitest.config.ts or vitest.mcp.config.ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      thresholds: {
        lines: 90,
        functions: 85,
        branches: 80,
        statements: 90
      }
    }
  }
});

💡 Note: Coverage analysis is always available regardless of threshold settings. Threshold reporting just helps keep your LLM on target.

Vitest-MCP Configuration File

Optional .vitest-mcp.json in home or project directory:

{
  "safety": {
    "allowedPaths": ["/Users/username/Projects"]
  },
  "testDefaults": {
    "format": "detailed",
    "timeout": 60000
  }
}

Priority Order

Configuration is merged in the following order (highest priority first):

  1. Command-line flags
  2. Environment variables
  3. Configuration file
  4. Built-in defaults

🧑🏻‍💻 Development Mode & Debugging

Enable Debug Mode

Set the debug environment variable for detailed logging:

VITEST_MCP_DEBUG=true

Or in your MCP config file:

{
  "mcpServers": {
    "vitest": {
      "command": "npx",
      "args": ["-y", "@djankies/vitest-mcp"],
      "env": {
        "VITEST_MCP_DEBUG": "true"
      }
    }
  }
}

Development Mode

Enable to test the server on its own codebase:

# Set in .env.development, or in your mcp config file 
VITEST_MCP_DEV_MODE=true

🔧 Troubleshooting

"Vitest not found" - Install it in the project you set as root: npm install --save-dev vitest

"Coverage provider not found" / "does not match Vitest" - Install the provider matching your Vitest major, e.g. npm install --save-dev @vitest/coverage-v8@4 for Vitest 4 📊

Hook issues - Bypass with: BYPASS_VITEST_HOOK=1 npm test

Found a bug? Submit a bug report, please, and thank you.

📜 License

MIT — See LICENSE.

版本与发布记录

@djankies/vitest-mcp 共发布 19 个版本,首次发布于 2025/08/05;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.0latest2026/10/1726 天前
0.5.12025/08/151.2 年前
0.5.02025/08/151.2 年前
0.4.9-beta.0beta2025/08/081.3 年前
0.4.8-beta.02025/08/081.3 年前
0.4.7-beta.02025/08/081.3 年前

关于 @djankies/vitest-mcp 的常见问题

@djankies/vitest-mcp 是什么?

A Model Context Protocol server for Vitest test execution and management

如何安装 @djankies/vitest-mcp?

在项目目录运行 npm install @djankies/vitest-mcp,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 vitest-mcp,可以用 npx vitest-mcp 直接运行。

@djankies/vitest-mcp 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@djankies/vitest-mcp 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@djankies/vitest-mcp 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@djankies/vitest-mcp 可以商用吗?

@djankies/vitest-mcp 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@djankies/vitest-mcp 的下载量有多少?

根据 npm 官方下载统计,@djankies/vitest-mcp 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,839 次,日均约 128 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@djankies/vitest-mcp 还在维护吗?

最新版本 0.6.0 发布于 2026/10/17(26 天前);至今共发布 19 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/08/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。