返回市场
MCP模拟器

MCP模拟器

作者:strowk6 星标更新:2024-12-22

项目介绍

<h1 align="center"> mcpmock </h1> <h4 align="center">这是一个命令行工具,可以从YAML文件中的用例列表生成一个模拟的<a href="https://modelcontextprotocol.io">Model Context Protocol</a>服务器。</h4> <p align="center"> <a href="https://github.com/strowk/mcpmock/actions/workflows/test.yaml"><img src="https://github.com/strowk/mcpmock/actions/workflows/test.yaml/badge.svg"/></a> <a href="https://github.com/strowk/mcpmock/actions/workflows/golangci-lint.yaml"><img src="https://github.com/strowk/mcpmock/actions/workflows/golangci-lint.yaml/badge.svg"/></a> </p>

安装

从npm安装:npm install -g @strowk/mcpmock

从GitHub Releases下载:下载,解压并将二进制文件放入你的PATH中

从源码安装:

go get github.com/strowk/mcpmock
go install github.com/strowk/mcpmock

使用

例如,如果你在YAML文件中定义了如下内容:

case: 列出工具

# 请求工具列表
in: {"jsonrpc": "2.0", "method": "tools/list", "id": 1}

# 预期列表中有一个工具
out: {"jsonrpc": "2.0", "result":{ "tools": [{"description": "Hello MCP", "inputSchema": {"type": "object"}, "name": "hello"}] }, "id": 1}

---

case: 调用Hello工具

# 调用工具
in: {"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "hello", "arguments": {}}, "id": 1}

# 预期输出为"Hi!"
out: {
  "jsonrpc": "2.0", 
  "id": 1,
  "result": {
    "content": [
      {"type": "text", "text": "Hi!"}
    ],
    "isError": false
  }, 
}

然后,如果你将它放在testdata文件夹中(确保文件以_test.yaml结尾),并像这样运行mcpmock:

mcpmock serve testdata

它会启动一个使用标准I/O传输的模拟MCP服务器,并提供YAML文件中定义的用例。

现在,如果你将以下内容复制粘贴到终端中:

{"jsonrpc": "2.0", "method": "tools/list", "id": 1}

你应该能看到如第一个MCP用例所定义的工具列表。

发送以下内容:

{"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "hello", "arguments": {}}, "id": 1}

你应该得到如第二个用例所定义的输出。

它还会处理响应中的id字段替换为请求中的id字段,因此如果你发送以下内容:

{"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "hello", "arguments": {}}, "id": 2}

你应该得到如第二个用例所定义的输出,但id字段设置为2