Class: MCP::Icon

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/icon.rb

Overview

An icon attached to a server, tool, prompt, or resource, per the specification's Icon type: https://modelcontextprotocol.io/specification/2026-07-28/basic#icons

Each argument is checked against the schema's type, as the TypeScript and Python SDKs check theirs, so an icon that would serialize into something a client rejects fails here instead. What a value means is not judged: src may be any non-empty String (the schema types it as a URI, which an empty String is not, and the specification allows an HTTP/HTTPS URL or a data: URI), and a size may be any String (the specification expects WxH or "any").

Wherever an Icon is accepted (Server.new(icons:) and the icons of a tool, prompt, resource, or resource template), a Hash is accepted too and converted through Icon.from, so it is checked the same way.

Constant Summary collapse

SUPPORTED_THEMES =
["light", "dark"].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mime_type: nil, sizes: nil, src:, theme: nil) ⇒ Icon

Returns a new instance of Icon.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mcp/icon.rb', line 78

def initialize(mime_type: nil, sizes: nil, src:, theme: nil)
  unless src.is_a?(String) && !src.empty?
    raise ArgumentError, "The value of src must be a non-empty String (got #{src.class})."
  end

  unless mime_type.nil? || mime_type.is_a?(String)
    raise ArgumentError, "The value of mime_type must be a String (got #{mime_type.class})."
  end

  if (problem = sizes_problem(sizes))
    raise ArgumentError, "The value of sizes must be an Array of Strings such as [\"48x48\"] or [\"any\"] (#{problem})."
  end

  unless theme.nil? || SUPPORTED_THEMES.include?(theme)
    raise ArgumentError, 'The value of theme must specify "light" or "dark".'
  end

  @mime_type = mime_type
  @sizes = sizes
  @src = src
  @theme = theme
end

Instance Attribute Details

#mime_type ⇒ Object (readonly)

Returns the value of attribute mime_type.



76
77
78
# File 'lib/mcp/icon.rb', line 76

def mime_type
  @mime_type
end

#sizes ⇒ Object (readonly)

Returns the value of attribute sizes.



76
77
78
# File 'lib/mcp/icon.rb', line 76

def sizes
  @sizes
end

#src ⇒ Object (readonly)

Returns the value of attribute src.



76
77
78
# File 'lib/mcp/icon.rb', line 76

def src
  @src
end

#theme ⇒ Object (readonly)

Returns the value of attribute theme.



76
77
78
# File 'lib/mcp/icon.rb', line 76

def theme
  @theme
end

Class Method Details

.from(value) ⇒ Object

Returns value when it is already an Icon, builds one from a Hash, and refuses anything else.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mcp/icon.rb', line 31

def from(value)
  return value if value.is_a?(Icon)

  unless value.is_a?(Hash)
    raise ArgumentError, "An icon must be an MCP::Icon or a Hash (got #{value.class})."
  end

  keywords = {}
  value.each do |key, member|
    unless key.is_a?(Symbol) || key.is_a?(String)
      raise ArgumentError, "An icon Hash key must be a Symbol or a String (got #{key.class})."
    end

    keyword = HASH_KEYWORDS[key.to_s]
    unless keyword
      raise ArgumentError, "An icon Hash may only hold src, mimeType (or mime_type), sizes, and theme (got #{key.inspect})."
    end
    raise ArgumentError, "An icon Hash gives #{keyword} twice." if keywords.key?(keyword)

    keywords[keyword] = member
  end

  new(**keywords)
end

.from_list(value) ⇒ Object

Converts the icons argument of a server, tool, prompt, resource, or resource template: nil stays nil, each element of an Array goes through from into a frozen Array, and anything else is refused. The frozen Array keeps a later << on a reader from adding an element these checks never saw.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mcp/icon.rb', line 59

def from_list(value)
  return if value.nil?

  unless value.is_a?(Array)
    raise ArgumentError, "icons must be nil or an Array of MCP::Icon or Hash (got #{value.class})."
  end

  icons = value.each_with_index.map do |icon, index|
    from(icon)
  rescue ArgumentError => e
    raise ArgumentError, "icons[#{index}]: #{e.message}"
  end

  icons.freeze
end

Instance Method Details

#to_h ⇒ Object



101
102
103
# File 'lib/mcp/icon.rb', line 101

def to_h
  { mimeType: mime_type, sizes: sizes, src: src, theme: theme }.compact
end