From 0034952df93c1090f2b5c134c0e2fc7aae95f3e4 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 2 Oct 2025 01:08:37 +0900 Subject: [PATCH] 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. --- lib/mcp/tool/response.rb | 2 +- test/mcp/tool/response_test.rb | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/mcp/tool/response.rb b/lib/mcp/tool/response.rb index 51d827d..af3ec8f 100644 --- a/lib/mcp/tool/response.rb +++ b/lib/mcp/tool/response.rb @@ -13,7 +13,7 @@ def initialize(content = nil, deprecated_error = NOT_GIVEN, error: false, struct error = deprecated_error end - @content = content + @content = content || [] @error = error @structured_content = structured_content end diff --git a/test/mcp/tool/response_test.rb b/test/mcp/tool/response_test.rb index 17a6ea9..5fd70a8 100644 --- a/test/mcp/tool/response_test.rb +++ b/test/mcp/tool/response_test.rb @@ -101,13 +101,24 @@ class ResponseTest < ActiveSupport::TestCase 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 [:isError, :structuredContent], actual.keys - assert_nil actual[:content] + assert_equal [:content, :isError, :structuredContent], actual.keys + assert_empty actual[:content] assert_equal structured_content, actual[:structuredContent] refute actual[:isError] end