Class: OmniAI::MCP::JRPC::Response

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

Overview

Examples:

request = OmniAI::MCP::JRPC::Response.new(id: 0, result: "OK")
request.generate #=> { "jsonrpc": "2.0", "id": 0, "result": "OK" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, result:) ⇒ Response

Returns a new instance of Response.

Parameters:

  • id (Integer)
  • result (String)


20
21
22
23
# File 'lib/omniai/mcp/jrpc/response.rb', line 20

def initialize(id:, result:)
  @id = id
  @result = result
end

Instance Attribute Details

#idInteger

Returns:

  • (Integer)


12
13
14
# File 'lib/omniai/mcp/jrpc/response.rb', line 12

def id
  @id
end

#resultString

Returns:

  • (String)


16
17
18
# File 'lib/omniai/mcp/jrpc/response.rb', line 16

def result
  @result
end

Class Method Details

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

Parameters:

  • text (String)

Returns:

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/omniai/mcp/jrpc/response.rb', line 44

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"))
  result = data["result"] || raise(Error.new(code: Error::Code::PARSE_ERROR, message: "missing result"))

  new(id:, result:)
end

Instance Method Details

#generateString

Returns:

  • (String)


31
32
33
34
35
36
37
# File 'lib/omniai/mcp/jrpc/response.rb', line 31

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

#inspectString

Returns:

  • (String)


26
27
28
# File 'lib/omniai/mcp/jrpc/response.rb', line 26

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