MCP Pytest Server

local-only server

The server can only run on the client’s local machine because it depends on local resources.

Integrations

  • Allows integration with pytest for test automation, including recording test session starts, test outcomes, and session finishes. The integration enables pytest to use the MCP service tools through a conftest.py configuration.

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

查看日志

查看服务器输出和日志:

  1. 查看服务器正在运行的实时终端输出
  2. 检查 ~/workspace/mcp-pytest-server/output.log 处的日志文件
  3. 使用tail实时跟踪日志:
    tail -f ~/workspace/mcp-pytest-server/output.log
  4. 对于历史日志,使用 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后台执行):
    1. 安装 uvx: npm install -g uvx
    2. 创建 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' } } } }
    3. 启动服务: uvx start memory

仅适用于 mcp-pytest-server 开发的安装

导航到项目目录

cd ~/workspace/mcp-pytest-server

安装 JavaScript 依赖项

npm install @modelcontextprotocol/sdk npm install

启动 MCP Pytest 服务器

node index.js

使用 MCP 集成运行 Pytest

pytest --mcp

检查服务

检查内存服务

检查内存服务:

  1. 以调试模式启动服务:
    npx --node-options='--inspect' @modelcontextprotocol/server-memory
  2. 打开 Chrome DevTools,网址为 chrome://inspect
  3. 点击“打开 Node 专用 DevTools”
  4. 设置断点并检查服务的执行情况

或者,使用 VSCode 的内置 Node.js 调试:

  1. 创建 launch.json 配置:
{ "type": "node", "request": "launch", "name": "Debug Memory Service", "runtimeExecutable": "npx", "runtimeArgs": ["@modelcontextprotocol/server-memory"], "args": [], "console": "integratedTerminal" }

在开发过程中检查 MCP-Pytest 服务

要检查 mcp-pytest 服务:

  1. 以调试模式启动服务:
    node --inspect ~/workspace/mcp-pytest-server/index.js
  2. 打开 Chrome DevTools,网址为 chrome://inspect
  3. 点击“打开 Node 专用 DevTools”
  4. 设置断点并检查服务的执行情况

或者,使用 VSCode 的内置 Node.js 调试:

  1. 创建 launch.json 配置:
{ "type": "node", "request": "launch", "name": "Debug MCP-Pytest Service", "program": "${workspaceFolder}/index.js", "console": "integratedTerminal" }

架构与实施

概述

MCP pytest 集成由多个组件组成:

  1. mcp-pytest-server :实现 MCP 服务工具的 Node.js 服务器
  2. conftest.py :将 pytest 与 MCP 服务集成的测试配置
  3. 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

Python SDK

  • 位置:〜/工作区/ mcp-pytest-server / python-sdk
  • 软件包状态:未在任何软件包管理器(PyPI、Conda 等)上发布
  • 用途:由 pytest 集成内部使用
  • 实现:提供用于 MCP 集成的 Python 客户端
  • 多个项目的安装:
    1. 导航到包目录:cd ~/workspace/mcp-pytest-server/python-sdk
    2. 以开发模式安装:pip install -e。
    3. 该包现在可供系统上的所有 Python 项目使用
    4. 要更新,只需从存储库中提取最新更改

实施情况

