Class: FastMcp::Resource

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Resource

Initialize with instance variables

Parameters:

  • params (Hash) (defaults to: {})

    The parameters for this resource instance



161
162
163
# File 'lib/mcp/resource.rb', line 161

def initialize(params = {})
  @params = params
end

Class Attribute Details

.serverObject

Returns the value of attribute server.



13
14
15
# File 'lib/mcp/resource.rb', line 13

def server
  @server
end

Instance Attribute Details

#paramsHash (readonly)

Get parameters from the URI template

Returns:

  • (Hash)

    The parameters extracted from the URI



191
192
193
# File 'lib/mcp/resource.rb', line 191

def params
  @params
end

Class Method Details

.addressable_templateAddressable::Template

Get the Addressable::Template for this resource

Returns:

  • (Addressable::Template)

    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

Parameters:

  • value (String, nil) (defaults to: nil)

    The description for this resource

Returns:

  • (String)

    The 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)

Parameters:

  • file_path (String)

    Path to the file

Returns:

  • (Resource)

    New resource instance with content loaded from file



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

Parameters:

  • uri (String)

    The URI to initialize from

Returns:

  • (Resource)

    A new resource instance



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

Parameters:

  • uri (String)

    The URI to match

Returns:

  • (Addressable::Template::MatchData, nil)

    The match data if the URI matches, nil otherwise



58
59
60
# File 'lib/mcp/resource.rb', line 58

def match(uri)
  addressable_template.match(uri)
end

.metadataHash

Get the resource metadata (without content)

Returns:

  • (Hash)

    Resource metadata



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

Parameters:

  • value (String, nil) (defaults to: nil)

    The MIME type for this resource

Returns:

  • (String)

    The 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

.nameObject



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

Returns:

  • (Boolean)

    true if the URI does not contain template parameters



51
52
53
# File 'lib/mcp/resource.rb', line 51

def non_templated?
  !templated?
end

.original_nameObject



84
# File 'lib/mcp/resource.rb', line 84

alias original_name name

.params_from_uri(uri) ⇒ Hash

Get the parameters from the given URI

Parameters:

  • uri (String)

    The URI to get the parameters from

Returns:

  • (Hash)

    The parameters from the 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

Parameters:

  • value (String, nil) (defaults to: nil)

    The name for this resource

Returns:

  • (String)

    The 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_variablesArray

Get the template variables for this resource

Returns:

  • (Array)

    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

Returns:

  • (Boolean)

    true if the URI contains template parameters



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

Parameters:

  • value (String, nil) (defaults to: nil)

    The URI for this resource

Returns:

  • (String)

    The 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

Parameters:

  • params (Hash) (defaults to: {})

    The parameters to variabilize the URI with

Returns:

  • (String)

    The variabilized URI



27
28
29
# File 'lib/mcp/resource.rb', line 27

def variabilized_uri(params = {})
  addressable_template.partial_expand(params).pattern
end

Instance Method Details

#binary?Boolean

Check if the resource is binary

Returns:

  • (Boolean)

    true if the resource is binary, false otherwise



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

#contentString?

Method to be overridden by subclasses to dynamically generate content

Returns:

  • (String, nil)

    Generated content for this resource

Raises:

  • (NotImplementedError)


195
196
197
# File 'lib/mcp/resource.rb', line 195

def content
  raise NotImplementedError, 'Subclasses must implement content'
end

#descriptionString?

Description of the resource - delegates to class method

Returns:

  • (String, nil)

    The description for this resource



179
180
181
# File 'lib/mcp/resource.rb', line 179

def description
  self.class.description
end

#mime_typeString?

MIME type of the resource - delegates to class method

Returns:

  • (String, nil)

    The MIME type for this resource



185
186
187
# File 'lib/mcp/resource.rb', line 185

def mime_type
  self.class.mime_type
end

#nameString?

Name of the resource - delegates to class method

Returns:

  • (String, nil)

    The name for this resource



173
174
175
# File 'lib/mcp/resource.rb', line 173

def name
  self.class.resource_name
end

#uriString?

URI of the resource - delegates to class method

Returns:

  • (String, nil)

    The URI for this resource



167
168
169
# File 'lib/mcp/resource.rb', line 167

def uri
  self.class.uri
end