Class: VectorMCP::Definitions::Root

Inherits:
Struct
  • Object
show all
Defined in:
lib/vector_mcp/definitions.rb

Overview

Represents an MCP root definition. Roots define filesystem boundaries where servers can operate.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



220
221
222
# File 'lib/vector_mcp/definitions.rb', line 220

def name
  @name
end

#uriObject

Returns the value of attribute uri

Returns:

  • (Object)

    the current value of uri



220
221
222
# File 'lib/vector_mcp/definitions.rb', line 220

def uri
  @uri
end

Class Method Details

.from_path(path, name: nil) ⇒ Root

Class method to create a root from a local directory path.

Parameters:

  • path (String)

    Local filesystem path to the directory.

  • name (String) (defaults to: nil)

    Human-readable name for the root.

Returns:

  • (Root)

    A new Root instance.

Raises:

  • (ArgumentError)

    If the path is invalid or not accessible.



264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/vector_mcp/definitions.rb', line 264

def self.from_path(path, name: nil)
  # Expand path to get absolute path and resolve any relative components
  expanded_path = File.expand_path(path)

  # Create file:// URI
  uri = "file://#{expanded_path}"

  # Generate name if not provided
  default_name = name || File.basename(expanded_path)

  root = new(uri, default_name)
  root.validate! # Ensure the root is valid
  root
end

Instance Method Details

#as_mcp_definitionHash

Converts the root to its MCP definition hash.

Returns:

  • (Hash)

    A hash representing the root in MCP format.



223
224
225
226
227
228
# File 'lib/vector_mcp/definitions.rb', line 223

def as_mcp_definition
  {
    uri: uri.to_s,
    name: name
  }.compact
end

#pathString

Returns the filesystem path for file:// URIs.

Returns:

  • (String)

    The filesystem path.

Raises:

  • (ArgumentError)

    If the URI is not a file:// scheme.



282
283
284
285
286
287
# File 'lib/vector_mcp/definitions.rb', line 282

def path
  parsed_uri = URI(uri.to_s)
  raise ArgumentError, "Cannot get path for non-file URI: #{uri}" unless parsed_uri.scheme == "file"

  parsed_uri.path
end

#validate!Boolean

Validates that the root URI is properly formatted and secure.

Returns:

  • (Boolean)

    True if the root is valid.

Raises:

  • (ArgumentError)

    If the root is invalid.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/vector_mcp/definitions.rb', line 233

def validate!
  # Validate URI format
  parsed_uri = begin
    URI(uri.to_s)
  rescue URI::InvalidURIError
    raise ArgumentError, "Invalid URI format: #{uri}"
  end

  # Currently, only file:// scheme is supported per MCP spec
  raise ArgumentError, "Only file:// URIs are supported for roots, got: #{parsed_uri.scheme}://" unless parsed_uri.scheme == "file"

  # Validate path exists and is a directory
  path = parsed_uri.path
  raise ArgumentError, "Root directory does not exist: #{path}" unless File.exist?(path)

  raise ArgumentError, "Root path is not a directory: #{path}" unless File.directory?(path)

  # Security check: ensure we can read the directory
  raise ArgumentError, "Root directory is not readable: #{path}" unless File.readable?(path)

  # Validate against path traversal attempts in the URI itself
  raise ArgumentError, "Root path contains unsafe traversal patterns: #{path}" if path.include?("..") || path.include?("./")

  true
end