xk6 mcp
k6扩展用于测试 模型上下文协议(MCP) 服务器。
\[!小心\] 此扩展是 实验性的 并且没有得到Grafana Labs的正式支持。
安装
- 首先,确保你有 xk6 安装:
go install go.k6.io/xk6/cmd/xk6@latest- 使用xk6-mcp扩展构建k6二进制文件:
xk6 build --with github.com/dgzlopes/xk6-mcp- 将mcp模块导入到测试脚本顶部的脚本中:
import mcp from 'k6/x/mcp';- 构建的二进制文件将位于您当前的目录中。您可以将其移动到PATH或直接使用它:
./k6 run script.js示例
⚠️ 这个例子取决于 mcp示例服务器\ 您可以从发布页面下载最新版本。
import mcp from 'k6/x/mcp';
export default function () {
// Initialize MCP Client with stdio transport
const client = new mcp.StdioClient({
path: './mcp-example-server',
});
// Check connection to MCP server
console.log('MCP server running:', client.ping());
// List all available tools
console.log('Tools available:');
const tools = client.listAllTools().tools;
tools.forEach(tool => console.log(` - ${tool.name}`));
// List all available resources
console.log('Resources available:');
const resources = client.listAllResources().resources;
resources.forEach(resource => console.log(` - ${resource.uri}`));
// List all available prompts
console.log('Prompts available:');
const prompts = client.listAllPrompts().prompts;
prompts.forEach(prompt => console.log(` - ${prompt.name}`));
// Call a sample tool
const toolResult = client.callTool({ name: 'greet', arguments: { name: 'Grafana k6' } });
console.log(`Greet tool response: "${toolResult.content[0].text}"`);
// Read a sample resource
const resourceContent = client.readResource({ uri: 'embedded:info' });
console.log(`Resource content: ${resourceContent.contents[0].text}`);
// Get a sample prompt
const prompt = client.getPrompt({ name: 'greet' });
console.log(`Prompt: ${prompt.messages[0].content.text}`);
}常见问题
那么非stdio传输呢?
SSE和流式HTTP都支持!
你可以这样使用它们:
// SSE
const client = new mcp.SSEClient({
base_url: 'http://localhost:3002',
});
// Streamable HTTP
const client = new mcp.StreamableHTTPClient({
base_url: 'http://localhost:3001',
});分页怎么样?
该扩展提供了两种列出资源、工具和提示的方法:
// With All: Handles pagination automatically
const allTools = client.listAllTools();
const allResources = client.listAllResources();
const allPrompts = client.listAllPrompts();
// Without All: Requires manual pagination
const first = client.listTools()
const second = client.listTools({ cursor: first.next_cursor });那么指标呢?
扩展程序会自动跟踪每个MCP操作的RED风格指标:
mcp_request_duration(趋势):每个MCP请求的持续时间(以毫秒为单位)。mcp_request_count(计数器):发出的MCP请求数。mcp_request_errors(计数器):失败的MCP请求数。
每个指标都标记为:
methodMCP方法被称为(例如。,GetPrompt,ListTools).

