QA AND TEST AUTOMATION · PART 10 · TESTING AI FEATURES · AGENTS · 93 OF 112

Testing MCP Servers and WebMCP Tools

An MCP server gives AI agents tools, so it needs the same tests as an API plus a few of its own. A small ShopRung MCP server tested with pytest and the official Python SDK: 14 tests covering the tool list and schemas, results, tool errors, a second user, a real stdio process and a schema snapshot. They caught ShopRung bugs and a loosened limit; also how WebMCP tools in a web page are tested.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 14

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • In-memory client
  • contract + snapshot
  • structured results
  • is_error
  • second user
  • backend down
  • real stdio

Notes

The server

The server has three tools. Search products and get order only read. Add to cart writes. Each tool calls the ShopRung API with the signed in customer's token, so ShopRung's own rules still apply. The server is written in Python with the official MCP SDK; argument limits such as a query of two to fifty characters are declared in the code and become part of the tool's schema.

shoprung MCP server
search_products(query, limit=5)
  read-only, query 2 to 50 chars
get_order(order_id)
  read-only, own orders only
add_to_cart(product_id,
            quantity=1)
  writes, quantity 1 to 10

calls the ShopRung API with the
  customer's token

No network needed

The SDK's client can connect to the server object directly, in memory, with no process and no network. A pytest fixture opens it, and each test calls list tools or call tool exactly as an agent would. ShopRung itself runs for real, reset before each test, so results come from real data. The whole suite of fourteen tests ran in about two point four seconds.

conftest.py
from mcp import Client
import server

@pytest.fixture
async def client(shoprung):
  async with Client(server.mcp)
      as c:
    yield c
r = await client.call_tool(
  "search_products",
  {"query": "mug"})

Test the contract

First, the contract the agent sees. The tool names are exact. Every tool and every argument has a description, because the model chooses tools by reading them. The schema carries the limits: query between two and fifty characters, quantity between one and ten, query required. And the hints: both readers are marked read only, add to cart is not, so a client can ask the user before it writes.

test_contract.py
names == ["add_to_cart",
  "get_order", "search_products"]
every tool and argument has a
  description
query: minLength 2, maxLength 50
quantity: minimum 1, maximum 10
required == ["query"]
read_only_hint: True, True,
  False for add_to_cart

Test the results

Then the results. Tools return structured content, so assert on fields, not on text: searching mug returns the green and red mugs, and the red one is out of stock. Alice's order two is shipped. For a tool that writes, check the side effect in the real system, not just the reply: after add to cart, the test reads Alice's cart straight from the ShopRung API and finds two green mugs.

test_tools.py
search "mug":
  ["Green Build Mug",
   "Red Build Mug"]
  Red Build Mug in_stock False
get_order 2 as Alice:
  status "SHIPPED"
add_to_cart 4 x 2, then
  GET /api/cart directly:
  [("Green Build Mug", 2)]

Tool errors

Errors matter more than usual, because a model reads them and decides what to do next. Invalid arguments come back as a tool result with is error set, and a readable reason, so the agent can correct itself. Note one surprise: the order id as the text two in digits was quietly accepted as a number, which helps small models that send strings. Out of stock and unknown tools also gave clear messages.

Real error results
query "m"     → is_error:
  "String should have at least 2
   characters"
quantity 11   → is_error
order_id "two" → is_error
order_id "2"  → accepted as 2
Red mug       → "Red Build Mug
                is out of stock"
unknown tool  → "Unknown tool:
                 nope"

A second user

Permissions need a second user. Bob asking for Alice's order got no order with id two, the same message as a missing order, which reveals nothing. Then we turned on ShopRung's order leak bug and ran the same suite: one test failed, because Bob now received Alice's keyboard order through the agent's tool. With case sensitive search on, three tests failed. An MCP server inherits every bug of the API behind it.

Bob asks for order 2
normal: is_error,
  "No order with id 2"

with the order-leak bug on:
  1 failed, 13 passed
  is_error=False, status SHIPPED,
  "1 x Quiet Mechanical Keyboard"

with search-case on:
  3 failed (search, limit, stdio)

Layers help

One bug did not show. With ShopRung accepting a quantity of eleven, all fourteen tests still passed, because the tool schema rejected eleven before ShopRung was ever called. That is defence in layers working for customers. But it also means the MCP suite cannot see the API bug, so the API keeps its own tests. Know which layer each test really checks.

