Skip to content

Commit 7b4ece2

Browse files
committed
Fix and add test for client tools functionality
- Add test for empty tools array response to ensure client handles empty results correctly - Update call_tool test to use proper content format (array of content objects) instead of plain string based on modelcontextprotocol spec. https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/2025-03-26/server/tools.mdx#calling-tools Follow up: modelcontextprotocol#160
1 parent 92a08ba commit 7b4ece2

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

test/mcp/client_test.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,27 @@ def test_tools_sends_request_to_transport_and_returns_tools_array
2929
assert_equal("tool2", tools.last.name)
3030
end
3131

32+
def test_tools_returns_empty_array_when_no_tools
33+
transport = mock
34+
mock_response = { "result" => { "tools" => [] } }
35+
36+
# Only checking for the essential parts of the request
37+
transport.expects(:send_request).with do |args|
38+
args in { request: { method: "tools/list", jsonrpc: "2.0" } }
39+
end.returns(mock_response).once
40+
41+
client = Client.new(transport: transport)
42+
tools = client.tools
43+
44+
assert_empty(tools)
45+
end
46+
3247
def test_call_tool_sends_request_to_transport_and_returns_content
3348
transport = mock
3449
tool = MCP::Client::Tool.new(name: "tool1", description: "tool1", input_schema: {})
3550
arguments = { foo: "bar" }
3651
mock_response = {
37-
"result" => { "content" => "result" },
52+
"result" => { "content" => [{ "type": "text", "text": "Hello, world!" }] },
3853
}
3954

4055
# Only checking for the essential parts of the request
@@ -55,7 +70,7 @@ def test_call_tool_sends_request_to_transport_and_returns_content
5570
result = client.call_tool(tool: tool, arguments: arguments)
5671
content = result.dig("result", "content")
5772

58-
assert_equal("result", content)
73+
assert_equal([{ type: "text", text: "Hello, world!" }], content)
5974
end
6075

6176
def test_resources_sends_request_to_transport_and_returns_resources_array

0 commit comments

Comments
 (0)