MCP服务器模拟测试工具

一个轻量级的MCP服务器模拟工具,用于单元测试或集成测试,提供可控和可预测的测试环境。

  • by lobehub
  • JavaScript
  • 本地部署
  • 22 星标
  • 更新于
在 GitHub 查看 lobehub/mcp-hello-world

使用说明

MCP Hello World-用于测试的MCP服务器模拟

这是一个 最小模型上下文协议(MCP)服务器 用TypeScript实现,主要用作 测试双/模拟服务器.

核心目的:为以下对象提供轻量级、可控和可预测的MCP服务器环境 单元测试 或 集成测试 需要与MCP服务器交互的客户端代码。

注: 这个项目是 不适合生产环境 或部署为通用MCP服务器。

为什么使用 mcp-hello-world 在测试中?

在测试与MCP客户端相关的代码时,您通常不希望依赖于真实的、潜在的复杂且响应不可预测的AI后端服务。使用 mcp-hello-world 作为测试替身,它有几个优点:

  1. 隔离:将测试重点放在客户端逻辑上,而不必担心网络问题或实际服务器的可用性。
  2. 可预测性:提供的 echo 和 debug 工具具有简单、固定的行为,使得编写断言变得容易。
  3. 速度:启动和响应时间快,适合在单元测试中频繁使用。
  4. 轻量级:依赖关系少,易于集成到测试环境中。
  5. 协议覆盖范围:支持两者 STDIO 和 HTTP/SSE MCP传输协议,允许您测试不同连接方法下的客户端行为。

安装

将此包添加为 dev依赖性 对于您的项目:

# Using pnpm
pnpm add --save-dev mcp-hello-world

# Or using bun
bun add --dev mcp-hello-world

手动执行(用于调试测试)

有时您可能需要手动运行服务器来调试测试或客户端行为。

STDIO模式

这是最简单的运行方式,尤其是在本地开发和调试期间。

# Ensure it's installed (globally or in the project)
# Using npx (universal)
npx mcp-hello-world

# Or using pnpm dlx
pnpm dlx mcp-hello-world

# Or using bunx
bunx mcp-hello-world

服务器将监听标准输入,并将MCP响应输出到标准输出。你可以使用以下工具 MCP检查员 以连接到该过程。

要在MCP客户端中配置此服务器,请在配置中添加以下内容:

{
  "mcpServers": {
    "mcp-hello-world": {
      "command": "npx",
      "args": ["mcp-hello-world"]
    }
  }
}

HTTP/SSE模式

如果您需要通过网络接口进行调试或测试基于HTTP的MCP客户端。

# 1. Clone the repository (if not already installed in the project)
# git clone https://github.com/lobehub/mcp-hello-world.git
# cd mcp-hello-world
# pnpm install / bun install

# 2. Build the project
# Using pnpm
pnpm build
# Or using bun
bun run build

# 3. Start the HTTP server
# Using pnpm
pnpm start:http
# Or using bun
bun run start:http

服务器将于启动 http://localhost:3000 并提供:

  • SSE端点: /sse
  • 消息端点: /messages

测试中的使用

您可以通过编程启动和停止 mcp-hello-world 测试框架中的服务器(如Jest、Vitest、Mocha等)用于自动化测试。

示例:使用STDIO模式(Node.js)进行测试

// test/my-mcp-client.test.ts (Example using Jest)
import { spawn } from 'child_process';
import { MCPClient } from '../src/my-mcp-client'; // Assuming this is your client code

describe('My MCP Client (STDIO)', () => {
  let mcpServerProcess;
  let client: MCPClient;

  beforeAll(() => {
    // Start the mcp-hello-world process before tests
    // Using npx (or pnpm dlx / bunx) ensures the command is found and executed
    mcpServerProcess = spawn('npx', ['mcp-hello-world']);

    // Instantiate your client and connect to the subprocess's stdio
    client = new MCPClient(mcpServerProcess.stdin, mcpServerProcess.stdout);
  });

  afterAll(() => {
    // Shut down the mcp-hello-world process after tests
    mcpServerProcess.kill();
  });

  it('should receive echo response', async () => {
    const request = {
      jsonrpc: '2.0',
      id: 1,
      method: 'tools/invoke',
      params: { name: 'echo', parameters: { message: 'test message' } },
    };

    const response = await client.sendRequest(request); // Assuming your client has this method

    expect(response).toEqual({
      jsonrpc: '2.0',
      id: 1,
      result: { content: [{ type: 'text', text: 'Hello test message' }] },
    });
  });

  it('should get greeting resource', async () => {
    const request = {
      jsonrpc: '2.0',
      id: 2,
      method: 'resources/get',
      params: { uri: 'greeting://Alice' },
    };
    const response = await client.sendRequest(request);
    expect(response).toEqual({
      jsonrpc: '2.0',
      id: 2,
      result: { data: 'Hello Alice!' }, // Confirm return format based on actual implementation
    });
  });

  // ... other test cases
});

示例:使用HTTP/SSE模式进行测试

对于HTTP/SSE,您可能需要:

  1. 使用 exec 或 spawn 在 beforeAll 开始 pnpm start:http 或 bun run start:http.
  2. 使用HTTP客户端(如 axios, node-fetch,或测试框架的内置客户端)连接到 http://localhost:3000/sse 和 /messages 用于测试。
  3. 确保在中关闭已启动的服务器进程 afterAll.

提供MCP功能(用于测试断言)

mcp-hello-world 为测试中的交互和断言提供了以下固定功能:

资源

  • hello://world
    • 描述:一个静态的Hello World资源。
    • 方法: resources/get
    • 参数:无
    • 退货: { data: 'Hello World!' }
  • greeting://{name}
    • 描述:一个动态的问候资源。
    • 方法: resources/get
    • 参数: name 包括在URI中。, greeting://Bob.
    • 退货: { data: 'Hello {name}!' } (例如。, { data: 'Hello Bob!' })

工具

  • echo
    • 描述:回显输入消息,前缀为“Hello”。
    • 方法: tools/invoke
    • 参数: { name: 'echo', parameters: { message: string } }
    • 退货: { content: [{ type: 'text', text: 'Hello {message}' }] } (例如。, { content: [{ type: 'text', text: 'Hello test' }] })
  • debug
    • 说明:列出服务器上所有可用的MCP方法定义。
    • 方法: tools/invoke
    • 参数: { name: 'debug', parameters: {} }
    • 返回:一个JSON结构,包含所有已注册资源、工具和提示的定义。

提示

  • helpful-assistant
    • 说明:一个基本的助手提示定义。
    • 方法: prompts/get
    • 参数:无
    • 返回:带有预定义的提示的JSON结构 system 和 user 角色。

许可证

麻省理工学院