Class: Otto::MCP::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/otto/mcp/protocol.rb

Overview

MCP protocol handler providing Model Context Protocol functionality

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(otto_instance) ⇒ Protocol

Returns a new instance of Protocol.



14
15
16
17
# File 'lib/otto/mcp/protocol.rb', line 14

def initialize(otto_instance)
  @otto     = otto_instance
  @registry = Registry.new
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



12
13
14
# File 'lib/otto/mcp/protocol.rb', line 12

def registry
  @registry
end

Instance Method Details

#handle_request(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/otto/mcp/protocol.rb', line 19

def handle_request(env)
  request = @otto.request_class.new(env)

  unless request.post? && request.content_type&.include?('application/json')
    return error_response(nil, -32_600, 'Invalid Request', 'Only JSON-RPC POST requests supported')
  end

  begin
    body = request.body.read
    data = JSON.parse(body)
  rescue JSON::ParserError
    return error_response(nil, -32_700, 'Parse error', 'Invalid JSON')
  end

  unless valid_jsonrpc_request?(data)
    return error_response(data['id'], -32_600, 'Invalid Request', 'Missing jsonrpc, method, or id fields')
  end

  case data['method']
  when 'initialize'
    handle_initialize(data)
  when 'resources/list'
    handle_resources_list(data)
  when 'resources/read'
    handle_resources_read(data)
  when 'tools/list'
    handle_tools_list(data)
  when 'tools/call'
    handle_tools_call(data, env)
  else
    error_response(data['id'], -32_601, 'Method not found', "Unknown method: #{data['method']}")
  end
end