MCP Pytest Server
Pytest MCP Service
Package Status
We are running the published npm package (@modelcontextprotocol/mcp-pytest-server), not locally compiled source. This is confirmed by:
- The executable path: ~/.npm/_npx/15b07286cbcc3329/node_modules/.bin/mcp-server-memory
- The package.json configuration specifying the binary should be built to dist/index.js
- The presence in the npm global cache
For reference, the Python SDK releases are available at: https://github.com/modelcontextprotocol/python-sdk/tags
Viewing Logs
To view the server output and logs:
- View the live terminal output where the server is running
- Check the log file at ~/workspace/mcp-pytest-server/output.log
- Use tail to follow the log in real-time:Copytail -f ~/workspace/mcp-pytest-server/output.log
- For historical logs, use less or cat:Copyless ~/workspace/mcp-pytest-server/output.log cat ~/workspace/mcp-pytest-server/output.log
Getting Started
Prerequisites
- Node.js v16 or higher
- Python 3.8 or higher
- npm installed
- Memory service (@modelcontextprotocol/server-memory) running (recommended to use uvx for background execution):
- Install uvx:
npm install -g uvx
- Create uvx config (uvx.config.js):Copymodule.exports = { services: { memory: { command: 'node ~/.npm/_npx/15b07286cbcc3329/node_modules/.bin/mcp-server-memory', autorestart: true, log: 'memory.log', env: { NODE_ENV: 'production' } } } }
- Start service:
uvx start memory
- Install uvx:
Installation for mcp-pytest-server development only
Navigate to Project Directory
Install JavaScript Dependencies
Start MCP Pytest Server
Run Pytest with MCP Integration
Inspecting Services
Inspecting the Memory Service
To inspect the memory service:
- Start the service in debug mode:Copynpx --node-options='--inspect' @modelcontextprotocol/server-memory
- Open Chrome DevTools at chrome://inspect
- Click "Open dedicated DevTools for Node"
- Set breakpoints and inspect the service's execution
Alternatively, use VSCode's built-in Node.js debugging:
- Create a launch.json configuration:
Inspecting the MCP-Pytest Service during development
To inspect the mcp-pytest service:
- Start the service in debug mode:Copynode --inspect ~/workspace/mcp-pytest-server/index.js
- Open Chrome DevTools at chrome://inspect
- Click "Open dedicated DevTools for Node"
- Set breakpoints and inspect the service's execution
Alternatively, use VSCode's built-in Node.js debugging:
- Create a launch.json configuration:
Architecture and Implementation
Overview
The MCP pytest integration consists of multiple components:
- mcp-pytest-server: A Node.js server that implements the MCP service tools
- conftest.py: Test configuration that integrates pytest with the MCP service
- SDKs: Both JavaScript and Python SDKs for MCP integration
Component Details
mcp-pytest-server (JavaScript)
- Location: ~/workspace/mcp-pytest-server
- Implementation: Node.js (index.js)
- Status: Running the published npm package (not locally compiled)
- Package Status: Published as '@modelcontextprotocol/mcp-pytest-server' on npm
- Executable Path: ~/.npm/_npx/15b07286cbcc3329/node_modules/.bin/mcp-server-memory (confirms published package usage)
- Functionality: Provides MCP service tools for pytest integration
conftest.py (Python)
- Location: ~/workspace/textgrad/tests/conftest.py
- Purpose: Configures pytest to integrate with MCP services
- Current State: Successfully using Python SDK from ~/workspace/mcp-pytest-server/python-sdk
SDKs
JavaScript SDK
- Location: https://github.com/modelcontextprotocol/typescript-sdk
- Package Status: Published as '@modelcontextprotocol/sdk' on npm
- Usage: Can be installed via npm install @modelcontextprotocol/sdk
- Implementation: Provides TypeScript/JavaScript client for MCP integration
Python SDK
- Location: ~/workspace/mcp-pytest-server/python-sdk
- Package Status: Not published on any package manager (PyPI, Conda, etc.)
- Usage: Used internally by pytest integration
- Implementation: Provides Python client for MCP integration
- Installation for Multiple Projects:
- Navigate to the package directory: cd ~/workspace/mcp-pytest-server/python-sdk
- Install in development mode: pip install -e .
- The package will now be available to all Python projects on the system
- To update, simply pull the latest changes from the repository
Implementation Status
The core functionality for all three tools (record_session_start, record_test_outcome, record_session_finish) has been implemented in index.js. The implementation includes:
Implementation Status: The core functionality for all three tools (record_session_start, record_test_outcome, record_session_finish) has been implemented in index.js. The implementation includes:
- Input validation for all tools
- Proper error handling and logging
- Tool registration and request handling
- Basic response generation
1. record_session_start
[IMPLEMENTED]
Description:
This tool is called at the beginning of a pytest session. It initializes the context for the current test run by creating or updating the "TestRun_Latest" and "Env_Current" entities in the memory
MCP server. Importantly, this tool also ensures that any data from previous test runs associated with "TestRun_Latest" is cleared to maintain a single source of truth for the last run.
Implementation Details:
- Input validation for environment.os and environment.python_version
- Basic response generation with environment details
- Error handling for invalid parameters
Input Schema:
mcp call 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": "string", "outcome": "string (passed|failed|skipped)", "duration": "number", "error": "string (optional)" }
use_mcp_tool pytest-mcp record_test_outcome '{"nodeid": "test_module.py", "outcome": "passed", "duration": 0.123}' use_mcp_tool pytest-mcp record_test_outcome '{"nodeid": "test_module.py", "outcome": "failed", "duration": 0.05, "error": "AssertionError: ... "}'
{ "summary": { "total_tests": "integer", "passed": "integer", "failed": "integer", "skipped": "integer", "exitstatus": "integer" } }
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 <port_number>
use_pytest-mcp
A Node.js server that integrates with pytest to facilitate the ModelContextProtocol (MCP) service tools, enabling test execution recording and environment tracking.