Module: VectorMCP::Server::Registry

Included in:
VectorMCP::Server
Defined in:
lib/vector_mcp/server/registry.rb

Overview

Handles registration of tools, resources, prompts, and roots

Instance Method Summary collapse

Instance Method Details

#register(*tool_classes) ⇒ self

Registers one or more class-based tool definitions with the server.

Each argument must be a subclass of VectorMCP::Tool that declares its metadata via the class-level DSL (tool_name, description, param) and implements #call.

Examples:

Register a single tool

server.register(ListProviders)

Register multiple tools

server.register(ListProviders, CreateProvider, UpdateProvider)

Parameters:

  • tool_classes (Array<Class>)

    One or more VectorMCP::Tool subclasses.

Returns:

  • (self)

    The server instance, for chaining.

Raises:

  • (ArgumentError)

    If any argument is not a VectorMCP::Tool subclass.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vector_mcp/server/registry.rb', line 47

def register(*tool_classes)
  tool_classes.each do |tool_class|
    unless tool_class.is_a?(Class) && tool_class < VectorMCP::Tool
      raise ArgumentError, "#{tool_class.inspect} is not a VectorMCP::Tool subclass"
    end

    definition = tool_class.to_definition
    register_tool(
      name: definition.name,
      description: definition.description,
      input_schema: definition.input_schema,
      &definition.handler
    )
  end
  self
end

#register_image_prompt(name:, description:, image_argument: "image", additional_arguments: [], &handler) ⇒ self

Helper method to register a prompt that supports image arguments. Thin wrapper: delegates to Definitions::Prompt.with_image_support.

Parameters:

  • name (String)

    Unique name for the prompt.

  • description (String)

    Human-readable description.

  • image_argument (String) (defaults to: "image")

    Name of the image argument (default: “image”).

  • additional_arguments (Array<Hash>) (defaults to: [])

    Additional prompt arguments.

Returns:

  • (self)


195
196
197
198
199
200
201
202
# File 'lib/vector_mcp/server/registry.rb', line 195

def register_image_prompt(name:, description:, image_argument: "image", additional_arguments: [], &handler)
  prompt = VectorMCP::Definitions::Prompt.with_image_support(
    name: name, description: description, image_argument_name: image_argument,
    additional_arguments: additional_arguments, &handler
  )
  register_prompt(name: prompt.name, description: prompt.description,
                  arguments: prompt.arguments, &prompt.handler)
end

#register_image_resource(uri:, file_path:, name: nil, description: nil) ⇒ self

Helper method to register an image resource from a file path. Thin wrapper: delegates schema-building to Definitions::Resource.from_image_file, then stores the result via register_resource.

Parameters:

  • uri (String)

    Unique URI for the resource.

  • file_path (String)

    Path to the image file.

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

    Human-readable name (auto-generated if nil).

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

    Description (auto-generated if nil).

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If the file doesn’t exist or isn’t a valid image.



143
144
145
146
147
148
149
# File 'lib/vector_mcp/server/registry.rb', line 143

def register_image_resource(uri:, file_path:, name: nil, description: nil)
  resource = VectorMCP::Definitions::Resource.from_image_file(
    uri: uri, file_path: file_path, name: name, description: description
  )
  register_resource(uri: resource.uri, name: resource.name,
                    description: resource.description, mime_type: resource.mime_type, &resource.handler)
end

#register_image_resource_from_data(uri:, image_data:, name:, description: nil, mime_type: nil) ⇒ self

Helper method to register an image resource from binary data. Thin wrapper: delegates to Definitions::Resource.from_image_data.

Parameters:

  • uri (String)

    Unique URI for the resource.

  • image_data (String)

    Binary image data.

  • name (String)

    Human-readable name.

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

    Description (auto-generated if nil).

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

    MIME type (auto-detected if nil).

Returns:

  • (self)


160
161
162
163
164
165
166
# File 'lib/vector_mcp/server/registry.rb', line 160

def register_image_resource_from_data(uri:, image_data:, name:, description: nil, mime_type: nil)
  resource = VectorMCP::Definitions::Resource.from_image_data(
    uri: uri, image_data: image_data, name: name, description: description, mime_type: mime_type
  )
  register_resource(uri: resource.uri, name: resource.name,
                    description: resource.description, mime_type: resource.mime_type, &resource.handler)
end

#register_image_tool(name:, description:, image_parameter: "image", additional_parameters: {}, required_parameters: [], &handler) ⇒ self

Helper method to register a tool that accepts image inputs. Thin wrapper: delegates schema-building to Definitions::Tool.with_image_support.

Parameters:

  • name (String)

    Unique name for the tool.

  • description (String)

    Human-readable description.

  • image_parameter (String) (defaults to: "image")

    Name of the image parameter (default: “image”).

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

    Additional JSON Schema properties.

  • required_parameters (Array<String>) (defaults to: [])

    List of required parameter names.

Returns:

  • (self)


177
178
179
180
181
182
183
184
185
# File 'lib/vector_mcp/server/registry.rb', line 177

