Class: OmniAI::MCP::JRPC::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/mcp/jrpc/request.rb

Overview

Examples:

request = OmniAI::MCP::JRPC::Request.new(id: 0, method: "ping", params: {})
request.id #=> 0
request.method #=> "ping"
request.params #=> {}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, method:, params:) ⇒ Request

Returns a new instance of Request.

Parameters:

  • id (Integer)
  • method (String)
  • params (Hash)


27
28
29
30
31
# File 'lib/omniai/mcp/jrpc/request.rb', line 27

def initialize(id:, method:, params:)
  @id = id
  @method = method
  @params = params
end

Instance Attribute Details

#idInteger

Returns:

  • (Integer)


14
15
16
# File 'lib/omniai/mcp/jrpc/request.rb', line 14

def id
  @id
end

#methodString

Returns:

  • (String)


18
19
20
# File 'lib/omniai/mcp/jrpc/request.rb', line 18

def method
  @method
end

#paramsHash

Returns:

  • (Hash)


22
23
24
# File 'lib/omniai/mcp/jrpc/request.rb', line 22

def params
  @params
end

Class Method Details

.parse(text) ⇒ OmniAI::MCP::JRPC::Request

Parameters:

  • text (String)

Returns:

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/omniai/mcp/jrpc/request.rb', line 53

def self.parse(text)
  data =
    begin
      JSON.parse(text)
    rescue JSON::ParserError => e
      raise Error.new(code: Error::Code::PARSE_ERROR, message: e.message)
    end

  id = data["id"] || raise(Error.new(code: Error::Code::PARSE_ERROR, message: "missing id"))
  method = data["method"] || raise(Error.new(code: Error::Code::PARSE_ERROR, message: "missing method"))
  params = data["params"] || raise(Error.new(code: Error::Code::PARSE_ERROR, message: "missing params"))

  new(id:, method:, params:)
end

Instance Method Details

#generateString

Returns:

  • (String)


39
40
41
42
43
44
45
46
# File 'lib/omniai/mcp/jrpc/request.rb', line 39

def generate
  JSON.generate({
    jsonrpc: VERSION,
    id: @id,
    method: @method,
    params: @params,
  })
end

#inspectString

Returns:

  • (String)


34
35
36
# File 'lib/omniai/mcp/jrpc/request.rb', line 34

def inspect
  "#<#{self.class.name} id=#{@id} method=#{@method} params=#{@params}>"
end