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
This commit is contained in:
parent
92a08bafc7
commit
7b4ece294b
1 changed files with 17 additions and 2 deletions
|
|
@ -29,12 +29,27 @@ module MCP
|
||||||
assert_equal("tool2", tools.last.name)
|
assert_equal("tool2", tools.last.name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_tools_returns_empty_array_when_no_tools
|
||||||
|
transport = mock
|
||||||
|
mock_response = { "result" => { "tools" => [] } }
|
||||||
|
|
||||||
|
# Only checking for the essential parts of the request
|
||||||
|
transport.expects(:send_request).with do |args|
|
||||||
|
args in { request: { method: "tools/list", jsonrpc: "2.0" } }
|
||||||
|
end.returns(mock_response).once
|
||||||
|
|
||||||
|
client = Client.new(transport: transport)
|
||||||
|
tools = client.tools
|
||||||
|
|
||||||
|
assert_empty(tools)
|
||||||
|
end
|
||||||
|
|
||||||
def test_call_tool_sends_request_to_transport_and_returns_content
|
def test_call_tool_sends_request_to_transport_and_returns_content
|
||||||
transport = mock
|
transport = mock
|
||||||
tool = MCP::Client::Tool.new(name: "tool1", description: "tool1", input_schema: {})
|
tool = MCP::Client::Tool.new(name: "tool1", description: "tool1", input_schema: {})
|
||||||
arguments = { foo: "bar" }
|
arguments = { foo: "bar" }
|
||||||
mock_response = {
|
mock_response = {
|
||||||
"result" => { "content" => "result" },
|
"result" => { "content" => [{ "type": "text", "text": "Hello, world!" }] },
|
||||||
}
|
}
|
||||||
|
|
||||||
# Only checking for the essential parts of the request
|
# Only checking for the essential parts of the request
|
||||||
|
|
@ -55,7 +70,7 @@ module MCP
|
||||||
result = client.call_tool(tool: tool, arguments: arguments)
|
result = client.call_tool(tool: tool, arguments: arguments)
|
||||||
content = result.dig("result", "content")
|
content = result.dig("result", "content")
|
||||||
|
|
||||||
assert_equal("result", content)
|
assert_equal([{ type: "text", text: "Hello, world!" }], content)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_resources_sends_request_to_transport_and_returns_resources_array
|
def test_resources_sends_request_to_transport_and_returns_resources_array
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue