Class: VectorMCP::RequestContext
- Inherits:
-
Object
- Object
- VectorMCP::RequestContext
- Defined in:
- lib/vector_mcp/request_context.rb
Overview
Encapsulates request-specific data for MCP sessions. This provides a formal interface for transports to populate request context and for handlers to access request data without coupling to session internals.
Instance Attribute Summary collapse
-
#headers ⇒ Hash
readonly
HTTP headers from the request.
-
#method ⇒ String?
readonly
HTTP method (GET, POST, etc.) or transport-specific method.
-
#params ⇒ Hash
readonly
Query parameters from the request.
-
#path ⇒ String?
readonly
Request path or transport-specific path.
-
#transport_metadata ⇒ Hash
readonly
Transport-specific metadata.
Class Method Summary collapse
-
.from_rack_env(rack_env, transport_type) ⇒ RequestContext
Create a request context from a Rack environment.
-
.minimal(transport_type) ⇒ RequestContext
Create a minimal request context for non-HTTP transports.
Instance Method Summary collapse
-
#header(name) ⇒ String?
Get a specific header value.
-
#headers? ⇒ Boolean
Check if the request context has any headers.
-
#http_transport? ⇒ Boolean
Check if this is an HTTP-based transport.
-
#initialize(headers: {}, params: {}, method: nil, path: nil, transport_metadata: {}) ⇒ RequestContext
constructor
Initialize a new request context with the provided data.
-
#inspect ⇒ String
Detailed string representation for debugging.
-
#metadata(key) ⇒ Object?
Get transport-specific metadata.
-
#param(name) ⇒ String?
Get a specific parameter value.
-
#params? ⇒ Boolean
Check if the request context has any parameters.
-
#to_h ⇒ Hash
Convert the request context to a hash representation.
-
#to_s ⇒ String
String representation of the request context.
Constructor Details
#initialize(headers: {}, params: {}, method: nil, path: nil, transport_metadata: {}) ⇒ RequestContext
Initialize a new request context with the provided data.
23 24 25 26 27 28 29 |
# File 'lib/vector_mcp/request_context.rb', line 23 def initialize(headers: {}, params: {}, method: nil, path: nil, transport_metadata: {}) @headers = normalize_headers(headers).freeze @params = normalize_params(params).freeze @method = method&.to_s&.freeze @path = path&.to_s&.freeze @transport_metadata = ().freeze end |
Instance Attribute Details
#headers ⇒ Hash (readonly)
HTTP headers from the request
13 14 15 |
# File 'lib/vector_mcp/request_context.rb', line 13 def headers @headers end |
#method ⇒ String? (readonly)
HTTP method (GET, POST, etc.) or transport-specific method
13 14 15 |
# File 'lib/vector_mcp/request_context.rb', line 13 def method @method end |
#params ⇒ Hash (readonly)
Query parameters from the request
13 14 15 |
# File 'lib/vector_mcp/request_context.rb', line 13 def params @params end |
#path ⇒ String? (readonly)
Request path or transport-specific path
13 14 15 |
# File 'lib/vector_mcp/request_context.rb', line 13 def path @path end |
#transport_metadata ⇒ Hash (readonly)
Transport-specific metadata
13 14 15 |
# File 'lib/vector_mcp/request_context.rb', line 13 def @transport_metadata end |
Class Method Details
.from_rack_env(rack_env, transport_type) ⇒ RequestContext
Create a request context from a Rack environment. This is a convenience method for HTTP-based transports.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/vector_mcp/request_context.rb', line 115 def self.from_rack_env(rack_env, transport_type) # Handle nil rack_env by returning a minimal context return minimal(transport_type) if rack_env.nil? new( headers: VectorMCP::Util.extract_headers_from_rack_env(rack_env), params: VectorMCP::Util.extract_params_from_rack_env(rack_env), method: rack_env["REQUEST_METHOD"], path: rack_env["PATH_INFO"], transport_metadata: { transport_type: transport_type.to_s, remote_addr: rack_env["REMOTE_ADDR"], user_agent: rack_env["HTTP_USER_AGENT"], content_type: rack_env["CONTENT_TYPE"] } ) end |
.minimal(transport_type) ⇒ RequestContext
Create a minimal request context for non-HTTP transports. This is useful for stdio and other command-line transports.
99 100 101 102 103 104 105 106 107 |
# File 'lib/vector_mcp/request_context.rb', line 99 def self.minimal(transport_type) new( headers: {}, params: {}, method: transport_type.to_s.upcase, path: "/", transport_metadata: { transport_type: transport_type.to_s } ) end |
Instance Method Details
#header(name) ⇒ String?
Get a specific header value.
63 64 65 |
# File 'lib/vector_mcp/request_context.rb', line 63 def header(name) @headers[name.to_s] end |
#headers? ⇒ Boolean
Check if the request context has any headers.
48 49 50 |
# File 'lib/vector_mcp/request_context.rb', line 48 def headers? !@headers.empty? end |
#http_transport? ⇒ Boolean
Check if this is an HTTP-based transport.
86 87 88 89 90 91 92 |
# File 'lib/vector_mcp/request_context.rb', line 86 def http_transport? return false unless @method && @path # Check if method is an HTTP method http_methods = %w[GET POST PUT DELETE HEAD OPTIONS PATCH TRACE CONNECT] http_methods.include?(@method.upcase) end |
#inspect ⇒ String
Detailed string representation for debugging.
143 144 145 146 147 148 |
# File 'lib/vector_mcp/request_context.rb', line 143 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} " \ "method=#{@method.inspect} path=#{@path.inspect} " \ "headers=#{@headers.inspect} params=#{@params.inspect} " \ "transport_metadata=#{@transport_metadata.inspect}>" end |
#metadata(key) ⇒ Object?
Get transport-specific metadata.
79 80 81 |
# File 'lib/vector_mcp/request_context.rb', line 79 def (key) @transport_metadata[key.to_s] end |
#param(name) ⇒ String?
Get a specific parameter value.
71 72 73 |
# File 'lib/vector_mcp/request_context.rb', line 71 def param(name) @params[name.to_s] end |
#params? ⇒ Boolean
Check if the request context has any parameters.
55 56 57 |
# File 'lib/vector_mcp/request_context.rb', line 55 def params? !@params.empty? end |
#to_h ⇒ Hash
Convert the request context to a hash representation. This is useful for serialization and debugging.
35 36 37 38 39 40 41 42 43 |
# File 'lib/vector_mcp/request_context.rb', line 35 def to_h { headers: @headers, params: @params, method: @method, path: @path, transport_metadata: @transport_metadata } end |
#to_s ⇒ String
String representation of the request context.
136 137 138 |
# File 'lib/vector_mcp/request_context.rb', line 136 def to_s "<RequestContext method=#{@method} path=#{@path} headers=#{@headers.keys.size} params=#{@params.keys.size}>" end |