Mcp Autotest
用于自动测试 MCP 服务器的实用程序
strowk
README
<h4 align="center">以语言无关的方式自动测试 MCP 服务器</h4>
<h1 align="center"> <img src="docs/images/logo.png" width="180"/> <br/> mcp-autotest </h1>
<p align="center"> <a href="https://github.com/strowk/mcp-autotest/actions/workflows/test.yaml"><img src="https://github.com/strowk/mcp-autotest/actions/workflows/test.yaml/badge.svg"></a> <a href="https://github.com/strowk/mcp-autotest/actions/workflows/golangci-lint.yaml"><img src="https://github.com/strowk/mcp-autotest/actions/workflows/golangci-lint.yaml/badge.svg"/></a> <a href="https://goreportcard.com/report/github.com/strowk/mcp-autotest"><img src="https://goreportcard.com/badge/github.com/strowk/mcp-autotest" alt="Go Report Card"></a> </p>
<p align="center"> <a href="#installation">安装</a> ⚙ <a href="#usage">使用</a> ⚙ <a href="#quick-demo">快速演示</a> ⚙ <a href="#dynamic-matching">动态匹配</a> </p>
一个简单的工具,允许通过定义包含请求和响应的 YAML 文件,使用 MCP 协议测试您的 MCP 服务器。
MCP 用例文件是一个多文档 YAML,它将每个用例定义为一个单独的文档,如下所示:
case: Listing tools
in: { "jsonrpc": "2.0", "method": "tools/list", "id": 1 }
out:
{
"jsonrpc": "2.0",
"id": 1,
"result":
{
"tools":
[
{
"description": "Run a read-only SQL query",
"inputSchema":
{
"type": "object",
"properties": { "sql": { "type": "string" } },
},
"name": "query",
},
],
}
}
---
# next case...
文件必须以 _test.yaml
后缀命名,才能被识别为测试用例文件。
安装
npm
npm install -g mcp-autotest
Github Releases
从 releases 页面下载预构建的二进制文件,并将其放入您的 PATH 中
Build from source
go install github.com/strowk/mcp-autotest@latest
使用
mcp-autotest [flags] run path/to/tests/folder [--] command-to-run-server [server-args]
示例:
# 启动 go MCP 服务器并通过 stdio 传输进行测试
mcp-autotest run testdata go run main.go
# 启动 Postgres MCP 服务器并通过 stdio 传输进行测试
mcp-autotest run -v testdata -- npx -y @modelcontextprotocol/server-postgres localhost:5432
# 启动 go MCP 服务器并通过 Streamable HTTP 传输进行测试
mcp-autotest run --url http://localhost:8080/mcp testdata go run main.go
传输方式
mcp-autotest 默认使用 stdio 传输,但如果您想使用 HTTP 传输,可以使用 --url
标志来指定 MCP 服务器的 URL。
URL 必须是 http://host:port/path
或 https://host:port/path
格式。
目前,Streamable HTTP 仅支持 POST
方法,但计划在未来支持通过 GET
进行测试。
快速演示
在 bash shell 中运行以下命令
# 创建测试数据文件夹
mkdir -p testdata
# 创建测试用例文件
cat << EOF > testdata/list_tools_test.yaml
# 这是一个测试用例文件。它包含 YAML 格式的测试用例列表。
# 每个测试用例都有可变数量的输入(以“in”开头的键)和输出(以“out”开头的键)。
# 测试用例由新行上的“---”(三个破折号)分隔,使其成为多文档 YAML 文件。
# 文件名必须以“_test.yaml”结尾才能被识别为测试用例文件。
case: List tools
# 请求工具列表
in: { "jsonrpc": "2.0", "method": "tools/list", "id": 1 }
# 期望列表中有一个工具
out:
{
"jsonrpc": "2.0",
"id": 1,
"result":
{
"tools":
[
{
"description": "Run a read-only SQL query",
"inputSchema":
{
"type": "object",
"properties": { "sql": { "type": "string" } },
},
"name": "query",
},
],
}
}
EOF
# 现在运行 autotest
npx mcp-autotest run testdata -- npx -y @modelcontextprotocol/server-postgres localhost:5432
输出应该只打印一个单词 PASS
。
现在,如果您在行 "description": "Run a read-only SQL query",
中更改某些内容,例如更改为 "description": "Run a read-only SQL query 2"
, 并再次运行最后一个命令,您应该看到如下输出:
2025/03/31 22:23:39 actual json did not match expectation,
got: '{"id":1,"jsonrpc":"2.0","result":{"tools":[{"description":"Run a read-only SQL query","inputSchema":{"properties":{"sql":{"type":"string"}},"type":"object"},"name":"query"}]}}'
diff with expected:
"result": {
"tools": {
"0": {
"description": {
^ value mismatch,
expected string: 'Run a read-only SQL query 2',
got string: 'Run a read-only SQL query'
FAIL
动态匹配
有时,当您的响应中包含动态值时,您可能希望使用正则表达式来匹配它们。您可以通过在预期输出中使用 !!re
标签来实现。例如:
case: List tools
in_list_tools: {"jsonrpc":"2.0","method":"tools/list","id":1}
out_list_tools: {"jsonrpc":"2.0","result":{"tools":[ { "name": !!re "list-[a-z]+" } ]},"id":1}
这个例子有些过于简化,实际上您可能会将 !!re
与时间戳、UUID 或其他动态值一起使用,而不是工具名称。
但是,它确实表明带有 !!re
标签的字符串将被视为正则表达式,并将使用正则表达式匹配而不是字符串相等性来针对实际值进行匹配。
请参阅用于测试 postgres server 的工作示例。
嵌入式正则表达式
嵌入式正则表达式只是嵌入在字符串中的正则表达式。它用于匹配字符串的一部分。例如:
case: List tools
in_list_tools: {"jsonrpc":"2.0","method":"tools/list","id":1}
out_list_tools: {"jsonrpc":"2.0","result":{"tools":[ { "name": !!ere "list-/[a-z]+/-tool" } ]},"id":1}
本质上,!!ere
允许您将字符串的大部分视为普通字符串,但将一部分(或几部分)视为正则表达式。
斜杠 /
内的所有内容都将被视为正则表达式,这里 "list-" 和 "-tool" 是普通字符串,但 [a-z]+
是一个正则表达式,它将匹配任何小写字母的非空字符串,因此它将匹配 "list-abc-tool"、"list-xyz-tool" 等。
这种方法允许您不必考虑如何在字符串的其余部分中转义正则表达式语法,而只需匹配您需要动态的部分。
转义正斜杠
对于嵌入式正则表达式,您可能需要在字符串中需要使用斜杠 /
但不想指定正则表达式的地方转义斜杠 /
,例如:"url": !! "https:\\/\\/github.com\\//[a-z]+/"
将匹配 "url": "https://github.com/strowk"
。
在这里,\\/
用于变成 /
,[a-z]+
用于匹配任何小写字母的非空字符串。之所以有两个反斜杠 \\
,是因为在 YAML 字符串中,反斜杠是一个转义字符,因此要在字符串中包含单个反斜杠,您需要用另一个反斜杠来转义它。
推荐服务器
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
MCP Package Docs Server
促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。
Claude Code MCP
一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。
@kazuph/mcp-taskmanager
用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。
mermaid-mcp-server
一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。
Jira-Context-MCP
MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

Linear MCP Server
一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。
Curri MCP Server
通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。