Class: VectorMCP::Middleware::Context

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

Overview

Context object passed to middleware hooks containing operation metadata Provides access to request data, session info, and mutable response data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Context

Returns a new instance of Context.

Parameters:

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

    Context initialization options

Options Hash (options):

  • :operation_type (Symbol)

    Type of operation (:tool_call, :resource_read, etc.)

  • :operation_name (String)

    Name of the specific operation being performed

  • :params (Hash)

    Request parameters

  • :session (VectorMCP::Session)

    Current session

  • :server (VectorMCP::Server)

    Server instance

  • :metadata (Hash)

    Additional metadata about the operation



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vector_mcp/middleware/context.rb', line 18

def initialize(options = {})
  @operation_type = options[:operation_type]
  @operation_name = options[:operation_name]
  self.params = options[:params]
  @session = options[:session]
  @server = options[:server]
  @metadata = (options[:metadata] || {}).dup
  @result = nil
  @error = nil
  @skip_remaining_hooks = false
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/vector_mcp/middleware/context.rb', line 9

def error
  @error
end

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/vector_mcp/middleware/context.rb', line 8

def 
  @metadata
end

#operation_nameObject (readonly)

Returns the value of attribute operation_name.



8
9
10
# File 'lib/vector_mcp/middleware/context.rb', line 8

def operation_name
  @operation_name
end

#operation_typeObject (readonly)

Returns the value of attribute operation_type.



8
9
10
# File 'lib/vector_mcp/middleware/context.rb', line 8

def operation_type
  @operation_type
end

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/vector_mcp/middleware/context.rb', line 8

def params
  @params
end

#resultObject

Returns the value of attribute result.



9
10
11
# File 'lib/vector_mcp/middleware/context.rb', line 9

def result
  @result
end

#serverObject (readonly)

Returns the value of attribute server.



8
9
10
# File 'lib/vector_mcp/middleware/context.rb', line 8

def server
  @server
end

#sessionObject (readonly)

Returns the value of attribute session.



8
9
10
# File 'lib/vector_mcp/middleware/context.rb', line 8

def session
  @session
end

#skip_remaining_hooksObject

Returns the value of attribute skip_remaining_hooks.



9
10
11
# File 'lib/vector_mcp/middleware/context.rb', line 9

def skip_remaining_hooks
  @skip_remaining_hooks
end

Instance Method Details

#add_metadata(key, value) ⇒ Object

Add custom metadata

Parameters:

  • key (Symbol, String)

    Metadata key

  • value (Object)

    Metadata value



67
68
69
# File 'lib/vector_mcp/middleware/context.rb', line 67

def (key, value)
  @metadata[key] = value
end

#error?Boolean

Check if operation failed

Returns:

  • (Boolean)

    true if error occurred



48
49
50
# File 'lib/vector_mcp/middleware/context.rb', line 48

def error?
  !@error.nil?
end

#success?Boolean

Check if operation completed successfully

Returns:

  • (Boolean)

    true if no error occurred



42
43
44
# File 'lib/vector_mcp/middleware/context.rb', line 42

def success?
  @error.nil?
end

#timingHash

Get operation timing information

Returns:

  • (Hash)

    Timing metadata



60
61
62
# File 'lib/vector_mcp/middleware/context.rb', line 60

def timing
  @metadata[:timing] || {}
end

#to_hHash

Get all available data as hash for logging/debugging

Returns:

  • (Hash)

    Context summary



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vector_mcp/middleware/context.rb', line 73

def to_h
  {
    operation_type: @operation_type,
    operation_name: @operation_name,
    params: @params,
    session_id: @session&.id,
    metadata: @metadata,
    success: success?,
    error: @error&.class&.name
  }
end

#userHash?

Get user context from session if available

Returns:

  • (Hash, nil)

    User context or nil if not authenticated



54
55
56
# File 'lib/vector_mcp/middleware/context.rb', line 54

def user
  @session&.security_context&.user
end