Module: Rack::Test::McpProtocol

Defined in:
lib/stack-service-base/mcp/rack_test_mcp_protocol.rb

Instance Method Summary collapse

Instance Method Details

#mcp_call_tool(name:, arguments:) ⇒ Object



28
29
30
31
32
33
# File 'lib/stack-service-base/mcp/rack_test_mcp_protocol.rb', line 28

def mcp_call_tool(name:, arguments:)
  response = mcp_call_tool_raw(name: name, arguments: arguments)
  tool_results = response.dig(:result, :content)
  expect(tool_results[0][:type]).to eq('text')
  JSON.parse(tool_results[0][:text], symbolize_names: true)
end

#mcp_call_tool_raw(name:, arguments:) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/stack-service-base/mcp/rack_test_mcp_protocol.rb', line 35

def mcp_call_tool_raw(name:, arguments:)
  rpc_request(
    id: next_id,
    method: 'tools/call',
    params: { 'name' => name, 'arguments' => arguments }
  )
end

#mcp_list_toolsObject



21
22
23
24
25
26
# File 'lib/stack-service-base/mcp/rack_test_mcp_protocol.rb', line 21

def mcp_list_tools
  req_id = next_id
  response = rpc_request(id: req_id, method: 'tools/list', params: {})
  expect(response[:id]).to eq(req_id)
  response.dig(:result, :tools)
end

#parsed_response_bodyObject



12
13
14
15
16
17
18
19
# File 'lib/stack-service-base/mcp/rack_test_mcp_protocol.rb', line 12

def parsed_response_body
  body = +''
  last_response.each { |chunk| body << chunk.to_s }
  data_lines = body.lines.select { |line| line.start_with?('data:') }
  payload_line = data_lines.reverse.find { |l| l !~ /ping/i } || data_lines.last
  payload = (payload_line || body).sub(/\Adata:\s*/, '').sub(/\n\z/, '')
  JSON.parse(payload, symbolize_names: true)
end

#rpc_request(payload) ⇒ Object



6
7
8
9
10
# File 'lib/stack-service-base/mcp/rack_test_mcp_protocol.rb', line 6

def rpc_request(payload)
  post '/mcp', JSON.dump(payload), { 'CONTENT_TYPE' => 'application/json' }
  expect(last_response.status).to eq(200)
  parsed_response_body
end