MCP Test Framework

MCP开发工具

提供方 aryanjp1

开发工具 stdio · 本地进程 无需鉴权 社区 / 第三方
查看接入方式

一个用于测试MCP(Model Context Protocol)服务器的pytest插件,提供丰富的断言、快照测试和服务器生命周期管理功能。

一句话选型结论

按资料字段整理

MCP Test Framework 属于开发工具类 MCP 服务器,stdio · 本地进程,无需鉴权,有可直接执行的安装命令。接入前先确认权限范围;涉及写入、删除或付款的操作,建议先在测试环境验证。

分类开发工具 传输方式stdio · 本地进程 鉴权无需鉴权 部署方式本地部署
GitHub 星标 1 仓库热度,无公开仓库时显示未标注
工具数 未标注 暴露给模型的工具数量
资源 / 提示词 0 / 0 资源与提示词模板数量
维护方 社区 / 第三方 核验等级 仅资料核对

工具与能力

开发工具
数量来自来源目录的标注,具体工具清单以官方文档为准
工具数量未标注暴露给模型可调用的工具个数
资源数量0可读取的资源条目
提示词数量0内置提示词模板数量

能力标签

适用场景

测试框架Python本地部署pytest插件MCP协议服务器测试自动化测试

安装与配置

stdio · 本地进程 无需鉴权
客户端在本机拉起子进程,数据不出本机
pip install mcp-test-framework

这条命令只是入口,具体启动参数请按官方文档补齐,不要直接照搬到别的服务器。

  1. 1
    准备运行环境

    stdio 服务在本地起进程,需要先装好 Node.js 等运行时,再按安装命令拉取依赖。

  2. 2
    安装服务

    复制上面的安装命令执行;命令里带占位符的位置,换成自己的密钥或路径。

  3. 3
    写入客户端配置

    在客户端的 MCP 配置里填好启动命令和参数,保存后完全退出客户端再打开,让新配置生效。

  4. 4
    验证是否可用

    连接成功后随便调一个只读工具,确认能返回结果,再接入正式流程。

典型使用场景

3 类
按分类与标签整理的常见用法,具体以官方文档为准
代码与仓库操作

在对话里直接查代码、提 issue、走 PR,省掉来回切工具。

本地命令与脚本

把构建、测试、格式化这类重复命令交给模型执行。

接口联调与测试

让模型调你的接口、跑一遍用例,快速定位问题。

选择建议

选之前先看这几条
按条目字段给出的判断,不替你决定用不用

值得选的地方

  • 有现成的安装命令,不用自己找入口,照着跑就能起服务。
  • 资料标注无需鉴权,装好就能连。

先想清楚的地方

  • stdio 方式要求本地装好运行时(如 Node.js),换机器要重新装一遍。
  • 资料没标注工具数量,实际能力要装好后确认。
  • 版本更新与行为细节以官方仓库为准。

更适合

  • 需要在开发工具场景里让模型直接调用外部能力的人。
  • 想先快速试一下、能接受在本地或自有环境跑服务的人。

不太适合

  • 完全不想在本地装环境的人。
  • 需要厂商级 SLA 和技术支持的生产环境。

同类对比

6 个同类
同分类里挑最接近的几条,字段以各自详情页为准
MCP 服务器工具数传输方式部署方式安装命令最后核验
MCP Test Framework当前 未标注 stdio · 本地进程 本地部署 有 2026-05-17
Git 838e2ec1 12 stdio · 本地进程 本地部署 有 2026-05-17
Markitdown MCP 2c653a5f 1 stdio · 本地进程 本地部署 有 2026-05-17
JavaGuide 未标注 stdio · 本地进程 本地部署 有 2026-04-11
awesome-claude-skills 未标注 stdio · 本地进程 本地部署 有 2026-04-26
Context7 831acf40 未标注 stdio · 本地进程 本地部署 有 2026-05-17

服务器档案

开发工具
字段来自项目仓库与公开索引,中文说明由本站标注
提供方aryanjp1上游标注的维护方 aryanjp1
分类开发工具本站按上游分类展示
连接方式stdio · 本地进程客户端在本机拉起子进程,数据不出本机
鉴权方式无需鉴权远程服务建议用最小权限凭据
维护方社区 / 第三方核验等级 仅资料核对
包名 / 运行时未标注 · 未标注本地安装时用到的包标识与运行时
部署类型本地部署上游给出的部署形态
最近核验2026-05-17上游最后核验时间

