From 05c0119e53b6484115991eb330dba3cd915baa94 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 26 Jan 2026 01:17:04 +0900 Subject: [PATCH] Fix `Resource::Embedded` not setting `@resource` in `initialize` The `initialize` method was missing the assignment of `@resource`, causing the resource method to always return `nil` and `to_h` to return an incorrect hash with an empty resource value. Added comprehensive tests to verify the fix and prevent regression. --- lib/mcp/resource/embedded.rb | 1 + test/mcp/resource/embedded_test.rb | 107 +++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 test/mcp/resource/embedded_test.rb diff --git a/lib/mcp/resource/embedded.rb b/lib/mcp/resource/embedded.rb index 9df04c65..df94fc43 100644 --- a/lib/mcp/resource/embedded.rb +++ b/lib/mcp/resource/embedded.rb @@ -6,6 +6,7 @@ class Embedded attr_reader :resource, :annotations def initialize(resource:, annotations: nil) + @resource = resource @annotations = annotations end diff --git a/test/mcp/resource/embedded_test.rb b/test/mcp/resource/embedded_test.rb new file mode 100644 index 00000000..2967de93 --- /dev/null +++ b/test/mcp/resource/embedded_test.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +require "test_helper" + +module MCP + class Resource + class EmbeddedTest < ActiveSupport::TestCase + test "initializes with resource" do + resource = Resource.new( + uri: "file:///test.txt", + name: "test_resource", + description: "a test resource", + ) + embedded = Resource::Embedded.new(resource: resource) + + assert_equal resource, embedded.resource + end + + test "initializes with resource and annotations" do + resource = Resource.new( + uri: "file:///test.txt", + name: "test_resource", + description: "a test resource", + ) + annotations = { audience: ["user"], priority: 1.0 } + embedded = Resource::Embedded.new(resource: resource, annotations: annotations) + + assert_equal resource, embedded.resource + assert_equal annotations, embedded.annotations + end + + test "initializes with annotations as nil when not provided" do + resource = Resource.new( + uri: "file:///test.txt", + name: "test_resource", + description: "a test resource", + ) + embedded = Resource::Embedded.new(resource: resource) + + assert_nil embedded.annotations + end + + test "#to_h returns hash with resource data when annotations is nil" do + resource = Resource.new( + uri: "file:///test.txt", + name: "test_resource", + description: "a test resource", + ) + embedded = Resource::Embedded.new(resource: resource) + + expected = { + resource: { + uri: "file:///test.txt", + name: "test_resource", + description: "a test resource", + }, + } + + assert_equal expected, embedded.to_h + end + + test "#to_h returns hash with resource and annotations when both are present" do + resource = Resource.new( + uri: "file:///test.txt", + name: "test_resource", + description: "a test resource", + ) + annotations = { audience: ["user"], priority: 1.0 } + embedded = Resource::Embedded.new(resource: resource, annotations: annotations) + + expected = { + resource: { + uri: "file:///test.txt", + name: "test_resource", + description: "a test resource", + }, + annotations: { audience: ["user"], priority: 1.0 }, + } + + assert_equal expected, embedded.to_h + end + + test "#to_h handles resource with all optional fields" do + resource = Resource.new( + uri: "file:///test.txt", + name: "test_resource", + title: "Test Resource", + description: "a test resource", + mime_type: "text/plain", + ) + embedded = Resource::Embedded.new(resource: resource) + + expected = { + resource: { + uri: "file:///test.txt", + name: "test_resource", + title: "Test Resource", + description: "a test resource", + mimeType: "text/plain", + }, + } + + assert_equal expected, embedded.to_h + end + end + end +end