Class: Foobara::McpConnector::JsonrpcRequest
- Inherits:
-
CommandConnector::Request
- Object
- CommandConnector::Request
- Foobara::McpConnector::JsonrpcRequest
- Defined in:
- foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb
Overview
Isolating jsonrpc specific logic to this abstract class
Direct Known Subclasses
Defined Under Namespace
Classes: EmptyBatchError, InvalidJsonError, InvalidJsonrpcMethodError, InvalidJsonrpcParamsError, InvalidJsonrpcRequestStructureError, InvalidJsonrpcVersionError
Constant Summary
Constants included from TruncatedInspect
Instance Attribute Summary collapse
-
#batch ⇒ Object
TODO: push response into base class?.
-
#is_batch_child ⇒ Object
TODO: push response into base class?.
-
#parsed_request_body ⇒ Object
TODO: push response into base class?.
-
#raw_request_json ⇒ Object
TODO: push response into base class?.
-
#request_id ⇒ Object
TODO: push response into base class?.
Attributes inherited from CommandConnector::Request
#action, #command, #command_class, #command_connector, #error, #full_command_name, #inputs, #response, #serializers
Instance Method Summary collapse
- #batch? ⇒ Boolean
- #batch_child? ⇒ Boolean
-
#error? ⇒ Boolean
TODO: push this up into foobara gem.
-
#initialize(request_json, is_batch_child: false) ⇒ JsonrpcRequest
constructor
A new instance of JsonrpcRequest.
- #method ⇒ Object
- #notification? ⇒ Boolean
- #params ⇒ Object
- #set_parsed_json ⇒ Object
- #valid_batch? ⇒ Boolean
- #validate_batch_not_empty ⇒ Object
- #validate_request_structure ⇒ Object
- #verify_jsonrpc_version ⇒ Object
Methods inherited from CommandConnector::Request
#error_collection, #outcome, #response_body, #result, #serializer, #success?
Methods included from TruncatedInspect
Constructor Details
#initialize(request_json, is_batch_child: false) ⇒ JsonrpcRequest
Returns a new instance of JsonrpcRequest.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 15 def initialize(request_json, *, is_batch_child: false, **, &) self.is_batch_child = is_batch_child self.raw_request_json = request_json set_parsed_json unless error if batch? self.batch = parsed_request_body.map do |request| self.class.new(request, *, is_batch_child: true, **, &) end validate_batch_not_empty else validate_request_structure unless error self.request_id = parsed_request_body["id"] verify_jsonrpc_version end end end super(*, **, &) end |
Instance Attribute Details
#batch ⇒ Object
TODO: push response into base class?
13 14 15 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 13 def batch @batch end |
#is_batch_child ⇒ Object
TODO: push response into base class?
13 14 15 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 13 def is_batch_child @is_batch_child end |
#parsed_request_body ⇒ Object
TODO: push response into base class?
13 14 15 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 13 def parsed_request_body @parsed_request_body end |
#raw_request_json ⇒ Object
TODO: push response into base class?
13 14 15 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 13 def raw_request_json @raw_request_json end |
#request_id ⇒ Object
TODO: push response into base class?
13 14 15 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 13 def request_id @request_id end |
Instance Method Details
#batch? ⇒ Boolean
86 87 88 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 86 def batch? parsed_request_body.is_a?(::Array) end |
#batch_child? ⇒ Boolean
70 71 72 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 70 def batch_child? is_batch_child end |
#error? ⇒ Boolean
TODO: push this up into foobara gem
111 112 113 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 111 def error? !!error end |
#method ⇒ Object
55 56 57 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 55 def method @method ||= parsed_request_body.is_a?(::Hash) && parsed_request_body["method"] end |
#notification? ⇒ Boolean
74 75 76 77 78 79 80 81 82 83 84 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 74 def notification? return false if response&.status == -32_600 if parsed_request_body if valid_batch? batch.all?(&:notification?) else parsed_request_body.is_a?(::Hash) && !parsed_request_body.key?("id") end end end |
#params ⇒ Object
59 60 61 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 59 def params @params ||= parsed_request_body["params"] end |
#set_parsed_json ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 41 def set_parsed_json self.parsed_request_body = if batch_child? raw_request_json else begin JSON.parse(raw_request_json) rescue => e self.error = InvalidJsonError.new("Could not parse request: #{e.}") error.set_backtrace(caller) nil end end end |
#valid_batch? ⇒ Boolean
90 91 92 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 90 def valid_batch? batch? && batch && !batch.empty? end |
#validate_batch_not_empty ⇒ Object
94 95 96 97 98 99 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 94 def validate_batch_not_empty if batch? && batch.empty? self.error = EmptyBatchError.new("An empty array/batch is not allowed") error.set_backtrace(caller) end end |
#validate_request_structure ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 101 def validate_request_structure unless parsed_request_body.is_a?(::Hash) self.error = InvalidJsonrpcRequestStructureError.new( "Invalid jsonrpc request structure. Expected a hash but got a #{parsed_request_body.class}" ) error.set_backtrace(caller) end end |
#verify_jsonrpc_version ⇒ Object
63 64 65 66 67 68 |
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 63 def verify_jsonrpc_version if parsed_request_body["jsonrpc"] != "2.0" self.error = InvalidJsonrpcVersionError.new("Unsupported jsonrpc version: #{parsed_request_body["jsonrpc"]}") error.set_backtrace(caller) end end |