Module: ModelContextProtocol::RSpec::Matchers

Defined in:
lib/model_context_protocol/rspec/matchers.rb,
lib/model_context_protocol/rspec/matchers/have_text_content.rb,
lib/model_context_protocol/rspec/matchers/be_valid_mcp_class.rb,
lib/model_context_protocol/rspec/matchers/have_audio_content.rb,
lib/model_context_protocol/rspec/matchers/have_image_content.rb,
lib/model_context_protocol/rspec/matchers/have_message_count.rb,
lib/model_context_protocol/rspec/matchers/have_resource_blob.rb,
lib/model_context_protocol/rspec/matchers/have_resource_text.rb,
lib/model_context_protocol/rspec/matchers/be_mcp_error_response.rb,
lib/model_context_protocol/rspec/matchers/have_message_with_role.rb,
lib/model_context_protocol/rspec/matchers/have_resource_mime_type.rb,
lib/model_context_protocol/rspec/matchers/have_structured_content.rb,
lib/model_context_protocol/rspec/matchers/have_resource_annotations.rb,
lib/model_context_protocol/rspec/matchers/be_valid_mcp_tool_response.rb,
lib/model_context_protocol/rspec/matchers/have_resource_link_content.rb,
lib/model_context_protocol/rspec/matchers/be_valid_mcp_prompt_response.rb,
lib/model_context_protocol/rspec/matchers/be_valid_mcp_resource_response.rb,
lib/model_context_protocol/rspec/matchers/have_embedded_resource_content.rb

Defined Under Namespace

Classes: BeMcpErrorResponse, BeValidMcpClass, BeValidMcpPromptResponse, BeValidMcpResourceResponse, BeValidMcpToolResponse, HaveAudioContent, HaveEmbeddedResourceContent, HaveImageContent, HaveMessageCount, HaveMessageWithRole, HaveResourceAnnotations, HaveResourceBlob, HaveResourceLinkContent, HaveResourceMimeType, HaveResourceText, HaveStructuredContent, HaveTextContent

Instance Method Summary collapse

Instance Method Details

#be_mcp_error_response(expected_message = nil) ⇒ Object

Matcher that validates a tool response is an error response.

Examples:

Basic usage (any error)

expect(response).to be_mcp_error_response

With message match (string)

expect(response).to be_mcp_error_response("Connection timed out")

With message match (regex)

expect(response).to be_mcp_error_response(/failed.*timeout/i)


17
18
19
# File 'lib/model_context_protocol/rspec/matchers/be_mcp_error_response.rb', line 17

def be_mcp_error_response(expected_message = nil)
  BeMcpErrorResponse.new(expected_message)
end

#be_valid_mcp_class(expected_type = nil) ⇒ Object

Matcher that validates a class is a properly defined MCP class.

Examples:

Basic usage

expect(MyTool).to be_valid_mcp_class

With type constraint

expect(MyTool).to be_valid_mcp_class(:tool)
expect(MyPrompt).to be_valid_mcp_class(:prompt)
expect(MyResource).to be_valid_mcp_class(:resource)
expect(MyTemplate).to be_valid_mcp_class(:resource_template)


17
18
19
# File 'lib/model_context_protocol/rspec/matchers/be_valid_mcp_class.rb', line 17

def be_valid_mcp_class(expected_type = nil)
  BeValidMcpClass.new(expected_type)
end

#be_valid_mcp_prompt_responseObject

Matcher that validates a response is a properly structured MCP prompt response.

Examples:

Basic usage

expect(response).to be_valid_mcp_prompt_response


11
12
13
# File 'lib/model_context_protocol/rspec/matchers/be_valid_mcp_prompt_response.rb', line 11

def be_valid_mcp_prompt_response
  BeValidMcpPromptResponse.new
end

#be_valid_mcp_resource_responseObject

Matcher that validates a response is a properly structured MCP resource response.

Examples:

Basic usage

expect(response).to be_valid_mcp_resource_response


11
12
13
# File 'lib/model_context_protocol/rspec/matchers/be_valid_mcp_resource_response.rb', line 11

def be_valid_mcp_resource_response
  BeValidMcpResourceResponse.new
end

#be_valid_mcp_tool_responseObject

Matcher that validates a response is a properly structured MCP tool response.

Examples:

Basic usage

expect(response).to be_valid_mcp_tool_response


11
12
13
# File 'lib/model_context_protocol/rspec/matchers/be_valid_mcp_tool_response.rb', line 11

def be_valid_mcp_tool_response
  BeValidMcpToolResponse.new
end

#have_audio_content(mime_type: nil) ⇒ Object

Matcher that validates a tool response contains audio content.

Examples:

Basic usage

expect(response).to have_audio_content

With mime type constraint

expect(response).to have_audio_content(mime_type: "audio/mp3")


14
15
16
# File 'lib/model_context_protocol/rspec/matchers/have_audio_content.rb', line 14

def have_audio_content(mime_type: nil)
  HaveAudioContent.new(mime_type: mime_type)
end

#have_embedded_resource_content(uri: nil, mime_type: nil) ⇒ Object

Matcher that validates a tool response contains embedded resource content.

Examples:

Basic usage

expect(response).to have_embedded_resource_content

With URI constraint