def register_image_tool(name:, description:, image_parameter: "image",
                        additional_parameters: {}, required_parameters: [], &handler)
  tool = VectorMCP::Definitions::Tool.with_image_support(
    name: name, description: description, image_parameter: image_parameter,
    additional_parameters: additional_parameters, required_parameters: required_parameters, &handler
  )
  register_tool(name: tool.name, description: tool.description,
                input_schema: tool.input_schema, &tool.handler)
end

#register_prompt(name:, description:, arguments: []) {|Hash| ... } ⇒ self

Registers a new prompt with the server.

Parameters:

  • name (String, Symbol)

    The unique name for the prompt.

  • description (String)

    A human-readable description of the prompt.

  • arguments (Array<Hash>) (defaults to: [])

    An array defining the prompt’s arguments.

Yields:

  • (Hash)

    A block that generates the prompt.

Returns:

  • (self)

    The server instance, for chaining.

Raises:

  • (ArgumentError)

    if a prompt with the same name is already registered.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vector_mcp/server/registry.rb', line 90

def register_prompt(name:, description:, arguments: [], &handler)
  name_s = name.to_s
  raise ArgumentError, "Prompt '#{name_s}' already registered" if @prompts[name_s]

  validate_prompt_arguments(arguments)
  @prompts[name_s] = VectorMCP::Definitions::Prompt.new(name_s, description, arguments, handler)
  @prompts_list_changed = true
  notify_prompts_list_changed
  logger.debug("Registered prompt: #{name_s}")
  self
end

#register_resource(uri:, name:, description:, mime_type: "text/plain") {|Hash| ... } ⇒ self

Registers a new resource with the server.

Parameters:

  • uri (String, URI)

    The unique URI for the resource.

  • name (String)

    A human-readable name for the resource.

  • description (String)

    A description of the resource.

  • mime_type (String) (defaults to: "text/plain")

    The MIME type of the resource’s content (default: “text/plain”).

Yields:

  • (Hash)

    A block that provides the resource’s content.

Returns:

  • (self)

    The server instance, for chaining.

Raises:

  • (ArgumentError)

    if a resource with the same URI is already registered.



73
74
75
76
77
78
79
80
# File 'lib/vector_mcp/server/registry.rb', line 73

def register_resource(uri:, name:, description:, mime_type: "text/plain", &handler)
  uri_s = uri.to_s
  raise ArgumentError, "Resource '#{uri_s}' already registered" if @resources[uri_s]

  @resources[uri_s] = VectorMCP::Definitions::Resource.new(uri, name, description, mime_type, handler)
  logger.debug("Registered resource: #{uri_s}")
  self
end

#register_root(uri:, name:) ⇒ self

Registers a new root with the server.

Parameters:

  • uri (String, URI)

    The unique URI for the root (must be file:// scheme).

  • name (String)

    A human-readable name for the root.

Returns:

  • (self)

    The server instance, for chaining.

Raises:

  • (ArgumentError)

    if a root with the same URI is already registered.



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vector_mcp/server/registry.rb', line 108

def register_root(uri:, name:)
  uri_s = uri.to_s
  raise ArgumentError, "Root '#{uri_s}' already registered" if @roots[uri_s]

  root = VectorMCP::Definitions::Root.new(uri, name)
  root.validate! # This will raise ArgumentError if invalid

  @roots[uri_s] = root
  @roots_list_changed = true
  notify_roots_list_changed
  logger.debug("Registered root: #{uri_s} (#{name})")
  self
end

#register_root_from_path(path, name: nil) ⇒ self

Helper method to register a root from a local directory path.

Parameters:

  • path (String)

    Local filesystem path to the directory.

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

    Human-readable name for the root.

Returns:

  • (self)

    The server instance, for chaining.

Raises:

  • (ArgumentError)

    if the path is invalid or not accessible.



128
129
130
131
# File 'lib/vector_mcp/server/registry.rb', line 128

def register_root_from_path(path, name: nil)
  root = VectorMCP::Definitions::Root.from_path(path, name: name)
  register_root(uri: root.uri, name: root.name)
end

#register_tool(name:, description:, input_schema:) {|Hash| ... } ⇒ self

Registers a new tool with the server.

Parameters:

  • name (String, Symbol)

    The unique name for the tool.

  • description (String)

    A human-readable description of the tool.

  • input_schema (Hash)

    A JSON Schema object that precisely describes the structure of the argument hash your tool expects.

Yields:

  • (Hash)

    A block implementing the tool logic.

Returns:

  • (self)

    Returns the server instance so you can chain registrations.

Raises:

  • (ArgumentError)

    If another tool with the same name is already registered.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vector_mcp/server/registry.rb', line 20

def register_tool(name:, description:, input_schema:, &handler)
  name_s = name.to_s
  raise ArgumentError, "Tool '#{name_s}' already registered" if @tools[name_s]

  # Validate schema format during registration
  validate_schema_format!(input_schema) if input_schema

  @tools[name_s] = VectorMCP::Definitions::Tool.new(name_s, description, input_schema, handler)
  logger.debug("Registered tool: #{name_s}")
  self
end