用于测试模型上下文协议(MCP)服务器的工具。此客户端通过提供一个简单的接口来调用工具并验证响应,帮助您测试MCP服务器实现。
bun install mcp-test-client
import { MCPTestClient } from 'mcp-test-client';
describe('MCP 服务器测试', () => {
let client: MCPTestClient;
beforeAll(async () => {
client = new MCPTestClient({
serverCommand: 'bun',
serverArgs: ['./path/to/your/server.ts'],
});
await client.init();
});
afterAll(async () => {
await client.cleanup();
});
test('应列出可用工具', async () => {
const tools = await client.listTools();
expect(tools).toContainEqual(
expect.objectContaining({
name: 'your-tool-name',
description: '您的工具描述',
})
);
});
test('应调用工具', async () => {
await client.assertToolCall(
'your-tool-name',
{ arg1: 'value1', arg2: 'value2' },
(result) => {
expect(result.content[0].text).toBe('预期结果');
}
);
});
});
该包包含一个用于测试和学习目的的模拟计算器服务器:
import { MCPTestClient } from 'mcp-test-client';
describe('计算器服务器测试', () => {
let client: MCPTestClient;
beforeAll(async () => {
client = new MCPTestClient({
serverCommand: 'bun',
serverArgs: ['./tests/mocks/calculator.ts'],
});
await client.init();
});
afterAll(async () => {
await client.cleanup();
});
test('应执行加法运算', async () => {
await client.assertToolCall(
'calculate',
{ operation: 'add', a: 5, b: 3 },
(result) => {
expect(result.content[0].text).toBe('8');
}
);
});
});
MCPTestClientconstructor(config: { serverCommand: string; serverArgs: string[] })
init(): Promise<void> - 初始化客户端并连接到服务器listTools(): Promise<Tool[]> - 获取服务器上的可用工具列表callTool(toolName: string, args: Record<string, unknown>): Promise<ToolResult> - 调用特定工具assertToolCall(toolName: string, args: Record<string, unknown>, assertion: (result: ToolResult) => void | Promise<void>): Promise<void> - 调用工具并对结果进行断言cleanup(): Promise<void> - 清理资源并从服务器断开连接git clone <repository-url>
cd mcp-test-client
bun install
bun test
MIT
git checkout -b feature/amazing-feature)git commit -m '添加一些精彩的功能')git push origin feature/amazing-feature)