expect(response).to have_embedded_resource_content(uri: "file:///path/to/file.txt")


14
15
16
# File 'lib/model_context_protocol/rspec/matchers/have_embedded_resource_content.rb', line 14

def have_embedded_resource_content(uri: nil, mime_type: nil)
  HaveEmbeddedResourceContent.new(uri: uri, mime_type: mime_type)
end

#have_image_content(mime_type: nil) ⇒ Object

Matcher that validates a tool response contains image content.

Examples:

Basic usage

expect(response).to have_image_content

With mime type constraint

expect(response).to have_image_content(mime_type: "image/png")


14
15
16
# File 'lib/model_context_protocol/rspec/matchers/have_image_content.rb', line 14

def have_image_content(mime_type: nil)
  HaveImageContent.new(mime_type: mime_type)
end

#have_message_count(expected_count) ⇒ Object

Matcher that validates a prompt response contains a specific number of messages.

Examples:

Basic usage

expect(response).to have_message_count(3)


11
12
13
# File 'lib/model_context_protocol/rspec/matchers/have_message_count.rb', line 11

def have_message_count(expected_count)
  HaveMessageCount.new(expected_count)
end

#have_message_with_role(role) ⇒ Object

Matcher that validates a prompt response contains a message with a specific role.

Examples:

Basic usage

expect(response).to have_message_with_role("user")

With content constraint

expect(response).to have_message_with_role("assistant").containing("How can I help?")

With regex content constraint

expect(response).to have_message_with_role("user").containing(/generate.*excuses/i)


17
18
19
# File 'lib/model_context_protocol/rspec/matchers/have_message_with_role.rb', line 17

def have_message_with_role(role)
  HaveMessageWithRole.new(role)
end

#have_resource_annotations(expected_annotations) ⇒ Object

Matcher that validates a resource response has specific annotations.

Examples:

Basic usage

expect(response).to have_resource_annotations(priority: 0.9)

With audience

expect(response).to have_resource_annotations(audience: ["user", "assistant"])

With multiple annotations

expect(response).to have_resource_annotations(priority: 0.9, audience: ["user"])


17
18
19
# File 'lib/model_context_protocol/rspec/matchers/have_resource_annotations.rb', line 17

def have_resource_annotations(expected_annotations)
  HaveResourceAnnotations.new(expected_annotations)
end

#have_resource_blob(expected_blob = nil) ⇒ Object

Matcher that validates a resource response contains binary blob content.

Examples:

Basic usage

expect(response).to have_resource_blob

With base64 content match

expect(response).to have_resource_blob("dGVzdA==")


14
15
16
# File 'lib/model_context_protocol/rspec/matchers/have_resource_blob.rb', line 14

def have_resource_blob(expected_blob = nil)
  HaveResourceBlob.new(expected_blob)
end

Matcher that validates a tool response contains resource link content.

Examples:

Basic usage

expect(response).to have_resource_link_content

With URI constraint

expect(response).to have_resource_link_content(uri: "file:///path/to/file.txt")

With name constraint

expect(response).to have_resource_link_content(name: "my-resource")

With both constraints

expect(response).to have_resource_link_content(uri: "file:///path", name: "my-resource")


20
21
22
# File 'lib/model_context_protocol/rspec/matchers/have_resource_link_content.rb', line 20

def have_resource_link_content(uri: nil, name: nil)
  HaveResourceLinkContent.new(uri: uri, name: name)
end

#have_resource_mime_type(expected_mime_type) ⇒ Object

Matcher that validates a resource response has a specific mime type.

Examples:

Basic usage

expect(response).to have_resource_mime_type("text/plain")

With regex match

expect(response).to have_resource_mime_type(/^image\//)


14
15
16
# File 'lib/model_context_protocol/rspec/matchers/have_resource_mime_type.rb', line 14

def have_resource_mime_type(expected_mime_type)
  HaveResourceMimeType.new(expected_mime_type)
end

#have_resource_text(expected_text) ⇒ Object

Matcher that validates a resource response contains specific text content.

Examples:

With exact string match

expect(response).to have_resource_text("Hello, World!")

With regex match

expect(response).to have_resource_text(/secret.*plans/i)


14
15
16
# File 'lib/model_context_protocol/rspec/matchers/have_resource_text.rb', line 14

def have_resource_text(expected_text)
  HaveResourceText.new(expected_text)
end

#have_structured_content(expected_content) ⇒ Object

Matcher that validates a tool response contains specific structured content.

Examples:

Basic usage

expect(response).to have_structured_content(temperature: 22.5)

With nested content

expect(response).to have_structured_content(user: {name: "John", age: 30})


14
15
16
# File 'lib/model_context_protocol/rspec/matchers/have_structured_content.rb', line 14

def have_structured_content(expected_content)
  HaveStructuredContent.new(expected_content)
end

#have_text_content(expected_text) ⇒ Object

Matcher that validates a tool response contains specific text content.

Examples:

With exact string match

expect(response).to have_text_content("Hello, World!")

With regex match

expect(response).to have_text_content(/doubled is \d+/)


14
15
16
# File 'lib/model_context_protocol/rspec/matchers/have_text_content.rb', line 14

def have_text_content(expected_text)
  HaveTextContent.new(expected_text)
end