MCP测试客户端
模型上下文协议(MCP)服务器的测试实用程序。此客户端通过提供用于进行工具调用和验证响应的简单接口来帮助您测试MCP服务器实现。
特性
- MCP服务器易于使用的测试界面
- 内置对工具列表和工具调用的支持
- 使用TypeScript实现类型安全
- 用于验证服务器响应的断言实用程序
- 模拟计算器服务器实现示例
安装
bun install mcp-test-client用法
基础示例
import { MCPTestClient } from 'mcp-test-client';
describe('MCP Server Tests', () => {
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('should list available tools', async () => {
const tools = await client.listTools();
expect(tools).toContainEqual(
expect.objectContaining({
name: 'your-tool-name',
description: 'Your tool description',
})
);
});
test('should call a tool', async () => {
await client.assertToolCall(
'your-tool-name',
{ arg1: 'value1', arg2: 'value2' },
(result) => {
expect(result.content[0].text).toBe('expected result');
}
);
});
});计算器服务器示例
该软件包包括一个用于测试和学习目的的模拟计算器服务器:
import { MCPTestClient } from 'mcp-test-client';
describe('Calculator Server Tests', () => {
let client: MCPTestClient;
beforeAll(async () => {
client = new MCPTestClient({
serverCommand: 'bun',
serverArgs: ['./tests/mocks/calculator.ts'],
});
await client.init();
});
afterAll(async () => {
await client.cleanup();
});
test('should perform addition', async () => {
await client.assertToolCall(
'calculate',
{ operation: 'add', a: 5, b: 3 },
(result) => {
expect(result.content[0].text).toBe('8');
}
);
});
});api参考
MCPTestClient
构造函数
constructor(config: { serverCommand: string; serverArgs: string[] })方法
init(): Promise-初始化客户端并连接到服务器listTools(): Promise-从服务器获取可用工具列表callTool(toolName: string, args: Record): Promise-调用特定工具assertToolCall(toolName: string, args: Record, assertion: (result: ToolResult) => void | Promise): Promise-调用工具并对结果运行断言cleanup(): Promise-清理资源并断开与服务器的连接
发展
先决条件
- 包子 (v1.0.0或更高版本)
设置
- 克隆存储库
git clone
cd mcp-test-client- 安装依赖项
bun install- 运行测试
bun test许可证
麻省理工学院
贡献
- 分叉存储库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
