k6 MCP服务器
一 实验性的 k6的MCP(模型上下文协议)服务器,用Go编写。它提供脚本验证、测试执行、快速全文文档搜索(嵌入式SQLite FTS5)和引导脚本生成。
特性
提示
- 脚本生成 随着
/generate_k6_script:根据简单的英语要求生成生产就绪的k6测试脚本。它通过利用嵌入式最佳实践、文档和类型定义自动遵循现代测试实践。
工具
- 脚本验证:
validate_k6_script以最少的配置(1个VU,1次迭代)运行k6脚本,并返回可操作的错误,以帮助快速生成正确的代码。 - 测试执行:
run_k6_script使用可配置的VU、持续时间、阶段和选项在本地运行k6性能测试,并在可能的情况下从结果中提取见解。 - 文档搜索(默认):
search_k6_documentation提供对官方k6文档(嵌入式SQLite FTS5索引)的快速全文搜索,以帮助编写现代、高效的k6脚本。 - 地形(格拉法纳k6云):
generate_k6_cloud_terraform_load_test_resource为Grafana Cloud k6生成一个Terraform资源,允许您使用Grafana k6 Terraform提供程序定义和配置k6 Cloud测试。
资源
- 最佳实践资源:全面的k6脚本编写指南和模式,帮助您编写有效、地道和正确的测试。
- 类型定义:最新的k6 TypeScript类型定义,以提高准确性和编辑器工具。
快速开始
先决条件
安装以下组件:
- 转到1.24.4+:用于构建和运行MCP服务器
- k6:必须安装并在PATH中可用,以便执行脚本
- 就:开发任务的命令运行器(推荐)
安装 just:
# Just gives access to management commands for the project
brew install just快速设置
- 克隆存储库:
git clone https://github.com/oleiade/k6-mcp
cd k6-mcp- 安装k6 MCP服务器 (生成文档索引数据库,并安装
k6-mcp):
just install或者,在不安装系统范围的情况下运行:
just run编辑器集成
光标IDE
要将此MCP服务器与Cursor IDE一起使用,请执行以下操作:
- 确保已安装MCP服务器 (从上述步骤3开始)
- 配置MCP设置:
创建或更新MCP配置文件(~/.cursor/mcp_servers.json 或编辑器的MCP配置):
{
"mcpServers": {
"k6-mcp": {
"command": "k6-mcp",
"env": {}
}
}
}- 重新启动游标 或者重新加载MCP配置。
- 使用k6工具 在您的Cursor聊天中:
- 让它验证k6脚本 - 请求执行性能测试 - 搜索k6文档 - 根据需求生成k6脚本
克劳德代码
对于Claude Code,使用以下命令将k6 MCP服务器添加到您的配置中:
claude mcp add --scope=user --transport=stdio k6 k6-mcp请注意,这将把k6 MCP服务器添加到您的claude代码用户配置中。如果要将工具的范围限制在项目范围内,请将范围选项设置为 local 相反。
克劳德桌面
对于Claude Desktop,请在MCP配置中添加以下内容:
{
"mcpServers": {
"k6-mcp": {
"command": "k6-mcp",
"env": {}
}
}
}可用工具
validate_script
通过以最小配置(1个VU,1次迭代)运行k6脚本来验证它。
参数:
script(字符串,必填)
退货: valid, exit_code, stdout, stderr, error, duration
run_test
使用可配置参数运行k6性能测试。
参数:
script(字符串,必填)vus(数字,可选)duration(字符串,可选)iterations(数字,可选)stages(对象,可选)options(对象,可选)
退货: success, exit_code, stdout, stderr, error, duration, metrics, summary
搜索文档
在嵌入式k6文档索引(SQLite FTS5)上进行全文搜索。
参数:
keywords(string,必填):FTS5查询字符串max_results(数字,可选,默认10,最多20)
FTS5提示:
- 空格分隔的单词表示AND:
checks thresholds→checks AND thresholds - 精确短语的引用:
"load testing" - 支持的操作员:
AND,OR,NEAR、括号、前缀http*
返回一个结果数组 title, content, path.
可用资源
最佳实践指南
访问全面的k6脚本最佳实践,包括:
- 测试结构和组织
- 性能优化技术
- 错误处理和验证模式
- 身份验证和安全实践
- 浏览器测试指南
- 现代k6功能和协议
资源URI: docs://k6/best_practices
脚本生成模板
人工智能驱动的k6脚本生成,具有结构化的工作流程:
- 研究和发现阶段
- 最佳实践集成
- 制作就绪脚本创建
- 自动化验证和测试
- 文件系统集成
资源URI: prompts://k6/generate_script
发展
仅命令(推荐)
# Build and run the MCP server (generates the SQLite index if missing)
just run
# Build binary locally (generates the SQLite index if missing)
just build
# Install into your Go bin (generates the SQLite index if missing)
just install
# Optimized release build (stripped, reproducible paths)
just release
# (Re)generate the embedded SQLite docs index
just index
# Optional (experimental embeddings): start vector DBs / helpers
just chroma
just milvus
just ingest
just verify
just reset手动命令
如果你不想使用 just:
# 1) Generate the SQLite FTS5 docs index (required for build/run because it is embedded)
go run -tags fts5 ./cmd/indexer
# 2) Start the MCP server
go run -tags fts5 ./cmd/k6-mcp
# Build a local binary
go build -tags fts5 -o k6-mcp ./cmd/k6-mcp
# Release‑style build (macOS example)
CGO_ENABLED=1 go build -tags 'fts5 sqlite_fts5' -trimpath -ldflags '-s -w' -o k6-mcp ./cmd/k6-mcp
# Run tests
go test ./...
# Lint
golangci-lint run项目结构
├── cmd/
│ ├── k6-mcp/ # MCP server entry point
│ └── indexer/ # Builds the SQLite FTS5 docs index into dist/index.db
├── dist/
│ └── index.db # Embedded SQLite FTS5 index (generated)
├── internal/
│ ├── runner/ # Test execution engine
│ ├── search/ # Full‑text search and indexer
│ ├── security/ # Security utilities
│ └── validator/ # Script validation
├── resources/ # MCP resources
│ ├── practices/ # Best practices guide
│ └── prompts/ # AI prompt templates
├── python-services/ # Optional utilities (embeddings, verification)
└── k6/scripts/ # Generated k6 scripts安全
MCP服务器实施了全面的安全措施:
- 输入验证:大小限制(最大1MB)和危险模式检测
- 安全执行:阻止Node.js模块、系统访问和恶意代码模式
- 文件处理:受限权限(0600)和安全的临时文件管理
- 资源限制:命令执行超时(30秒验证,5米测试),最大50 VU
- 环境隔离:具有适当清理功能的最小k6执行环境
- Docker强化:非root用户,只读文件系统,无新权限
使用示例
基本脚本验证
# In your MCP-enabled editor, ask:
"Can you validate this k6 script?"
# Then provide your k6 script content性能测试
# In your MCP-enabled editor, ask:
"Run a load test with 10 VUs for 2 minutes using this script"
# The system will execute the test and provide detailed metrics文档搜索
# In your MCP-enabled editor, ask:
"Search for k6 authentication examples"
"How do I use thresholds in k6?"
"Show me WebSocket testing patterns"脚本生成
# In your MCP-enabled editor, ask:
"Generate a k6 script to test a REST API with authentication"
"Create a browser test for an e-commerce checkout flow"
"Generate a WebSocket load test script"故障排除
构建失败,显示“dist/index.db:没有匹配的文件”
首先生成文档索引:
just index搜索未返回任何结果
- 确保索引存在:
ls dist/index.db - 重建索引:
just index - 尝试更简单的查询或引用短语:
"load testing"
未找到MCP服务器
如果你的编辑器找不到k6-mcp服务器:
- 确保已安装:
just install - 检查编辑器的MCP配置
- 验证服务器是否启动:
k6-mcp(应显示MCP服务器输出)
测试执行失败
如果k6测试未能执行:
- 确认k6已安装:
k6 version - 首先使用验证工具检查脚本语法
- 确保资源不超过限制(50 VU,5米持续时间)
贡献
- 分叉存储库
- 创建要素分支
- 运行测试:
go test ./... - 运行门楣:
golangci-lint run - 提交拉取请求
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
