Add _meta field to resource, content, and result classes
## Motivation and Context MCP 2025-11-25 spec defines a `_meta` general field on many types, but the Ruby SDK only exposed it on `Tool` and `Prompt`. To attach metadata (e.g. MCP Apps CSP hints on resource contents), consumers had to subclass existing classes and override `to_h`. Add a `meta:` keyword argument and `_meta` serialization to: - `Resource::Contents`, `Resource::TextContents`, `Resource::BlobContents` - `Resource`, `ResourceTemplate` - `Content::Text`, `Content::Image`, `Content::Audio`, `Content::EmbeddedResource` - `Tool::Response` (`CallToolResult`) - `Prompt::Result` (`GetPromptResult`) This aligns the Ruby SDK with the Python and TypeScript SDKs, which both expose `_meta` on these types. Resolves #308. ## How Has This Been Tested? Added unit tests for each updated class covering two patterns: `_meta` present (asserts the value is reflected in `to_h[:_meta]`) and `_meta` omitted (asserts no `:_meta` key in `to_h`, i.e., `.compact` behavior). All existing tests continue to pass, and `rake rubocop` is clean. ## Breaking Changes None. All new `meta:` parameters are optional keyword arguments defaulting to `nil`. The `to_h` output stays backward-compatible because `nil` values are removed by the existing `.compact` call, so existing callers require no changes.
This commit is contained in:
parent
a05d941edd
commit
0ad9a89d97
1 changed files with 269 additions and 29 deletions
|
|
@ -5,9 +5,9 @@ module MCP
|
|||
class Response
|
||||
NOT_GIVEN = Object.new.freeze
|
||||
|
||||
attr_reader :content, :structured_content
|
||||
attr_reader :content, :structured_content, :meta
|
||||
|
||||
def initialize(content = nil, deprecated_error = NOT_GIVEN, error: false, structured_content: nil)
|
||||
def initialize(content = nil, deprecated_error = NOT_GIVEN, error: false, structured_content: nil, meta: nil)
|
||||
if deprecated_error != NOT_GIVEN
|
||||
warn("Passing `error` with the 2nd argument of `Response.new` is deprecated. Use keyword argument like `Response.new(content, error: error)` instead.", uplevel: 1)
|
||||
error = deprecated_error
|
||||
|
|
@ -16,6 +16,7 @@ module MCP
|
|||
@content = content || []
|
||||
@error = error
|
||||
@structured_content = structured_content
|
||||
@meta = meta
|
||||
end
|
||||
|
||||
def error?
|
||||
|
|
@ -23,7 +24,7 @@ module MCP
|
|||
end
|
||||
|
||||
def to_h
|
||||
{ content: content, isError: error?, structuredContent: @structured_content }.compact
|
||||
{ content: content, isError: error?, structuredContent: @structured_content, _meta: meta }.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue