## Motivation and Context The MCP specification defines an optional `annotations` field on both the `Resource` and `ResourceTemplate` types, but the Ruby SDK is the only official SDK that does not wire it up — TypeScript, Python, and PHP all implement it. https://modelcontextprotocol.io/specification/2025-11-25/server/resources#annotations The `MCP::Annotations` class already exists; this commit connects it. The field accepts an `MCP::Annotations` instance rather than a raw hash, for consistency with other typed fields and to get `lastModified` key normalization for free. ## How Has This Been Tested? Added tests in `test/mcp/resource_test.rb` and `test/mcp/resource_template_test.rb`: - `#to_h` omits `annotations` when nil - `#to_h` includes `annotations` when present (also asserting that `last_modified` is serialized as `lastModified`) The full unit suite and RuboCop pass locally. ## Breaking Changes None. `annotations:` is a new optional keyword argument defaulting to `nil`, and it is omitted from `#to_h` output when not set.
36 lines
920 B
Ruby
36 lines
920 B
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "resource/contents"
|
|
require_relative "resource/embedded"
|
|
|
|
module MCP
|
|
class Resource
|
|
attr_reader :uri, :name, :title, :description, :icons, :mime_type, :annotations, :size, :meta
|
|
|
|
def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil, annotations: nil, size: nil, meta: nil)
|
|
@uri = uri
|
|
@name = name
|
|
@title = title
|
|
@description = description
|
|
@icons = icons
|
|
@mime_type = mime_type
|
|
@annotations = annotations
|
|
@size = size
|
|
@meta = meta
|
|
end
|
|
|
|
def to_h
|
|
{
|
|
uri: uri,
|
|
name: name,
|
|
title: title,
|
|
description: description,
|
|
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
|
|
mimeType: mime_type,
|
|
annotations: annotations&.to_h,
|
|
size: size,
|
|
_meta: meta,
|
|
}.compact
|
|
end
|
|
end
|
|
end
|