Class: Foobara::McpConnector::JsonrpcRequest

Inherits:
CommandConnector::Request show all
Defined in:
foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb

Overview

Isolating jsonrpc specific logic to this abstract class

Direct Known Subclasses

Request

Defined Under Namespace

Classes: EmptyBatchError, InvalidJsonError, InvalidJsonrpcMethodError, InvalidJsonrpcParamsError, InvalidJsonrpcRequestStructureError, InvalidJsonrpcVersionError

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes inherited from CommandConnector::Request

#action, #command, #command_class, #command_connector, #error, #full_command_name, #inputs, #response, #serializers

Instance Method Summary collapse

Methods inherited from CommandConnector::Request

#error_collection, #outcome, #response_body, #result, #serializer, #success?

Methods included from TruncatedInspect

#inspect, truncating

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

#batchObject

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_childObject

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_bodyObject

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_jsonObject

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_idObject

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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


111
112
113
# File 'foobara-mcp-connector-0.0.4/src/jsonrpc_request.rb', line 111

def error?
  !!error
end

#methodObject



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

Returns:

  • (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

#paramsObject



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_jsonObject



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.message}")
                                 error.set_backtrace(caller)
                                 nil
                               end
                             end
end

#valid_batch?Boolean

Returns:

  • (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_emptyObject



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_structureObject



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_versionObject



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