Expand description
Fluent builder for mocking an MCP (Model Context Protocol) server.
This is an idiomatic Rust port of the Java/Node/Python client
McpMockBuilder (org.mockserver.client.McpMockBuilder). It produces the
same wire-level expectation JSON: a set of HTTP expectations that emulate a
Streamable-HTTP MCP server speaking JSON-RPC 2.0.
Each generated expectation matches a JSON-RPC method (initialize, ping,
notifications/initialized, tools/list, tools/call, resources/list,
resources/read, prompts/list, prompts/get) on POST <path> and
responds with a Velocity template that echoes back the incoming JSON-RPC id
via $!{request.jsonRpcRawId}.
§Example
use mockserver_client::mcp::mcp_mock;
let expectations = mcp_mock("/mcp")
.with_tool("get_weather")
.with_description("Get the weather for a city")
.with_input_schema("{\"type\":\"object\"}")
.responding_with("sunny", false)
.and()
.build();
// initialize, ping, notifications/initialized, tools/list, tools/call
assert_eq!(expectations.len(), 5);Structs§
- McpMock
Builder - Fluent builder producing the expectations for a mocked MCP server.
- McpPrompt
Builder - Sub-builder for a single MCP prompt. Call
andto return to the parentMcpMockBuilder. - McpResource
Builder - Sub-builder for a single MCP resource. Call
andto return to the parentMcpMockBuilder. - McpTool
Builder - Sub-builder for a single MCP tool. Call
andto return to the parentMcpMockBuilder.
Functions§
- escape_
json - JSON-escape a string for inlining inside a JSON string literal, returning the
contents WITHOUT the surrounding quotes. Mirrors Java’s
OBJECT_MAPPER.writeValueAsString(value)then stripping the outer quotes. - escape_
json_ path - Escape single quotes for safe inclusion inside a JSONPath string literal.
Mirrors Java’s
escapeJsonPath. - escape_
velocity - Escape Velocity meta-characters so literal
$/#in mock content are not interpreted as Velocity references/directives. Mirrors Java:replace("$", "${esc.d}").replace("#", "${esc.h}"). - mcp_
mock - Create a new MCP mock builder.
pathdefaults to/mcpif you usemcp_mock_default. - mcp_
mock_ default - Create a new MCP mock builder targeting the default
/mcppath. - validate_
and_ serialize_ json - Validate that the supplied string is valid JSON and return it re-serialised
in compact form. Mirrors Java’s
validateAndSerializeJson.