使用说明
MCP Hello World-用于测试的MCP服务器模拟
这是一个 最小模型上下文协议(MCP)服务器 用TypeScript实现,主要用作 测试双/模拟服务器.
核心目的:为以下对象提供轻量级、可控和可预测的MCP服务器环境 单元测试 或 集成测试 需要与MCP服务器交互的客户端代码。
注: 这个项目是 不适合生产环境 或部署为通用MCP服务器。
为什么使用 mcp-hello-world 在测试中?
在测试与MCP客户端相关的代码时,您通常不希望依赖于真实的、潜在的复杂且响应不可预测的AI后端服务。使用 mcp-hello-world 作为测试替身,它有几个优点:
- 隔离:将测试重点放在客户端逻辑上,而不必担心网络问题或实际服务器的可用性。
- 可预测性:提供的
echo和debug工具具有简单、固定的行为,使得编写断言变得容易。 - 速度:启动和响应时间快,适合在单元测试中频繁使用。
- 轻量级:依赖关系少,易于集成到测试环境中。
- 协议覆盖范围:支持两者
STDIO和HTTP/SSEMCP传输协议,允许您测试不同连接方法下的客户端行为。
安装
将此包添加为 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,您可能需要:
- 使用
exec或spawn在beforeAll开始pnpm start:http或bun run start:http. - 使用HTTP客户端(如
axios,node-fetch,或测试框架的内置客户端)连接到http://localhost:3000/sse和/messages用于测试。 - 确保在中关闭已启动的服务器进程
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角色。
许可证
麻省理工学院