quantity-limit bug on
ShopRung API now accepts 11

MCP suite: 14 passed
quantity 11 is rejected by the
  tool schema (maximum 10)
  before ShopRung is called

so test the API itself too:
  the MCP limit hides the bug

Failures and transport

Two more. When ShopRung is down, the tool must fail cleanly: a short message the agent can pass on, with no stack trace and no token in it. And one test starts the real server as a process over standard input and output, the way desktop apps run MCP servers, to prove packaging and start up work. In memory tests are fast; keep at least one over the real transport.

Two more tests
BASE_URL = localhost:1
  → is_error: "Error executing
  tool search_products: ShopRung
  is not reachable right now.
  Try again later."
  token not in the message

stdio: start server.py as a
  process, initialize, call
  search "keyboard" → passed

Snapshot the contract

Finally, snapshot the whole tool list. Descriptions and schemas are effectively prompts: a small wording change can change which tools a model picks. We changed one line, raising the quantity limit to ninety nine and shortening its description. Two tests failed: the limit test and the snapshot, which shows exactly what changed. Any change to the tool list then needs a reviewer, and ideally a rerun of the agent evals.

A developer changed one line
quantity: le=10 → le=99,
  "How many, 1 to 10."
  → "How many."

test_input_limits:
  (1, 99) == (1, 10)  FAILED
test_tool_list_matches_the
  _snapshot:  FAILED
  "tool list changed: review it"

WebMCP

WebMCP brings the same idea into the browser. A web page registers tools with a name, a description, an input schema and a function, or marks up a form with a tool name and description, and an agent in the browser calls them instead of clicking around. It is experimental, in a Chrome origin trial during twenty twenty six, so details may change. The tests stay the same: list the tools, call them, check errors and permissions.

WebMCP, experimental
in the page:
document.modelContext
  .registerTool({ name,
  description, inputSchema,
  execute })
or a <form toolname="..."
  tooldescription="...">
Chrome origin trial in 2026;
  the API may still change
same tests: list, call, errors,
  a second user

Common mistakes

Mistakes. Testing the server only through a chatbot, which is slow and random, when the server can be called directly. Asserting on text instead of structured content. Letting stack traces or secrets into tool errors, which models repeat. Testing with one user. Trusting what a write tool says instead of checking the real system. Never starting the real process. And letting tool schemas change without review.

Avoid these
  • testing only through a model
  • asserting on text, not structured content
  • stack traces or secrets in tool errors
  • one user only
  • trusting a write tool's reply
  • no test over the real transport
  • unreviewed schema changes

Say it like this

In the interview: I test it directly, without a model, using the SDK's in memory client in pytest. Contract tests cover tool names, descriptions, schemas and read only hints, plus a snapshot of the whole list. Behaviour tests assert on structured content, check side effects in the real system, cover tool errors, a second user and the backend being down, and one test runs the real server process.

In the interview
  • "Directly, without a model: an in-memory client in pytest.
  • Contract tests for names, descriptions, schemas and hints, plus a snapshot. Results as structured content, side effects
  • checked in the real system, tool
  • errors, a second user, the backend down, and one test over the real transport."

Interview questions

Q1What is an MCP server?

A program that exposes tools (and resources and prompts) to AI agents over the Model Context Protocol: each tool has a name, a description and a JSON schema for its arguments.

Q2Do you need a model to test an MCP server?

No. Call it directly with an MCP client, such as the Python SDK's in-memory Client, and assert exactly on what it returns.

Q3What is in an MCP contract test?

Tool names, descriptions for every tool and argument, input schema limits and required fields, annotations such as read-only hints, and a snapshot of the whole tool list.

Q4Why snapshot tool descriptions?

Descriptions and schemas act like prompts; a small change can change which tool a model picks, so changes should fail a test and get reviewed.

Q5How should a tool report bad input?

As a tool result with is_error set and a short, readable reason the model can act on, never a stack trace or a secret.

Q6How do you test permissions in an MCP server?

Call the same tool as a second user. With ShopRung's order-leak bug on, Bob received Alice's order and the test failed.

Q7Why did the quantity bug not fail the MCP suite?

The tool schema rejected quantity 11 before the API was called, so the API needs its own tests; each test checks a specific layer.

Q8What is WebMCP?

An experimental browser API (a Chrome origin trial in 2026) that lets a web page register tools for in-browser agents, with JavaScript or form attributes; it is tested the same way.