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]
  @params = (options[:params] || {}).dup.freeze # Immutable copy
  @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 (readonly)

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



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

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

#error?Boolean

Check if operation failed

Returns:

  • (Boolean)

    true if error occurred



38
39
40
# File 'lib/vector_mcp/middleware/context.rb', line 38

def error?
  !@error.nil?
end

#success?Boolean

Check if operation completed successfully

Returns:

  • (Boolean)

    true if no error occurred



32
33
34
# File 'lib/vector_mcp/middleware/context.rb', line 32

def success?
  @error.nil?
end

#timingHash

Get operation timing information

Returns:

  • (Hash)

    Timing metadata



50
51
52
# File 'lib/vector_mcp/middleware/context.rb', line 50

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

#to_hHash

Get all available data as hash for logging/debugging

Returns:

  • (Hash)

    Context summary



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vector_mcp/middleware/context.rb', line 63

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



44
45
46
# File 'lib/vector_mcp/middleware/context.rb', line 44

def user
  @session&.security_context&.user
end