mcp-testbench 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mcp_testbench-0.2.0/LICENSE +21 -0
- mcp_testbench-0.2.0/PKG-INFO +330 -0
- mcp_testbench-0.2.0/README.md +302 -0
- mcp_testbench-0.2.0/pyproject.toml +72 -0
- mcp_testbench-0.2.0/setup.cfg +4 -0
- mcp_testbench-0.2.0/src/mcp_testbench/__init__.py +28 -0
- mcp_testbench-0.2.0/src/mcp_testbench/cli.py +159 -0
- mcp_testbench-0.2.0/src/mcp_testbench/client.py +468 -0
- mcp_testbench-0.2.0/src/mcp_testbench/diff.py +403 -0
- mcp_testbench-0.2.0/src/mcp_testbench/lint.py +322 -0
- mcp_testbench-0.2.0/src/mcp_testbench/plugin.py +181 -0
- mcp_testbench-0.2.0/src/mcp_testbench/py.typed +0 -0
- mcp_testbench-0.2.0/src/mcp_testbench/replay.py +148 -0
- mcp_testbench-0.2.0/src/mcp_testbench/schema.py +63 -0
- mcp_testbench-0.2.0/src/mcp_testbench.egg-info/PKG-INFO +330 -0
- mcp_testbench-0.2.0/src/mcp_testbench.egg-info/SOURCES.txt +26 -0
- mcp_testbench-0.2.0/src/mcp_testbench.egg-info/dependency_links.txt +1 -0
- mcp_testbench-0.2.0/src/mcp_testbench.egg-info/entry_points.txt +5 -0
- mcp_testbench-0.2.0/src/mcp_testbench.egg-info/requires.txt +3 -0
- mcp_testbench-0.2.0/src/mcp_testbench.egg-info/top_level.txt +1 -0
- mcp_testbench-0.2.0/tests/test_diff.py +105 -0
- mcp_testbench-0.2.0/tests/test_diff_deep.py +191 -0
- mcp_testbench-0.2.0/tests/test_e2e.py +61 -0
- mcp_testbench-0.2.0/tests/test_http.py +245 -0
- mcp_testbench-0.2.0/tests/test_lint.py +178 -0
- mcp_testbench-0.2.0/tests/test_offline_and_cli.py +52 -0
- mcp_testbench-0.2.0/tests/test_replay.py +39 -0
- mcp_testbench-0.2.0/tests/test_replay_structured.py +70 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 agentella
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mcp-testbench
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Complete MCP server testing toolkit: MCP conformance and contract testing for Model Context Protocol servers -- schema snapshots, breaking change detection, recorded tool-call replay, a 21-rule MCP lint, and a pytest plugin for MCP CI over stdio, Streamable HTTP and SSE, with zero runtime dependencies.
|
|
5
|
+
Maintainer: agentella
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://pypi.org/project/mcp-testbench/
|
|
8
|
+
Project-URL: Documentation, https://pypi.org/project/mcp-testbench/
|
|
9
|
+
Keywords: MCP,Model Context Protocol,MCP server testing,MCP conformance,MCP contract testing,MCP schema snapshot,breaking change detection,tool-call replay,pytest plugin,pytest-mcp,FastMCP testing,MCP CI,MCP lint,JSON-RPC testing,AI agent tools,LLM tool testing,MCP server validation,mcp,model-context-protocol,testing,pytest,conformance
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Framework :: Pytest
|
|
19
|
+
Classifier: Topic :: Software Development :: Testing
|
|
20
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# mcp-testbench
|
|
30
|
+
|
|
31
|
+
**The complete MCP server testing toolkit** -- MCP conformance and contract
|
|
32
|
+
testing for Model Context Protocol servers, from a CLI or as a pytest plugin.
|
|
33
|
+
|
|
34
|
+
## What is mcp-testbench?
|
|
35
|
+
|
|
36
|
+
mcp-testbench is a testing, validation and CI tool for MCP (Model Context
|
|
37
|
+
Protocol) servers. It takes an MCP schema snapshot of a server's tools,
|
|
38
|
+
prompts and resources, detects breaking changes against that snapshot with
|
|
39
|
+
breaking change classification, lints the schema, and replays recorded
|
|
40
|
+
tool calls -- the full MCP server testing workflow in one package, built for
|
|
41
|
+
MCP CI. It is the only Python MCP testing package that combines schema
|
|
42
|
+
snapshot + breaking-change classification + recorded tool-call replay +
|
|
43
|
+
offline JSON definition mode in one zero-dependency package. It ships a
|
|
44
|
+
21-rule MCP lint covering tools, prompts and resources, works over stdio,
|
|
45
|
+
Streamable HTTP and the legacy SSE transport, and needs no dependency on the
|
|
46
|
+
official `mcp` package, so an SDK upgrade on the server side never breaks
|
|
47
|
+
your tests.
|
|
48
|
+
|
|
49
|
+
Use it for FastMCP testing, testing servers built on the official MCP SDK,
|
|
50
|
+
and black-box JSON-RPC testing of MCP servers written in any language (a
|
|
51
|
+
Node or Go server works too): mcp-testbench treats the server as a subprocess
|
|
52
|
+
or HTTP endpoint speaking JSON-RPC. It can also run entirely from an
|
|
53
|
+
**offline JSON server definition**, with no live server at all -- for MCP
|
|
54
|
+
server validation in restricted CI, pull-request checks, and reviewing a
|
|
55
|
+
vendor server you cannot run. If you test AI agent tools or LLM tool
|
|
56
|
+
calling, this is the pytest-mcp style plugin for contract-testing the MCP
|
|
57
|
+
servers those agents depend on.
|
|
58
|
+
|
|
59
|
+
## Install
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
pip install mcp-testbench
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Quickstart: snapshot -> check in CI -> replay
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
# 1. record the contract; commit the snapshot file
|
|
69
|
+
mcp-testbench snapshot -o mcp-snapshot.json -- python my_server.py
|
|
70
|
+
|
|
71
|
+
# 2. CI gate: exit 1 when a change breaks existing callers
|
|
72
|
+
mcp-testbench check mcp-snapshot.json -- python my_server.py
|
|
73
|
+
|
|
74
|
+
# 3. lint the schema (21 rules, tools + prompts + resources)
|
|
75
|
+
mcp-testbench lint -- python my_server.py
|
|
76
|
+
|
|
77
|
+
# 4. replay recorded tool calls (JSONL); exit 1 if any call fails
|
|
78
|
+
mcp-testbench replay calls.jsonl -- python my_server.py
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Or in pytest, with the built-in pytest plugin:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
def test_no_breaking_changes(mcp_schema, mcp_snapshot):
|
|
85
|
+
mcp_snapshot(mcp_schema, "mcp-snapshot.json") # writes on first run
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## How it compares
|
|
89
|
+
|
|
90
|
+
| Capability | mcp-testbench | MCP Inspector | mcp-contract | mcptest (`pytest-mcp-server`) | pytest-mcptest |
|
|
91
|
+
|---|---|---|---|---|---|
|
|
92
|
+
| Recorded tool-call replay | **Yes** | No | No (read-only, never calls tools) | No | Roadmap only, not shipped |
|
|
93
|
+
| Offline JSON definition mode | **Yes** | No | No | No | No |
|
|
94
|
+
| Schema snapshot + breaking-change diff | Yes | No | Yes | Yes | No |
|
|
95
|
+
| Lint rules | **21, across tools + prompts + resources** | None | None | 15, tools-first | None |
|
|
96
|
+
| Transports | stdio, Streamable HTTP, legacy SSE | stdio, Streamable HTTP, SSE | stdio | stdio, Streamable HTTP | stdio, Streamable HTTP, in-process |
|
|
97
|
+
| pytest plugin | Yes | No (npm / Node UI) | No | Yes | Yes |
|
|
98
|
+
| Runtime dependencies | **Zero (stdlib only)** | Node.js | -- | Zero | Requires the `mcp` SDK |
|
|
99
|
+
|
|
100
|
+
## Install from a local checkout (development)
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
pip install -e .
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Why this exists (the gap)
|
|
107
|
+
|
|
108
|
+
Researched 2026-12-05 (full write-up in the companion research notes):
|
|
109
|
+
|
|
110
|
+
- The official **MCP Inspector** is an interactive debugger (npm, Node) -- no
|
|
111
|
+
pytest plugin, no snapshot, no breaking-change diff, no replay.
|
|
112
|
+
- **FastMCP's in-memory `Client`** is excellent, but only for FastMCP servers
|
|
113
|
+
you can import in-process.
|
|
114
|
+
- Python incumbents `mcp-contract` and `pytest-mcp-server` (`mcptest`) do
|
|
115
|
+
snapshot + breaking diff well -- but `mcp-contract` never calls a tool
|
|
116
|
+
(read-only by design), and neither ships recorded-call replay or an offline
|
|
117
|
+
definition mode. `pytest-mcptest` lists record/replay as a future roadmap
|
|
118
|
+
item.
|
|
119
|
+
|
|
120
|
+
mcp-testbench's wedge is the combination in one zero-dependency package:
|
|
121
|
+
snapshot + classified diff + **JSONL replay** + **lint** + pytest fixtures +
|
|
122
|
+
**offline JSON definitions**, over stdio and HTTP.
|
|
123
|
+
|
|
124
|
+
## What v0.2 closed
|
|
125
|
+
|
|
126
|
+
v0.1 was behind the incumbents on three dimensions; v0.2 closes all three:
|
|
127
|
+
|
|
128
|
+
| Incumbent advantage (v0.1 gap) | v0.2 fix |
|
|
129
|
+
|---|---|
|
|
130
|
+
| `mcptest` / `pytest-mcptest`: Streamable HTTP transport | **Streamable HTTP client** (plain JSON and SSE-framed responses, `Mcp-Session-Id` handling, custom headers) plus the **legacy SSE transport** (`SSEClient` / `--sse`) |
|
|
131
|
+
| `mcp-contract`: deep, direction-aware diff (nested `$ref`, input vs output rules, annotation hints) | **Deep diff**: local `$ref` resolution, recursive nested diffing with dotted paths, direction-aware input/output rules, annotation-hint flips, title changes as cosmetic, enum / `additionalProperties` / constraint direction handling |
|
|
132
|
+
| `mcptest`: 15-rule linter | **`lint` command with 21 rules** covering tools **and** prompts **and** resources (the incumbent's lint is tools-first), including tool-poisoning / instruction-injection patterns |
|
|
133
|
+
|
|
134
|
+
Plus: replay expectations now cover **structured content**
|
|
135
|
+
(`expect.structured` deep-subset match, `expect.json_path` field equality),
|
|
136
|
+
not just text / `isError`.
|
|
137
|
+
|
|
138
|
+
## CLI
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
# look at a server -- stdio, HTTP, or offline
|
|
142
|
+
mcp-testbench inspect -- python my_server.py
|
|
143
|
+
mcp-testbench inspect --url http://localhost:8000/mcp
|
|
144
|
+
mcp-testbench inspect --url http://localhost:8000/sse --sse # legacy SSE
|
|
145
|
+
mcp-testbench inspect --definition server.json # offline, no server
|
|
146
|
+
|
|
147
|
+
# record the contract; commit the file
|
|
148
|
+
mcp-testbench snapshot -o mcp-snapshot.json -- python my_server.py
|
|
149
|
+
mcp-testbench snapshot -o mcp-snapshot.json --url https://example.com/mcp \
|
|
150
|
+
--header "Authorization: Bearer TOKEN"
|
|
151
|
+
|
|
152
|
+
# CI gate: exit 1 when a change breaks existing callers
|
|
153
|
+
mcp-testbench check mcp-snapshot.json -- python my_server.py
|
|
154
|
+
mcp-testbench check mcp-snapshot.json --definition server.json
|
|
155
|
+
mcp-testbench check mcp-snapshot.json --url http://localhost:8000/mcp
|
|
156
|
+
|
|
157
|
+
# lint the schema: exit 1 on errors (--strict also fails on warnings)
|
|
158
|
+
mcp-testbench lint -- python my_server.py
|
|
159
|
+
mcp-testbench lint --url http://localhost:8000/mcp
|
|
160
|
+
mcp-testbench lint --definition server.json --strict
|
|
161
|
+
|
|
162
|
+
# replay recorded tool calls (JSONL); exit 1 if any call fails
|
|
163
|
+
mcp-testbench replay calls.jsonl -- python my_server.py
|
|
164
|
+
mcp-testbench replay calls.jsonl --url http://localhost:8000/mcp
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Deep diff
|
|
168
|
+
|
|
169
|
+
Schemas are diffed recursively. Local `$ref` pointers (`#/$defs/...`,
|
|
170
|
+
`#/definitions/...`, `#/components/schemas/...`) are resolved, and changes
|
|
171
|
+
are reported with dotted paths, e.g. `tool echo.input.config.timeout`.
|
|
172
|
+
|
|
173
|
+
Change classes: `breaking`, `non-breaking`, `routing` (description or
|
|
174
|
+
annotation-hint changes -- no call breaks, but an agent may behave
|
|
175
|
+
differently; `destructiveHint` false -> true lands here), and `cosmetic`
|
|
176
|
+
(`title` changes). Only `breaking` fails `check`.
|
|
177
|
+
|
|
178
|
+
Input and output schemas follow opposite rules where the contract is
|
|
179
|
+
asymmetric:
|
|
180
|
+
|
|
181
|
+
| change | input schema | output schema |
|
|
182
|
+
|---|---|---|
|
|
183
|
+
| property removed | breaking | breaking |
|
|
184
|
+
| property added | breaking if required, else non-breaking | non-breaking |
|
|
185
|
+
| became required | breaking | non-breaking |
|
|
186
|
+
| no longer required | non-breaking | breaking |
|
|
187
|
+
| type / enum widened | non-breaking | breaking |
|
|
188
|
+
| type / enum narrowed | breaking | non-breaking |
|
|
189
|
+
| constraints tightened | breaking | non-breaking |
|
|
190
|
+
| constraints loosened | non-breaking | breaking |
|
|
191
|
+
|
|
192
|
+
The same direction logic applies to enum changes, `additionalProperties`
|
|
193
|
+
tightening/loosening, and numeric / length constraints (`minimum`,
|
|
194
|
+
`maxLength`, `pattern`, ...) at any nesting depth.
|
|
195
|
+
|
|
196
|
+
## Lint rules
|
|
197
|
+
|
|
198
|
+
`mcp-testbench lint` (Python API: `mcp_testbench.lint_schema(schema)`; pytest:
|
|
199
|
+
the `mcp_lint` fixture or `mcp_testbench.plugin.assert_no_lint_errors`).
|
|
200
|
+
Each finding carries a rule id, a severity (`error` / `warning`), the
|
|
201
|
+
surface and name, and a message. Exit code is 1 on any error; `--strict`
|
|
202
|
+
also fails on warnings.
|
|
203
|
+
|
|
204
|
+
| rule id | severity | surface | what it flags |
|
|
205
|
+
|---|---|---|---|
|
|
206
|
+
| `tool-missing-description` | error | tool | no / empty description |
|
|
207
|
+
| `tool-short-description` | warning | tool | description < 20 chars, too short to route on |
|
|
208
|
+
| `tool-poisoning-pattern` | error | tool | instruction-injection phrases ("ignore previous instructions", "you must always...", "do not tell the user", `<important>` tags, ...) |
|
|
209
|
+
| `tool-name-format` | warning | tool | name is not snake_case |
|
|
210
|
+
| `tool-name-too-long` | warning | tool | name > 64 chars |
|
|
211
|
+
| `tool-name-collision` | error | tool | two tools share a name (case-insensitive) |
|
|
212
|
+
| `tool-missing-output-schema` | warning | tool | no `outputSchema` declared |
|
|
213
|
+
| `param-missing-type` | error | tool | parameter with no type / `$ref` / enum / composition |
|
|
214
|
+
| `param-untyped-object` | warning | tool | `type: object` with no properties declared |
|
|
215
|
+
| `credential-param-unprotected` | warning | tool | credential-sounding parameter name (`password`, `api_key`, `token`, ...) with no `format` hint |
|
|
216
|
+
| `param-required-with-default` | error | tool | required parameter that also declares a default |
|
|
217
|
+
| `param-string-no-maxlength` | warning | tool | string parameter with no `maxLength` |
|
|
218
|
+
| `param-single-value-enum` | warning | tool | enum with exactly one value (use `const`) |
|
|
219
|
+
| `param-case-duplicate` | error | tool | two parameters in one tool differ only by casing |
|
|
220
|
+
| `prompt-missing-description` | error | prompt | no / empty description |
|
|
221
|
+
| `prompt-short-description` | warning | prompt | description < 20 chars |
|
|
222
|
+
| `prompt-poisoning-pattern` | error | prompt | instruction-injection phrases in a prompt description |
|
|
223
|
+
| `prompt-argument-missing-description` | warning | prompt | a prompt argument has no description |
|
|
224
|
+
| `resource-missing-description` | warning | resource | no / empty description |
|
|
225
|
+
| `resource-missing-mimetype` | warning | resource | no `mimeType` declared |
|
|
226
|
+
| `resource-invalid-uri` | error | resource | URI has no scheme |
|
|
227
|
+
| `resource-suspicious-scheme` | error | resource | scriptable / inline-data scheme (`javascript:`, `vbscript:`, `data:`) |
|
|
228
|
+
|
|
229
|
+
Parameter rules recurse into nested object properties and array items,
|
|
230
|
+
resolving local `$ref` pointers.
|
|
231
|
+
|
|
232
|
+
## Replay
|
|
233
|
+
|
|
234
|
+
One JSON object per line:
|
|
235
|
+
|
|
236
|
+
```
|
|
237
|
+
{"tool": "echo", "arguments": {"text": "hi"}, "expect": {"contains": "hi"}}
|
|
238
|
+
{"tool": "add", "arguments": {"a": 2, "b": 3}, "expect": {"text": "5"}}
|
|
239
|
+
{"tool": "boom", "arguments": {}, "expect": {"is_error": true}}
|
|
240
|
+
{"tool": "add", "arguments": {"a": 2, "b": 3}, "expect": {"structured": {"result": 5}}}
|
|
241
|
+
{"tool": "add", "arguments": {"a": 2, "b": 3}, "expect": {"json_path": {"result": 5}}}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
`expect` keys: `contains` / `text` (text result), `is_error`,
|
|
245
|
+
`structured` (deep-subset match against the result's `structuredContent`:
|
|
246
|
+
nested dicts subset-match recursively, lists match element-for-element,
|
|
247
|
+
extra actual keys are ignored), and `json_path` (dotted path -> expected
|
|
248
|
+
value, resolved against `structuredContent` when present, else against
|
|
249
|
+
the whole result). With no `expect`, a call passes if it does not error.
|
|
250
|
+
|
|
251
|
+
## pytest
|
|
252
|
+
|
|
253
|
+
Install the package and the plugin loads automatically (`pytest11` entry
|
|
254
|
+
point). Point it at your server in `conftest.py`:
|
|
255
|
+
|
|
256
|
+
```python
|
|
257
|
+
import sys
|
|
258
|
+
import pytest
|
|
259
|
+
|
|
260
|
+
@pytest.fixture
|
|
261
|
+
def mcp_server_command():
|
|
262
|
+
return [sys.executable, "my_server.py"]
|
|
263
|
+
|
|
264
|
+
# or, for an HTTP server:
|
|
265
|
+
@pytest.fixture
|
|
266
|
+
def mcp_url():
|
|
267
|
+
return "http://localhost:8000/mcp"
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
or on the command line / in ini: `--mcp-server "python my_server.py"`,
|
|
271
|
+
`--mcp-url http://localhost:8000/mcp` (plus repeatable
|
|
272
|
+
`--mcp-header "Authorization: Bearer X"`), or `--mcp-definition server.json`
|
|
273
|
+
for the offline mode. Then:
|
|
274
|
+
|
|
275
|
+
```python
|
|
276
|
+
def test_tools_exist(mcp_schema):
|
|
277
|
+
assert "echo" in [t["name"] for t in mcp_schema["tools"]]
|
|
278
|
+
|
|
279
|
+
def test_no_breaking_changes(mcp_schema, mcp_snapshot):
|
|
280
|
+
mcp_snapshot(mcp_schema, "mcp-snapshot.json") # writes on first run
|
|
281
|
+
|
|
282
|
+
def test_schema_lints_clean(mcp_lint):
|
|
283
|
+
assert mcp_lint() == [] # asserts no errors, returns all findings
|
|
284
|
+
|
|
285
|
+
def test_call(mcp_client):
|
|
286
|
+
result = mcp_client.call_tool("echo", {"text": "hi"})
|
|
287
|
+
assert result["content"][0]["text"] == "hi"
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Helpers without fixtures, from `mcp_testbench.plugin`:
|
|
291
|
+
`assert_no_breaking(old, new)`, `assert_matches_snapshot(schema, path)`,
|
|
292
|
+
and `assert_no_lint_errors(schema)`. The engines themselves are
|
|
293
|
+
`mcp_testbench.diff_snapshots(old, new)` and `mcp_testbench.lint_schema(schema)`.
|
|
294
|
+
|
|
295
|
+
## Try the sample servers
|
|
296
|
+
|
|
297
|
+
```
|
|
298
|
+
# stdio
|
|
299
|
+
mcp-testbench inspect -- python examples/echo_server.py
|
|
300
|
+
mcp-testbench snapshot -o /tmp/snap.json -- python examples/echo_server.py
|
|
301
|
+
mcp-testbench check /tmp/snap.json -- python examples/echo_server.py
|
|
302
|
+
mcp-testbench lint -- python examples/echo_server.py
|
|
303
|
+
|
|
304
|
+
# Streamable HTTP (plain JSON at /mcp, SSE-framed at /mcp-sse)
|
|
305
|
+
python examples/echo_server_http.py 8765 &
|
|
306
|
+
mcp-testbench inspect --url http://127.0.0.1:8765/mcp
|
|
307
|
+
mcp-testbench inspect --url http://127.0.0.1:8765/mcp-sse
|
|
308
|
+
mcp-testbench lint --url http://127.0.0.1:8765/mcp
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
## Scope of v0.2 / remaining limitations
|
|
312
|
+
|
|
313
|
+
- Transports: stdio, Streamable HTTP, and legacy SSE are all implemented
|
|
314
|
+
and tested. The legacy SSE client is single-session and does not
|
|
315
|
+
implement resumability / `Last-Event-ID` reconnection.
|
|
316
|
+
- Diffing covers tools (input + output schemas, deeply), prompts
|
|
317
|
+
(arguments), and resources (description, name/title, mimeType).
|
|
318
|
+
Composition keywords (`anyOf` / `oneOf` / `allOf`) are not diffed
|
|
319
|
+
branch-by-branch; a change inside a composition branch that does not
|
|
320
|
+
alter the compared keywords above will not be reported.
|
|
321
|
+
- Replay's structured assertions cover `structuredContent` (subset match
|
|
322
|
+
and dotted-path equality); arbitrary JSONPath expressions are not
|
|
323
|
+
supported -- `json_path` is dotted lookup only.
|
|
324
|
+
- Lint's missing-output-schema rule is a warning by design: whether a
|
|
325
|
+
tool *should* return structured data cannot be detected from the
|
|
326
|
+
schema alone.
|
|
327
|
+
|
|
328
|
+
## License
|
|
329
|
+
|
|
330
|
+
MIT
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# mcp-testbench
|
|
2
|
+
|
|
3
|
+
**The complete MCP server testing toolkit** -- MCP conformance and contract
|
|
4
|
+
testing for Model Context Protocol servers, from a CLI or as a pytest plugin.
|
|
5
|
+
|
|
6
|
+
## What is mcp-testbench?
|
|
7
|
+
|
|
8
|
+
mcp-testbench is a testing, validation and CI tool for MCP (Model Context
|
|
9
|
+
Protocol) servers. It takes an MCP schema snapshot of a server's tools,
|
|
10
|
+
prompts and resources, detects breaking changes against that snapshot with
|
|
11
|
+
breaking change classification, lints the schema, and replays recorded
|
|
12
|
+
tool calls -- the full MCP server testing workflow in one package, built for
|
|
13
|
+
MCP CI. It is the only Python MCP testing package that combines schema
|
|
14
|
+
snapshot + breaking-change classification + recorded tool-call replay +
|
|
15
|
+
offline JSON definition mode in one zero-dependency package. It ships a
|
|
16
|
+
21-rule MCP lint covering tools, prompts and resources, works over stdio,
|
|
17
|
+
Streamable HTTP and the legacy SSE transport, and needs no dependency on the
|
|
18
|
+
official `mcp` package, so an SDK upgrade on the server side never breaks
|
|
19
|
+
your tests.
|
|
20
|
+
|
|
21
|
+
Use it for FastMCP testing, testing servers built on the official MCP SDK,
|
|
22
|
+
and black-box JSON-RPC testing of MCP servers written in any language (a
|
|
23
|
+
Node or Go server works too): mcp-testbench treats the server as a subprocess
|
|
24
|
+
or HTTP endpoint speaking JSON-RPC. It can also run entirely from an
|
|
25
|
+
**offline JSON server definition**, with no live server at all -- for MCP
|
|
26
|
+
server validation in restricted CI, pull-request checks, and reviewing a
|
|
27
|
+
vendor server you cannot run. If you test AI agent tools or LLM tool
|
|
28
|
+
calling, this is the pytest-mcp style plugin for contract-testing the MCP
|
|
29
|
+
servers those agents depend on.
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
pip install mcp-testbench
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Quickstart: snapshot -> check in CI -> replay
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
# 1. record the contract; commit the snapshot file
|
|
41
|
+
mcp-testbench snapshot -o mcp-snapshot.json -- python my_server.py
|
|
42
|
+
|
|
43
|
+
# 2. CI gate: exit 1 when a change breaks existing callers
|
|
44
|
+
mcp-testbench check mcp-snapshot.json -- python my_server.py
|
|
45
|
+
|
|
46
|
+
# 3. lint the schema (21 rules, tools + prompts + resources)
|
|
47
|
+
mcp-testbench lint -- python my_server.py
|
|
48
|
+
|
|
49
|
+
# 4. replay recorded tool calls (JSONL); exit 1 if any call fails
|
|
50
|
+
mcp-testbench replay calls.jsonl -- python my_server.py
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Or in pytest, with the built-in pytest plugin:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
def test_no_breaking_changes(mcp_schema, mcp_snapshot):
|
|
57
|
+
mcp_snapshot(mcp_schema, "mcp-snapshot.json") # writes on first run
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## How it compares
|
|
61
|
+
|
|
62
|
+
| Capability | mcp-testbench | MCP Inspector | mcp-contract | mcptest (`pytest-mcp-server`) | pytest-mcptest |
|
|
63
|
+
|---|---|---|---|---|---|
|
|
64
|
+
| Recorded tool-call replay | **Yes** | No | No (read-only, never calls tools) | No | Roadmap only, not shipped |
|
|
65
|
+
| Offline JSON definition mode | **Yes** | No | No | No | No |
|
|
66
|
+
| Schema snapshot + breaking-change diff | Yes | No | Yes | Yes | No |
|
|
67
|
+
| Lint rules | **21, across tools + prompts + resources** | None | None | 15, tools-first | None |
|
|
68
|
+
| Transports | stdio, Streamable HTTP, legacy SSE | stdio, Streamable HTTP, SSE | stdio | stdio, Streamable HTTP | stdio, Streamable HTTP, in-process |
|
|
69
|
+
| pytest plugin | Yes | No (npm / Node UI) | No | Yes | Yes |
|
|
70
|
+
| Runtime dependencies | **Zero (stdlib only)** | Node.js | -- | Zero | Requires the `mcp` SDK |
|
|
71
|
+
|
|
72
|
+
## Install from a local checkout (development)
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
pip install -e .
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Why this exists (the gap)
|
|
79
|
+
|
|
80
|
+
Researched 2026-12-05 (full write-up in the companion research notes):
|
|
81
|
+
|
|
82
|
+
- The official **MCP Inspector** is an interactive debugger (npm, Node) -- no
|
|
83
|
+
pytest plugin, no snapshot, no breaking-change diff, no replay.
|
|
84
|
+
- **FastMCP's in-memory `Client`** is excellent, but only for FastMCP servers
|
|
85
|
+
you can import in-process.
|
|
86
|
+
- Python incumbents `mcp-contract` and `pytest-mcp-server` (`mcptest`) do
|
|
87
|
+
snapshot + breaking diff well -- but `mcp-contract` never calls a tool
|
|
88
|
+
(read-only by design), and neither ships recorded-call replay or an offline
|
|
89
|
+
definition mode. `pytest-mcptest` lists record/replay as a future roadmap
|
|
90
|
+
item.
|
|
91
|
+
|
|
92
|
+
mcp-testbench's wedge is the combination in one zero-dependency package:
|
|
93
|
+
snapshot + classified diff + **JSONL replay** + **lint** + pytest fixtures +
|
|
94
|
+
**offline JSON definitions**, over stdio and HTTP.
|
|
95
|
+
|
|
96
|
+
## What v0.2 closed
|
|
97
|
+
|
|
98
|
+
v0.1 was behind the incumbents on three dimensions; v0.2 closes all three:
|
|
99
|
+
|
|
100
|
+
| Incumbent advantage (v0.1 gap) | v0.2 fix |
|
|
101
|
+
|---|---|
|
|
102
|
+
| `mcptest` / `pytest-mcptest`: Streamable HTTP transport | **Streamable HTTP client** (plain JSON and SSE-framed responses, `Mcp-Session-Id` handling, custom headers) plus the **legacy SSE transport** (`SSEClient` / `--sse`) |
|
|
103
|
+
| `mcp-contract`: deep, direction-aware diff (nested `$ref`, input vs output rules, annotation hints) | **Deep diff**: local `$ref` resolution, recursive nested diffing with dotted paths, direction-aware input/output rules, annotation-hint flips, title changes as cosmetic, enum / `additionalProperties` / constraint direction handling |
|
|
104
|
+
| `mcptest`: 15-rule linter | **`lint` command with 21 rules** covering tools **and** prompts **and** resources (the incumbent's lint is tools-first), including tool-poisoning / instruction-injection patterns |
|
|
105
|
+
|
|
106
|
+
Plus: replay expectations now cover **structured content**
|
|
107
|
+
(`expect.structured` deep-subset match, `expect.json_path` field equality),
|
|
108
|
+
not just text / `isError`.
|
|
109
|
+
|
|
110
|
+
## CLI
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
# look at a server -- stdio, HTTP, or offline
|
|
114
|
+
mcp-testbench inspect -- python my_server.py
|
|
115
|
+
mcp-testbench inspect --url http://localhost:8000/mcp
|
|
116
|
+
mcp-testbench inspect --url http://localhost:8000/sse --sse # legacy SSE
|
|
117
|
+
mcp-testbench inspect --definition server.json # offline, no server
|
|
118
|
+
|
|
119
|
+
# record the contract; commit the file
|
|
120
|
+
mcp-testbench snapshot -o mcp-snapshot.json -- python my_server.py
|
|
121
|
+
mcp-testbench snapshot -o mcp-snapshot.json --url https://example.com/mcp \
|
|
122
|
+
--header "Authorization: Bearer TOKEN"
|
|
123
|
+
|
|
124
|
+
# CI gate: exit 1 when a change breaks existing callers
|
|
125
|
+
mcp-testbench check mcp-snapshot.json -- python my_server.py
|
|
126
|
+
mcp-testbench check mcp-snapshot.json --definition server.json
|
|
127
|
+
mcp-testbench check mcp-snapshot.json --url http://localhost:8000/mcp
|
|
128
|
+
|
|
129
|
+
# lint the schema: exit 1 on errors (--strict also fails on warnings)
|
|
130
|
+
mcp-testbench lint -- python my_server.py
|
|
131
|
+
mcp-testbench lint --url http://localhost:8000/mcp
|
|
132
|
+
mcp-testbench lint --definition server.json --strict
|
|
133
|
+
|
|
134
|
+
# replay recorded tool calls (JSONL); exit 1 if any call fails
|
|
135
|
+
mcp-testbench replay calls.jsonl -- python my_server.py
|
|
136
|
+
mcp-testbench replay calls.jsonl --url http://localhost:8000/mcp
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Deep diff
|
|
140
|
+
|
|
141
|
+
Schemas are diffed recursively. Local `$ref` pointers (`#/$defs/...`,
|
|
142
|
+
`#/definitions/...`, `#/components/schemas/...`) are resolved, and changes
|
|
143
|
+
are reported with dotted paths, e.g. `tool echo.input.config.timeout`.
|
|
144
|
+
|
|
145
|
+
Change classes: `breaking`, `non-breaking`, `routing` (description or
|
|
146
|
+
annotation-hint changes -- no call breaks, but an agent may behave
|
|
147
|
+
differently; `destructiveHint` false -> true lands here), and `cosmetic`
|
|
148
|
+
(`title` changes). Only `breaking` fails `check`.
|
|
149
|
+
|
|
150
|
+
Input and output schemas follow opposite rules where the contract is
|
|
151
|
+
asymmetric:
|
|
152
|
+
|
|
153
|
+
| change | input schema | output schema |
|
|
154
|
+
|---|---|---|
|
|
155
|
+
| property removed | breaking | breaking |
|
|
156
|
+
| property added | breaking if required, else non-breaking | non-breaking |
|
|
157
|
+
| became required | breaking | non-breaking |
|
|
158
|
+
| no longer required | non-breaking | breaking |
|
|
159
|
+
| type / enum widened | non-breaking | breaking |
|
|
160
|
+
| type / enum narrowed | breaking | non-breaking |
|
|
161
|
+
| constraints tightened | breaking | non-breaking |
|
|
162
|
+
| constraints loosened | non-breaking | breaking |
|
|
163
|
+
|
|
164
|
+
The same direction logic applies to enum changes, `additionalProperties`
|
|
165
|
+
tightening/loosening, and numeric / length constraints (`minimum`,
|
|
166
|
+
`maxLength`, `pattern`, ...) at any nesting depth.
|
|
167
|
+
|
|
168
|
+
## Lint rules
|
|
169
|
+
|
|
170
|
+
`mcp-testbench lint` (Python API: `mcp_testbench.lint_schema(schema)`; pytest:
|
|
171
|
+
the `mcp_lint` fixture or `mcp_testbench.plugin.assert_no_lint_errors`).
|
|
172
|
+
Each finding carries a rule id, a severity (`error` / `warning`), the
|
|
173
|
+
surface and name, and a message. Exit code is 1 on any error; `--strict`
|
|
174
|
+
also fails on warnings.
|
|
175
|
+
|
|
176
|
+
| rule id | severity | surface | what it flags |
|
|
177
|
+
|---|---|---|---|
|
|
178
|
+
| `tool-missing-description` | error | tool | no / empty description |
|
|
179
|
+
| `tool-short-description` | warning | tool | description < 20 chars, too short to route on |
|
|
180
|
+
| `tool-poisoning-pattern` | error | tool | instruction-injection phrases ("ignore previous instructions", "you must always...", "do not tell the user", `<important>` tags, ...) |
|
|
181
|
+
| `tool-name-format` | warning | tool | name is not snake_case |
|
|
182
|
+
| `tool-name-too-long` | warning | tool | name > 64 chars |
|
|
183
|
+
| `tool-name-collision` | error | tool | two tools share a name (case-insensitive) |
|
|
184
|
+
| `tool-missing-output-schema` | warning | tool | no `outputSchema` declared |
|
|
185
|
+
| `param-missing-type` | error | tool | parameter with no type / `$ref` / enum / composition |
|
|
186
|
+
| `param-untyped-object` | warning | tool | `type: object` with no properties declared |
|
|
187
|
+
| `credential-param-unprotected` | warning | tool | credential-sounding parameter name (`password`, `api_key`, `token`, ...) with no `format` hint |
|
|
188
|
+
| `param-required-with-default` | error | tool | required parameter that also declares a default |
|
|
189
|
+
| `param-string-no-maxlength` | warning | tool | string parameter with no `maxLength` |
|
|
190
|
+
| `param-single-value-enum` | warning | tool | enum with exactly one value (use `const`) |
|
|
191
|
+
| `param-case-duplicate` | error | tool | two parameters in one tool differ only by casing |
|
|
192
|
+
| `prompt-missing-description` | error | prompt | no / empty description |
|
|
193
|
+
| `prompt-short-description` | warning | prompt | description < 20 chars |
|
|
194
|
+
| `prompt-poisoning-pattern` | error | prompt | instruction-injection phrases in a prompt description |
|
|
195
|
+
| `prompt-argument-missing-description` | warning | prompt | a prompt argument has no description |
|
|
196
|
+
| `resource-missing-description` | warning | resource | no / empty description |
|
|
197
|
+
| `resource-missing-mimetype` | warning | resource | no `mimeType` declared |
|
|
198
|
+
| `resource-invalid-uri` | error | resource | URI has no scheme |
|
|
199
|
+
| `resource-suspicious-scheme` | error | resource | scriptable / inline-data scheme (`javascript:`, `vbscript:`, `data:`) |
|
|
200
|
+
|
|
201
|
+
Parameter rules recurse into nested object properties and array items,
|
|
202
|
+
resolving local `$ref` pointers.
|
|
203
|
+
|
|
204
|
+
## Replay
|
|
205
|
+
|
|
206
|
+
One JSON object per line:
|
|
207
|
+
|
|
208
|
+
```
|
|
209
|
+
{"tool": "echo", "arguments": {"text": "hi"}, "expect": {"contains": "hi"}}
|
|
210
|
+
{"tool": "add", "arguments": {"a": 2, "b": 3}, "expect": {"text": "5"}}
|
|
211
|
+
{"tool": "boom", "arguments": {}, "expect": {"is_error": true}}
|
|
212
|
+
{"tool": "add", "arguments": {"a": 2, "b": 3}, "expect": {"structured": {"result": 5}}}
|
|
213
|
+
{"tool": "add", "arguments": {"a": 2, "b": 3}, "expect": {"json_path": {"result": 5}}}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
`expect` keys: `contains` / `text` (text result), `is_error`,
|
|
217
|
+
`structured` (deep-subset match against the result's `structuredContent`:
|
|
218
|
+
nested dicts subset-match recursively, lists match element-for-element,
|
|
219
|
+
extra actual keys are ignored), and `json_path` (dotted path -> expected
|
|
220
|
+
value, resolved against `structuredContent` when present, else against
|
|
221
|
+
the whole result). With no `expect`, a call passes if it does not error.
|
|
222
|
+
|
|
223
|
+
## pytest
|
|
224
|
+
|
|
225
|
+
Install the package and the plugin loads automatically (`pytest11` entry
|
|
226
|
+
point). Point it at your server in `conftest.py`:
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
import sys
|
|
230
|
+
import pytest
|
|
231
|
+
|
|
232
|
+
@pytest.fixture
|
|
233
|
+
def mcp_server_command():
|
|
234
|
+
return [sys.executable, "my_server.py"]
|
|
235
|
+
|
|
236
|
+
# or, for an HTTP server:
|
|
237
|
+
@pytest.fixture
|
|
238
|
+
def mcp_url():
|
|
239
|
+
return "http://localhost:8000/mcp"
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
or on the command line / in ini: `--mcp-server "python my_server.py"`,
|
|
243
|
+
`--mcp-url http://localhost:8000/mcp` (plus repeatable
|
|
244
|
+
`--mcp-header "Authorization: Bearer X"`), or `--mcp-definition server.json`
|
|
245
|
+
for the offline mode. Then:
|
|
246
|
+
|
|
247
|
+
```python
|
|
248
|
+
def test_tools_exist(mcp_schema):
|
|
249
|
+
assert "echo" in [t["name"] for t in mcp_schema["tools"]]
|
|
250
|
+
|
|
251
|
+
def test_no_breaking_changes(mcp_schema, mcp_snapshot):
|
|
252
|
+
mcp_snapshot(mcp_schema, "mcp-snapshot.json") # writes on first run
|
|
253
|
+
|
|
254
|
+
def test_schema_lints_clean(mcp_lint):
|
|
255
|
+
assert mcp_lint() == [] # asserts no errors, returns all findings
|
|
256
|
+
|
|
257
|
+
def test_call(mcp_client):
|
|
258
|
+
result = mcp_client.call_tool("echo", {"text": "hi"})
|
|
259
|
+
assert result["content"][0]["text"] == "hi"
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Helpers without fixtures, from `mcp_testbench.plugin`:
|
|
263
|
+
`assert_no_breaking(old, new)`, `assert_matches_snapshot(schema, path)`,
|
|
264
|
+
and `assert_no_lint_errors(schema)`. The engines themselves are
|
|
265
|
+
`mcp_testbench.diff_snapshots(old, new)` and `mcp_testbench.lint_schema(schema)`.
|
|
266
|
+
|
|
267
|
+
## Try the sample servers
|
|
268
|
+
|
|
269
|
+
```
|
|
270
|
+
# stdio
|
|
271
|
+
mcp-testbench inspect -- python examples/echo_server.py
|
|
272
|
+
mcp-testbench snapshot -o /tmp/snap.json -- python examples/echo_server.py
|
|
273
|
+
mcp-testbench check /tmp/snap.json -- python examples/echo_server.py
|
|
274
|
+
mcp-testbench lint -- python examples/echo_server.py
|
|
275
|
+
|
|
276
|
+
# Streamable HTTP (plain JSON at /mcp, SSE-framed at /mcp-sse)
|
|
277
|
+
python examples/echo_server_http.py 8765 &
|
|
278
|
+
mcp-testbench inspect --url http://127.0.0.1:8765/mcp
|
|
279
|
+
mcp-testbench inspect --url http://127.0.0.1:8765/mcp-sse
|
|
280
|
+
mcp-testbench lint --url http://127.0.0.1:8765/mcp
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Scope of v0.2 / remaining limitations
|
|
284
|
+
|
|
285
|
+
- Transports: stdio, Streamable HTTP, and legacy SSE are all implemented
|
|
286
|
+
and tested. The legacy SSE client is single-session and does not
|
|
287
|
+
implement resumability / `Last-Event-ID` reconnection.
|
|
288
|
+
- Diffing covers tools (input + output schemas, deeply), prompts
|
|
289
|
+
(arguments), and resources (description, name/title, mimeType).
|
|
290
|
+
Composition keywords (`anyOf` / `oneOf` / `allOf`) are not diffed
|
|
291
|
+
branch-by-branch; a change inside a composition branch that does not
|
|
292
|
+
alter the compared keywords above will not be reported.
|
|
293
|
+
- Replay's structured assertions cover `structuredContent` (subset match
|
|
294
|
+
and dotted-path equality); arbitrary JSONPath expressions are not
|
|
295
|
+
supported -- `json_path` is dotted lookup only.
|
|
296
|
+
- Lint's missing-output-schema rule is a warning by design: whether a
|
|
297
|
+
tool *should* return structured data cannot be detected from the
|
|
298
|
+
schema alone.
|
|
299
|
+
|
|
300
|
+
## License
|
|
301
|
+
|
|
302
|
+
MIT
|