Class: FastMcp::Resource
- Inherits:
-
Object
- Object
- FastMcp::Resource
- Defined in:
- lib/mcp/resource.rb
Overview
Resource class for MCP Resources feature Represents a resource that can be exposed to clients
Class Attribute Summary collapse
-
.server ⇒ Object
Returns the value of attribute server.
Instance Attribute Summary collapse
-
#params ⇒ Hash
readonly
Get parameters from the URI template.
Class Method Summary collapse
-
.addressable_template ⇒ Addressable::Template
Get the Addressable::Template for this resource.
-
.description(value = nil) ⇒ String
Define description for this resource.
-
.from_file(file_path, name: nil, description: nil) ⇒ Resource
Load content from a file (class method).
-
.initialize_from_uri(uri) ⇒ Resource
Initialize a new instance from the given URI.
-
.match(uri) ⇒ Addressable::Template::MatchData?
Match the given URI against the resource’s addressable template.
-
.metadata ⇒ Hash
Get the resource metadata (without content).
-
.mime_type(value = nil) ⇒ String
Define MIME type for this resource.
- .name ⇒ Object
-
.non_templated? ⇒ Boolean
Check if this resource has a non-templated URI.
- .original_name ⇒ Object
-
.params_from_uri(uri) ⇒ Hash
Get the parameters from the given URI.
-
.resource_name(value = nil) ⇒ String
Define name for this resource.
-
.template_variables ⇒ Array
Get the template variables for this resource.
-
.templated? ⇒ Boolean
Check if this resource has a templated URI.
-
.uri(value = nil) ⇒ String
Define URI for this resource.
-
.variabilized_uri(params = {}) ⇒ String
Variabilize the URI with the given params.
Instance Method Summary collapse
-
#binary? ⇒ Boolean
Check if the resource is binary.
-
#content ⇒ String?
Method to be overridden by subclasses to dynamically generate content.
-
#description ⇒ String?
Description of the resource - delegates to class method.
-
#initialize(params = {}) ⇒ Resource
constructor
Initialize with instance variables.
-
#mime_type ⇒ String?
MIME type of the resource - delegates to class method.
-
#name ⇒ String?
Name of the resource - delegates to class method.
-
#uri ⇒ String?
URI of the resource - delegates to class method.
Constructor Details
#initialize(params = {}) ⇒ Resource
Initialize with instance variables
161 162 163 |
# File 'lib/mcp/resource.rb', line 161 def initialize(params = {}) @params = params end |
Class Attribute Details
.server ⇒ Object
Returns the value of attribute server.
13 14 15 |
# File 'lib/mcp/resource.rb', line 13 def server @server end |
Instance Attribute Details
#params ⇒ Hash (readonly)
Get parameters from the URI template
191 192 193 |
# File 'lib/mcp/resource.rb', line 191 def params @params end |
Class Method Details
.addressable_template ⇒ Addressable::Template
Get the Addressable::Template for this resource
33 34 35 |
# File 'lib/mcp/resource.rb', line 33 def addressable_template @addressable_template ||= Addressable::Template.new(uri) end |
.description(value = nil) ⇒ String
Define description for this resource
94 95 96 97 |
# File 'lib/mcp/resource.rb', line 94 def description(value = nil) @description = value if value @description || (superclass.respond_to?(:description) ? superclass.description : nil) end |
.from_file(file_path, name: nil, description: nil) ⇒ Resource
Load content from a file (class method)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/mcp/resource.rb', line 130 def from_file(file_path, name: nil, description: nil) file_uri = "file://#{File.absolute_path(file_path)}" file_name = name || File.basename(file_path) # Create a resource subclass on the fly Class.new(self) do uri file_uri resource_name file_name description description if description # Auto-detect mime type extension = File.extname(file_path) unless extension.empty? detected_types = MIME::Types.type_for(extension) mime_type detected_types.first.to_s unless detected_types.empty? end # Override content method to load from file define_method :content do if binary? File.binread(file_path) else File.read(file_path) end end end end |
.initialize_from_uri(uri) ⇒ Resource
Initialize a new instance from the given URI
65 66 67 |
# File 'lib/mcp/resource.rb', line 65 def initialize_from_uri(uri) new(params_from_uri(uri)) end |
.match(uri) ⇒ Addressable::Template::MatchData?
Match the given URI against the resource’s addressable template
58 59 60 |
# File 'lib/mcp/resource.rb', line 58 def match(uri) addressable_template.match(uri) end |
.metadata ⇒ Hash
Get the resource metadata (without content)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mcp/resource.rb', line 109 def if templated? { uriTemplate: uri, name: resource_name, description: description, mimeType: mime_type }.compact else { uri: uri, name: resource_name, description: description, mimeType: mime_type }.compact end end |
.mime_type(value = nil) ⇒ String
Define MIME type for this resource
102 103 104 105 |
# File 'lib/mcp/resource.rb', line 102 def mime_type(value = nil) @mime_type = value if value @mime_type || (superclass.respond_to?(:mime_type) ? superclass.mime_type : nil) end |
.name ⇒ Object
85 86 87 88 89 |
# File 'lib/mcp/resource.rb', line 85 def name return resource_name if resource_name original_name end |
.non_templated? ⇒ Boolean
Check if this resource has a non-templated URI
51 52 53 |
# File 'lib/mcp/resource.rb', line 51 def non_templated? !templated? end |
.original_name ⇒ Object
84 |
# File 'lib/mcp/resource.rb', line 84 alias original_name name |
.params_from_uri(uri) ⇒ Hash
Get the parameters from the given URI
72 73 74 |
# File 'lib/mcp/resource.rb', line 72 def params_from_uri(uri) match(uri).mapping.transform_keys(&:to_sym) end |
.resource_name(value = nil) ⇒ String
Define name for this resource
79 80 81 82 |
# File 'lib/mcp/resource.rb', line 79 def resource_name(value = nil) @name = value if value @name || (superclass.respond_to?(:resource_name) ? superclass.resource_name : nil) end |
.template_variables ⇒ Array
Get the template variables for this resource
39 40 41 |
# File 'lib/mcp/resource.rb', line 39 def template_variables addressable_template.variables end |
.templated? ⇒ Boolean
Check if this resource has a templated URI
45 46 47 |
# File 'lib/mcp/resource.rb', line 45 def templated? template_variables.any? end |
.uri(value = nil) ⇒ String
Define URI for this resource
18 19 20 21 22 |
# File 'lib/mcp/resource.rb', line 18 def uri(value = nil) @uri = value if value @uri || (superclass.respond_to?(:uri) ? superclass.uri : nil) end |
.variabilized_uri(params = {}) ⇒ String
Variabilize the URI with the given params
27 28 29 |
# File 'lib/mcp/resource.rb', line 27 def variabilized_uri(params = {}) addressable_template.(params).pattern end |
Instance Method Details
#binary? ⇒ Boolean
Check if the resource is binary
201 202 203 204 205 206 207 208 |
# File 'lib/mcp/resource.rb', line 201 def binary? return false if mime_type.nil? !(mime_type.start_with?('text/') || mime_type == 'application/json' || mime_type == 'application/xml' || mime_type == 'application/javascript') end |
#content ⇒ String?
Method to be overridden by subclasses to dynamically generate content
195 196 197 |
# File 'lib/mcp/resource.rb', line 195 def content raise NotImplementedError, 'Subclasses must implement content' end |
#description ⇒ String?
Description of the resource - delegates to class method
179 180 181 |
# File 'lib/mcp/resource.rb', line 179 def description self.class.description end |
#mime_type ⇒ String?
MIME type of the resource - delegates to class method
185 186 187 |
# File 'lib/mcp/resource.rb', line 185 def mime_type self.class.mime_type end |
#name ⇒ String?
Name of the resource - delegates to class method
173 174 175 |
# File 'lib/mcp/resource.rb', line 173 def name self.class.resource_name end |
#uri ⇒ String?
URI of the resource - delegates to class method
167 168 169 |
# File 'lib/mcp/resource.rb', line 167 def uri self.class.uri end |