Class: VectorMCP::Middleware::Manager

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

Overview

Central manager for middleware hooks and execution Thread-safe registry and execution engine for all middleware

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



10
11
12
13
# File 'lib/vector_mcp/middleware/manager.rb', line 10

def initialize
  @hooks = Concurrent::Map.new { |h, k| h[k] = Concurrent::Array.new }
  @logger = VectorMCP.logger_for("middleware.manager")
end

Instance Method Details

#clear!Object

Clear all registered hooks (useful for testing)



92
93
94
95
# File 'lib/vector_mcp/middleware/manager.rb', line 92

def clear!
  @hooks.clear
  @logger.debug("Cleared all middleware hooks")
end

#execute_hooks(hook_type, context) ⇒ VectorMCP::Middleware::Context

Execute all hooks for a specific hook type with timing

Parameters:

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vector_mcp/middleware/manager.rb', line 59

def execute_hooks(hook_type, context)
  hook_type_str = hook_type.to_s
  hooks = get_sorted_hooks(hook_type_str)

  return context if hooks.empty?

  execution_state = initialize_execution_state(hook_type_str, hooks, context)
  execute_hook_chain(hooks, context, execution_state)
  finalize_execution(context, execution_state)

  context
end

#register(middleware_class, hooks, priority: Hook::DEFAULT_PRIORITY, conditions: {}) ⇒ Object

Register a middleware for specific hook types

Examples:

manager.register(MyMiddleware, [:before_tool_call, :after_tool_call])
manager.register(AuthMiddleware, :before_request, priority: 10)

Parameters:

  • middleware_class (Class)

    Middleware class inheriting from Base

  • hooks (Array<String, Symbol>)

    Hook types to register for

  • priority (Integer) (defaults to: Hook::DEFAULT_PRIORITY)

    Execution priority (lower numbers execute first)

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

    Conditions for when middleware should run



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vector_mcp/middleware/manager.rb', line 23

def register(middleware_class, hooks, priority: Hook::DEFAULT_PRIORITY, conditions: {})
  Array(hooks).each do |hook_type|
    hook = Hook.new(middleware_class, hook_type, priority: priority, conditions: conditions)
    add_hook(hook)
  end

  @logger.debug("Registered middleware") do
    {
      middleware: middleware_class.name,
      hooks: Array(hooks),
      priority: priority
    }
  end
end

#statsHash

Get statistics about registered middleware

Returns:

  • (Hash)

    Statistics summary



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vector_mcp/middleware/manager.rb', line 74

def stats
  hook_counts = {}
  total_hooks = 0

  @hooks.each do |hook_type, hook_array|
    count = hook_array.size
    hook_counts[hook_type] = count
    total_hooks += count
  end

  {
    total_hooks: total_hooks,
    hook_types: hook_counts.keys.sort,
    hooks_by_type: hook_counts
  }
end

#unregister(middleware_class) ⇒ Object

Remove all hooks for a specific middleware class

Parameters:

  • middleware_class (Class)

    Middleware class to remove



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vector_mcp/middleware/manager.rb', line 40

def unregister(middleware_class)
  removed_count = 0

  @hooks.each_value do |hook_array|
    removed_count += hook_array.delete_if { |hook| hook.middleware_class == middleware_class }.size
  end

  @logger.debug("Unregistered middleware") do
    {
      middleware: middleware_class.name,
      hooks_removed: removed_count
    }
  end
end