详细介绍

7.1 KB
来自项目仓库与上游目录的原文,未做删改

mcp测试框架

用于测试MCP(模型上下文协议)服务器的pytest插件。

快速开始```bash

pip install mcp-test-framework
``````python
import pytest
from mcp import StdioServerParameters
from pytest_mcp import assert_tool_exists

@pytest.fixture
def mcp_server():
return StdioServerParameters(
command="python", args=["my_server.py"]
)

async def test_my_tool(mcp_client):
await assert_tool_exists(mcp_client, "my_tool")
result = await mcp_client.call_tool("my_tool", {"arg": "value"})
assert result is not None


该插件自动处理服务器生命周期、连接管理,并提供丰富的断言。

## 特性

### 模拟MCP客户端

在没有网络开销的情况下测试服务器:```python
from pytest_mcp import MockMCPClient

async with MockMCPClient(command="python", args=["server.py"]) as client:
tools = await client.list_tools()
result = await client.call_tool("add", {"a": 1, "b": 2})

自动注射夹具

定义您的服务器设备并自动连接客户端:```python
@pytest.fixture
def mcp_server():
return {"command": "python", "args": ["server.py"]}

async def test_tool(mcp_client):
tools = await mcp_client.list_tools()
assert len(tools) > 0


### 丰富的断言

使用为MCP测试设计的描述性断言:```python
from pytest_mcp import (
assert_tool_exists,
assert_tool_output_matches,
assert_tool_returns_error,
assert_resource_exists,
)

async def test_calculator(mcp_client):
await assert_tool_exists(mcp_client, "add")

result = await mcp_client.call_tool("add", {"a": 2, "b": 3})
await assert_tool_output_matches(result, 5)

await assert_tool_returns_error(
mcp_client, "divide", {"a": 1, "b": 0},
error_message="division by zero"
)

await assert_resource_exists(mcp_client, "config://settings")

快照测试

保存并比较测试运行中的工具输出:```python
async def test_user_data(mcp_client, snapshot):
result = await mcp_client.call_tool("get_user", {"id": 1})
snapshot.assert_match(result, "user_1_response")


需要时更新快照:```bash
pytest --mcp-update-snapshots

服务器生命周期管理

控制服务器启动和关闭以进行集成测试:```python
from pytest_mcp import MCPTestServer

async def test_integration():
async with MCPTestServer("python", ["server.py"]) as server:
client = server.get_client()
result = await client.call_tool("hello", {"name": "world"})
await server.restart()


## API 参考

### 客户端

#### 模拟MCPClient```python
MockMCPClient(
server_params: StdioServerParameters | None = None,
*,
command: str | None = None,
args: Sequence[str] | None = None,
env: dict[str, str] | None = None,
)

方法:

  • async list_tools() -> list[Tool] -列出可用工具
  • async call_tool(name: str, arguments: dict) -> CallToolResult -执行工具
  • async list_resources() -> list[Resource] -列出可用资源
  • async read_resource(uri: str) -> ReadResourceResult -阅读资源
  • async get_tool(name: str) -> Tool | None -按名称获取特定工具

夹具

  • mcp_client -自动注入客户端连接到您的服务器
  • mcp_server -返回服务器参数的用户定义夹具
  • mcp_test_server -具有生命周期控制的高级夹具
  • snapshot -快照测试助手
  • mcp_server_env -服务器的环境变量

断言```python

Tool assertions

await assert_tool_exists(client, "tool_name")
await assert_tool_count(client, expected_count)
await assert_tool_output_matches(result, expected_value, partial=False)
await assert_tool_returns_error(client, "tool_name", args, error_message="...")
await assert_tools_have_unique_names(client)

Schema validation

assert_tool_schema_valid(tool)

Resource assertions

await assert_resource_exists(client, "resource://uri")
await assert_resource_content_matches(client, "resource://uri", expected_content)


### 快照测试```python
# JSON snapshots
snapshot.assert_match(data, "snapshot_name")
snapshot.assert_match_json({"key": "value"}, "json_snapshot")

# Text snapshots
snapshot.assert_match_text("output", "text_snapshot")

# Utilities
snapshot.get_snapshot("name")
snapshot.delete_snapshot("name")
snapshot.list_snapshots()

服务器管理```python

async with MCPTestServer(command, args, env) as server:
client = server.get_client()
await server.restart()
await server.wait_for_ready()


## 使用示例

### 基本计算器服务器

**服务器.py:**```python
from mcp.server import Server
from mcp.types import Tool, TextContent

