mockserver-client
An idiomatic Rust client for MockServer's control-plane REST API.
Installation
Add to your Cargo.toml:
[]
= "7.0"
Quick Start
use ;
LLM and MCP mocking
The llm and mcp modules provide fluent builders that
produce the same expectation wire JSON as the Java, Node, and Python clients, so a
mock scripted from Rust behaves identically to one scripted from any other client.
LLM completions, embeddings, conversations, and failover
use ;
// A single chat-completion mock (action carried in `httpLlmResponse`).
llm_mock
.with_provider
.with_model
.responding_with
.apply_to?;
// An embedding mock (clears any completion).
llm_mock
.with_provider
.responding_with
.apply_to?;
// A multi-turn conversation using MockServer scenario-state advancement.
conversation
.with_path
.with_provider
.isolate_by // optional per-session isolation
.turn
.turn
.apply_to?;
// Fail N times (with default provider-shaped error bodies), then succeed.
llm_failover
.with_path
.with_provider
.fail_with_count // coalesced into one expectation with times = 2
.fail_with
.then_respond_with
.apply_to?;
provider is serialised UPPERCASE (OPENAI, ANTHROPIC, ...). Conversation turns
advance through scenario states Started -> turn_1 -> ... -> __done. Each builder also
has a build() method returning the raw serde_json::Value(s) if you want to inspect
or submit them yourself via client.upsert_raw(...).
MCP (Model Context Protocol) servers
mcp_mock builds the set of expectations needed to emulate a Streamable-HTTP MCP
server speaking JSON-RPC 2.0 (initialize, ping, notifications/initialized,
tools/list, tools/call, resources/list, resources/read, prompts/list,
prompts/get). Responses use Velocity templates that echo the inbound JSON-RPC id.
use mcp_mock;
use Role;
mcp_mock
.with_server_name
.with_tool
.with_description
.with_input_schema
.responding_with
.and
.with_resource
.with_name
.with_content
.and
.with_prompt
.with_argument
.responding_with
.and
.apply_to?;
Use mcp_mock_default() for the default /mcp path. build() returns the ordered
Vec<serde_json::Value> of expectations.
Features
- Fluent builder API —
client.when(request).respond(response) - Response, Forward, and Error actions — full MVP control-plane coverage
- Verification —
verify(count-based) andverify_sequence(order-based) - Retrieve — recorded requests, active expectations, recorded expectations, logs
- Clear / Reset — by request matcher, by expectation ID, or full reset
- Status / Bind — query ports, bind additional ports
- LLM and MCP builders — fluent
llm/mcpmock builders, wire-identical to the other clients - Blocking (synchronous) — uses
reqwestblocking client; no async runtime needed - TLS support — optional HTTPS with configurable certificate verification
- Secured control plane —
control_plane_bearer_token(..)(JWT auth),ca_cert_pem_path(..)/ca_cert_pem(..)(trust a server CA), andclient_cert_pem(cert, key)(mTLS)
API Overview
use *;
let client = new.build.unwrap;
// Fluent expectation creation
client.when
.times
.respond?;
// Forward action
client.when
.forward?;
// Verify
client.verify?;
// Verify sequence
client.verify_sequence?;
// Clear by request matcher
client.clear?;
// Clear by ID
client.clear_by_id?;
// Retrieve recorded requests
let requests = client.retrieve_recorded_requests?;
// Retrieve active expectations
let expectations = client.retrieve_active_expectations?;
// Server status
let ports = client.status?;
println!;
// Reset everything
client.reset?;
Interactive Breakpoints
Register breakpoint matchers to pause forwarded/proxied traffic at REQUEST, RESPONSE, RESPONSE_STREAM, or INBOUND_STREAM phases. A callback WebSocket connection is opened automatically.
use *;
let client = new.build?;
// REQUEST-only breakpoint
let id = client.add_request_breakpoint?;
// REQUEST + RESPONSE breakpoint
let id2 = client.add_request_response_breakpoint?;
// Streaming breakpoint
let id3 = client.add_stream_breakpoint?;
// Manage matchers
let list = client.list_breakpoint_matchers?;
client.remove_breakpoint_matcher?;
client.clear_breakpoint_matchers?;
client.close_breakpoint_websocket;
Stream frame decisions: StreamFrameDecision::continue_frame, ::modify, ::drop_frame, ::inject, ::close.
Start / Launch MockServer
The Rust client can download and launch a local MockServer instance directly -- no Java installation and no Docker required. The launcher downloads a self-contained platform bundle (mockserver-<version>-<os>-<arch>) from the GitHub Release, verifies its SHA-256, caches it per-user, and starts it.
Quick start
use launcher;
Just ensure the binary is present
let launcher_path = ensure_launcher?;
println!;
Specify a version
let mut handle = start_with_version?;
API reference
| Function / Type | Description |
|---|---|
launcher::ensure_launcher() |
Download, verify, cache the default-version binary, and return the launcher PathBuf. |
launcher::ensure_binary(version, opts) |
Same as above, but for a specific version. |
launcher::start(port) |
Ensure the binary and start MockServer at the default version. Returns a ServerHandle. |
launcher::start_with_version(version, port, opts) |
Start MockServer at a specific version. Returns a ServerHandle. |
launcher::ServerHandle |
Handle to the running process. Methods: stop(), wait(), port(). |
launcher::VERSION |
The default MockServer version, derived from Cargo.toml at compile time via env!("CARGO_PKG_VERSION"). |
Supported platforms
| OS | Architecture |
|---|---|
| Linux | x86_64, aarch64 |
| macOS (darwin) | x86_64, aarch64 |
| Windows | x86_64, aarch64 |
Environment variables
| Variable | Purpose |
|---|---|
MOCKSERVER_BINARY_BASE_URL |
Mirror host for the release assets (corporate / air-gapped networks) |
MOCKSERVER_BINARY_CACHE |
Override the cache directory (default: ~/.cache/mockserver/binaries on Unix) |
MOCKSERVER_SKIP_BINARY_DOWNLOAD |
Fail instead of downloading (use with a pre-seeded cache in CI) |
Version
By default the launcher downloads the MockServer version matching this crate (derived from Cargo.toml at compile time via env!("CARGO_PKG_VERSION")). Pass an explicit version to override.
Building
Integration Tests
Integration tests require a running MockServer and are skipped by default:
# Start MockServer (e.g., via Docker)
# Run integration tests
MOCKSERVER_URL=http://localhost:1080
License
Apache-2.0