Class: McpOnRuby::Resource
- Inherits:
-
Object
- Object
- McpOnRuby::Resource
- Defined in:
- lib/mcp_on_ruby/resource.rb
Overview
Base class for MCP resources - data sources that AI can read
Direct Known Subclasses
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#mime_type ⇒ Object
readonly
Returns the value of attribute mime_type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#authorized?(context = {}) ⇒ Boolean
Check if resource is authorized for the given context.
-
#initialize(uri:, name: nil, description: '', mime_type: 'application/json', metadata: {}, tags: []) ⇒ Resource
constructor
Create a new resource.
-
#read(params = {}, context = {}) ⇒ Hash
Read the resource content with given parameters.
-
#template? ⇒ Boolean
Check if this resource is a template (contains param placeholders).
-
#template_params ⇒ Array<String>
Extract parameter names from template URI.
-
#to_schema ⇒ Hash
Get the resource schema for MCP protocol.
Constructor Details
#initialize(uri:, name: nil, description: '', mime_type: 'application/json', metadata: {}, tags: []) ⇒ Resource
Create a new resource
15 16 17 18 19 20 21 22 |
# File 'lib/mcp_on_ruby/resource.rb', line 15 def initialize(uri:, name: nil, description: '', mime_type: 'application/json', metadata: {}, tags: []) @uri = uri.to_s @name = name @description = description @mime_type = mime_type @metadata = @tags = Array() end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
6 7 8 |
# File 'lib/mcp_on_ruby/resource.rb', line 6 def description @description end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
6 7 8 |
# File 'lib/mcp_on_ruby/resource.rb', line 6 def @metadata end |
#mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type.
6 7 8 |
# File 'lib/mcp_on_ruby/resource.rb', line 6 def mime_type @mime_type end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/mcp_on_ruby/resource.rb', line 6 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
6 7 8 |
# File 'lib/mcp_on_ruby/resource.rb', line 6 def @tags end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
6 7 8 |
# File 'lib/mcp_on_ruby/resource.rb', line 6 def uri @uri end |
Instance Method Details
#authorized?(context = {}) ⇒ Boolean
Check if resource is authorized for the given context
91 92 93 94 95 96 97 98 |
# File 'lib/mcp_on_ruby/resource.rb', line 91 def (context = {}) return true unless respond_to?(:authorize, true) (context) rescue => error McpOnRuby.logger.warn("Authorization check failed for resource '#{uri}': #{error.}") false end |
#read(params = {}, context = {}) ⇒ Hash
Read the resource content with given parameters
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mcp_on_ruby/resource.rb', line 28 def read(params = {}, context = {}) # Validate parameters if this is a template validate_template_params!(params) if template? # Get the content content = fetch_content(params, context) # Wrap in MCP resource format { contents: [ { uri: resolve_uri(params), mimeType: mime_type, text: serialize_content(content) } ] } rescue => error McpOnRuby.logger.error("Resource '#{uri}' read failed: #{error.}") McpOnRuby.logger.error(error.backtrace.join("\n")) { error: { code: -32603, message: "Resource read failed: #{error.}", data: { uri: uri, error_type: error.class.name } } } end |
#template? ⇒ Boolean
Check if this resource is a template (contains param placeholders)
76 77 78 |
# File 'lib/mcp_on_ruby/resource.rb', line 76 def template? uri.include?('{') && uri.include?('}') end |
#template_params ⇒ Array<String>
Extract parameter names from template URI
82 83 84 85 86 |
# File 'lib/mcp_on_ruby/resource.rb', line 82 def template_params return [] unless template? uri.scan(/\{([^}]+)\}/).flatten end |
#to_schema ⇒ Hash
Get the resource schema for MCP protocol
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mcp_on_ruby/resource.rb', line 60 def to_schema schema = { uri: uri, mimeType: mime_type } schema[:name] = name if name schema[:description] = description unless description.empty? schema[:metadata] = unless .empty? schema[:tags] = unless .empty? schema end |