这三个工具(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" } } **Example Usage:**

mcp 调用 pytest-mcp record_session_start '{"environment": {"os": "Macos", "python_version": "3.13.1"}}'

Expected Behavior: Clear Previous Data: Deletes the "TestRun_Latest" entity and any relations where "TestRun_Latest" is the from or to entity from the memory MCP server. This ensures no accumulation of historical data. Create "Env_Current" Entity: Creates an entity named "Env_Current" with the entity type "TestEnvironment" and observations for the operating system and Python version. Create "TestRun_Latest" Entity: Creates an entity named "TestRun_Latest" with the entity type "TestRun" and an initial observation like "status: running". Create Relation: Creates a relation of type "ran_on" from "TestRun_Latest" to "Env_Current". Example Interaction (run in cline window):

use_mcp_tool pytest-mcp record_session_start'{“environment”:{“os”:“Macos”,“python_version”:“3.13.1”}}'

## 2. record_test_outcome [IMPLEMENTED] Description: This tool is called after each individual test case has finished executing. It records the outcome of the test (passed, failed, skipped), its duration, and any error information if the test failed. **Implementation Details:** - Input validation for nodeid, outcome, duration, and optional error - Basic response generation with test outcome details - Error handling for invalid parameters Input Schema:

{ “nodeid”:“字符串”, “outcome”:“字符串(通过|失败|跳过)”, “duration”:“数字”, “error”:“字符串(可选)” }

Expected Behavior: Create/Update TestCase Entity: Creates or updates an entity with the name matching the nodeid (e.g., "test_module.py::test_function"), setting its entity type to "TestCase". Add Outcome Observation: Adds an observation with the format "outcome: <outcome>" to the TestCase entity. Add Duration Observation: Adds an observation with the format "duration: <duration>" to the TestCase entity. Add Error Observation (if applicable): If the outcome is "failed" and the error field is provided, add an observation with the format "error: <error>" to the TestCase entity. Create Relation: Creates a relation of type "contains_test" from "TestRun_Latest" to the TestCase entity. Example Interaction (run in cline window):

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:...”}'

## 3. record_session_finish [IMPLEMENTED] Description: This tool is called at the end of a pytest session. It records summary information about the entire test run, such as the total number of tests, the counts of passed, failed, and skipped tests, and the exit status of the pytest process. It also updates the status of the "TestRun_Latest" entity to "finished". **Implementation Details:** - Input validation for summary object - Basic response generation with session summary - Error handling for invalid parameters Input Schema:

{ “summary”:{ “total_tests”:“整数”, “passed”:“整数”, “failed”:“整数”, “skipped”:“整数”, “exitstatus”:“整数” } }

Expected Behavior: Update TestRun_Latest Status: Updates the "TestRun_Latest" entity's observation "status: running" to "status: finished". Add Summary Observations: Adds observations to the "TestRun_Latest" entity for total_tests, passed, failed, skipped, and exitstatus based on the input summary. Add End Time Observation: Adds an observation with the format "end_time: <timestamp>" to the "TestRun_Latest" entity. Example Interaction (run in cline window):

use_mcp_tool pytest-mcp record_session_finish '{"summary": {"total_tests": 10, "passed": 7, "failed": 2, "skipped": 1, "exitstatus": 0}}'

## Debugging the service

节点〜/工作区/mcp-pytest-server/index.js

ps aux | grep index.js sudo tcpdump -i any -s 0 -w mcp_traffic.pcap port <port_number>

cline

使用pytest-mcp

#Development Suggested Optimizations: ## Faster JSON Use a Faster JSON Library: Replace the built-in json module with orjson for faster parsing and serialization. import orjson as json ## Dispatch mechanism Implement a Dispatch Mechanism: Use dictionaries to map request types and tool names to handler functions. def handle_list_tools(request): # ... def handle_record_session_start(args): # ... # ... other tool handlers ... request_handlers = { "list_tools": handle_list_tools, "call_tool": { "record_session_start": handle_record_session_start, # ... other tools ... } } 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}"}) ## Concurrency Concurrency: Explore using asynchronous programming (e.g., asyncio) or threading to handle multiple requests concurrently. This would require more significant changes to the server's structure. ## Python SDK Implementation Summary ### Current Status - Python SDK package structure created at ~/workspace/mcp-pytest-server/python-sdk - Basic package files implemented: - setup.py with package configuration - src/mcp/__init__.py with version information - Package successfully installed in development mode using pip install -e . - PYTHONPATH configuration verified to allow package import - Currently running as a development installation with full source access - Service level: Development/Testing (not production-ready) ### Service Level Details - **Development Mode**: Running with pip install -e . allows for immediate code changes without reinstallation - **Source Access**: Full access to source code for debugging and development - **Dependencies**: Managed through setup.py with direct access to local development environment - **Stability**: Suitable for testing and development, not recommended for production use - **Performance**: May include debug logging and unoptimized code paths ### Remaining Tasks - Implement core MCP client functionality in Python SDK - Add pytest integration hooks - Create proper test suite for Python SDK - Publish package to PyPI for easier distribution - Optimize for production deployment
ID: vrkz0o2puq