class RubyLLM::MCP::Resource
A resource on an MCP server: a file, a record, or any data the server names with a URI. Pass one to a chat like any attachment, or save it.
readme = files.resource("file:///project/README.md") readme.content # => "# My Project..." readme.save("README.md") chat.ask "Summarize this", with: readme
Resources from MCP#resources are read from the server the first time their content is needed.
Attributes
What the resource is, or nil.
The MIME type the server reports, or nil.
The resource’s name.
A human-readable title, or nil.
The URI that identifies the resource on its server.
Public Instance Methods
Source
# File lib/ruby_llm/mcp/resource.rb, line 45 def content @content ||= if @data.key?('text') then @data['text'] elsif @data.key?('blob') then Base64.decode64(@data['blob']) else @mcp.resource(uri).content end end
Returns the resource’s content: text for text resources, bytes for binary ones.
Source
# File lib/ruby_llm/mcp/resource.rb, line 58 def save(path) File.binwrite(File.expand_path(path), to_blob) path end
Writes the content to path, expanding it first. Returns path.
Source
# File lib/ruby_llm/mcp/resource.rb, line 65 def to_attachment filename = Content.filename(uri) Attachment.new(StringIO.new(to_blob), filename: filename.empty? ? name : filename) end
Returns the resource as an Attachment, which is how chats take it through with:.
Source
# File lib/ruby_llm/mcp/resource.rb, line 53 def to_blob content.b end
Returns the content as a String of bytes.