ruby-sdk/lib/mcp/resource.rb
Koichi ITO bd416c7a5f Omit icons from responses when empty or nil
Previously, the `icons` parameter was always included in responses (as an empty array)
even when not configured. This differs from the response representation in MCP Ruby SDK 0.4.0.

Since `icons` is optional, it should be omitted from responses when unused to reduce
unnecessary context window usage.
https://modelcontextprotocol.io/specification/2025-11-25/schema#implementation

This fix applies to `Tool#to_h`, `Prompt#to_h`, `Resource#to_h`, `ResourceTemplate#to_h`,
and `Server#server_info`.

It doesn't seem critical, but if there are no other issues, it would be good to cut 0.5.1 bugfix release soon.
2026-01-12 09:46:45 +09:00

27 lines
625 B
Ruby

# frozen_string_literal: true
module MCP
class Resource
attr_reader :uri, :name, :title, :description, :icons, :mime_type
def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil)
@uri = uri
@name = name
@title = title
@description = description
@icons = icons
@mime_type = mime_type
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,
}.compact
end
end
end