This document explains how MCP-Jest works internally and why it's uniquely designed for testing MCP servers.
┌─────────────────────────────────────────────────────────────────────────────┐
│ MCP-JEST Architecture v1.2.0 │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ CLI │ │ Library │ │ Types │ │
│ │ (cli.ts) │ │ (index.ts) │ │ (types.ts) │ │
│ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │ │
│ └────────────────────┴────────────────────┘ │
│ │ │
│ ┌─────────────────────────┼─────────────────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────┐ ┌─────────────────┐ ┌──────────────────┐ │
│ │ MCPDiscovery │ │ MCPTestRunner │ │ MCPProtocolValid │ │
│ │(discovery.ts)│ │ (runner.ts) │ │ (validator.ts) │ │
│ └──────────────┘ └────────┬────────┘ └──────────────────┘ │
│ │ │
│ ┌───────────────────┼───────────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────┐ ┌───────────────┐ ┌──────────────┐ │
│ │ MCPTestClient│ │SnapshotManager│ │ Expectation │ │
│ │ (client.ts) │ │ (snapshot.ts) │ │ Evaluator │ │
│ └──────┬───────┘ └───────┬───────┘ └──────┬───────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ MCP Protocol Communication │ │
│ │ (via @modelcontextprotocol/sdk) │ │
│ └──────────────────────────────────────────────────┘ │
│ │ │
│ ┌──────────────┐ ┌───────────────┐ ┌──────────────┐ │
│ │ WatchMode │ │ HTMLReporter │ │ GitHub Action│ │
│ │ (watch.ts) │ │ (reporter.ts) │ │ (action.yml) │ │
│ └──────────────┘ └───────────────┘ └──────────────┘ │
│ │ │
└─────────────────────────────┼──────────────────────────────────────────────┘
│
▼
┌─────────────────┐
│ Your MCP Server │
│ (Being Tested) │
└─────────────────┘
Entry point for command-line usage. Parses arguments and delegates to appropriate handlers.
Programmatic API for integration into existing test suites (Jest, Vitest, etc.).
Orchestrates the entire test flow:
- Spawns server process
- Establishes MCP connection
- Runs discovery and functional tests
- Collects and reports results
Handles MCP protocol communication:
- Connection management
- Tool execution
- Resource reading
- Prompt retrieval
Auto-discovers server capabilities and generates test configurations.
Validates MCP protocol compliance with detailed scoring.
Manages snapshot testing for output comparison.
Generates interactive HTML test reports.
File watcher for auto-rerunning tests during development.
Your MCP Server → MCP Client → Test Runner → Results
↑ ↓
Auto-spawned Formatted Output
- Server Startup: MCP-Jest spawns your server as a child process
- Connection: Establishes MCP client connection via configured transport
- Discovery: Lists available tools, resources, and prompts
- Capability Testing: Verifies expected capabilities exist
- Functional Testing: Executes tools, reads resources, gets prompts
- Validation: Checks results against expectations
- Reporting: Outputs detailed results with pass/fail status
- Cleanup: Terminates server process and connections
MCP-Jest is purpose-built for the MCP protocol, unlike generic testing frameworks:
┌─────────────────────────────────────────────────────────┐
│ Generic Testing vs MCP-JEST │
├─────────────────────────────────────────────────────────┤
│ │
│ Generic Test Framework: │
│ ┌─────────┐ │
│ │ Test │──[HTTP/Function Call]──> Response │
│ └─────────┘ │
│ │
│ MCP-JEST: │
│ ┌─────────┐ │
│ │ Test │ │
│ └────┬────┘ │
│ │ │
│ ├──[1. Process Management] │
│ ├──[2. Transport Setup (stdio/HTTP/SSE)] │
│ ├──[3. MCP Protocol Handshake] │
│ ├──[4. Capability Discovery] │
│ ├──[5. Tool/Resource/Prompt Execution] │
│ └──[6. Structured Validation] │
│ │
└─────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────┐
│ MCP-JEST Test Coverage │
├────────────────────────────────────────────────┤
│ │
│ Connection Layer: │
│ • Server startup │
│ • Protocol handshake │
│ • Timeout handling │
│ │
│ Discovery Layer: │
│ • Available tools │
│ • Available resources │
│ • Available prompts │
│ • Capability matching │
│ │
│ Functional Layer: │
│ • Tool execution with arguments │
│ • Resource reading │
│ • Prompt generation │
│ • Error handling │
│ │
│ Validation Layer: │
│ • Response structure │
│ • Content validation │
│ • Snapshot comparison │
│ • Custom expectations │
│ │
└────────────────────────────────────────────────┘
MCP-Jest uses a deterministic snapshot comparison algorithm:
┌─────────────────────────────────────────────────────────┐
│ Snapshot Comparison Algorithm │
├───────────────────────────────────────────────────────── ┤
│ │
│ 1. Normalize Data: │
│ • Sort object keys alphabetically │
│ • Remove volatile fields (timestamps, IDs) │
│ • Apply inclusion/exclusion rules │
│ │
│ 2. Generate Hash: │
│ • Create deterministic string representation │
│ • Use SHA-256 for consistency │
│ │
│ 3. Compare: │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Current │ │ Stored │ │
│ │ Output │ <=> │ Snapshot │ │
│ └─────────────┘ └─────────────┘ │
│ │ │ │
│ └────────┬───────────┘ │
│ │ │
│ ┌────┴────┐ │
│ │ Equal? │ │
│ └────┬────┘ │
│ │ │
│ ┌─────────┴─────────┐ │
│ │ │ │
│ [Yes] [No] │
│ │ │ │
│ ✓ Pass Check Update Mode │
│ │ │
│ ┌────────┴────────┐ │
│ │ │ │
│ [Update Mode] [Normal Mode] │
│ │ │ │
│ Update & Pass Show Diff & Fail │
│ │
└─────────────────────────────────────────────────────────┘
MCP-Jest supports multiple transport protocols:
- Spawns server as child process
- Communicates via stdin/stdout
- Best for local development
- Connects to HTTP endpoints
- Supports streaming responses
- Good for remote servers
- Real-time event streaming
- One-way server-to-client
- Useful for long-running operations
┌─────────────────────────────────────────────────┐
│ Developer Experience Flow │
├──────────────────────────────────────────────────┤
│ │
│ 1. Write Test Config (JSON) │
│ Simple, declarative, no code needed │
│ │
│ 2. Run Tests │
│ $ mcp-jest test-config.json │
│ │
│ 3. See Results │
│ ✓ Connection test passed (50ms) │
│ ✓ Tool: calculate - passed (23ms) │
│ ✗ Resource: data - failed (15ms) │
│ Expected: "value" │
│ Received: "other" │
│ │
│ 4. Update Snapshots (if needed) │
│ $ mcp-jest test-config.json -u │
│ │
│ 5. Integrate with CI/CD │
│ Exit codes, JSON output, timing info │
│ │
└─────────────────────────────────────────────────┘
| Metric | Typical Value |
|---|---|
| Average test suite | 100-500ms |
| Complex servers | 1-3 seconds |
| Memory footprint | Minimal |
| Server isolation | Each suite spawns own instance |
- Getting Started - Quick start guide
- CLI Reference - Complete CLI documentation
- API Reference - Library API documentation
- Comparison - MCP-Jest vs alternatives