Skip to main content

Module mcp

Module mcp 

Source
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§

McpMockBuilder
Fluent builder producing the expectations for a mocked MCP server.
McpPromptBuilder
Sub-builder for a single MCP prompt. Call and to return to the parent McpMockBuilder.
McpResourceBuilder
Sub-builder for a single MCP resource. Call and to return to the parent McpMockBuilder.
McpToolBuilder
Sub-builder for a single MCP tool. Call and to return to the parent McpMockBuilder.

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. path defaults to /mcp if you use mcp_mock_default.
mcp_mock_default
Create a new MCP mock builder targeting the default /mcp path.
validate_and_serialize_json
Validate that the supplied string is valid JSON and return it re-serialised in compact form. Mirrors Java’s validateAndSerializeJson.