Module: MockServer::MCP

Defined in:
lib/mockserver/mcp.rb

Overview

Fluent builder for mocking an MCP (Model Context Protocol) server.

Mirrors the Java/Node/Python 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 on POST <path> and responds with a Velocity template that echoes back the incoming JSON-RPC id via $!{request.jsonRpcRawId}.

Examples:

MockServer::MCP.mcp_mock('/mcp')
  .with_tool('get_weather')
    .with_description('Get weather for a city')
    .with_input_schema('{"type":"object"}')
    .responding_with('72F and sunny')
  .and_then
  .apply_to(client)

Defined Under Namespace

Classes: McpMockBuilder, McpPromptBuilder, McpResourceBuilder, McpToolBuilder, PromptArg, PromptDef, PromptMessage, RawExpectation, ResourceDef, ToolDef

Class Method Summary collapse

Class Method Details

.escape_json(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

JSON-escape a string the same way Jackson writeValueAsString does, then strip the surrounding quotes — yielding only the escaped inner content.



27
28
29
30
31
32
# File 'lib/mockserver/mcp.rb', line 27

def self.escape_json(value)
  return '' if value.nil?

  quoted = JSON.generate(value.to_s)
  quoted[1...-1]
end

.escape_json_path(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Escape single quotes for safe inclusion inside a JSONPath string literal.



44
45
46
47
48
# File 'lib/mockserver/mcp.rb', line 44

def self.escape_json_path(value)
  return '' if value.nil?

  value.to_s.gsub("'", "\\\\'")
end

.escape_velocity(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Escape Velocity metacharacters so literal $ / # survive rendering.



36
37
38
39
40
# File 'lib/mockserver/mcp.rb', line 36

def self.escape_velocity(value)
  return value if value.nil?

  value.to_s.gsub('$', '${esc.d}').gsub('#', '${esc.h}')
end

.mcp_mock(path = '/mcp') ⇒ McpMockBuilder

Create a new MCP mock builder. path defaults to /mcp.

Returns:



449
450
451
# File 'lib/mockserver/mcp.rb', line 449

def self.mcp_mock(path = '/mcp')
  McpMockBuilder.new(path)
end

.validate_and_serialize_json(raw) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse + re-serialize JSON (compact) to validate it. Raises on invalid JSON.



52
53
54
55
56
# File 'lib/mockserver/mcp.rb', line 52

def self.validate_and_serialize_json(raw)
  JSON.generate(JSON.parse(raw))
rescue JSON::ParserError => e
  raise ArgumentError, "Invalid JSON for inputSchema: #{e.message}"
end

.velocity_json_rpc_response(result_json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



59
60
61
62
63
# File 'lib/mockserver/mcp.rb', line 59

def self.velocity_json_rpc_response(result_json)
  '{"statusCode": 200, ' \
    '"headers": [{"name": "Content-Type", "values": ["application/json"]}], ' \
    '"body": {"jsonrpc": "2.0", "result": ' + result_json + ', "id": $!{request.jsonRpcRawId}}}'
end