Class: RubyLLM::MCP::Resource
- Inherits:
-
Object
- Object
- RubyLLM::MCP::Resource
- Defined in:
- lib/ruby_llm/mcp/resource.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#apps_metadata ⇒ Object
readonly
Returns the value of attribute apps_metadata.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#mime_type ⇒ Object
readonly
Returns the value of attribute mime_type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subscribed ⇒ Object
readonly
Returns the value of attribute subscribed.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #content ⇒ Object
- #content_loaded? ⇒ Boolean
- #include(chat, **args) ⇒ Object
-
#initialize(adapter, resource) ⇒ Resource
constructor
A new instance of Resource.
- #reset_content! ⇒ Object
- #subscribe! ⇒ Object
- #to_content ⇒ Object
- #to_h ⇒ Object (also: #to_json)
- #unsubscribe! ⇒ Object
Constructor Details
#initialize(adapter, resource) ⇒ Resource
Returns a new instance of Resource.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ruby_llm/mcp/resource.rb', line 8 def initialize(adapter, resource) @adapter = adapter @uri = resource["uri"] @name = resource["name"] @description = resource["description"] @mime_type = resource["mimeType"] @apps_metadata = Extensions::Apps::ResourceMetadata.new(resource[Extensions::Apps::Constants::META_KEY]) if resource.key?("content_response") @content_response = resource["content_response"] @content = @content_response["text"] || @content_response["blob"] end @subscribed = false end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
6 7 8 |
# File 'lib/ruby_llm/mcp/resource.rb', line 6 def adapter @adapter end |
#apps_metadata ⇒ Object (readonly)
Returns the value of attribute apps_metadata.
6 7 8 |
# File 'lib/ruby_llm/mcp/resource.rb', line 6 def @apps_metadata end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
6 7 8 |
# File 'lib/ruby_llm/mcp/resource.rb', line 6 def description @description end |
#mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type.
6 7 8 |
# File 'lib/ruby_llm/mcp/resource.rb', line 6 def mime_type @mime_type end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/ruby_llm/mcp/resource.rb', line 6 def name @name end |
#subscribed ⇒ Object (readonly)
Returns the value of attribute subscribed.
6 7 8 |
# File 'lib/ruby_llm/mcp/resource.rb', line 6 def subscribed @subscribed end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
6 7 8 |
# File 'lib/ruby_llm/mcp/resource.rb', line 6 def uri @uri end |
Instance Method Details
#content ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby_llm/mcp/resource.rb', line 23 def content return @content unless @content.nil? result = read_response result.raise_error! if result.error? @content_response = result.value.dig("contents", 0) @content = @content_response["text"] || @content_response["blob"] end |
#content_loaded? ⇒ Boolean
33 34 35 |
# File 'lib/ruby_llm/mcp/resource.rb', line 33 def content_loaded? !@content.nil? end |
#include(chat, **args) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/ruby_llm/mcp/resource.rb', line 62 def include(chat, **args) = RubyLLM::Message.new( role: "user", content: to_content(**args) ) chat.() end |
#reset_content! ⇒ Object
57 58 59 60 |
# File 'lib/ruby_llm/mcp/resource.rb', line 57 def reset_content! @content = nil @content_response = nil end |
#subscribe! ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/ruby_llm/mcp/resource.rb', line 37 def subscribe! if @adapter.capabilities.resource_subscribe? @adapter.resources_subscribe(uri: @uri) @subscribed = true else = "Resource subscribe is not available for this MCP server" raise Errors::Capabilities::ResourceSubscribeNotAvailable.new(message: ) end end |
#to_content ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ruby_llm/mcp/resource.rb', line 71 def to_content content = self.content case content_type when "text" MCP::Content.new(text: "#{name}: #{description}\n\n#{content}") when "blob" = MCP::Attachment.new(content, mime_type) MCP::Content.new(text: "#{name}: #{description}", attachments: []) end end |
#to_h ⇒ Object Also known as: to_json
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby_llm/mcp/resource.rb', line 82 def to_h { uri: @uri, name: @name, description: @description, mime_type: @mime_type, contented_loaded: content_loaded?, content: @content } end |
#unsubscribe! ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/ruby_llm/mcp/resource.rb', line 47 def unsubscribe! if @adapter.capabilities.resource_subscribe? @adapter.resources_unsubscribe(uri: @uri) @subscribed = false else = "Resource unsubscribe is not available for this MCP server" raise Errors::Capabilities::ResourceSubscribeNotAvailable.new(message: ) end end |