app = Server("calculator")

@app.list_tools()
async def list_tools():
return [
Tool(
name="add",
description="Add two numbers",
inputSchema={
"type": "object",
"properties": {
"a": {"type": "number"},
"b": {"type": "number"},
},
"required": ["a", "b"],
},
)
]

@app.call_tool()
async def call_tool(name, arguments):
if name == "add":
result = arguments["a"] + arguments["b"]
return [TextContent(type="text", text=str(result))]

test_server.py:```python
import pytest
from pytest_mcp import assert_tool_exists, assert_tool_output_matches

@pytest.fixture
def mcp_server():
return {"command": "python", "args": ["server.py"]}

async def test_add(mcp_client):
await assert_tool_exists(mcp_client, "add")
result = await mcp_client.call_tool("add", {"a": 5, "b": 3})
await assert_tool_output_matches(result, "8")


### 高级功能

**测试资源:**```python
async def test_resources(mcp_client):
resources = await mcp_client.list_resources()
assert len(resources) > 0

content = await mcp_client.read_resource("config://settings")
assert content is not None

错误处理:```python
async def test_validation(mcp_client):
await assert_tool_returns_error(
mcp_client,
"divide",
{"a": 10, "b": 0},
error_message="Cannot divide by zero"
)


**快照测试:**```python
async def test_complex_output(mcp_client, snapshot):
result = await mcp_client.call_tool("get_report", {"id": 123})
snapshot.assert_match(result, "report_123")

配置

pytest.ini / pyproject.toml```ini

[tool.pytest.ini_options]
asyncio_mode = "auto"

markers = [
"mcp: MCP server test (auto-applied)",
"mcp_integration: MCP integration test",
"mcp_slow: Slow MCP test",
]


### 命令行选项```bash
# Set log level
pytest --mcp-log-level=DEBUG

# Set operation timeout
pytest --mcp-timeout=60

# Update snapshots
pytest --mcp-update-snapshots

与FastMCP集成

使用 FastMCP:```python
from fastmcp import FastMCP
from pytest_mcp import MockMCPClient

mcp = FastMCP("My Server")

@mcp.tool()
def greet(name: str) -> str:
return f"Hello, {name}!"

@pytest.fixture
def mcp_server():
return mcp.get_server_params()

async def test_greet(mcp_client):
result = await mcp_client.call_tool("greet", {"name": "Alice"})
await assert_tool_output_matches(result, "Hello, Alice!")


## 贡献

欢迎捐款。开始:```bash
git clone https://github.com/aryanjp1/pytest-mcp.git
cd pytest-mcp
pip install -e ".[dev]"
pytest
black .
ruff check .
mypy src/

看 贡献.md 详细指南。

许可证

MIT许可证-请参阅 许可证 文件。

致谢

  • 为 模型上下文协议 通过Anthropic
  • 受pytest插件架构启发
  • 使用 主控程序 开发包

资源

  • MCP官方文件
  • MCP Python SDK
  • pytest文档

社区

  • GitHub 问题 -Bug报告和功能请求
  • 讨论 -问题和想法

常见问题

7 问
答案由本站依据条目字段整理,不引用上游原文
MCP Test Framework 怎么安装?

在客户端按下面的安装命令接入:pip install mcp-test-framework。装完在客户端配置里引用同一个包或端点。

MCP Test Framework 怎么在客户端里配置?

它的传输方式未标注,在客户端 MCP 配置里按官方文档填写启动命令和参数,保存后重启客户端生效。

MCP Test Framework 收费吗?

本站不代收费用,也没有标注具体价格。是否收费取决于服务本身和背后的接口额度,正式使用前按官方说明确认。

MCP Test Framework 需要 API Key 吗?

资料标注为无需认证,装好就能连;如果实际调用时报鉴权错误,按官方文档补上凭据。

MCP Test Framework 能提供哪些能力?

资料显示能力标签包括 测试框架、服务器测试、Python、自动化测试。具体工具清单以官方文档为准。

MCP Test Framework 需要什么运行环境?

属于本地进程,需要在本机装好运行环境。

MCP Test Framework 是本地还是远程?

它是本地进程(stdio),由客户端在本机拉起。传输方式为stdio · 本地进程。

热门专题

按标签挑服务器
全部专题 →

服务器信息以作者仓库与官方文档为准;分类、传输方式与鉴权方式的中文说明为中文整理,最近核验 2026-05-17。