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实时跟踪日志:
- 对于历史日志,使用 less 或 cat:
入门
先决条件
- Node.js v16 或更高版本
- Python 3.8 或更高版本
- npm 安装
- 内存服务(@modelcontextprotocol/server-memory)运行(建议使用uvx后台执行):
- 安装 uvx:
npm install -g uvx
- 创建 uvx 配置(uvx.config.js):
- 启动服务:
uvx start memory
- 安装 uvx:
仅适用于 mcp-pytest-server 开发的安装
导航到项目目录
安装 JavaScript 依赖项
启动 MCP Pytest 服务器
使用 MCP 集成运行 Pytest
检查服务
检查内存服务
检查内存服务:
- 以调试模式启动服务:
- 打开 Chrome DevTools,网址为 chrome://inspect
- 点击“打开 Node 专用 DevTools”
- 设置断点并检查服务的执行情况
或者,使用 VSCode 的内置 Node.js 调试:
- 创建 launch.json 配置:
在开发过程中检查 MCP-Pytest 服务
要检查 mcp-pytest 服务:
- 以调试模式启动服务:
- 打开 Chrome DevTools,网址为 chrome://inspect
- 点击“打开 Node 专用 DevTools”
- 设置断点并检查服务的执行情况
或者,使用 VSCode 的内置 Node.js 调试:
- 创建 launch.json 配置:
架构与实施
概述
MCP pytest 集成由多个组件组成:
- mcp-pytest-server :实现 MCP 服务工具的 Node.js 服务器
- conftest.py :将 pytest 与 MCP 服务集成的测试配置
- SDKs :用于 MCP 集成的 JavaScript 和 Python SDKs
组件详细信息
mcp-pytest-server(JavaScript)
- 位置:〜/工作区/ 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
- 位置:〜/工作区/ 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 的输入验证
- 使用环境详细信息生成基本响应
- 无效参数的错误处理
输入模式:
mcp 调用 pytest-mcp record_session_start '{"environment": {"os": "Macos", "python_version": "3.13.1"}}'
use_mcp_tool pytest-mcp record_session_start'{“environment”:{“os”:“Macos”,“python_version”:“3.13.1”}}'
{ “nodeid”:“字符串”, “outcome”:“字符串(通过|失败|跳过)”, “duration”:“数字”, “error”:“字符串(可选)” }
use_mcp_tool pytest-mcp record_test_outcome '{“nodeid”:“test_module.py:_example”,“outcome”:“passed”,“duration”:0.123}' use_mcp_tool pytest-mcp record_test_outcome '{“nodeid”:“test_module.py:_failure”,“outcome”:“failed”,“duration”:0.05, “error”:“AssertionError:...”}'
{ “summary”:{ “total_tests”:“整数”, “passed”:“整数”, “failed”:“整数”, “skipped”:“整数”, “exitstatus”:“整数” } }
use_mcp_tool pytest-mcp record_session_finish '{"summary": {"total_tests": 10, "passed": 7, "failed": 2, "skipped": 1, "exitstatus": 0}}'
节点〜/工作区/mcp-pytest-server/index.js
ps aux | grep index.js sudo tcpdump -i any -s 0 -w mcp_traffic.pcap port <port_number>
使用pytest-mcp
This server cannot be installed
local-only server
The server can only run on the client's local machine because it depends on local resources.
与 pytest 集成的 Node.js 服务器,以促进 ModelContextProtocol (MCP) 服务工具,实现测试执行记录和环境跟踪。
Related Resources
Related MCP Servers
- AsecurityAlicenseAqualityNode.js server implementing Model Context Protocol (MCP) for filesystem operations.Last updated -14116,82863,134TypeScriptMIT License
- -securityAlicense-qualityNode.js server implementing Model Context Protocol (MCP) for filesystem operations, allowing AI systems to read, write, edit files and manage directories within specified allowed paths.Last updated -116,828JavaScriptMIT License
- -securityAlicense-qualityNode.js server implementing Model Context Protocol (MCP) for filesystem operations with regex support for allowed directories, enabling AI assistants to safely read, write, and manipulate files through natural language.Last updated -116,828JavaScriptMIT License
- AsecurityFlicenseAqualityA testing server that demonstrates Model Context Protocol features, providing access to user data, todos, and system information through resources, tools for user management and calculations, and prompt templates for various analyses.Last updated -4TypeScript