MCP测试工具

一款用于测试模型上下文协议(MCP)服务器的工具,提供简单的接口进行工具调用和响应验证。

  • by crazyrabbitLTC
  • TypeScript
  • 本地部署
  • 13 星标
  • 更新于
在 GitHub 查看 crazyrabbitLTC/mcp-test-client

使用说明

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<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> -清理资源并断开与服务器的连接

发展

先决条件

  • 包子 (v1.0.0或更高版本)

设置

  1. 克隆存储库
git clone <repository-url>
cd mcp-test-client
  1. 安装依赖项
bun install
  1. 运行测试
bun test

许可证

麻省理工学院

贡献

  1. 分叉存储库
  2. 创建功能分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add some amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求