Class: VectorMCP::Middleware::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_mcp/middleware/hook.rb

Overview

Represents a single middleware hook with priority and execution logic

Constant Summary collapse

DEFAULT_PRIORITY =

Default priority for middleware (lower numbers execute first)

100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(middleware_class, hook_type, priority: DEFAULT_PRIORITY, conditions: {}) ⇒ Hook

Returns a new instance of Hook.

Parameters:

  • middleware_class (Class)

    The middleware class to execute

  • hook_type (String, Symbol)

    Type of hook (before_tool_call, etc.)

  • priority (Integer) (defaults to: DEFAULT_PRIORITY)

    Execution priority (lower numbers execute first)

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

    Conditions for when this hook should run



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vector_mcp/middleware/hook.rb', line 16

def initialize(middleware_class, hook_type, priority: DEFAULT_PRIORITY, conditions: {})
  @middleware_class = middleware_class
  @hook_type = hook_type.to_s
  @priority = priority
  @conditions = conditions

  validate_hook_type!
  validate_middleware_class!

  @operation_type = HOOK_OPERATION_TYPES[@hook_type]
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



7
8
9
# File 'lib/vector_mcp/middleware/hook.rb', line 7

def conditions
  @conditions
end

#hook_typeObject (readonly)

Returns the value of attribute hook_type.



7
8
9
# File 'lib/vector_mcp/middleware/hook.rb', line 7

def hook_type
  @hook_type
end

#middleware_classObject (readonly)

Returns the value of attribute middleware_class.



7
8
9
# File 'lib/vector_mcp/middleware/hook.rb', line 7

def middleware_class
  @middleware_class
end

#operation_typeObject (readonly)

Returns the value of attribute operation_type.



7
8
9
# File 'lib/vector_mcp/middleware/hook.rb', line 7

def operation_type
  @operation_type
end

#priorityObject (readonly)

Returns the value of attribute priority.



7
8
9
# File 'lib/vector_mcp/middleware/hook.rb', line 7

def priority
  @priority
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare hooks for sorting by priority

Parameters:

  • other (Hook)

    Other hook to compare

Returns:

  • (Integer)

    Comparison result



57
58
59
# File 'lib/vector_mcp/middleware/hook.rb', line 57

def <=>(other)
  @priority <=> other.priority
end

#execute(context) ⇒ void

This method returns an undefined value.

Execute this hook with the given context

Parameters:



31
32
33
34
35
36
37
38
39
# File 'lib/vector_mcp/middleware/hook.rb', line 31

def execute(context)
  return unless should_execute?(context)

  # Create middleware instance and execute hook
  middleware_instance = create_middleware_instance(context)
  execute_hook_method(middleware_instance, context)
rescue StandardError => e
  handle_hook_error(e, context)
end

#should_execute?(context) ⇒ Boolean

Check if this hook should execute for the given context

Parameters:

Returns:

  • (Boolean)

    true if hook should execute



44
45
46
47
48
49
50
51
52
# File 'lib/vector_mcp/middleware/hook.rb', line 44

def should_execute?(context)
  return false if context.skip_remaining_hooks

  # Check operation type match
  return false unless matches_operation_type?(context)

  # Check custom conditions
  @conditions.all? { |key, value| condition_matches?(key, value, context) }
end