An MCP server has more failure surfaces than a normal library: schema generation, protocol serialization, transport framing, process lifecycle, and the upstream service. A single happy-path test proves almost nothing. Test each layer with the cheapest tool that can catch its failures.
Layer 1: handler unit tests
Test the business logic directly. In TypeScript, factor tool handlers into functions that the MCP registration calls. In Python, FastMCP tools are ordinary functions aside from context injection. Cover:
- Valid input.
- Missing required values.
- Boundary values.
- Permission failures.
- Upstream errors and timeouts.
- Empty results and truncation.
Mock external APIs with httpx.MockTransport in Python or a small fake client in TypeScript. Do not let unit tests depend on live credentials.
Layer 2: schema contract tests
The schema is part of the public API. Snapshot the generated tool list or assert on specific structures:
const tools = await client.listTools();
const search = tools.tools.find((tool) => tool.name === "search_issues");
expect(search?.inputSchema.required).toContain("query");
expect(search?.inputSchema.properties?.limit).toMatchObject({ type: "number", maximum: 50 });
These tests catch accidental renames and unbounded fields. If the SDK generates schemas from Zod or Pydantic, the test verifies the generated result, not just the source type.
Layer 3: in-memory protocol tests
The TypeScript SDK provides an in-memory transport pair. It exercises the real request/response flow without spawning a process:
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
const client = new Client({ name: "test-client", version: "1.0.0" });
await client.connect(clientTransport);
const result = await client.callTool({
name: "count_lines",
arguments: { text: "one\ntwo" },
});
expect(result.content).toBeDefined();
This catches serialization errors, missing registration, and schema mismatches. It does not test process startup or HTTP behavior.
Layer 4: transport smoke tests
For stdio, spawn the real command in a child process and perform a server-discovery/tools-list exchange (use the legacy initialize handshake only for an older revision). Check that the process exits when stdin closes and that stdout contains only JSON-RPC. A stray log line is easiest to catch here.
For Streamable HTTP, start the server on a random local port, send a real request, and verify authentication and headers. Include:
- Missing token → 401.
- Invalid Origin → 403 (where configured).
- Malformed body → controlled 400.
- Oversized body → rejected.
- A tool timeout → controlled error.
Do not test only the happy path over HTTP.
Layer 5: Inspector and a real client
MCP Inspector is the fastest way to inspect what a client sees. It shows the schema, allows manual calls, and displays errors. Use it before writing an end-to-end automated test.
Then test one real client configuration end to end. For each client, verify:
- The server appears in the server list.
- The tools appear with expected names.
- One read-only call succeeds.
- One invalid call fails visibly.
- Restarting the client still works.
This catches path and scope problems that an in-memory test cannot.
Layer 6: failure injection
Production failures are usually environmental:
- Upstream returns 500, 401, or 429.
- Network connection times out.
- A required environment variable is missing.
- The server receives malformed arguments.
- The client disconnects mid-call.
- The database is unavailable.
Add tests for the failures you can simulate. Verify that the server returns a structured error, does not crash, and does not leak credentials or stack traces.
What counts as evidence
A test report is evidence only if it names the command, the environment, and the result. “Tests pass” is not enough. Record:
- Commit or build artifact.
- Test command and version.
- Number of tests, failures, and skips.
- Any test that required network or live credentials.
- Anything not covered.
Do not mark a client as supported because a config parsed. Do not mark a remote endpoint as verified because a health check returned 200. Those are different claims.
CI shape
A practical pipeline:
- Type check and lint.
- Unit and schema tests.
- In-memory/integration tests.
- Build the artifact.
- Spawn the built stdio server and run a smoke test.
- For remote servers, start the built container and run HTTP tests against it.
- Run MCP Inspector or a scripted client against the artifact.
The earlier a layer fails, the cheaper the fix. Keep the slowest end-to-end test small and deterministic.
For specific runtime failures, use the error knowledge base and Config Doctor. For deployment verification, see deploy an MCP server.
Sources
Sources were checked on Oct 23, 2026. Versions and client behavior can change; confirm the current documentation before relying on a configuration.