ruby-sdk/test/mcp/tool/response_test.rb
Koichi ITO 0034952df9 Set default content to an empty array instead of nil
Follow-up to https://github.com/modelcontextprotocol/ruby-sdk/pull/147.

According to the specification schema, `content` is not optional and therefore cannot be omitted:

```typescript
interface CallToolResult {
  _meta?: { [key: string]: unknown };
  content: ContentBlock[];
  isError?: boolean;
  structuredContent?: { [key: string]: unknown };
  [key: string]: unknown;
}
```

https://modelcontextprotocol.io/specification/2025-06-18/schema#calltoolresult

Instead of `nil`, an empty array is set as the default value.

There may be a better value for `content`, but at the very least it should not be missing.
2025-10-02 23:52:19 +09:00

127 lines
4 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
module MCP
class Tool
class ResponseTest < ActiveSupport::TestCase
test "#initialize with content" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content)
assert_equal content, response.content
refute response.error?
end
test "#initialize with content and error set to true" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: true)
assert_equal content, response.content
assert response.error?
end
test "#initialize with content and error explicitly set to false" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: false)
assert_equal content, response.content
refute response.error?
end
test "#initialize with content and structuredContent" do
content = [{
type: "text",
text: "{\"code\":401,\"message\":\"Unauthorized\"}",
}]
structured_content = { code: 401, message: "Unauthorized" }
response = Response.new(content, structured_content: structured_content)
assert_equal content, response.content
assert_equal structured_content, response.structured_content
refute response.error?
end
test "#error? for a standard response" do
response = Response.new(nil, error: false)
refute response.error?
end
test "#error? for an error response" do
response = Response.new(nil, error: true)
assert response.error?
end
test "#to_h for a standard response" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content)
actual = response.to_h
assert_equal [:content, :isError], actual.keys
assert_equal content, actual[:content]
refute actual[:isError]
end
test "#to_h for an error response" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: true)
actual = response.to_h
assert_equal [:content, :isError], actual.keys
assert_equal content, actual[:content]
assert actual[:isError]
end
test "#to_h for a standard response with content and structured content" do
content = [{
type: "text",
text: "{\"code\":401,\"message\":\"Unauthorized\"}",
}]
structured_content = { code: 401, message: "Unauthorized" }
response = Response.new(content, structured_content: structured_content)
actual = response.to_h
assert_equal [:content, :isError, :structuredContent], actual.keys
assert_equal content, actual[:content]
assert_equal structured_content, actual[:structuredContent]
refute actual[:isError]
end
test "#to_h for a standard response with nil content and structured content" do
structured_content = { code: 401, message: "Unauthorized" }
response = Response.new(nil, structured_content: structured_content)
actual = response.to_h
assert_equal [:content, :isError, :structuredContent], actual.keys
assert_empty actual[:content]
assert_equal structured_content, actual[:structuredContent]
refute actual[:isError]
end
test "#to_h for a standard response with structured content only" do
structured_content = { code: 401, message: "Unauthorized" }
response = Response.new(structured_content: structured_content)
actual = response.to_h
assert_equal [:content, :isError, :structuredContent], actual.keys
assert_empty actual[:content]
assert_equal structured_content, actual[:structuredContent]
refute actual[:isError]
end
end
end
end