|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "faraday" |
| 5 | +require "securerandom" |
| 6 | +require "webmock/minitest" |
| 7 | + |
| 8 | +module ModelContextProtocol |
| 9 | + module Client |
| 10 | + class HttpTest < Minitest::Test |
| 11 | + def test_initialization_with_default_version |
| 12 | + assert_equal("0.1.0", client.version) |
| 13 | + assert_equal(url, client.url) |
| 14 | + end |
| 15 | + |
| 16 | + def test_initialization_with_custom_version |
| 17 | + custom_version = "1.2.3" |
| 18 | + client = Http.new(url:, version: custom_version) |
| 19 | + assert_equal(custom_version, client.version) |
| 20 | + end |
| 21 | + |
| 22 | + def test_tools_returns_tools_instance |
| 23 | + stub_request(:post, url) |
| 24 | + .with( |
| 25 | + body: { |
| 26 | + method: "tools/list", |
| 27 | + jsonrpc: "2.0", |
| 28 | + id: mock_request_id, |
| 29 | + mcp: { |
| 30 | + method: "tools/list", |
| 31 | + jsonrpc: "2.0", |
| 32 | + id: mock_request_id, |
| 33 | + }, |
| 34 | + }, |
| 35 | + ) |
| 36 | + .to_return( |
| 37 | + status: 200, |
| 38 | + headers: { |
| 39 | + "Content-Type" => "application/json", |
| 40 | + }, |
| 41 | + body: { |
| 42 | + result: { |
| 43 | + tools: [ |
| 44 | + { |
| 45 | + name: "test_tool", |
| 46 | + description: "A test tool", |
| 47 | + inputSchema: { |
| 48 | + type: "object", |
| 49 | + properties: {}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + }.to_json, |
| 55 | + ) |
| 56 | + |
| 57 | + tools = client.tools |
| 58 | + assert_instance_of(Tools, tools) |
| 59 | + assert_equal(1, tools.count) |
| 60 | + assert_equal("test_tool", tools.first.name) |
| 61 | + end |
| 62 | + |
| 63 | + def test_call_tool_returns_tool_response |
| 64 | + tool = Tool.new( |
| 65 | + "name" => "test_tool", |
| 66 | + "description" => "A test tool", |
| 67 | + "inputSchema" => { |
| 68 | + "type" => "object", |
| 69 | + "properties" => {}, |
| 70 | + }, |
| 71 | + ) |
| 72 | + input = { "param" => "value" } |
| 73 | + |
| 74 | + stub_request(:post, url) |
| 75 | + .with( |
| 76 | + body: { |
| 77 | + jsonrpc: "2.0", |
| 78 | + id: mock_request_id, |
| 79 | + method: "tools/call", |
| 80 | + params: { |
| 81 | + name: "test_tool", |
| 82 | + arguments: input, |
| 83 | + }, |
| 84 | + mcp: { |
| 85 | + jsonrpc: "2.0", |
| 86 | + id: mock_request_id, |
| 87 | + method: "tools/call", |
| 88 | + params: { |
| 89 | + name: "test_tool", |
| 90 | + arguments: input, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + ) |
| 95 | + .to_return( |
| 96 | + status: 200, |
| 97 | + headers: { |
| 98 | + "Content-Type" => "application/json", |
| 99 | + }, |
| 100 | + body: { |
| 101 | + result: { |
| 102 | + content: [ |
| 103 | + { |
| 104 | + text: "Tool response", |
| 105 | + }, |
| 106 | + ], |
| 107 | + }, |
| 108 | + }.to_json, |
| 109 | + ) |
| 110 | + |
| 111 | + response = client.call_tool(tool: tool, input: input) |
| 112 | + assert_equal("Tool response", response) |
| 113 | + end |
| 114 | + |
| 115 | + def test_call_tool_handles_empty_response |
| 116 | + tool = Tool.new( |
| 117 | + "name" => "test_tool", |
| 118 | + "description" => "A test tool", |
| 119 | + "inputSchema" => { |
| 120 | + "type" => "object", |
| 121 | + "properties" => {}, |
| 122 | + }, |
| 123 | + ) |
| 124 | + input = { "param" => "value" } |
| 125 | + |
| 126 | + stub_request(:post, url) |
| 127 | + .with( |
| 128 | + body: { |
| 129 | + jsonrpc: "2.0", |
| 130 | + id: mock_request_id, |
| 131 | + method: "tools/call", |
| 132 | + params: { |
| 133 | + name: "test_tool", |
| 134 | + arguments: input, |
| 135 | + }, |
| 136 | + mcp: { |
| 137 | + jsonrpc: "2.0", |
| 138 | + id: mock_request_id, |
| 139 | + method: "tools/call", |
| 140 | + params: { |
| 141 | + name: "test_tool", |
| 142 | + arguments: input, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + ) |
| 147 | + .to_return( |
| 148 | + status: 200, |
| 149 | + headers: { |
| 150 | + "Content-Type" => "application/json", |
| 151 | + }, |
| 152 | + body: { |
| 153 | + result: { |
| 154 | + content: [], |
| 155 | + }, |
| 156 | + }.to_json, |
| 157 | + ) |
| 158 | + |
| 159 | + response = client.call_tool(tool: tool, input: input) |
| 160 | + assert_nil(response) |
| 161 | + end |
| 162 | + |
| 163 | + private |
| 164 | + |
| 165 | + def stub_request(method, url) |
| 166 | + WebMock.stub_request(method, url) |
| 167 | + end |
| 168 | + |
| 169 | + def mock_request_id |
| 170 | + "random_request_id" |
| 171 | + end |
| 172 | + |
| 173 | + def url |
| 174 | + "http://example.com" |
| 175 | + end |
| 176 | + |
| 177 | + def client |
| 178 | + @client ||= begin |
| 179 | + client = Http.new(url:) |
| 180 | + client.stubs(:request_id).returns(mock_request_id) |
| 181 | + client |
| 182 | + end |
| 183 | + end |
| 184 | + end |
| 185 | + end |
| 186 | +end |
0 commit comments