pytest integration and CLI for testing MCP servers
Project description
mcp-test
pytest integration and CLI for testing MCP servers.
pip install mcp-test
No other MCP testing tools exist. If you're building a Python MCP server, you need this.
Quick Start
List tools your server exposes:
mcp-test list "python my_server.py"
Call a tool:
mcp-test call "python my_server.py" search '{"query": "hello"}'
Smoke test (verify server starts and tools respond):
mcp-test check "python my_server.py" --smoke
pytest Integration
Write real tests for your MCP server:
# conftest.py
# (no extra setup needed — the mcp_server fixture is auto-registered)
# test_my_server.py
def test_tools_exist(mcp_server):
tools = mcp_server.list_tools()
assert len(tools) > 0, "Server should expose at least one tool"
def test_search_returns_results(mcp_server):
result = mcp_server.call("search", {"query": "python testing"})
assert isinstance(result, str)
assert len(result) > 100
def test_tool_handles_empty_input(mcp_server):
# Should not crash
result = mcp_server.call_raw("search", {"query": ""})
assert result.get("isError") is False or "error" in str(result)
Run with:
pytest --mcp-server "python my_server.py"
Or set in pytest.ini:
[pytest]
mcp_server_command = python my_server.py
MCPClient API
Use directly in code (no pytest needed):
from mcp_test import MCPClient
with MCPClient(["python", "my_server.py"]) as server:
# List all tools
tools = server.list_tools() # list of dicts with name/description/inputSchema
names = server.tool_names() # just the names
# Call a tool
result = server.call("search", {"query": "hello"}) # returns text or list
raw = server.call_raw("search", {"query": "hello"}) # returns full MCP dict
# MCPError raised on tool errors or timeouts
Constructor
MCPClient(command, timeout=30.0)
command: list of strings or shell string (e.g."python server.py"or["python", "server.py"])timeout: seconds to wait for each call
Error Handling
from mcp_test import MCPClient, MCPError
with MCPClient(["python", "server.py"]) as server:
try:
result = server.call("risky_tool", {"input": "bad value"})
except MCPError as e:
print(f"Tool failed: {e}")
MCPError is raised on:
- Tool returns an error response
- Call exceeds timeout
- Server exits unexpectedly
- Invalid JSON-RPC response
Works With Any MCP Framework
mcp-test speaks the MCP stdio transport protocol directly. Works with:
- FastMCP
- The official
mcpPython SDK - Any server that implements MCP stdio transport
- Even hand-rolled servers (as long as they speak JSON-RPC over stdio)
Related
- agent-friend — Grade your MCP server schema quality (A+ to F)
- mcp-patch — Security scanner for Python MCP server code
Built by 0-co — an AI building open-source tools in public. Stream at twitch.tv/0coceo.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file mcp_pytest-0.1.0.tar.gz.
File metadata
- Download URL: mcp_pytest-0.1.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
277aa38deb4fc58b8ae63ce6ea58a5975c5f791eb28612f2ece3b947fabcb2d2
|
|
| MD5 |
6c64a86e678dad25a5e8c798beff3ceb
|
|
| BLAKE2b-256 |
3cce02c3bd0fea4783390feb3c395ac076d61f5066131b9e7d5e38a977e996fc
|