CSE Roadmap

Core tier · about 9 hours · 13 parts

Part 11 of 13 · 5 min read

Interview answers and mistakes

Answering "how would you test an MCP server?" in an interview

The level's two earlier questions have answers in modules 1 to 5. They are "How does MCP differ from plain function calling?" and "How would you secure an MCP server that exposes internal data?" This module gives the third a candidate for a build role will get: "How would you test an MCP server?"

A strong answer is layered and specific. I would start with unit tests on the rules with no protocol in sight. That includes a threaded test for retries and a hostile-input test for injection. Then contract tests that lint descriptions, schemas and error text. Does it say what to do next, not just what went wrong? Then a snapshot of everything the model reads, so a change needs review before it ships. Then conformance checks for the protocol's rules in every era I support: deterministic tool order, isError for fixable failures, a structured result that matches its schema, and a read-only tool that really changes nothing. Then a real child process to catch a stray print on stdout, and a real socket for HTTP. Then security tests that show scopes and ownership each fail without the other. That way I know neither lock alone is doing the job. Finally, a second, independent client in CI. That is the Inspector's command-line mode, branching on its exit codes. My own suite is then not the only thing that has ever talked to the server.

Then the part that separates answers: how do you know your tests actually work? Mutation testing — break the server on purpose and see what notices. In my own run, 2 of 25 defects escaped every layer, and 12 were noticed by exactly one layer, which told me exactly where a dropped layer would cost me.

Follow-ups worth preparing for: "Where do you put a business rule?" (in the service, so a unit test can reach it). "Why is the error text part of the contract?" (a model reads it; tests of status codes miss it entirely). "What can't your suite tell you?" (how a real model actually behaves — that needs a small evaluation with a real model, covered in module 9). A weak answer says only "I call each tool and check it returns 200".

Mistakes that undercut a server's tests

  • Trusting a green suite without ever breaking anything on purpose. Symptom: every check passes, every release. Cause: no mutation testing was ever run. Fix: plant a handful of deliberate defects and confirm each is caught before trusting the count of passing tests.
  • Testing idempotency with one caller instead of several at once. Symptom: the retry test passes, but duplicate rows show up in production under load. Cause: a single-threaded test cannot land in the gap between a look and a write. Fix: a threaded test with a pause point, plus a database constraint as the real backstop.
  • Putting a business rule in the adapter so only the protocol can test it. Symptom: a simple rule change needs a full tool call to verify. Cause: the rule was written next to the schema instead of next to the data. Fix: move it into the service layer, where a plain function call reaches it.
  • Never starting the server as a real process. Symptom: every in-process test passes, then the real client hangs on the first message. Cause: a start-up banner or log line landed on stdout, where only JSON-RPC may go. Fix: a transport test that launches the actual child process and checks stdout byte for byte.
  • Checking tool schemas but never the error text. Symptom: every call either succeeds or fails with a technically correct but useless message. Cause: contract tests covered L01 to L12 but skipped the error-text rules. Fix: provoke every known failure and lint the message for a code, a plain explanation and a next step.

Check yourself

Your tests all pass, but a planted change that removes the length check on a field also passes. What does that tell you?

Show answer

The tests do not cover that check. Passing tests show only what they exercise; mutation testing shows what they would miss.

For the curious Why seven layers

Unit tests check the rules, contract and snapshot tests keep tool names and schemas from drifting, conformance tests check the protocol in both eras, end-to-end tests use a stand-in reader, transport tests check each door, and security tests check authorisation and injection. Each catches something the others cannot.

What you will use daily at work

Practice What it looks like
Tool errors that guide "Order 99 not found; call list_orders to see valid ids."
A snapshot of tool schemas A change to a description shows up in review.
A mutation run Break the code on purpose and see whether a test fails.

Quick notes: what you must be able to say

  • A business rule lives in the service layer, so a plain function call can test it without starting a server.
  • An isError result with message and hint is a protocol error's opposite: it is for anything the caller could fix.
  • A test with one caller cannot prove a retry-under-load bug is fixed; it needs several threads racing for the same row.
  • Passing tests prove nothing about bugs that have not happened yet — mutation testing breaks the code on purpose to check.
  • A real child process and a real socket catch failures (a stray print on stdout) no in-process test can see.