MCP Pytest Server MCP Server

Local setup required. This server has to be cloned and prepared on your machine before you register it in Claude Code.
1

Set the server up locally

Run this once to clone and prepare the server before adding it to Claude Code.

Run in terminal
pip install -r requirements.txt
2

Register it in Claude Code

After the local setup is done, run this command to point Claude Code at the built server.

Run in terminal
claude mcp add -e "DATABASE_URL=${DATABASE_URL}" mcp-pytest-server -- node "<FULL_PATH_TO_MCP_PYTEST_SERVER>/dist/index.js"

Replace <FULL_PATH_TO_MCP_PYTEST_SERVER>/dist/index.js with the actual folder you prepared in step 1.

Required:DATABASE_URL
README.md

An MCP-compliant server that executes pytest suites and stores results.

MCP Pytest 测试服务器

一个基于 Model Context Protocol (MCP) 的测试执行服务器,支持执行 pytest 测试并将结果存储到 QA 平台数据库。

功能特性

  • ✅ 支持 MCP 协议标准接口
  • ✅ 执行 pytest 测试套件
  • ✅ 解析测试结果并生成报告
  • ✅ 将测试结果存储到数据库
  • ✅ 提供工具调用接口供 Goose 使用
  • ✅ 支持异步执行和进度跟踪

架构设计

mcp-pytest-server/
├── src/                    # 源代码目录
│   ├── __init__.py
│   ├── main.py            # FastAPI 主应用
│   ├── mcp/               # MCP 协议实现
│   ├── pytest_executor/   # pytest 执行器
│   ├── database/          # 数据库操作
│   └── tools/             # 工具定义
├── tests/                 # 测试代码
├── config/                # 配置文件
├── docs/                  # 文档
└── requirements.txt       # 依赖包

快速开始

1. 安装依赖

pip install -r requirements.txt

2. 配置数据库

编辑 config/database.yaml 配置数据库连接。

3. 启动服务器

python -m src.main

4. 在 Goose 中配置

在 Goose 配置中添加 MCP 服务器:

mcp_servers:
  pytest_server:
    command: "python"
    args: ["-m", "src.main"]
    env:
      DATABASE_URL: "sqlite:///./test_results.db"

API 接口

MCP 标准接口

  • POST /initialize - 初始化连接
  • POST /tools/list - 列出可用工具
  • POST /tools/call - 调用工具
  • POST /resources/list - 列出资源
  • POST /resources/read - 读取资源

自定义工具

  1. run_pytest_tests - 执行 pytest 测试

    • 参数: test_path (测试路径), options (pytest 选项)
    • 返回: 测试执行结果和报告
  2. store_test_results - 存储测试结果到数据库

    • 参数: test_results (测试结果数据)
    • 返回: 存储状态和记录ID
  3. get_test_history - 获取历史测试记录

    • 参数: limit (限制数量), project (项目名称)
    • 返回: 历史测试记录列表

数据库设计

CREATE TABLE test_runs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    project_name TEXT NOT NULL,
    test_path TEXT NOT NULL,
    total_tests INTEGER,
    passed INTEGER,
    failed INTEGER,
    skipped INTEGER,
    duration REAL,
    status TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE test_cases (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    run_id INTEGER,
    test_name TEXT NOT NULL,
    status TEXT,
    duration REAL,
    error_message TEXT,
    stack_trace TEXT,
    FOREIGN KEY (run_id) REFERENCES test_runs (id)
);

开发指南

添加新工具

  1. src/tools/ 目录下创建新的工具模块
  2. 实现工具函数并添加装饰器
  3. src/tools/__init__.py 中注册工具

扩展数据库模型

  1. src/database/models.py 中添加新模型
  2. 更新数据库迁移脚本
  3. 添加相应的 CRUD 操作

许可证

MIT License

Tools (3)

run_pytest_testsExecutes pytest tests and returns the results and report.
store_test_resultsStores test results into the database.
get_test_historyRetrieves historical test records from the database.

Environment Variables

DATABASE_URLrequiredDatabase connection string for storing test results

Configuration

claude_desktop_config.json
{"mcp_servers": {"pytest_server": {"command": "python", "args": ["-m", "src.main"], "env": {"DATABASE_URL": "sqlite:///./test_results.db"}}}}

Try it

Run the pytest tests located in the /tests directory and summarize the failures.
Store the latest test execution results into the QA database.
Show me the history of the last 10 test runs for the current project.
Execute the test suite with verbose output enabled.

Frequently Asked Questions

What are the key features of MCP Pytest Server?

Executes pytest test suites via MCP protocol. Parses test results and generates execution reports. Stores test execution data into a database. Provides historical test data retrieval. Supports asynchronous execution and progress tracking.

What can I use MCP Pytest Server for?

Automating regression testing workflows directly from an AI assistant. Centralizing test execution logs and history for QA teams. Debugging failed test cases by querying historical error data. Triggering specific test suites during development cycles via natural language.

How do I install MCP Pytest Server?

Install MCP Pytest Server by running: pip install -r requirements.txt

What MCP clients work with MCP Pytest Server?

MCP Pytest Server works with any MCP-compatible client including Claude Desktop, Claude Code, Cursor, and other editors with MCP support.

Turn this server into reusable context

Keep MCP Pytest Server docs, env vars, and workflow notes in Conare so your agent carries them across sessions.

Need the old visual installer? Open Conare IDE.
Open Conare