Skip to content

Commit 91ff179

Browse files
committed
Add size field to MCP::Resource per MCP specification
## Motivation and Context The MCP specification defines an optional `size` field on the `Resource` type, added in spec revision 2025-06-18 and retained in 2025-11-25: https://modelcontextprotocol.io/specification/2025-11-25/server/resources > The size of the raw resource content, in bytes (i.e., before base64 > encoding or any tokenization), if known. This can be used by Hosts to > display file sizes and estimate context window usage. `MCP::Resource` was missing this field, so servers had no way to declare a resource's byte size. Without it, hosts cannot show file sizes or estimate context window usage before reading the resource contents. This aligns the Ruby SDK with the spec, matching the TypeScript SDK (modelcontextprotocol/typescript-sdk#1574). ## How Has This Been Tested? Added tests in `test/mcp/resource_test.rb`: - `#to_h` omits `size` when nil - `#to_h` includes `size` when present - `#to_h` includes `size` when zero (distinguishes an unset size from a 0-byte resource) The full unit suite and RuboCop pass locally. ## Breaking Changes None. `size:` is a new optional keyword argument defaulting to `nil`, and it is omitted from `#to_h` output when not set, so existing callers are unaffected.
1 parent cf44475 commit 91ff179

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

lib/mcp/resource.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
module MCP
77
class Resource
8-
attr_reader :uri, :name, :title, :description, :icons, :mime_type, :meta
8+
attr_reader :uri, :name, :title, :description, :icons, :mime_type, :size, :meta
99

10-
def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil, meta: nil)
10+
def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil, size: nil, meta: nil)
1111
@uri = uri
1212
@name = name
1313
@title = title
1414
@description = description
1515
@icons = icons
1616
@mime_type = mime_type
17+
@size = size
1718
@meta = meta
1819
end
1920

@@ -25,6 +26,7 @@ def to_h
2526
description: description,
2627
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
2728
mimeType: mime_type,
29+
size: size,
2830
_meta: meta,
2931
}.compact
3032
end

test/mcp/resource_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,23 @@ class ResourceTest < ActiveSupport::TestCase
4949

5050
assert_equal meta, resource.to_h[:_meta]
5151
end
52+
53+
test "#to_h omits size when nil" do
54+
resource = Resource.new(uri: "file:///test.txt", name: "resource_without_size")
55+
56+
refute resource.to_h.key?(:size)
57+
end
58+
59+
test "#to_h includes size when present" do
60+
resource = Resource.new(uri: "file:///test.txt", name: "resource_with_size", size: 12_345)
61+
62+
assert_equal 12_345, resource.to_h[:size]
63+
end
64+
65+
test "#to_h includes size when zero" do
66+
resource = Resource.new(uri: "file:///empty.txt", name: "empty_resource", size: 0)
67+
68+
assert_equal 0, resource.to_h[:size]
69+
end
5270
end
5371
end

0 commit comments

Comments
 (0)