Testing MCP servers

A practical workflow for testing Model Context Protocol servers: inspect, snapshot, lint, diff, replay.

Install: pip install mcp-testbench  |  PyPI: pypi.org/project/mcp-testbench

Why MCP servers need contract tests

An MCP server's real interface is not its source code -- it is the tool, prompt and resource schemas an agent sees at runtime. Rename a tool, make an optional parameter required, or narrow an enum, and every agent and client built against the old schema breaks silently. Testing an MCP server means testing that published contract, plus checking that the tools still behave when called.

The workflow

1. Inspect the server

mcp-testbench inspect -- python my_server.py
mcp-testbench inspect --url http://localhost:8000/mcp
mcp-testbench inspect --definition server.json   # offline, no live server

inspect performs the MCP handshake (initialize, tools/list, prompts/list, resources/list) and prints the normalized schema as JSON.

2. Snapshot the contract

mcp-testbench snapshot -o mcp-snapshot.json -- python my_server.py

Commit the snapshot. It is the contract your callers depend on -- see Snapshot testing.

3. Lint the schema

mcp-testbench lint -- python my_server.py

The lint flags missing or too-short descriptions (agents cannot route on them), instruction-injection / tool-poisoning phrases, untyped parameters, credential parameters without a format hint, and more, across tools, prompts and resources.

4. Gate changes in CI

mcp-testbench check mcp-snapshot.json -- python my_server.py

check diffs the live server against the committed snapshot and exits 1 on breaking changes. Wire it into CI with the GitHub Action or a pre-commit hook.

5. Replay recorded tool calls

mcp-testbench replay calls.jsonl -- python my_server.py

Schema tests prove the contract; replay proves behaviour -- recorded calls are re-run against the server and their results asserted, including structured content.

Any language, any framework

mcp-testbench speaks JSON-RPC to the server as a subprocess (stdio) or an HTTP endpoint (Streamable HTTP / legacy SSE). It never imports your server, so a Python, Node or Go server, a FastMCP server, or a server built on the official MCP SDK are all tested the same way. There is also an offline mode: run every step except replay from an exported JSON server definition, for restricted CI or for reviewing a vendor server you cannot run.

The HTTP transports are built for long-lived sessions: the legacy SSE client tracks event ids and resumes a dropped stream with Last-Event-ID (replayed events are deduplicated), and the Streamable HTTP client re-initializes once and retries once if the server forgets its session (HTTP 404), terminates the session with HTTP DELETE on close, and bounds every request with a timeout so a hung server cannot hang CI.

In pytest

# conftest.py
import sys, pytest

@pytest.fixture
def mcp_server_command():
    return [sys.executable, "my_server.py"]

# tests
def test_tools_exist(mcp_schema):
    assert "echo" in [t["name"] for t in mcp_schema["tools"]]

def test_schema_lints_clean(mcp_lint):
    assert mcp_lint() == []