
MCP Pytest Server
一个集成了 pytest 的 Node.js 服务器,用于辅助 ModelContextProtocol (MCP) 服务工具,从而实现测试执行记录和环境跟踪。
README
Pytest MCP 服务
包状态
我们正在运行已发布的 npm 包 (@modelcontextprotocol/mcp-pytest-server),而不是本地编译的源代码。这已通过以下方式确认:
- 可执行文件路径:~/.npm/_npx/15b07286cbcc3329/node_modules/.bin/mcp-server-memory
- package.json 配置指定二进制文件应构建到 dist/index.js
- 存在于 npm 全局缓存中
作为参考,Python SDK 版本可在以下网址获取:https://github.com/modelcontextprotocol/python-sdk/tags
查看日志
要查看服务器输出和日志:
- 查看服务器正在运行的实时终端输出
- 检查 ~/workspace/mcp-pytest-server/output.log 中的日志文件
- 使用 tail 实时跟踪日志:
tail -f ~/workspace/mcp-pytest-server/output.log
- 对于历史日志,请使用 less 或 cat:
less ~/workspace/mcp-pytest-server/output.log cat ~/workspace/mcp-pytest-server/output.log
入门
前提条件
- Node.js v16 或更高版本
- Python 3.8 或更高版本
- 已安装 npm
- 正在运行的内存服务 (@modelcontextprotocol/server-memory)(建议使用 uvx 进行后台执行):
- 安装 uvx:
npm install -g uvx
- 创建 uvx 配置 (uvx.config.js):
module.exports = { services: { memory: { command: 'node ~/.npm/_npx/15b07286cbcc3329/node_modules/.bin/mcp-server-memory', autorestart: true, log: 'memory.log', env: { NODE_ENV: 'production' } } } }
- 启动服务:
uvx start memory
- 安装 uvx:
仅用于 mcp-pytest-server 开发的安装
导航到项目目录
cd ~/workspace/mcp-pytest-server
安装 JavaScript 依赖项
npm install @modelcontextprotocol/sdk
npm install
启动 MCP Pytest 服务器
node index.js
运行带有 MCP 集成的 Pytest
pytest --mcp
检查服务
检查内存服务
要检查内存服务:
-
在调试模式下启动服务:
npx --node-options='--inspect' @modelcontextprotocol/server-memory
-
在 chrome://inspect 打开 Chrome DevTools
-
单击“Open dedicated DevTools for Node”
-
设置断点并检查服务的执行
或者,使用 VSCode 的内置 Node.js 调试:
- 创建 launch.json 配置:
{
"type": "node",
"request": "launch",
"name": "Debug Memory Service",
"runtimeExecutable": "npx",
"runtimeArgs": ["@modelcontextprotocol/server-memory"],
"args": [],
"console": "integratedTerminal"
}
在开发期间检查 MCP-Pytest 服务
要检查 mcp-pytest 服务:
-
在调试模式下启动服务:
node --inspect ~/workspace/mcp-pytest-server/index.js
-
在 chrome://inspect 打开 Chrome DevTools
-
单击“Open dedicated DevTools for Node”
-
设置断点并检查服务的执行
或者,使用 VSCode 的内置 Node.js 调试:
- 创建 launch.json 配置:
{
"type": "node",
"request": "launch",
"name": "Debug MCP-Pytest Service",
"program": "${workspaceFolder}/index.js",
"console": "integratedTerminal"
}
架构和实现
概述
MCP pytest 集成由多个组件组成:
- mcp-pytest-server:一个实现 MCP 服务工具的 Node.js 服务器
- conftest.py:将 pytest 与 MCP 服务集成的测试配置
- SDK:用于 MCP 集成的 JavaScript 和 Python SDK
组件详细信息
mcp-pytest-server (JavaScript)
- 位置:~/workspace/mcp-pytest-server
- 实现:Node.js (index.js)
- 状态:运行已发布的 npm 包(未本地编译)
- 包状态:在 npm 上发布为“@modelcontextprotocol/mcp-pytest-server”
- 可执行文件路径:~/.npm/_npx/15b07286cbcc3329/node_modules/.bin/mcp-server-memory(确认已发布包的使用)
- 功能:为 pytest 集成提供 MCP 服务工具
conftest.py (Python)
- 位置:~/workspace/textgrad/tests/conftest.py
- 目的:配置 pytest 以与 MCP 服务集成
- 当前状态:成功使用来自 ~/workspace/mcp-pytest-server/python-sdk 的 Python SDK
SDK
JavaScript SDK
- 位置:https://github.com/modelcontextprotocol/typescript-sdk
- 包状态:在 npm 上发布为“@modelcontextprotocol/sdk”
- 用法:可以通过 npm install @modelcontextprotocol/sdk 安装
- 实现:为 MCP 集成提供 TypeScript/JavaScript 客户端
Python SDK
- 位置:~/workspace/mcp-pytest-server/python-sdk
- 包状态:未在任何包管理器(PyPI、Conda 等)上发布
- 用法:由 pytest 集成在内部使用
- 实现:为 MCP 集成提供 Python 客户端
- 多个项目的安装:
- 导航到包目录:cd ~/workspace/mcp-pytest-server/python-sdk
- 在开发模式下安装:pip install -e .
- 该包现在可用于系统上的所有 Python 项目
- 要更新,只需从存储库中提取最新的更改
实现状态
所有三个工具(record_session_start、record_test_outcome、record_session_finish)的核心功能已在 index.js 中实现。该实现包括:
实现状态: 所有三个工具(record_session_start、record_test_outcome、record_session_finish)的核心功能已在 index.js 中实现。该实现包括:
- 所有工具的输入验证
- 正确的错误处理和日志记录
- 工具注册和请求处理
- 基本响应生成
1. record_session_start
[已实现]
描述:
此工具在 pytest 会话开始时调用。它通过在 memory
MCP 服务器中创建或更新“TestRun_Latest”和“Env_Current”实体来初始化当前测试运行的上下文。重要的是,此工具还确保清除与“TestRun_Latest”关联的先前测试运行中的任何数据,以保持上次运行的单一事实来源。
实现细节:
- environment.os 和 environment.python_version 的输入验证
- 带有环境详细信息的基本响应生成
- 无效参数的错误处理
输入模式:
{
"environment": {
"os": "string",
"python_version": "string"
}
}
**用法示例:**
mcp call pytest-mcp record_session_start '{"environment": {"os": "Macos", "python_version": "3.13.1"}}'
预期行为:
清除先前数据:从内存 MCP 服务器中删除“TestRun_Latest”实体以及“TestRun_Latest”作为 from 或 to 实体的任何关系。这确保了不会累积历史数据。
创建“Env_Current”实体:创建一个名为“Env_Current”的实体,实体类型为“TestEnvironment”,并观察操作系统和 Python 版本。
创建“TestRun_Latest”实体:创建一个名为“TestRun_Latest”的实体,实体类型为“TestRun”,并进行初始观察,例如“status: running”。
创建关系:创建从“TestRun_Latest”到“Env_Current”的“ran_on”类型的关系。
示例交互(在 cline 窗口中运行):
use_mcp_tool pytest-mcp record_session_start '{"environment": {"os": "Macos", "python_version": "3.13.1"}}'
## 2. record_test_outcome [已实现]
描述:
此工具在每个单独的测试用例执行完成后调用。它记录测试的结果(通过、失败、跳过)、其持续时间以及测试失败时的任何错误信息。
**实现细节:**
- nodeid、outcome、duration 和可选错误的输入验证
- 带有测试结果详细信息的基本响应生成
- 无效参数的错误处理
输入模式:
{ "nodeid": "string", "outcome": "string (passed|failed|skipped)", "duration": "number", "error": "string (optional)" }
预期行为:
创建/更新 TestCase 实体:创建或更新一个名称与 nodeid 匹配的实体(例如,“test_module.py::test_function”),将其实体类型设置为“TestCase”。
添加结果观察:向 TestCase 实体添加一个格式为“outcome: <outcome>”的观察。
添加持续时间观察:向 TestCase 实体添加一个格式为“duration: <duration>”的观察。
添加错误观察(如果适用):如果结果为“failed”并且提供了 error 字段,则向 TestCase 实体添加一个格式为“error: <error>”的观察。
创建关系:创建从“TestRun_Latest”到 TestCase 实体的“contains_test”类型的关系。
示例交互(在 cline 窗口中运行):
use_mcp_tool pytest-mcp record_test_outcome '{"nodeid": "test_module.py::test_example", "outcome": "passed", "duration": 0.123}' use_mcp_tool pytest-mcp record_test_outcome '{"nodeid": "test_module.py::test_failure", "outcome": "failed", "duration": 0.05, "error": "AssertionError: ... "}'
## 3. record_session_finish [已实现]
描述:
此工具在 pytest 会话结束时调用。它记录有关整个测试运行的摘要信息,例如测试总数、通过、失败和跳过的测试计数以及 pytest 进程的退出状态。它还会将“TestRun_Latest”实体的状态更新为“finished”。
**实现细节:**
- 摘要对象的输入验证
- 带有会话摘要的基本响应生成
- 无效参数的错误处理
输入模式:
{ "summary": { "total_tests": "integer", "passed": "integer", "failed": "integer", "skipped": "integer", "exitstatus": "integer" } }
预期行为:
更新 TestRun_Latest 状态:将“TestRun_Latest”实体的观察“status: running”更新为“status: finished”。
添加摘要观察:根据输入摘要,向“TestRun_Latest”实体添加 total_tests、passed、failed、skipped 和 exitstatus 的观察。
添加结束时间观察:向“TestRun_Latest”实体添加一个格式为“end_time: <timestamp>”的观察。
示例交互(在 cline 窗口中运行):
use_mcp_tool pytest-mcp record_session_finish '{"summary": {"total_tests": 10, "passed": 7, "failed": 2, "skipped": 1, "exitstatus": 0}}'
## 调试服务
node ~/workspace/mcp-pytest-server/index.js
ps aux | grep index.js sudo tcpdump -i any -s 0 -w mcp_traffic.pcap port <端口号>
cline
use_pytest-mcp
#开发
建议的优化:
## 更快的 JSON
使用更快的 JSON 库:将内置的 json 模块替换为 orjson,以实现更快的解析和序列化。
import orjson as json
## 分发机制
实现分发机制:使用字典将请求类型和工具名称映射到处理函数。
def handle_list_tools(request):
# ...
def handle_record_session_start(args):
# ...
# ... 其他工具处理程序 ...
request_handlers = {
"list_tools": handle_list_tools,
"call_tool": {
"record_session_start": handle_record_session_start,
# ... 其他工具 ...
}
}
def handle_request(request):
request_type = request["type"]
handler = request_handlers.get(request_type)
if handler:
if request_type == "call_tool":
tool_name = request["name"]
tool_handler = handler.get(tool_name)
if tool_handler:
tool_handler(request["arguments"])
else:
send_response({"type": "error", "code": -32601, "message": f"Unknown tool: {tool_name}"})
else:
handler(request)
else:
send_response({"type": "error", "code": -32601, "message": f"Unknown request type: {request_type}"})
## 并发
并发:探索使用异步编程(例如,asyncio)或线程来并发处理多个请求。 这将需要对服务器的结构进行更重大的更改。
## Python SDK 实现摘要
### 当前状态
- Python SDK 包结构已在 ~/workspace/mcp-pytest-server/python-sdk 中创建
- 已实现基本包文件:
- 带有包配置的 setup.py
- 带有版本信息的 src/mcp/__init__.py
- 使用 pip install -e . 在开发模式下成功安装包
- 已验证 PYTHONPATH 配置以允许包导入
- 当前作为具有完全源代码访问权限的开发安装运行
- 服务级别:开发/测试(未准备好用于生产)
### 服务级别详细信息
- **开发模式**:使用 pip install -e . 运行允许立即进行代码更改,而无需重新安装
- **源代码访问**:完全访问源代码以进行调试和开发
- **依赖项**:通过 setup.py 管理,可直接访问本地开发环境
- **稳定性**:适用于测试和开发,不建议用于生产用途
- **性能**:可能包括调试日志记录和未优化的代码路径
### 剩余任务
- 在 Python SDK 中实现核心 MCP 客户端功能
- 添加 pytest 集成钩子
- 为 Python SDK 创建适当的测试套件
- 将包发布到 PyPI 以便更轻松地分发
- 针对生产部署进行优化
推荐服务器
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 工单信息。
mixpanel
连接到您的 Mixpanel 数据。从 Mixpanel 分析查询事件、留存和漏斗数据。

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

Sequential Thinking MCP Server
这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。