|
12 | 12 | let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } |
13 | 13 |
|
14 | 14 | it 'initializes the client session' do |
| 15 | + req_id = next_id |
15 | 16 | response = rpc_request( |
16 | | - id: 10, |
| 17 | + id: req_id, |
17 | 18 | method: 'initialize', |
18 | 19 | params: {} |
19 | 20 | ) |
20 | 21 |
|
21 | | - expect(response['id']).to eq(10) |
22 | | - result = response['result'] |
| 22 | + expect(response[:id]).to eq(req_id) |
| 23 | + result = response[:result] |
23 | 24 |
|
24 | | - expect(result['serverInfo']).to include( |
25 | | - 'name' => 'mcp-server', |
26 | | - 'title' => 'MCP Server', |
27 | | - 'version' => '1.0.0' |
28 | | - ) |
| 25 | + expect(result[:serverInfo]).to include( |
| 26 | + name: 'mcp-server', |
| 27 | + title: 'MCP Server', |
| 28 | + version: '1.0.0' |
| 29 | + ) |
29 | 30 |
|
30 | | - expect(result['protocolVersion']).to eq(McpProcessor::PROTOCOL_VERSION) |
31 | | - expect(result['capabilities']).to include( |
32 | | - 'logging' => {}, |
33 | | - 'prompts' => { 'listChanged' => false }, |
34 | | - 'resources' => { 'listChanged' => false }, |
35 | | - 'tools' => { 'listChanged' => false } |
36 | | - ) |
| 31 | + expect(result[:protocolVersion]).to eq(McpProcessor::PROTOCOL_VERSION) |
| 32 | + expect(result[:capabilities]).to include( |
| 33 | + logging: {}, |
| 34 | + prompts: { listChanged: false }, |
| 35 | + resources: { listChanged: false }, |
| 36 | + tools: { listChanged: false } |
| 37 | + ) |
37 | 38 | end |
38 | 39 |
|
39 | 40 | it 'lists tools via GET /mcp' do |
40 | 41 | get '/mcp' |
41 | 42 | expect(last_response.status).to eq(200) |
42 | 43 | body = parsed_response_body |
43 | | - expect(body['id']).to be_nil |
44 | | - expect(body.dig('result', 'tools').map { |tool| tool['name'] }).to include('service_status', 'restart_service') |
| 44 | + expect(body[:id]).to be_nil |
| 45 | + expect(body.dig(:result, :tools).map { |tool| tool[:name] }).to include('service_status', 'restart_service') |
45 | 46 | end |
46 | 47 |
|
47 | 48 | it 'lists tools via RPC' do |
48 | | - body = rpc_request(id: 1, method: 'tools/list', params: {}) |
49 | | - expect(body['id']).to eq(1) |
50 | | - names = body.dig('result', 'tools').map { |tool| tool['name'] } |
| 49 | + names = mcp_list_tools.map { |tool| tool[:name] } |
51 | 50 | expect(names).to include('service_status', 'restart_service') |
52 | 51 | end |
53 | 52 |
|
54 | 53 | it 'executes service_status successfully' do |
55 | | - response = rpc_request( |
56 | | - id: 2, |
57 | | - method: 'tools/call', |
58 | | - params: { 'name' => 'service_status', 'arguments' => { 'service_name' => 'database-backend' } } |
59 | | - ) |
60 | | - tool_results = response.dig('result', 'content') |
61 | | - expect(tool_results[0]['type']).to eq('text') |
62 | | - tool_result = JSON.parse tool_results[0]['text'] |
63 | | - expect(tool_result['service_name']).to eq('database-backend') |
64 | | - expect(tool_result['status']).to eq('running') |
| 54 | + tool_result = mcp_call_tool(name: 'service_status', arguments: { service_name: 'database-backend' }) |
| 55 | + expect(tool_result[:service_name]).to eq('database-backend') |
| 56 | + expect(tool_result[:status]).to eq('running') |
65 | 57 | end |
66 | 58 |
|
67 | 59 | it 'returns tool error when service is missing' do |
68 | | - response = rpc_request( |
69 | | - id: 3, |
70 | | - method: 'tools/call', |
71 | | - params: { 'name' => 'service_status', 'arguments' => { 'service_name' => 'ghost' } } |
72 | | - ) |
73 | | - expect(response['result']).to be_nil |
74 | | - expect(response.dig('error', 'code')).to eq(404) |
75 | | - expect(response.dig('error', 'message')).to include('Unknown service ghost') |
| 60 | + response = mcp_call_tool_raw(name: 'service_status', arguments: { service_name: 'ghost' }) |
| 61 | + expect(response[:result]).to be_nil |
| 62 | + expect(response.dig(:error, :code)).to eq(-32000) |
| 63 | + expect(response.dig(:error, :message)).to include('Unknown service ghost') |
76 | 64 | end |
77 | 65 |
|
78 | 66 | it 'returns error for unknown tools' do |
79 | | - response = rpc_request( |
80 | | - id: 4, |
81 | | - method: 'tools/call', |
82 | | - params: { 'name' => 'unknown', 'arguments' => {} } |
83 | | - ) |
84 | | - expect(response.dig('error', 'code')).to eq(-32601) |
| 67 | + response = mcp_call_tool_raw(name: 'unknown', arguments: {}) |
| 68 | + expect(response.dig(:error, :code)).to eq(-32601) |
85 | 69 | end |
86 | 70 |
|
87 | 71 | it 'handles malformed JSON payloads' do |
88 | 72 | post '/mcp', '{invalid', headers |
89 | 73 | expect(last_response.status).to eq(400) |
90 | 74 | body = parsed_response_body |
91 | | - expect(body.dig('error', 'code')).to eq(-32700) |
92 | | - end |
93 | | - |
94 | | - def rpc_request(payload) |
95 | | - post '/mcp', JSON.dump(payload), headers |
96 | | - expect(last_response.status).to eq(200) |
97 | | - parsed_response_body |
98 | | - end |
99 | | - |
100 | | - def parsed_response_body |
101 | | - body = +'' |
102 | | - last_response.each { |chunk| body << chunk.to_s } |
103 | | - data_lines = body.lines.select { |line| line.start_with?('data:') } |
104 | | - payload_line = data_lines.reverse.find { |l| l !~ /ping/i } || data_lines.last |
105 | | - payload = (payload_line || body).sub(/\Adata:\s*/, '').sub(/\n\z/, '') |
106 | | - JSON.parse(payload) |
| 75 | + expect(body.dig(:error, :code)).to eq(-32700) |
107 | 76 | end |
108 | 77 | end |
109 | 78 | end |
0 commit comments